def set_patches(user, project, action, data, patches, context): errors = [] form = MultiplePatchForm(project = project, data = data) try: project = Project.objects.get(id = data['project']) except: errors = ['No such project'] return (errors, form) str = '' # this may be a bundle action, which doesn't modify a patch. in this # case, don't require a valid form, or patch editing permissions if action in bundle_actions: errors = set_bundle(user, project, action, data, patches, context) return (errors, form) if not form.is_valid(): errors = ['The submitted form data was invalid'] return (errors, form) for patch in patches: if not patch.is_editable(user): errors.append('You don\'t have permissions to edit the ' + \ 'patch "%s"' \ % patch.name) continue if action == 'update': form.save(patch) str = 'updated' elif action == 'ack': pass elif action == 'archive': patch.archived = True patch.save() str = 'archived' elif action == 'unarchive': patch.archived = False patch.save() str = 'un-archived' elif action == 'delete': patch.delete() str = 'un-archived' if len(patches) > 0: if len(patches) == 1: str = 'patch ' + str else: str = 'patches ' + str context.add_message(str) return (errors, form)
def generic_list(request, project, view, view_args=None, filter_settings=None, patches=None, editable_order=False): if not filter_settings: filter_settings = [] filters = Filters(request) context = { 'project': project, 'projects': Project.objects.all(), 'filters': filters, } # pagination params = filters.params() for param in ['order', 'page']: data = {} if request.method == 'GET': data = request.GET elif request.method == 'POST': data = request.POST value = data.get(param, None) if value: params.append((param, value)) data = {} if request.method == 'GET': data = request.GET elif request.method == 'POST': data = request.POST order = Order(data.get('order'), editable=editable_order) context.update({ 'order': order, 'list_view': { 'view': view, 'view_params': view_args or {}, 'params': params }}) # form processing # Explicitly set data to None because request.POST will be an empty dict # when the form is not submitted, but passing a non-None data argument to # a forms.Form will make it bound and we don't want that to happen unless # there's been a form submission. if request.method != 'POST': data = None user = request.user properties_form = None if user.is_authenticated: # we only pass the post data to the MultiplePatchForm if that was # the actual form submitted data_tmp = None if data and data.get('form', '') == 'patchlistform': data_tmp = data properties_form = MultiplePatchForm(project, data=data_tmp) if request.method == 'POST' and data.get('form') == 'patchlistform': action = data.get('action', '').lower() # special case: the user may have hit enter in the 'create bundle' # text field, so if non-empty, assume the create action: if data.get('bundle_name', False): action = 'create' ps = Patch.objects.filter(id__in=get_patch_ids(data)) if action in bundle_actions: errors = set_bundle(request, project, action, data, ps, context) elif properties_form and action == properties_form.action: errors = process_multiplepatch_form(request, properties_form, action, ps, context) else: errors = [] if errors: context['errors'] = errors for (filterclass, setting) in filter_settings: if isinstance(setting, dict): context['filters'].set_status(filterclass, **setting) elif isinstance(setting, list): context['filters'].set_status(filterclass, *setting) else: context['filters'].set_status(filterclass, setting) if patches is None: patches = Patch.objects.filter(patch_project=project) # annotate with tag counts patches = patches.with_tag_counts(project) patches = context['filters'].apply(patches) if not editable_order: patches = order.apply(patches) # we don't need the content, diff or headers for a list; they're text # fields that can potentially contain a lot of data patches = patches.defer('content', 'diff', 'headers') # but we will need to follow the state and submitter relations for # rendering the list template patches = patches.select_related('state', 'submitter', 'delegate') # we also need checks and series patches = patches.prefetch_related('check_set', 'series') paginator = Paginator(request, patches) context.update({ 'page': paginator.current_page, 'patchform': properties_form, 'project': project, 'order': order, }) return context