X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fmodels.py;h=d0c2a6ed5d4c467fe289aa2ea1eddc64096c6786;hb=c105cb0;hp=bf8efba20b6c0cf788d5e014951f20bfd50ee32d;hpb=f26f929de02369cf73fc8ab54fff3a046855dd9f;p=patchwork diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index bf8efba..d0c2a6e 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -23,7 +23,6 @@ from django.core.urlresolvers import reverse from django.contrib.sites.models import Site from django.conf import settings from patchwork.parser import hash_patch -import django.oldforms as oldforms import re import datetime, time @@ -31,11 +30,13 @@ import string import random try: - from email.mime.text import MIMEText + from email.mime.nonmultipart import MIMENonMultipart + from email.encoders import encode_7or8bit import email.utils except ImportError: # Python 2.4 compatibility - from email.MIMEText import MIMEText + from email.MIMENonMultipart import MIMENonMultipart + from email.Encoders import encode_7or8bit import email.Utils email.utils = email.Utils @@ -167,9 +168,17 @@ class HashField(models.CharField): def db_type(self): return 'char(%d)' % self.n_bytes +class PatchMbox(MIMENonMultipart): + patch_charset = 'utf-8' + def __init__(self, _text): + MIMENonMultipart.__init__(self, 'text', 'plain', + **{'charset': self.patch_charset}) + self.set_payload(_text.encode(self.patch_charset)) + encode_7or8bit(self) + class Patch(models.Model): project = models.ForeignKey(Project) - msgid = models.CharField(max_length=255, unique = True) + msgid = models.CharField(max_length=255) name = models.CharField(max_length=255) date = models.DateTimeField(default=datetime.datetime.now) submitter = models.ForeignKey(Person) @@ -238,11 +247,11 @@ class Patch(models.Model): body += self.content - mail = MIMEText(body) + mail = PatchMbox(body) mail['Subject'] = self.name mail['Date'] = email.utils.formatdate( time.mktime(self.date.utctimetuple())) - mail['From'] = str(self.submitter) + mail['From'] = unicode(self.submitter) mail['X-Patchwork-Id'] = str(self.id) mail.set_unixfrom('From patchwork ' + self.date.ctime()) @@ -256,10 +265,11 @@ class Patch(models.Model): class Meta: verbose_name_plural = 'Patches' ordering = ['date'] + unique_together = [('msgid', 'project')] class Comment(models.Model): patch = models.ForeignKey(Patch) - msgid = models.CharField(max_length=255, unique = True) + msgid = models.CharField(max_length=255) submitter = models.ForeignKey(Person) date = models.DateTimeField(default = datetime.datetime.now) headers = models.TextField(blank = True) @@ -273,17 +283,26 @@ class Comment(models.Model): class Meta: ordering = ['date'] + unique_together = [('msgid', 'patch')] class Bundle(models.Model): owner = models.ForeignKey(User) project = models.ForeignKey(Project) name = models.CharField(max_length = 50, null = False, blank = False) - patches = models.ManyToManyField(Patch) + patches = models.ManyToManyField(Patch, through = 'BundlePatch') public = models.BooleanField(default = False) def n_patches(self): return self.patches.all().count() + def append_patch(self, patch): + # todo: use the aggregate queries in django 1.1 + orders = BundlePatch.objects.filter(bundle = self).values('order') + max_order = max([ v for (k, v) in orders]) + + bp = BundlePatch.objects.create(bundle = self, patch = patch, order = max_order + 1) + bp.save() + class Meta: unique_together = [('owner', 'name')] @@ -302,6 +321,14 @@ class Bundle(models.Model): return '\n'.join([p.mbox().as_string(True) \ for p in self.patches.all()]) +class BundlePatch(models.Model): + patch = models.ForeignKey(Patch) + bundle = models.ForeignKey(Bundle) + order = models.IntegerField() + + class Meta: + unique_together = [('bundle', 'patch'), ('bundle', 'order')] + class UserPersonConfirmation(models.Model): user = models.ForeignKey(User) email = models.CharField(max_length = 200) @@ -314,7 +341,7 @@ class UserPersonConfirmation(models.Model): return person = None try: - person = Person.objects.get(email = self.email) + person = Person.objects.get(email__iexact = self.email) except Exception: pass if not person: