[PATCH 04/10] Move view-related code from utils->views/__init__
Stephen Finucane
stephen.finucane at intel.com
Mon Jan 11 10:07:53 AEDT 2016
These functions are view-related and belong in the views folder.
Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
patchwork/utils.py | 148 +-------------------------------------------
patchwork/views/__init__.py | 148 +++++++++++++++++++++++++++++++++++++++++++-
patchwork/views/bundle.py | 3 +-
3 files changed, 149 insertions(+), 150 deletions(-)
diff --git a/patchwork/utils.py b/patchwork/utils.py
index 0b6594d..62ffc3a 100644
--- a/patchwork/utils.py
+++ b/patchwork/utils.py
@@ -27,154 +27,10 @@ from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import EmailMessage
from django.db.models import Max, Q, F
-from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
-from patchwork.models import (Bundle, BundlePatch, PatchChangeNotification,
- EmailOptout, EmailConfirmation)
-
-
-def get_patch_ids(d, prefix='patch_id'):
- ids = []
-
- for (k, v) in d.items():
- a = k.split(':')
- if len(a) != 2:
- continue
- if a[0] != prefix:
- continue
- if not v:
- continue
- ids.append(a[1])
-
- return ids
-
-
-class Order(object):
- order_map = {
- 'date': 'date',
- 'name': 'name',
- 'state': 'state__ordering',
- 'submitter': 'submitter__name',
- 'delegate': 'delegate__username',
- }
- default_order = ('date', True)
-
- def __init__(self, str=None, editable=False):
- self.reversed = False
- self.editable = editable
- (self.order, self.reversed) = self.default_order
-
- if self.editable:
- return
-
- if str is None or str == '':
- return
-
- reversed = False
- if str[0] == '-':
- str = str[1:]
- reversed = True
-
- if str not in self.order_map:
- return
-
- self.order = str
- self.reversed = reversed
-
- def __str__(self):
- str = self.order
- if self.reversed:
- str = '-' + str
- return str
-
- def name(self):
- return self.order
-
- def reversed_name(self):
- if self.reversed:
- return self.order
- else:
- return '-' + self.order
-
- def updown(self):
- if self.reversed:
- return 'up'
- return 'down'
-
- def apply(self, qs):
- q = self.order_map[self.order]
- if self.reversed:
- q = '-' + 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):
- # set up the bundle
- 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()
- context.add_message("Bundle %s created" % bundle.name)
-
- elif action == 'add':
- bundle = get_object_or_404(Bundle, id=data['bundle_id'])
-
- elif action == 'remove':
- bundle = get_object_or_404(Bundle, id=data['removed_bundle_id'])
-
- if not bundle:
- return ['no such bundle']
-
- for patch in patches:
- if action == 'create' or action == 'add':
- bundlepatch_count = BundlePatch.objects.filter(bundle=bundle,
- patch=patch).count()
- if bundlepatch_count == 0:
- bundle.append_patch(patch)
- context.add_message("Patch '%s' added to bundle %s" %
- (patch.name, bundle.name))
- else:
- context.add_message("Patch '%s' already in bundle %s" %
- (patch.name, bundle.name))
-
- elif action == 'remove':
- try:
- bp = BundlePatch.objects.get(bundle=bundle, patch=patch)
- bp.delete()
- context.add_message("Patch '%s' removed from bundle %s\n" %
- (patch.name, bundle.name))
- except Exception:
- pass
-
- bundle.save()
-
- return []
+from patchwork.models import (PatchChangeNotification, EmailOptout,
+ EmailConfirmation)
def send_notifications():
diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py
index 3a0c8e9..325cc17 100644
--- a/patchwork/views/__init__.py
+++ b/patchwork/views/__init__.py
@@ -31,10 +31,154 @@ from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404
from patchwork.forms import MultiplePatchForm
-from patchwork.models import Comment, Patch, EmailConfirmation
+from patchwork.models import (Bundle, BundlePatch, Comment, Patch,
+ EmailConfirmation)
from patchwork.paginator import Paginator
from patchwork.requestcontext import PatchworkRequestContext
-from patchwork.utils import Order, get_patch_ids, bundle_actions, set_bundle
+
+
+bundle_actions = ['create', 'add', 'remove']
+
+
+def get_patch_ids(d, prefix='patch_id'):
+ ids = []
+
+ for (k, v) in d.items():
+ a = k.split(':')
+ if len(a) != 2:
+ continue
+ if a[0] != prefix:
+ continue
+ if not v:
+ continue
+ ids.append(a[1])
+
+ return ids
+
+
+class Order(object):
+ order_map = {
+ 'date': 'date',
+ 'name': 'name',
+ 'state': 'state__ordering',
+ 'submitter': 'submitter__name',
+ 'delegate': 'delegate__username',
+ }
+ default_order = ('date', True)
+
+ def __init__(self, str=None, editable=False):
+ self.reversed = False
+ self.editable = editable
+ (self.order, self.reversed) = self.default_order
+
+ if self.editable:
+ return
+
+ if str is None or str == '':
+ return
+
+ reversed = False
+ if str[0] == '-':
+ str = str[1:]
+ reversed = True
+
+ if str not in self.order_map:
+ return
+
+ self.order = str
+ self.reversed = reversed
+
+ def __str__(self):
+ str = self.order
+ if self.reversed:
+ str = '-' + str
+ return str
+
+ def name(self):
+ return self.order
+
+ def reversed_name(self):
+ if self.reversed:
+ return self.order
+ else:
+ return '-' + self.order
+
+ def updown(self):
+ if self.reversed:
+ return 'up'
+ return 'down'
+
+ def apply(self, qs):
+ q = self.order_map[self.order]
+ if self.reversed:
+ q = '-' + 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)
+
+
+def set_bundle(user, project, action, data, patches, context):
+ # set up the bundle
+ 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()
+ context.add_message("Bundle %s created" % bundle.name)
+
+ elif action == 'add':
+ bundle = get_object_or_404(Bundle, id=data['bundle_id'])
+
+ elif action == 'remove':
+ bundle = get_object_or_404(Bundle, id=data['removed_bundle_id'])
+
+ if not bundle:
+ return ['no such bundle']
+
+ for patch in patches:
+ if action == 'create' or action == 'add':
+ bundlepatch_count = BundlePatch.objects.filter(bundle=bundle,
+ patch=patch).count()
+ if bundlepatch_count == 0:
+ bundle.append_patch(patch)
+ context.add_message("Patch '%s' added to bundle %s" %
+ (patch.name, bundle.name))
+ else:
+ context.add_message("Patch '%s' already in bundle %s" %
+ (patch.name, bundle.name))
+
+ elif action == 'remove':
+ try:
+ bp = BundlePatch.objects.get(bundle=bundle, patch=patch)
+ bp.delete()
+ context.add_message("Patch '%s' removed from bundle %s\n" %
+ (patch.name, bundle.name))
+ except Exception:
+ pass
+
+ bundle.save()
+
+ return []
def generic_list(request, project, view,
diff --git a/patchwork/views/bundle.py b/patchwork/views/bundle.py
index 04067ff..c180cde 100644
--- a/patchwork/views/bundle.py
+++ b/patchwork/views/bundle.py
@@ -29,8 +29,7 @@ from patchwork.filters import DelegateFilter
from patchwork.forms import BundleForm, DeleteBundleForm
from patchwork.models import Patch, Bundle, BundlePatch, Project
from patchwork.requestcontext import PatchworkRequestContext
-from patchwork.utils import get_patch_ids
-from patchwork.views import generic_list, patch_to_mbox
+from patchwork.views import generic_list, patch_to_mbox, get_patch_ids
@login_required
--
2.0.0
More information about the Patchwork
mailing list