diff options
author | Magnus Hagander | 2014-07-14 17:13:12 +0000 |
---|---|---|
committer | Magnus Hagander | 2014-07-14 17:13:12 +0000 |
commit | 384ba4a380e17641743625f21429169aadbd2e3d (patch) | |
tree | 47e80b2dc9833b7ed1f0e8d7e279db3ba17f8756 /pgcommitfest/commitfest/ajax.py | |
parent | 75e66a3eaa14f1106f3540953b8aae8dd64e68b4 (diff) |
Properly normalize table of mailinglist threads
Diffstat (limited to 'pgcommitfest/commitfest/ajax.py')
-rw-r--r-- | pgcommitfest/commitfest/ajax.py | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/pgcommitfest/commitfest/ajax.py b/pgcommitfest/commitfest/ajax.py index 643af70..d0e5620 100644 --- a/pgcommitfest/commitfest/ajax.py +++ b/pgcommitfest/commitfest/ajax.py @@ -85,17 +85,32 @@ def attachThread(request): raise Exception("Something happened that cannot happen") def doAttachThread(cf, patch, msgid, user): + # Note! Must be called in an open transaction! 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. + threads = MailThread.objects.filter(messageid=r[0]['msgid']) + if len(threads): + thread = threads[0] + if thread.patches.filter(id=patch.id).exists(): + # We have everything, so claim we're done. + return True + + # We did not exist, so we'd better add ourselves. + # While at it, we update the thread entry with the latest data from the + # archives. + thread.patches.add(patch) + thread.latestmessage=r[-1]['date'] + thread.latestauthor=r[-1]['from'] + thread.latestsubject=r[-1]['subj'] + thread.altestmsgid=r[-1]['msgid'] + thread.save() return True + # No existing thread existed, so create it # Now create a new mailthread entry m = MailThread(messageid=r[0]['msgid'], - patch=patch, subject=r[0]['subj'], firstmessage=r[0]['date'], firstauthor=r[0]['from'], @@ -105,6 +120,8 @@ def doAttachThread(cf, patch, msgid, user): latestmsgid=r[-1]['msgid'], ) m.save() + m.patches.add(patch) + m.save() parse_and_add_attachments(r, m) PatchHistory(patch=patch, by=user, what='Attached mail thread %s' % r[0]['msgid']).save() patch.update_lastmail() @@ -117,9 +134,9 @@ def doAttachThread(cf, patch, msgid, user): def detachThread(request): cf = get_object_or_404(CommitFest, pk=int(request.POST['cf'])) patch = get_object_or_404(Patch, pk=int(request.POST['p']), commitfests=cf) - thread = get_object_or_404(MailThread, patch=patch, messageid=request.POST['msg']) + thread = get_object_or_404(MailThread, messageid=request.POST['msg']) - thread.delete() + patch.mailthread_set.remove(thread) PatchHistory(patch=patch, by=request.user, what='Detached mail thread %s' % request.POST['msg']).save() patch.update_lastmail() patch.set_modified() |