[copr] bkabrda-workspace: Make sure that user can't create two coprs with the same name (3e1d319)

bkabrda at fedorahosted.org bkabrda at fedorahosted.org
Fri Nov 16 12:16:53 UTC 2012


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : bkabrda-workspace

>---------------------------------------------------------------

commit 3e1d3192be12dea4850597f5a5ae25216313e8ec
Author: Slavek Kabrda <bkabrda at redhat.com>
Date:   Fri Nov 16 13:06:07 2012 +0100

    Make sure that user can't create two coprs with the same name


>---------------------------------------------------------------

 wsgi/coprs/forms.py                          |   25 +++++++++++++++++++++++--
 wsgi/coprs/templates/coprs/_coprs_forms.html |    1 +
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/wsgi/coprs/forms.py b/wsgi/coprs/forms.py
index 351ecbe..8387ba5 100644
--- a/wsgi/coprs/forms.py
+++ b/wsgi/coprs/forms.py
@@ -1,10 +1,13 @@
 import re
 import urlparse
 
+import flask
+
 from flask.ext import wtf
 
 from coprs import constants
 from coprs import helpers
+from coprs import models
 
 class UrlListValidator(object):
     def __init__(self, message = None):
@@ -41,6 +44,21 @@ class AllowedArchesValidator(object):
             if a not in constants.CHROOTS[form.release.data]:
                 raise wtf.ValidationError(self.message.format(a))
 
+class CoprUniqueNameValidator(object):
+    def __init__(self, message = None):
+        if not message:
+            message = 'You already have copr named "{0}".'
+        self.message = message
+
+    def __call__(self, form, field):
+        existing = models.Copr.query.filter(models.Copr.name == field.data).\
+                                     filter(models.Copr.owner_id == flask.g.user.id)
+        if form.id.data:
+            existing = existing.filter(models.Copr.id != form.id.data)
+
+        if existing.first():
+            raise wtf.ValidationError(self.message.format(field.data))
+
 
 class StringListFilter(object):
     def __call__(self, value):
@@ -55,9 +73,13 @@ class StringListFilter(object):
 
 class CoprForm(wtf.Form):
     # TODO: validations
+    # also use id here, to be able to find out whether user is updating a copr
+    # if so, we don't want to shout that name already exists
+    id = wtf.HiddenField()
     name = wtf.TextField('Name',
                          validators = [wtf.Required(),
-                         wtf.Regexp(re.compile(r'^[\w-]+$'), message = 'Name must contain only letters, digits, underscores and dashes.')])
+                         wtf.Regexp(re.compile(r'^[\w-]+$'), message = 'Name must contain only letters, digits, underscores and dashes.'),
+                         CoprUniqueNameValidator()])
     # choices must be list of tuples
     # => make list like [(fedora-18, fedora-18), ...]
     release = wtf.SelectField('Release', choices = [(x, x) for x in constants.CHROOTS])
@@ -72,7 +94,6 @@ class CoprForm(wtf.Form):
     def chroots(self):
         return ['{0}-{1}'.format(self.release.data, arch) for arch in self.arches.data ]
 
-
 class BuildForm(wtf.Form):
     pkgs = wtf.TextAreaField('Pkgs',
                              validators = [wtf.Required(), UrlListValidator()],
diff --git a/wsgi/coprs/templates/coprs/_coprs_forms.html b/wsgi/coprs/templates/coprs/_coprs_forms.html
index 1786477..d5016ae 100644
--- a/wsgi/coprs/templates/coprs/_coprs_forms.html
+++ b/wsgi/coprs/templates/coprs/_coprs_forms.html
@@ -5,6 +5,7 @@
   <form action="{% if copr %}{{ url_for(view, username = copr.owner.name, coprname = copr.name) }}{% else %}{{ url_for(view) }}{% endif %}" method=post class=add-entry>
     <dl>
       {{ form.csrf_token }}
+      {{ render_field(form.id, hidden = True) }}
       {{ render_field(form.name) }}
       {# TODO: validate/change arches dynamically by JS #}
       {{ render_field(form.release) }}



More information about the copr-devel mailing list