Return-Path: X-Original-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9A74410580 for ; Mon, 8 Jul 2013 19:28:14 +0000 (UTC) Received: (qmail 92023 invoked by uid 500); 8 Jul 2013 19:28:14 -0000 Delivered-To: apmail-incubator-allura-commits-archive@incubator.apache.org Received: (qmail 91984 invoked by uid 500); 8 Jul 2013 19:28:14 -0000 Mailing-List: contact allura-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: allura-dev@incubator.apache.org Delivered-To: mailing list allura-commits@incubator.apache.org Received: (qmail 91788 invoked by uid 99); 8 Jul 2013 19:28:14 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Jul 2013 19:28:14 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id F13D2888529; Mon, 8 Jul 2013 19:28:13 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: johnsca@apache.org To: allura-commits@incubator.apache.org Date: Mon, 08 Jul 2013 19:28:36 -0000 Message-Id: <38aee78055fb45488a06aea785653636@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [24/25] git commit: [#4656] Refactor project name validation [#4656] Refactor project name validation Signed-off-by: Tim Van Steenburgh Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/db2fc75f Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/db2fc75f Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/db2fc75f Branch: refs/heads/cj/4656 Commit: db2fc75f7a2c6dfce1a7200a5953c162c00bb657 Parents: 584a728 Author: Tim Van Steenburgh Authored: Tue Jul 2 19:56:40 2013 +0000 Committer: Cory Johns Committed: Mon Jul 8 19:27:57 2013 +0000 ---------------------------------------------------------------------- Allura/allura/controllers/project.py | 16 ++++++------ Allura/allura/lib/plugin.py | 41 ++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/db2fc75f/Allura/allura/controllers/project.py ---------------------------------------------------------------------- diff --git a/Allura/allura/controllers/project.py b/Allura/allura/controllers/project.py index bff0399..e5edd80 100644 --- a/Allura/allura/controllers/project.py +++ b/Allura/allura/controllers/project.py @@ -175,21 +175,23 @@ class NeighborhoodController(object): @expose('json:') def suggest_name(self, project_name=''): - result = dict() - result['suggested_name'] = re.sub("[^A-Za-z0-9]", "", project_name).lower()[:15] - return result + provider = plugin.ProjectRegistrationProvider.get() + return dict(suggested_name=provider.suggest_name(project_name, + self.neighborhood)) @expose('json:') def check_names(self, project_name='', unix_name=''): + provider = plugin.ProjectRegistrationProvider.get() result = dict() try: W.add_project.fields['project_name'].validate(project_name, '') except Invalid as e: result['name_message'] = str(e) - if not h.re_project_name.match(unix_name) or not (3 <= len(unix_name) <= 15): - result['unixname_message'] = 'Please use only letters, numbers, and dashes 3-15 characters long.' - else: - result['unixname_message'] = plugin.ProjectRegistrationProvider.get().name_taken(unix_name, self.neighborhood) + + unixname_invalid_err = provider.validate_project_shortname(unix_name, + self.neighborhood) + result['unixname_message'] = (unixname_invalid_err or + provider.name_taken(unix_name, self.neighborhood)) return result @h.vardec http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/db2fc75f/Allura/allura/lib/plugin.py ---------------------------------------------------------------------- diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py index 855e71c..c0319f7 100644 --- a/Allura/allura/lib/plugin.py +++ b/Allura/allura/lib/plugin.py @@ -365,6 +365,10 @@ class ProjectRegistrationProvider(object): return app_globals.Globals().entry_points['registration'][method]() def name_taken(self, project_name, neighborhood): + """Return False if ``project_name`` is available in ``neighborhood``. + If unavailable, return an error message (str) explaining why. + + """ from allura import model as M p = M.Project.query.get(shortname=project_name, neighborhood_id=neighborhood._id) if p: @@ -375,18 +379,28 @@ class ProjectRegistrationProvider(object): return False def extra_name_checks(self): - '''This should be a list or iterator containing tuples. - The first tiem in the tuple should be an error message and the - second should be a regex. If the user attempts to register a - project with a name that matches the regex, the field will - be marked invalid with the message displayed to the user. - ''' + """Return an iterable of ``(error_message, regex)`` tuples. + + If user attempts to register a project with a name that matches + ``regex``, the field will be marked invalid, and ``error_message`` + displayed to the user. + + """ return [] + def suggest_name(self, project_name, neighborhood): + """Return a suggested project shortname for the full ``project_name``. + + Example: "My Great Project" -> "mygreatproject" + + """ + return re.sub("[^A-Za-z0-9]", "", project_name).lower() + def rate_limit(self, user, neighborhood): - '''Check the various config-defined project registration rate + """Check the various config-defined project registration rate limits, and if any are exceeded, raise ProjectRatelimitError. - ''' + + """ if security.has_access(neighborhood, 'admin', user=user)(): return # have to have the replace because, despite being UTC, @@ -468,13 +482,22 @@ class ProjectRegistrationProvider(object): check_shortname = shortname.replace('u/', '', 1) else: check_shortname = shortname - if not h.re_project_name.match(check_shortname): + err = self.validate_project_shortname(check_shortname, neighborhood) + if err: raise ValueError('Invalid project shortname: %s' % shortname) p = M.Project.query.get(shortname=shortname, neighborhood_id=neighborhood._id) if p: raise forge_exc.ProjectConflict('%s already exists in nbhd %s' % (shortname, neighborhood._id)) + def validate_project_shortname(self, shortname, neighborhood): + """Return an error message if ``shortname`` is invalid for + ``neighborhood``, else return None. + + """ + if not h.re_project_name.match(shortname): + return 'Please use only letters, numbers, and dashes 3-15 characters long.' + def _create_project(self, neighborhood, shortname, project_name, user, user_project, private_project, apps): ''' Actually create the project, no validation. This should not be called directly