[PATCH 3/3] models: Pull common email behavior into mixin

Stephen Finucane stephen.finucane at intel.com
Wed Mar 9 08:54:04 AEDT 2016


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 at intel.com>
---
 patchwork/migrations/0008_add_email_mixin.py | 19 ++++++++
 patchwork/models.py                          | 66 +++++++++++-----------------
 2 files changed, 45 insertions(+), 40 deletions(-)
 create mode 100644 patchwork/migrations/0008_add_email_mixin.py

diff --git a/patchwork/migrations/0008_add_email_mixin.py b/patchwork/migrations/0008_add_email_mixin.py
new file mode 100644
index 0000000..e7ffd16
--- /dev/null
+++ b/patchwork/migrations/0008_add_email_mixin.py
@@ -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),
+        ),
+    ]
diff --git a/patchwork/models.py b/patchwork/models.py
index c481ece..480459b 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -300,12 +300,8 @@ class PatchManager(models.Manager):
         return self.get_queryset().with_tag_counts(project)
 
 
- at 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)
@@ -315,12 +311,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
+
+
+ at 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)
@@ -334,18 +351,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()
@@ -476,31 +481,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()
-- 
2.0.0



More information about the Patchwork mailing list