From: Stephen Finucane Date: Thu, 15 Oct 2015 23:32:01 +0000 (+0100) Subject: models: Pull common email behavior into mixin X-Git-Tag: v2.0.0-rc1~413 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=070083154c3cebb0f44e52fe742b00e68442ef22;p=thirdparty%2Fpatchwork.git models: Pull common email behavior into mixin 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 Reviewed-by: Andy Doan --- diff --git a/patchwork/migrations/0008_add_email_mixin.py b/patchwork/migrations/0008_add_email_mixin.py new file mode 100644 index 00000000..e7ffd161 --- /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 3e701fd7..bdd797aa 100644 --- a/patchwork/models.py +++ b/patchwork/models.py @@ -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()