Collect and show attachment filenames
authorMagnus Hagander <magnus@hagander.net>
Sun, 8 Feb 2015 15:54:12 +0000 (16:54 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sun, 8 Feb 2015 15:54:12 +0000 (16:54 +0100)
pgcommitfest/commitfest/ajax.py
pgcommitfest/commitfest/models.py
pgcommitfest/commitfest/templates/patch.html
tools/commitfest/oneoff/update_attachment_filenames.py [new file with mode: 0755]

index f489d12b554f09bf9ac9a4b17b474d09e614ffcc..92c4575175ffb91c7112103a9ff587b03b2ef065 100644 (file)
@@ -75,7 +75,8 @@ def parse_and_add_attachments(threadinfo, mailthread):
                                                                                                           defaults={
                                                                                                                   'date': t['date'],
                                                                                                                   'author': t['from'],
-                                                                                                                  'attachmentid': t['atts'][0],
+                                                                                                                  'attachmentid': t['atts'][0]['id'],
+                                                                                                                  'filename': t['atts'][0]['name'],
                                                                                                           })
                # In theory we should remove objects if they don't have an
                # attachment, but how could that ever happen? Ignore for now.
index f118bc21fdf408243ad68cf6aa088418a74bdca5..124af720aa4e4d646a814124aec195494475d785 100644 (file)
@@ -219,6 +219,7 @@ class MailThreadAttachment(models.Model):
        mailthread = models.ForeignKey(MailThread, null=False, blank=False)
        messageid = models.CharField(max_length=1000, null=False, blank=False)
        attachmentid = models.IntegerField(null=False, blank=False)
+       filename = models.CharField(max_length=1000, null=False, blank=True)
        date = models.DateTimeField(null=False, blank=False)
        author = models.CharField(max_length=500, null=False, blank=False)
        ispatch = models.NullBooleanField()
index 7fcf9fdcfb4ed6ee766168b829996c6677098363..97ac9b52910511a9513780427135abe18e113fef 100644 (file)
          Latest at <a href="http://www.postgresql.org/message-id/{{t.latestmsgid}}/">{{t.latestmessage}}</a> by {{t.latestauthor|hidemail}}<br/>
          {%for ta in t.mailthreadattachment_set.all%}
          {%if forloop.first%}
-         Latest attachment at <a href="http://www.postgresql.org/message-id/{{ta.messageid}}/">{{ta.date}}</a> from {{ta.author|hidemail}} <button type="button" class="btn btn-default btn-xs" data-toggle="collapse" data-target="#att{{t.pk}}" title="Show all attachments"><i class="glyphicon glyphicon-plus"></i></button>
+         Latest attachment (<a href="http://www.postgresql.org/message-id/attachment/{{ta.attachmentid}}/{{ta.filename}}">{{ta.filename}}</a>) at <a href="http://www.postgresql.org/message-id/{{ta.messageid}}/">{{ta.date}}</a> from {{ta.author|hidemail}} <button type="button" class="btn btn-default btn-xs" data-toggle="collapse" data-target="#att{{t.pk}}" title="Show all attachments"><i class="glyphicon glyphicon-plus"></i></button>
            <div id="att{{t.pk}}" class="collapse">
           {%endif%}
-           &nbsp;&nbsp;&nbsp;&nbsp;Attachment at <a href="http://www.postgresql.org/message-id/{{ta.messageid}}/">{{ta.date}}</a> from {{ta.author|hidemail}} (Patch: {{ta.ispatch|yesno:"Yes,No,Pending check"}})<br/>
+           &nbsp;&nbsp;&nbsp;&nbsp;Attachment (<a href="http://www.postgresql.org/message-id/attachment/{{ta.attachmentid}}/{{ta.filename}}">{{ta.filename}}</a>) at <a href="http://www.postgresql.org/message-id/{{ta.messageid}}/">{{ta.date}}</a> from {{ta.author|hidemail}} (Patch: {{ta.ispatch|yesno:"Yes,No,Pending check"}})<br/>
           {%if forloop.last%}</div>{%endif%}
          {%endfor%}
         </dd>
diff --git a/tools/commitfest/oneoff/update_attachment_filenames.py b/tools/commitfest/oneoff/update_attachment_filenames.py
new file mode 100755 (executable)
index 0000000..3ee675c
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+#
+# update_attachment_filenames.py
+
+# Go through all old attachments in the system and update the filename based on
+# the information in the archives. Required after we added the filename field
+# to attachments.
+#
+
+import os
+import sys
+import socket
+import httplib
+import magic
+import logging
+
+import simplejson
+
+# Set up for accessing django
+from django.core.management import setup_environ
+sys.path.append(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), '../../../pgcommitfest'))
+import settings
+setup_environ(settings)
+
+from django.db import connection
+
+from commitfest.models import MailThreadAttachment
+
+if __name__ == "__main__":
+       debug = "--debug" in sys.argv
+
+       # Logging always done to stdout, but we can turn on/off how much
+       logging.basicConfig(format='%(asctime)s %(levelname)s: %(msg)s',
+                                               level=debug and logging.DEBUG or logging.INFO,
+                                               stream=sys.stdout)
+
+       socket.setdefaulttimeout(settings.ARCHIVES_TIMEOUT)
+       mag = magic.open(magic.MIME)
+       mag.load()
+
+       logging.info("Fetching attachment filenames from archives")
+
+       for a in MailThreadAttachment.objects.filter(filename=""):
+               url = "/message-id.json/%s" % a.messageid
+               logging.debug("Checking attachment %s" % a.attachmentid)
+
+               h = httplib.HTTPConnection(settings.ARCHIVES_SERVER,
+                                                                  settings.ARCHIVES_PORT,
+                                                                  True,
+                                                                  settings.ARCHIVES_TIMEOUT)
+               h.request('GET', url, headers={
+                       'Host': settings.ARCHIVES_HOST,
+                       })
+               resp = h.getresponse()
+               if resp.status != 200:
+                       logging.error("Failed to get %s: %s" % (url, resp.status))
+                       continue
+
+               contents = resp.read()
+               resp.close()
+               h.close()
+
+               obj = simplejson.loads(contents)
+
+               try:
+                       for msg in obj:
+                               for att in msg['atts']:
+                                       if att['id'] == a.attachmentid:
+                                               print "id %s, att id %s, filename %s" % (a.id, a.attachmentid, att['name'])
+                                               a.filename = att['name']
+                                               a.save()
+                                               raise StopIteration
+                       logging.error("No match found for attachmentid %s" % a.attachmentid)
+               except StopIteration:
+                       # Success
+                       pass
+
+       connection.close()
+       logging.debug("Done.")