1. Chia-Huan Wu
  2. freehg

Source

freehg / repos / forms.py

Diff from to

File repos/forms.py

  • Ignore whitespace
 
 
 class EditRepoForm(forms.Form):
+    def __init__(self, repo, data=None, **kwargs):
+        self.repo = repo
+        if data is None:
+            data = dict(reponame=repo.name, long_name=repo.long_name,
+                    description=repo.description)
+        forms.Form.__init__(self, data, **kwargs)
+    reponame = forms.CharField(max_length=30,
+            widget=forms.TextInput(attrs={'class': 'required title'}),
+            label=u"Repository Name", help_text=u"the name used in the url. " +
+            u"(Changing this will change the url.)")
     long_name = forms.CharField(max_length=50, required=False,
             label=u"Aesthetic Name",
             widget=forms.TextInput(attrs={'class': 'required text'}),
             help_text=u"optional version of the name fit for human consumption")
     description = forms.CharField(max_length=5000, required=False,
             widget=forms.Textarea())
+
+    def clean_reponame(self):
+        reponame = self.cleaned_data.get('reponame', None)
+        if reponame and reponame != self.repo.name:
+            reponame = reponame.lower()
+            if not re.search(r'^\w(\w|-|\.)+\w$', reponame):
+                msg = (u'Repository names can only contain letters, ' +
+                       u'numbers, underscores, dashes and periods. ' +
+                       u'They must begin and end with a letter or number.')
+                raise forms.ValidationError(msg)
+            try:
+                repo = self.repo.owner.repo_set.get(name__exact=reponame)
+            except Repo.DoesNotExist:
+                return reponame
+            raise forms.ValidationError(
+                    u'You already have a repository with this name.')