From patchwork Tue Mar 8 21:54:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 594358 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 77A2F14012C for ; Wed, 9 Mar 2016 08:54:46 +1100 (AEDT) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 5D9371A0341 for ; Wed, 9 Mar 2016 08:54:46 +1100 (AEDT) X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lists.ozlabs.org (Postfix) with ESMTP id 087581A02FA for ; Wed, 9 Mar 2016 08:54:24 +1100 (AEDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga102.jf.intel.com with ESMTP; 08 Mar 2016 13:54:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,558,1449561600"; d="scan'208";a="932533977" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga002.fm.intel.com with ESMTP; 08 Mar 2016 13:54:17 -0800 Received: from sivswdev01.ir.intel.com (sivswdev01.ir.intel.com [10.237.217.45]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id u28LsF7Q026769; Tue, 8 Mar 2016 21:54:15 GMT Received: from sivswdev01.ir.intel.com (localhost [127.0.0.1]) by sivswdev01.ir.intel.com with ESMTP id u28LsFkE032188; Tue, 8 Mar 2016 21:54:15 GMT Received: (from sfinucan@localhost) by sivswdev01.ir.intel.com with id u28LsF8a032184; Tue, 8 Mar 2016 21:54:15 GMT From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH 3/3] models: Pull common email behavior into mixin Date: Tue, 8 Mar 2016 21:54:04 +0000 Message-Id: <1457474044-32132-3-git-send-email-stephen.finucane@intel.com> X-Mailer: git-send-email 2.0.0 In-Reply-To: <1457474044-32132-1-git-send-email-stephen.finucane@intel.com> References: <1457474044-32132-1-git-send-email-stephen.finucane@intel.com> X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" 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 --- 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) -@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 + + +@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()