Patchwork [v2,2/5] models: Add sent_from() and need_sent_from() methods to Patch model

login
register
mail settings
Submitter Doug Anderson
Date Nov. 18, 2013, 6 a.m.
Message ID <1384754442-27219-3-git-send-email-dianders@chromium.org>
Download mbox | patch
Permalink /patch/291946/
State New
Headers show

Comments

Doug Anderson - Nov. 18, 2013, 6 a.m.
The sent_from will return the actual name/address that a patch was
sent from.  This is useful in the case where the patchwork "Submitter"
name doesn't match the name that was actually associated with the
patch (even though the email address must match).

The need_sent_from() tells you when we need to include the "sent_from"
because the patchwork Submitter name didn't match the name associated
with the patch.

Signed-off-by: Doug Anderson <dianders@chromium.org>
---
 apps/patchwork/models.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Patch

diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py
index ec5727d..c4f79f8 100644
--- a/apps/patchwork/models.py
+++ b/apps/patchwork/models.py
@@ -22,12 +22,19 @@  from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.contrib.sites.models import Site
 from django.conf import settings
+from patchwork.emailutils import clean_header, parse_from
 from patchwork.parser import hash_patch
 
 import re
 import datetime, time
 import random
 
+try:
+    from email.parser import HeaderParser
+except ImportError:
+    # Python 2.4 compatibility
+    from email.Parser import HeaderParser
+
 class Person(models.Model):
     email = models.CharField(max_length=255, unique = True)
     name = models.CharField(max_length=255, null = True, blank = True)
@@ -197,6 +204,23 @@  class Patch(models.Model):
     def comments(self):
         return Comment.objects.filter(patch = self)
 
+    def sent_from(self):
+        """Return who the patch was sent from.
+
+        We'll use the raw header (but decoded to Unicode) since that's what will
+        be in the .patch file that's applied by the user.
+
+        @return: A unicode string representing the "From" of the original email.
+        """
+        orig_headers = HeaderParser().parsestr(str(self.headers))
+        return clean_header(orig_headers.get('From'))
+
+    def need_sent_from(self):
+        """Return true if the name in the "From" doesn't match the submitter."""
+        orig_headers = HeaderParser().parsestr(str(self.headers))
+        name, _ = parse_from(orig_headers.get('From'))
+        return name != self.submitter.name
+
     def save(self):
         try:
             s = self.state