]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/utils.py
pwclient: honor PAGER for view
[patchwork] / apps / patchwork / utils.py
index 1771167ae12f35fb55479c35aa865be496ce2664..9ed9e41e35f60affafe0e791ed01d978a6c271ad 100644 (file)
@@ -22,14 +22,15 @@ import itertools
 import datetime
 from django.shortcuts import get_object_or_404
 from django.template.loader import render_to_string
+from django.contrib.auth.models import User
 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.models import Max, Q, F
 from django.db.utils import IntegrityError
 from patchwork.forms import MultiplePatchForm
 from patchwork.models import Bundle, Project, BundlePatch, UserProfile, \
-        PatchChangeNotification, EmailOptout
+        PatchChangeNotification, EmailOptout, EmailConfirmation
 
 def get_patch_ids(d, prefix = 'patch_id'):
     ids = []
@@ -93,11 +94,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
+
+        orders = [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
+            orders.append(q)
+
+        return qs.order_by(*orders)
 
 bundle_actions = ['create', 'add', 'remove']
 def set_bundle(user, project, action, data, patches, context):
@@ -105,6 +118,9 @@ 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']
 
@@ -172,10 +188,11 @@ 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(
-                                pk__in = notifications).delete()
+            pks = [ n.pk for n in notifications ]
+            PatchChangeNotification.objects.filter(pk__in = pks).delete()
 
         if EmailOptout.is_optout(recipient.email):
             delete_notifications()
@@ -185,7 +202,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()
@@ -206,3 +225,24 @@ def send_notifications():
         delete_notifications()
 
     return errors
+
+def do_expiry():
+    # expire any pending confirmations
+    q = (Q(date__lt = datetime.datetime.now() - EmailConfirmation.validity) |
+            Q(active = False))
+    EmailConfirmation.objects.filter(q).delete()
+
+    # expire inactive users with no pending confirmation
+    pending_confs = EmailConfirmation.objects.values('user')
+    users = User.objects.filter(
+                is_active = False,
+                last_login = F('date_joined')
+            ).exclude(
+                id__in = pending_confs
+            )
+
+    # delete users
+    users.delete()
+
+
+