Ensure duplicate patches aren't created when an invalid msgid is entered
authorMagnus Hagander <magnus@hagander.net>
Fri, 14 Aug 2015 10:34:39 +0000 (12:34 +0200)
committerMagnus Hagander <magnus@hagander.net>
Fri, 14 Aug 2015 10:34:39 +0000 (12:34 +0200)
pgcommitfest/commitfest/forms.py
pgcommitfest/commitfest/views.py

index 2a832ae915ef64a5792b0da95c819cd75bc7f696..67205d20743004241bdc5f2ccce073462146ef83 100644 (file)
@@ -3,6 +3,7 @@ from django.forms import ValidationError
 from django.forms.widgets import HiddenInput
 from django.db.models import Q
 from django.contrib.auth.models import User
+from django.http import Http404
 
 from selectable.forms.widgets import AutoCompleteSelectMultipleWidget
 
@@ -58,6 +59,15 @@ class NewPatchForm(forms.ModelForm):
                model = Patch
                exclude = ('commitfests', 'mailthreads', 'modified', 'authors', 'reviewers', 'committer', 'wikilink', 'gitlink', 'lastmail', )
 
+       def clean_threadmsgid(self):
+               try:
+                       _archivesAPI('/message-id.json/%s' % self.cleaned_data['threadmsgid'])
+               except Http404:
+                       raise ValidationError("Message not found in archives")
+               except:
+                       raise ValidationError("Error in API call to validate thread")
+               return self.cleaned_data['threadmsgid']
+
 def _fetch_thread_choices(patch):
        for mt in patch.mailthread_set.order_by('-latestmessage'):
                ti = sorted(_archivesAPI('/message-id.json/%s' % mt.messageid), key=lambda x: x['date'], reverse=True)
index ddc579aa7540014abe53a9ff250d12eadd00afb1..6e86743c83d69c5f374fb25094f2596f3178f960 100644 (file)
@@ -282,6 +282,9 @@ def newpatch(request, cfid):
                                form._errors['threadmsgid'] = form.error_class(('Selected thread did not exist in the archives',))
                        except Exception:
                                form._errors['threadmsgid'] = form.error_class(('An error occurred looking up the thread in the archives.',))
+                       # In this case, we have created a patch - delete it. This causes a agp in id's, but it should
+                       # not happen very often. If we successfully attached to it, we will have already returned.
+                       patch.delete()
        else:
                form = NewPatchForm()