[Patchew-devel] [PATCH v2 4/4] views: include tags in download link

Paolo Bonzini pbonzini at redhat.com
Fri Sep 28 14:58:45 UTC 2018


Retrieve the tags that have been collected by the tags module, and place them
in the mbox before the --- line.

Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
---
 api/models.py |  2 +-
 www/views.py  | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/api/models.py b/api/models.py
index ab0bd06..a688c77 100644
--- a/api/models.py
+++ b/api/models.py
@@ -336,7 +336,7 @@ class MessageManager(models.Manager):
     def find_series(self, message_id, project_name=None):
         return self.series_heads(project_name).filter(message_id=message_id).first()
 
-    def find_message(self, message_id, project_name):
+    def find_message(self, message_id, project_name=None):
         return self.project_messages(project_name).filter(message_id=message_id).first()
 
     def patches(self):
diff --git a/www/views.py b/www/views.py
index 11043a1..daa2362 100644
--- a/www/views.py
+++ b/www/views.py
@@ -16,7 +16,11 @@ from django.urls import reverse
 from django.utils.html import format_html
 from django.conf import settings
 import api
+import email
+from mbox import decode_payload
+import re
 from mod import dispatch_module_hook
+from patchew.tags import lines_iter
 import subprocess
 
 PAGE_SIZE = 50
@@ -247,17 +251,54 @@ def view_series_list(request, project):
     return render_series_list_page(request, query, project=project)
 
 def view_mbox(request, project, message_id):
-    s = api.models.Message.objects.find_message(project, message_id)
+    # FIXME: what happens with base64 messages?
+    def mbox_with_tags_iter(mbox, tags):
+        regex = "^[-A-Za-z]*:"
+        old_tags = set()
+        lines = lines_iter(mbox)
+        need_minusminusminus = False
+        for line in lines:
+            if line.startswith('---'):
+                need_minusminusminus = True
+                break
+            yield line
+            if re.match(regex, line):
+                old_tags.add(line)
+
+        # If no --- line, tags go at the end as there's no better place
+        for tag in tags:
+            if not tag in old_tags:
+                yield tag
+        if need_minusminusminus:
+            yield line
+        yield from lines
+
+    def get_mbox_with_tags(m):
+        mbox = m.get_mbox()
+        try:
+            msg = email.message_from_string(mbox)
+        except:
+            return mbox
+        container = msg.get_payload(0) if msg.is_multipart() else msg
+        if container.get_content_type() != "text/plain":
+            return mbox
+
+        payload = decode_payload(container)
+        container.set_payload('\n'.join(mbox_with_tags_iter(payload, m.tags)))
+        return msg.as_string()
+
+    s = api.models.Message.objects.find_message(message_id, project_name=project)
+    global _instance
     if not s:
         raise Http404("Series not found")
-    if s.is_cover_letter:
+    if not s.is_patch:
         if not s.is_complete:
             raise Http404("Series not complete")
         messages = s.get_patches()
     else:
         messages = [s]
     mbox = "\n".join(["From %s %s\n" % (x.get_sender_addr(), x.get_asctime()) + \
-                      x.get_mbox(x) for x in messages])
+                      get_mbox_with_tags(x) for x in messages])
     return HttpResponse(mbox, content_type="text/plain")
 
 def view_series_detail(request, project, message_id):
-- 
2.17.1




More information about the Patchew-devel mailing list