From: Stephen Finucane Date: Mon, 7 Dec 2015 21:07:48 +0000 (+0000) Subject: Move view-related code from utils->views/__init__ X-Git-Tag: v1.1.0~69 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=78333195e2c571c66f22c60b801dba91d8d19b8b;p=thirdparty%2Fpatchwork.git Move view-related code from utils->views/__init__ These functions are view-related and belong in the views folder. Signed-off-by: Stephen Finucane --- diff --git a/patchwork/utils.py b/patchwork/utils.py index 0b6594d4..62ffc3af 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 4cfbc2cf..00ff96e8 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 04067ffc..c180cde5 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