[PATCH 1/2] Use an explicit initial default patch state

Dirk Wallenstein halsmit at t-online.de
Sun Apr 29 23:11:48 EST 2012


This will make editing states through the admin UI less error-prone, and
will facilitate parsing patches when relying on a default state, as well as
testing.

Use the explicit default state when parsing mails.

Signed-off-by: Dirk Wallenstein <halsmit at t-online.de>
---
I've found this very informative.  I somehow missed a way to obtain the
default, otherwise.
http://djangodays.com/2009/05/11/django-foreign-key-default-value-example/

 apps/patchwork/bin/parsemail.py |    5 ++---
 apps/patchwork/models.py        |    5 ++++-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/apps/patchwork/bin/parsemail.py b/apps/patchwork/bin/parsemail.py
index 52c85fe..f156c42 100755
--- a/apps/patchwork/bin/parsemail.py
+++ b/apps/patchwork/bin/parsemail.py
@@ -34,10 +34,9 @@ except ImportError:
     from email.Utils import parsedate_tz, mktime_tz
 
 from patchwork.parser import parse_patch
-from patchwork.models import Patch, Project, Person, Comment, State
+from patchwork.models import Patch, Project, Person, Comment, State, get_default_initial_patch_state
 from django.contrib.auth.models import User
 
-default_patch_state = 'New'
 list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list']
 
 whitespace_re = re.compile('\s+')
@@ -349,7 +348,7 @@ def get_state(state_name):
             return State.objects.get(name__iexact=state_name)
         except State.DoesNotExist:
             pass
-    return State.objects.get(name=default_patch_state)
+    return get_default_initial_patch_state()
 
 def get_delegate(delegate_email):
     """ Return the delegate with the given email or None """
diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py
index bb8d8e7..09e2c34 100644
--- a/apps/patchwork/models.py
+++ b/apps/patchwork/models.py
@@ -191,6 +191,9 @@ class PatchMbox(MIMENonMultipart):
         self.set_payload(_text.encode(self.patch_charset))
         encode_7or8bit(self)
 
+def get_default_initial_patch_state():
+    return State.objects.get(name="New")
+
 class Patch(models.Model):
     project = models.ForeignKey(Project)
     msgid = models.CharField(max_length=255)
@@ -198,7 +201,7 @@ class Patch(models.Model):
     date = models.DateTimeField(default=datetime.datetime.now)
     submitter = models.ForeignKey(Person)
     delegate = models.ForeignKey(User, blank = True, null = True)
-    state = models.ForeignKey(State)
+    state = models.ForeignKey(State, default=get_default_initial_patch_state)
     archived = models.BooleanField(default = False)
     headers = models.TextField(blank = True)
     content = models.TextField(null = True, blank = True)
-- 
1.7.5.4



More information about the Patchwork mailing list