Commits

Matthew Marshall committed 067bd18

Added support for renaming repositories.

  • Participants
  • Parent commits 5bc05c1

Comments (0)

Files changed (2)

File repos/forms.py

 
 
 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.')

File repos/views.py

     repo = get_object_or_404(Repo, owner__username=username, name=reponame)
 
     if request.method == 'POST':
-        form = EditRepoForm(request.POST)
+        form = EditRepoForm(repo, request.POST)
         if form.is_valid():
+            old_path = repo.file_path
+            repo.name = form.cleaned_data['reponame']
             repo.long_name = form.cleaned_data['long_name']
             repo.description = form.cleaned_data['description']
+            if old_path != repo.file_path:
+                # FIXME move this into the pre_save signal or something? (just
+                # as soon as I figure out how to determin the previous value.)
+                # FIXME Have some sort of error handling?
+                import os
+                os.rename(old_path, repo.file_path)
             repo.save()
             return HttpResponseRedirect(repo.get_absolute_url())
     else:
-        form = EditRepoForm({'long_name':repo.long_name,
-                'description':repo.description})
+        form = EditRepoForm(repo)
     return render_to_response('repos/edit.html',
         dict(
             repo=repo,