]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
models: Pull common email behavior into mixin
authorStephen Finucane <stephen.finucane@intel.com>
Thu, 15 Oct 2015 23:32:01 +0000 (00:32 +0100)
committerStephen Finucane <stephen.finucane@intel.com>
Wed, 16 Mar 2016 09:38:00 +0000 (09:38 +0000)
A "Patch" and "Comment" share a common ancestor: emails. As a result
of this many fields are similar between the two. Rather than
duplicating code, pull the similar code out of the aforementioned
classes and into a Mixin.

This has the side effect of removing restrictions on Comment.content,
but this is probably OK: it's possible that someone could send an
empty mail in reply to a patch and we should show that.

Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
Reviewed-by: Andy Doan <andy.doan@linaro.org>
patchwork/migrations/0008_add_email_mixin.py [new file with mode: 0644]
patchwork/models.py

diff --git a/patchwork/migrations/0008_add_email_mixin.py b/patchwork/migrations/0008_add_email_mixin.py
new file mode 100644 (file)
index 0000000..e7ffd16
--- /dev/null
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('patchwork', '0007_move_comment_content_to_patch_content'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='comment',
+            name='content',
+            field=models.TextField(null=True, blank=True),
+        ),
+    ]
index 3e701fd78423b966a2a3f77d81f1ca82520e606f..bdd797aaa8d414f93ed0ce8d0e06add588970d6a 100644 (file)
@@ -260,12 +260,8 @@ class PatchManager(models.Manager):
         return self.get_queryset().with_tag_counts(project)
 
 
-@python_2_unicode_compatible
-class Patch(models.Model):
-    # parent
-
-    project = models.ForeignKey(Project)
-
+class EmailMixin(models.Model):
+    """Mixin for models with an email-origin."""
     # email metadata
 
     msgid = models.CharField(max_length=255)
@@ -275,12 +271,33 @@ class Patch(models.Model):
     # content
 
     submitter = models.ForeignKey(Person)
-    name = models.CharField(max_length=255)
     content = models.TextField(null=True, blank=True)
-    diff = models.TextField(null=True, blank=True)
+
+    response_re = re.compile(
+        r'^(Tested|Reviewed|Acked|Signed-off|Nacked|Reported)-by: .*$',
+        re.M | re.I)
+
+    def patch_responses(self):
+        if not self.content:
+            return ''
+
+        return ''.join([match.group(0) + '\n' for match in
+                        self.response_re.finditer(self.content)])
+
+    class Meta:
+        abstract = True
+
+
+@python_2_unicode_compatible
+class Patch(EmailMixin, models.Model):
+    # parent
+
+    project = models.ForeignKey(Project)
 
     # patch metadata
 
+    name = models.CharField(max_length=255)
+    diff = models.TextField(null=True, blank=True)
     commit_ref = models.CharField(max_length=255, null=True, blank=True)
     pull_url = models.CharField(max_length=255, null=True, blank=True)
     tags = models.ManyToManyField(Tag, through=PatchTag)
@@ -294,18 +311,6 @@ class Patch(models.Model):
 
     objects = PatchManager()
 
-    response_re = re.compile(
-        r'^(Tested|Reviewed|Acked|Signed-off|Nacked|Reported)-by: .*$',
-        re.M | re.I)
-
-    # TODO(stephenfin) Drag this out into a mixin
-    def patch_responses(self):
-        if not self.content:
-            return ''
-
-        return ''.join([match.group(0) + '\n' for match in
-                        self.response_re.finditer(self.content)])
-
     def _set_tag(self, tag, count):
         if count == 0:
             self.patchtag_set.filter(tag=tag).delete()
@@ -436,31 +441,12 @@ class Patch(models.Model):
         unique_together = [('msgid', 'project')]
 
 
-class Comment(models.Model):
+class Comment(EmailMixin, models.Model):
     # parent
 
     patch = models.ForeignKey(Patch, related_name='comments',
                               related_query_name='comment')
 
-    # email metadata
-
-    msgid = models.CharField(max_length=255)
-    date = models.DateTimeField(default=datetime.datetime.now)
-    headers = models.TextField(blank=True)
-
-    # content
-
-    submitter = models.ForeignKey(Person)
-    content = models.TextField()
-
-    response_re = re.compile(
-        r'^(Tested|Reviewed|Acked|Signed-off|Nacked|Reported)-by: .*$',
-        re.M | re.I)
-
-    def patch_responses(self):
-        return ''.join([match.group(0) + '\n' for match in
-                        self.response_re.finditer(self.content)])
-
     def save(self, *args, **kwargs):
         super(Comment, self).save(*args, **kwargs)
         self.patch.refresh_tag_counts()