]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/models.py
[models] Make patches unique on (msgid, project), not just (msgid)
[patchwork] / apps / patchwork / models.py
index c36f960827b333573f89e7690d1d49b021a26a1a..91c0f97f3f02e1208047757a1c022ccdd94df205 100644 (file)
@@ -23,7 +23,6 @@ from django.core.urlresolvers import reverse
 from django.contrib.sites.models import Site
 from django.conf import settings
 from patchwork.parser import hash_patch
-import django.oldforms as oldforms
 
 import re
 import datetime, time
@@ -31,11 +30,13 @@ import string
 import random
 
 try:
-    from email.mime.text import MIMEText
+    from email.mime.nonmultipart import MIMENonMultipart
+    from email.encoders import encode_7or8bit
     import email.utils
 except ImportError:
     # Python 2.4 compatibility
-    from email.MIMEText import MIMEText
+    from email.MIMENonMultipart import MIMENonMultipart
+    from email.Encoders import encode_7or8bit
     import email.Utils
     email.utils = email.Utils
 
@@ -167,9 +168,17 @@ class HashField(models.CharField):
     def db_type(self):
         return 'char(%d)' % self.n_bytes
 
+class PatchMbox(MIMENonMultipart):
+    patch_charset = 'utf-8'
+    def __init__(self, _text):
+        MIMENonMultipart.__init__(self, 'text', 'plain',
+                        **{'charset': self.patch_charset})
+        self.set_payload(_text.encode(self.patch_charset))
+        encode_7or8bit(self)
+
 class Patch(models.Model):
     project = models.ForeignKey(Project)
-    msgid = models.CharField(max_length=255, unique = True)
+    msgid = models.CharField(max_length=255)
     name = models.CharField(max_length=255)
     date = models.DateTimeField(default=datetime.datetime.now)
     submitter = models.ForeignKey(Person)
@@ -220,20 +229,29 @@ class Patch(models.Model):
     def mbox(self):
         comment = None
         try:
-            comment = Comment.objects.get(msgid = self.msgid)
+            comment = Comment.objects.get(patch = self, msgid = self.msgid)
         except Exception:
             pass
 
         body = ''
         if comment:
-            body = comment.content.strip() + "\n\n"
+            body = comment.content.strip() + "\n"
+
+        responses = False
+        for comment in Comment.objects.filter(patch = self) \
+                .exclude(msgid = self.msgid):
+            body += comment.patch_responses()
+
+        if body:
+            body += '\n'
+
         body += self.content
 
-        mail = MIMEText(body)
+        mail = PatchMbox(body)
         mail['Subject'] = self.name
         mail['Date'] = email.utils.formatdate(
                         time.mktime(self.date.utctimetuple()))
-        mail['From'] = str(self.submitter)
+        mail['From'] = unicode(self.submitter)
         mail['X-Patchwork-Id'] = str(self.id)
         mail.set_unixfrom('From patchwork ' + self.date.ctime())
 
@@ -247,17 +265,25 @@ class Patch(models.Model):
     class Meta:
         verbose_name_plural = 'Patches'
         ordering = ['date']
+        unique_together = [('msgid', 'project')]
 
 class Comment(models.Model):
     patch = models.ForeignKey(Patch)
-    msgid = models.CharField(max_length=255, unique = True)
+    msgid = models.CharField(max_length=255)
     submitter = models.ForeignKey(Person)
     date = models.DateTimeField(default = datetime.datetime.now)
     headers = models.TextField(blank = True)
     content = models.TextField()
 
+    response_re = re.compile('^(Acked|Signed-off|Nacked)-by: .*$', re.M)
+
+    def patch_responses(self):
+        return ''.join([ match.group(0) + '\n' for match in \
+                                self.response_re.finditer(self.content)])
+
     class Meta:
         ordering = ['date']
+        unique_together = [('msgid', 'patch')]
 
 class Bundle(models.Model):
     owner = models.ForeignKey(User)