Ability to add new patches
authorMagnus Hagander <magnus@hagander.net>
Fri, 19 Jul 2013 15:04:01 +0000 (17:04 +0200)
committerMagnus Hagander <magnus@hagander.net>
Fri, 19 Jul 2013 15:04:01 +0000 (17:04 +0200)
pgcommitfest/commitfest/ajax.py
pgcommitfest/commitfest/models.py
pgcommitfest/commitfest/views.py

index b9c3db41707892370873e2bc24b28223994c4a25..7c73771f6f1f9f05b9c7ddc13df93dbc8a63cbf4 100644 (file)
@@ -74,14 +74,19 @@ def attachThread(request):
        patch = get_object_or_404(Patch, pk=int(request.POST['p']), commitfests=cf)
        msgid = request.POST['msg']
 
-       
+       if doAttachThread(cf, patch, msgid, request.user):
+               return 'OK'
+       else:
+               raise Exception("Something happened that cannot happen")
+
+def doAttachThread(cf, patch, msgid, user):
        r = sorted(_archivesAPI('/message-id.json/%s' % msgid), key=lambda x: x['date'])
        # We have the full thread metadata - using the first and last entry,
        # construct a new mailthread in our own model.
        # First, though, check if it's already there.
        if MailThread.objects.filter(messageid=r[0]['msgid'], patch=patch).exists():
                # It already existed. Pretend everything is fine.
-               return 'OK'
+               return True
 
        # Now create a new mailthread entry
        m = MailThread(messageid=r[0]['msgid'],
@@ -96,11 +101,11 @@ def attachThread(request):
                                   )
        m.save()
        parse_and_add_attachments(r, m)
-       PatchHistory(patch=patch, by=request.user, what='Attached mail thread %s' % r[0]['msgid']).save()
+       PatchHistory(patch=patch, by=user, what='Attached mail thread %s' % r[0]['msgid']).save()
        patch.set_modified()
        patch.save()
 
-       return 'OK'
+       return True
 
 @transaction.commit_on_success
 def detachThread(request):
index c8a11373500e33093abde7fd8021d16b1a09efd9..145efab209c55012d2c006836463178a70e5fc14 100644 (file)
@@ -94,7 +94,7 @@ class Patch(models.Model):
                # current timestamp.
                if not newmod:
                        newmod = datetime.now()
-               if newmod > self.modified:
+               if not self.modified or newmod > self.modified:
                        self.modified = newmod
 
        def __unicode__(self):
index 95ac285db67295545fba949883a8f439d94adc81..170102809ff719ec86edb2b57526adccb57ed7d6 100644 (file)
@@ -1,5 +1,5 @@
 from django.shortcuts import render_to_response, get_object_or_404
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, Http404
 from django.template import RequestContext
 from django.db import transaction
 from django.contrib import messages
@@ -13,6 +13,7 @@ from mailqueue.util import send_mail
 
 from models import CommitFest, Patch, PatchOnCommitFest, PatchHistory, Committer
 from forms import PatchForm, NewPatchForm, CommentForm
+from ajax import doAttachThread
 
 def home(request):
        commitfests = CommitFest.objects.all()
@@ -99,7 +100,22 @@ def newpatch(request, cfid):
        if request.method == 'POST':
                form = NewPatchForm(data=request.POST)
                if form.is_valid():
-                       raise Exception("Do something")
+                       patch = Patch(name=form.cleaned_data['name'],
+                                                 topic=form.cleaned_data['topic'])
+                       patch.set_modified()
+                       patch.save()
+                       poc = PatchOnCommitFest(patch=patch, commitfest=cf, enterdate=datetime.now())
+                       poc.save()
+                       # Now add the thread
+                       try:
+                               doAttachThread(cf, patch, form.cleaned_data['threadmsgid'], request.user)
+                               return HttpResponseRedirect("/%s/%s/" % (cf.id, patch.id))
+                       except Http404:
+                               # Thread not found!
+                               # This is a horrible breakage of API layers
+                               form._errors['threadmsgid'] = form.error_class(('Selected thread did not exist',))
+                       except Exception:
+                               form._errors['threadmsgid'] = form.error_class(('An error occurred looking up the thread in the archives.',))
        else:
                form = NewPatchForm()