]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/utils.py
views/order: Apply default ordering as secondary
[patchwork] / apps / patchwork / utils.py
index e7619c319091ce2dd9e78396f24ffbe8dac1f6db..37b85ce1f503396c3363d2ad3f59c70315471134 100644 (file)
@@ -26,6 +26,7 @@ from django.contrib.sites.models import Site
 from django.conf import settings
 from django.core.mail import EmailMessage
 from django.db.models import Max
+from django.db.utils import IntegrityError
 from patchwork.forms import MultiplePatchForm
 from patchwork.models import Bundle, Project, BundlePatch, UserProfile, \
         PatchChangeNotification, EmailOptout
@@ -92,11 +93,23 @@ class Order(object):
         else:
             return '-' + self.order
 
-    def query(self):
+    def apply(self, qs):
         q = self.order_map[self.order]
         if self.reversed:
             q = '-' + q
-        return q
+
+        qs = qs.order_by(q)
+
+        # if we're using a non-default order, add the default as a secondary
+        # ordering. We reverse the default if the primary is reversed.
+        (default_name, default_reverse) = self.default_order
+        if self.order != default_name:
+            q = self.order_map[default_name]
+            if self.reversed ^ default_reverse:
+                q = '-' + q
+            qs = qs.order_by(q)
+
+        return qs
 
 bundle_actions = ['create', 'add', 'remove']
 def set_bundle(user, project, action, data, patches, context):
@@ -104,9 +117,15 @@ def set_bundle(user, project, action, data, patches, context):
     bundle = None
     if action == 'create':
         bundle_name = data['bundle_name'].strip()
+        if '/' in bundle_name:
+            return ['Bundle names can\'t contain slashes']
+
         if not bundle_name:
             return ['No bundle name was specified']
 
+        if Bundle.objects.filter(owner = user, name = bundle_name).count() > 0:
+            return ['You already have a bundle called "%s"' % bundle_name]
+
         bundle = Bundle(owner = user, project = project,
                 name = bundle_name)
         bundle.save()
@@ -168,6 +187,7 @@ def send_notifications():
 
     for (recipient, notifications) in groups:
         notifications = list(notifications)
+        projects = set([ n.patch.project.linkname for n in notifications ])
 
         def delete_notifications():
             PatchChangeNotification.objects.filter(
@@ -181,7 +201,9 @@ def send_notifications():
             'site': Site.objects.get_current(),
             'person': recipient,
             'notifications': notifications,
+            'projects': projects,
         }
+
         subject = render_to_string(
                         'patchwork/patch-change-notification-subject.text',
                         context).strip()