]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/models.py
Add nacked-by tag
[patchwork] / apps / patchwork / models.py
index e2b636ead6208c8a62f9d02f40f9a60d704c581c..fa213dc03e8dfa9a068e11f780d581a358acdb4d 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
 
@@ -129,35 +130,6 @@ class UserProfile(models.Model):
     def __str__(self):
         return self.name()
 
-def _confirm_key():
-    allowedchars = string.ascii_lowercase + string.digits
-    str = ''
-    for i in range(1, 32):
-        str += random.choice(allowedchars)
-    return str;
-
-class UserPersonConfirmation(models.Model):
-    user = models.ForeignKey(User)
-    email = models.CharField(max_length = 200)
-    key = models.CharField(max_length = 32, default = _confirm_key)
-    date = models.DateTimeField(default=datetime.datetime.now)
-    active = models.BooleanField(default = True)
-
-    def confirm(self):
-        if not self.active:
-            return
-        person = None
-        try:
-            person = Person.objects.get(email = self.email)
-        except Exception:
-            pass
-        if not person:
-            person = Person(email = self.email)
-
-        person.link_to_user(self.user)
-        person.save()
-        self.active = False
-
 class State(models.Model):
     name = models.CharField(max_length = 100)
     ordering = models.IntegerField(unique = True)
@@ -176,34 +148,37 @@ class HashField(models.CharField):
         self.algorithm = algorithm
         try:
             import hashlib
-            self.hashlib = True
-            n_bytes = len(hashlib.new(self.algorithm).hexdigest())
+            def _construct(string = ''):
+                return hashlib.new(self.algorithm, string)
+            self.construct = _construct
+            self.n_bytes = len(hashlib.new(self.algorithm).hexdigest())
         except ImportError:
-            self.hashlib = False
-            if algorithm == 'sha1':
-                import sha
-                self.hash_constructor = sha.new
-            elif algorithm == 'md5':
-                import md5
-                self.hash_constructor = md5.new
-            else:
+            modules = { 'sha1': 'sha', 'md5': 'md5'}
+
+            if algorithm not in modules.keys():
                 raise NameError("Unknown algorithm '%s'" % algorithm)
-            n_bytes = len(self.hash_constructor().hexdigest())
 
-        kwargs['max_length'] = n_bytes
+            self.construct = __import__(modules[algorithm]).new
+
+        self.n_bytes = len(self.construct().hexdigest())
+
+        kwargs['max_length'] = self.n_bytes
         super(HashField, self).__init__(*args, **kwargs)
 
     def db_type(self):
-        if self.hashlib:
-            import hashlib
-            n_bytes = len(hashlib.new(self.algorithm).hexdigest())
-        else:
-            n_bytes = len(self.hash_constructor().hexdigest())
-        return 'char(%d)' % n_bytes
+        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)
@@ -252,22 +227,44 @@ class Patch(models.Model):
         return str.strip('-') + '.patch'
 
     def mbox(self):
+        postscript_re = re.compile('\n-{2,3} ?\n')
+
         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 += self.content
+            body = comment.content.strip() + "\n"
+
+        parts = postscript_re.split(body, 1)
+        if len(parts) == 2:
+            (body, postscript) = parts
+            body = body.strip() + "\n"
+            postscript = postscript.strip() + "\n"
+        else:
+            postscript = ''
+
+        responses = False
+        for comment in Comment.objects.filter(patch = self) \
+                .exclude(msgid = self.msgid):
+            body += comment.patch_responses()
 
-        mail = MIMEText(body)
+        if body:
+            body += '\n'
+
+        if postscript:
+            body += '---\n' + postscript.strip() + '\n'
+
+        body += '\n' + self.content
+
+        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())
 
@@ -281,28 +278,57 @@ 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('^(Tested|Reviewed|Acked|Signed-off|Nacked)-by: .*$', re.M | re.I)
+
+    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)
     project = models.ForeignKey(Project)
     name = models.CharField(max_length = 50, null = False, blank = False)
-    patches = models.ManyToManyField(Patch)
+    patches = models.ManyToManyField(Patch, through = 'BundlePatch')
     public = models.BooleanField(default = False)
 
     def n_patches(self):
         return self.patches.all().count()
 
+    def ordered_patches(self):
+        return self.patches.order_by('bundlepatch__order');
+
+    def append_patch(self, patch):
+        # todo: use the aggregate queries in django 1.1
+        orders = BundlePatch.objects.filter(bundle = self).order_by('-order') \
+                 .values('order')
+
+        if len(orders) > 0:
+            max_order = orders[0]['order']
+        else:
+            max_order = 0
+
+        # see if the patch is already in this bundle
+        if BundlePatch.objects.filter(bundle = self, patch = patch).count():
+            raise Exception("patch is already in bundle")
+
+        bp = BundlePatch.objects.create(bundle = self, patch = patch,
+                order = max_order + 1)
+        bp.save()
+
     class Meta:
         unique_together = [('owner', 'name')]
 
@@ -319,5 +345,45 @@ class Bundle(models.Model):
 
     def mbox(self):
         return '\n'.join([p.mbox().as_string(True) \
-                        for p in self.patches.all()])
+                        for p in self.ordered_patches()])
+
+class BundlePatch(models.Model):
+    patch = models.ForeignKey(Patch)
+    bundle = models.ForeignKey(Bundle)
+    order = models.IntegerField()
+
+    class Meta:
+        unique_together = [('bundle', 'patch')]
+        ordering = ['order']
+
+class UserPersonConfirmation(models.Model):
+    user = models.ForeignKey(User)
+    email = models.CharField(max_length = 200)
+    key = HashField()
+    date = models.DateTimeField(default=datetime.datetime.now)
+    active = models.BooleanField(default = True)
+
+    def confirm(self):
+        if not self.active:
+            return
+        person = None
+        try:
+            person = Person.objects.get(email__iexact = self.email)
+        except Exception:
+            pass
+        if not person:
+            person = Person(email = self.email)
+
+        person.link_to_user(self.user)
+        person.save()
+        self.active = False
+        self.save()
+
+    def save(self):
+        max = 1 << 32
+        if self.key == '':
+            str = '%s%s%d' % (self.user, self.email, random.randint(0, max))
+            self.key = self._meta.get_field('key').construct(str).hexdigest()
+        super(UserPersonConfirmation, self).save()
+