Do this before conducting major surgery on these views. Signed-off-by: Stephen Finucane <step...@that.guru> --- patchwork/views/__init__.py | 107 ++++++++++++++++++++++++------------ patchwork/views/api.py | 13 +++-- patchwork/views/bundle.py | 71 +++++++++++++++--------- patchwork/views/cover.py | 42 ++++++++------ patchwork/views/mail.py | 24 +++++--- patchwork/views/patch.py | 90 +++++++++++++++++++----------- patchwork/views/project.py | 7 ++- patchwork/views/pwclient.py | 5 +- patchwork/views/series.py | 3 +- patchwork/views/user.py | 96 +++++++++++++++++++------------- patchwork/views/utils.py | 27 +++++---- patchwork/views/xmlrpc.py | 63 ++++++++++++++------- 12 files changed, 350 insertions(+), 198 deletions(-)
diff --git patchwork/views/__init__.py patchwork/views/__init__.py index 3efe90cd..c3199ffd 100644 --- patchwork/views/__init__.py +++ patchwork/views/__init__.py @@ -124,8 +124,7 @@ def set_bundle(request, project, action, data, patches, context): 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 = Bundle(owner=user, project=project, name=bundle_name) bundle.save() messages.success(request, "Bundle %s created" % bundle.name) elif action == 'add': @@ -138,15 +137,22 @@ def set_bundle(request, project, action, data, patches, context): for patch in patches: if action in ['create', 'add']: - bundlepatch_count = BundlePatch.objects.filter(bundle=bundle, - patch=patch).count() + bundlepatch_count = BundlePatch.objects.filter( + bundle=bundle, patch=patch + ).count() if bundlepatch_count == 0: bundle.append_patch(patch) - messages.success(request, "Patch '%s' added to bundle %s" % - (patch.name, bundle.name)) + messages.success( + request, + "Patch '%s' added to bundle %s" + % (patch.name, bundle.name), + ) else: - messages.warning(request, "Patch '%s' already in bundle %s" % - (patch.name, bundle.name)) + messages.warning( + request, + "Patch '%s' already in bundle %s" + % (patch.name, bundle.name), + ) elif action == 'remove': try: bp = BundlePatch.objects.get(bundle=bundle, patch=patch) @@ -156,16 +162,24 @@ def set_bundle(request, project, action, data, patches, context): else: messages.success( request, - "Patch '%s' removed from bundle %s\n" % (patch.name, - bundle.name)) + "Patch '%s' removed from bundle %s\n" + % (patch.name, bundle.name), + ) bundle.save() return [] -def generic_list(request, project, view, view_args=None, filter_settings=None, - patches=None, editable_order=False): +def generic_list( + request, + project, + view, + view_args=None, + filter_settings=None, + patches=None, + editable_order=False, +): if not filter_settings: filter_settings = [] @@ -198,13 +212,16 @@ def generic_list(request, project, view, view_args=None, filter_settings=None, 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 - }}) + context.update( + { + 'order': order, + 'list_view': { + 'view': view, + 'view_params': view_args or {}, + 'params': params, + }, + } + ) # form processing @@ -240,8 +257,9 @@ def generic_list(request, project, view, view_args=None, filter_settings=None, 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) + errors = process_multiplepatch_form( + request, properties_form, action, ps, context + ) else: errors = [] @@ -272,25 +290,41 @@ def generic_list(request, project, view, view_args=None, filter_settings=None, # but we will need to follow the state and submitter relations for # rendering the list template - patches = patches.select_related('state', 'submitter', 'delegate', - 'series') - - patches = patches.only('state', 'submitter', 'delegate', 'project', - 'series__name', 'name', 'date', 'msgid') + patches = patches.select_related( + 'state', 'submitter', 'delegate', 'series' + ) + + patches = patches.only( + 'state', + 'submitter', + 'delegate', + 'project', + 'series__name', + 'name', + 'date', + 'msgid', + ) # we also need checks and series patches = patches.prefetch_related( - Prefetch('check_set', queryset=Check.objects.only( - 'context', 'user_id', 'patch_id', 'state', 'date'))) + Prefetch( + 'check_set', + queryset=Check.objects.only( + 'context', 'user_id', 'patch_id', 'state', 'date' + ), + ) + ) paginator = Paginator(request, patches) - context.update({ - 'page': paginator.current_page, - 'patchform': properties_form, - 'project': project, - 'order': order, - }) + context.update( + { + 'page': paginator.current_page, + 'patchform': properties_form, + 'project': project, + 'order': order, + } + ) return context @@ -308,8 +342,9 @@ def process_multiplepatch_form(request, form, action, patches, context): changed_patches = 0 for patch in patches: if not patch.is_editable(request.user): - errors.append("You don't have permissions to edit patch '%s'" - % patch.name) + errors.append( + "You don't have permissions to edit patch '%s'" % patch.name + ) continue changed_patches += 1 diff --git patchwork/views/api.py patchwork/views/api.py index 55ffad9c..283e6712 100644 --- patchwork/views/api.py +++ patchwork/views/api.py @@ -41,8 +41,9 @@ def _handle_request(request, queryset_fn, formatter): def submitters(request): def queryset(search): - return Person.objects.filter(Q(name__icontains=search) | - Q(email__icontains=search)) + return Person.objects.filter( + Q(name__icontains=search) | Q(email__icontains=search) + ) def formatter(submitter): return { @@ -56,9 +57,11 @@ def submitters(request): def delegates(request): def queryset(search): - return User.objects.filter(Q(username__icontains=search) | - Q(first_name__icontains=search) | - Q(last_name__icontains=search)) + return User.objects.filter( + Q(username__icontains=search) + | Q(first_name__icontains=search) + | Q(last_name__icontains=search) + ) def formatter(user): return { diff --git patchwork/views/bundle.py patchwork/views/bundle.py index 3e227f4c..323a1f74 100644 --- patchwork/views/bundle.py +++ patchwork/views/bundle.py @@ -52,8 +52,9 @@ def bundle_list(request, project_id=None): if form_name == DeleteBundleForm.name: form = DeleteBundleForm(request.POST) if form.is_valid(): - bundle = get_object_or_404(Bundle, - id=form.cleaned_data['bundle_id']) + bundle = get_object_or_404( + Bundle, id=form.cleaned_data['bundle_id'] + ) bundle.delete() if project_id is None: @@ -63,8 +64,9 @@ def bundle_list(request, project_id=None): bundles = request.user.bundles.filter(project=project) for bundle in bundles: - bundle.delete_form = DeleteBundleForm(auto_id=False, - initial={'bundle_id': bundle.id}) + bundle.delete_form = DeleteBundleForm( + auto_id=False, initial={'bundle_id': bundle.id} + ) context = { 'bundles': bundles, @@ -75,8 +77,9 @@ def bundle_list(request, project_id=None): def bundle_detail(request, username, bundlename): - bundle = get_object_or_404(Bundle, owner__username=username, - name=bundlename) + bundle = get_object_or_404( + Bundle, owner__username=username, name=bundlename + ) filter_settings = [(DelegateFilter, DelegateFilter.ANY_DELEGATE)] is_owner = request.user == bundle.owner @@ -105,30 +108,38 @@ def bundle_detail(request, username, bundlename): else: form = BundleForm(instance=bundle) - if (request.method == 'POST' and - request.POST.get('form') == 'reorderform'): + if ( + request.method == 'POST' + and request.POST.get('form') == 'reorderform' + ): order = get_object_or_404( BundlePatch, bundle=bundle, - patch__id=request.POST.get('order_start')).order + patch__id=request.POST.get('order_start'), + ).order for patch_id in request.POST.getlist('neworder'): - bundlepatch = get_object_or_404(BundlePatch, - bundle=bundle, - patch__id=patch_id) + bundlepatch = get_object_or_404( + BundlePatch, bundle=bundle, patch__id=patch_id + ) bundlepatch.order = order bundlepatch.save() order += 1 else: form = None - context = generic_list(request, bundle.project, - 'bundle-detail', - view_args={'username': bundle.owner.username, - 'bundlename': bundle.name}, - filter_settings=filter_settings, - patches=bundle.ordered_patches(), - editable_order=is_owner) + context = generic_list( + request, + bundle.project, + 'bundle-detail', + view_args={ + 'username': bundle.owner.username, + 'bundlename': bundle.name, + }, + filter_settings=filter_settings, + patches=bundle.ordered_patches(), + editable_order=is_owner, + ) context['bundle'] = bundle context['bundleform'] = form @@ -137,16 +148,18 @@ def bundle_detail(request, username, bundlename): def bundle_mbox(request, username, bundlename): - bundle = get_object_or_404(Bundle, owner__username=username, - name=bundlename) + bundle = get_object_or_404( + Bundle, owner__username=username, name=bundlename + ) request.user = rest_auth(request) if not (request.user == bundle.owner or bundle.public): return HttpResponseNotFound() response = HttpResponse(content_type='text/plain') - response['Content-Disposition'] = \ - 'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name) + response[ + 'Content-Disposition' + ] = 'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name) response.write(bundle_to_mbox(bundle)) return response @@ -162,7 +175,11 @@ def bundle_detail_redir(request, bundle_id): def bundle_mbox_redir(request, bundle_id): bundle = get_object_or_404(Bundle, id=bundle_id, owner=request.user) return HttpResponseRedirect( - reverse('bundle-mbox', kwargs={ - 'username': request.user.username, - 'bundlename': bundle.name, - })) + reverse( + 'bundle-mbox', + kwargs={ + 'username': request.user.username, + 'bundlename': bundle.name, + }, + ) + ) diff --git patchwork/views/cover.py patchwork/views/cover.py index 8ab0ba99..3368186b 100644 --- patchwork/views/cover.py +++ patchwork/views/cover.py @@ -18,12 +18,11 @@ from patchwork.views.utils import cover_to_mbox def cover_detail(request, project_id, msgid): project = get_object_or_404(Project, linkname=project_id) - db_msgid = ('<%s>' % msgid) + db_msgid = '<%s>' % msgid # redirect to patches where necessary try: - cover = get_object_or_404(Cover, project_id=project.id, - msgid=db_msgid) + cover = get_object_or_404(Cover, project_id=project.id, msgid=db_msgid) except Http404 as exc: patches = Patch.objects.filter( project_id=project.id, @@ -31,9 +30,11 @@ def cover_detail(request, project_id, msgid): ) if patches: return HttpResponseRedirect( - reverse('patch-detail', - kwargs={'project_id': project.linkname, - 'msgid': msgid})) + reverse( + 'patch-detail', + kwargs={'project_id': project.linkname, 'msgid': msgid}, + ) + ) raise exc context = { @@ -43,23 +44,22 @@ def cover_detail(request, project_id, msgid): comments = cover.comments.all() comments = comments.select_related('submitter') - comments = comments.only('submitter', 'date', 'id', 'content', - 'cover') + comments = comments.only('submitter', 'date', 'id', 'content', 'cover') context['comments'] = comments return render(request, 'patchwork/submission.html', context) def cover_mbox(request, project_id, msgid): - db_msgid = ('<%s>' % msgid) + db_msgid = '<%s>' % msgid project = get_object_or_404(Project, linkname=project_id) - cover = get_object_or_404(Cover, project_id=project.id, - msgid=db_msgid) + cover = get_object_or_404(Cover, project_id=project.id, msgid=db_msgid) response = HttpResponse(content_type='text/plain') response.write(cover_to_mbox(cover)) response['Content-Disposition'] = 'attachment; filename=%s.mbox' % ( - cover.filename) + cover.filename + ) return response @@ -67,8 +67,13 @@ def cover_mbox(request, project_id, msgid): def cover_by_id(request, cover_id): cover = get_object_or_404(Cover, id=cover_id) - url = reverse('cover-detail', kwargs={'project_id': cover.project.linkname, - 'msgid': cover.url_msgid}) + url = reverse( + 'cover-detail', + kwargs={ + 'project_id': cover.project.linkname, + 'msgid': cover.url_msgid, + }, + ) return HttpResponseRedirect(url) @@ -76,7 +81,12 @@ def cover_by_id(request, cover_id): def cover_mbox_by_id(request, cover_id): cover = get_object_or_404(Cover, id=cover_id) - url = reverse('cover-mbox', kwargs={'project_id': cover.project.linkname, - 'msgid': cover.url_msgid}) + url = reverse( + 'cover-mbox', + kwargs={ + 'project_id': cover.project.linkname, + 'msgid': cover.url_msgid, + }, + ) return HttpResponseRedirect(url) diff --git patchwork/views/mail.py patchwork/views/mail.py index 4bc7be0f..8b31fc9e 100644 --- patchwork/views/mail.py +++ patchwork/views/mail.py @@ -78,17 +78,21 @@ def _optinout(request, action): form = EmailForm(data=request.POST) if not form.is_valid(): - context['error'] = ('There was an error in the form. Please review ' - 'and re-submit.') + context['error'] = ( + 'There was an error in the form. Please review ' 'and re-submit.' + ) context['form'] = form return render(request, html_template, context) email = form.cleaned_data['email'] - if action == 'optin' and EmailOptout.objects.filter( - email=email).count() == 0: - context['error'] = ("The email address %s is not on the patchwork " - "opt-out list, so you don't need to opt back in" % - email) + if ( + action == 'optin' + and EmailOptout.objects.filter(email=email).count() == 0 + ): + context['error'] = ( + "The email address %s is not on the patchwork " + "opt-out list, so you don't need to opt back in" % email + ) context['form'] = form return render(request, html_template, context) @@ -104,8 +108,10 @@ def _optinout(request, action): send_mail(subject, message, conf_settings.DEFAULT_FROM_EMAIL, [email]) except smtplib.SMTPException: context['confirmation'] = None - context['error'] = ('An error occurred during confirmation . ' - 'Please try again later.') + context['error'] = ( + 'An error occurred during confirmation . ' + 'Please try again later.' + ) context['admins'] = conf_settings.ADMINS return render(request, html_template, context) diff --git patchwork/views/patch.py patchwork/views/patch.py index 00b0147f..0cf2ceb2 100644 --- patchwork/views/patch.py +++ patchwork/views/patch.py @@ -25,8 +25,12 @@ from patchwork.views.utils import series_patch_to_mbox def patch_list(request, project_id): project = get_object_or_404(Project, linkname=project_id) - context = generic_list(request, project, 'patch-list', - view_args={'project_id': project.linkname}) + context = generic_list( + request, + project, + 'patch-list', + view_args={'project_id': project.linkname}, + ) if request.user.is_authenticated: context['bundles'] = request.user.bundles.all() @@ -36,7 +40,7 @@ def patch_list(request, project_id): def patch_detail(request, project_id, msgid): project = get_object_or_404(Project, linkname=project_id) - db_msgid = ('<%s>' % msgid) + db_msgid = '<%s>' % msgid # redirect to cover letters where necessary try: @@ -48,15 +52,15 @@ def patch_detail(request, project_id, msgid): ) if covers: return HttpResponseRedirect( - reverse('cover-detail', - kwargs={'project_id': project.linkname, - 'msgid': msgid})) + reverse( + 'cover-detail', + kwargs={'project_id': project.linkname, 'msgid': msgid}, + ) + ) raise Http404('Patch does not exist') editable = patch.is_editable(request.user) - context = { - 'project': patch.project - } + context = {'project': patch.project} form = None createbundleform = None @@ -73,8 +77,9 @@ def patch_detail(request, project_id, msgid): if action == 'createbundle': bundle = Bundle(owner=request.user, project=project) - createbundleform = CreateBundleForm(instance=bundle, - data=request.POST) + createbundleform = CreateBundleForm( + instance=bundle, data=request.POST + ) if createbundleform.is_valid(): createbundleform.save() bundle.append_patch(patch) @@ -83,16 +88,20 @@ def patch_detail(request, project_id, msgid): messages.success(request, 'Bundle %s created' % bundle.name) elif action == 'addtobundle': bundle = get_object_or_404( - Bundle, id=request.POST.get('bundle_id')) + Bundle, id=request.POST.get('bundle_id') + ) if bundle.append_patch(patch): - messages.success(request, - 'Patch "%s" added to bundle "%s"' % ( - patch.name, bundle.name)) + messages.success( + request, + 'Patch "%s" added to bundle "%s"' + % (patch.name, bundle.name), + ) else: - messages.error(request, - 'Failed to add patch "%s" to bundle "%s": ' - 'patch is already in bundle' % ( - patch.name, bundle.name)) + messages.error( + request, + 'Failed to add patch "%s" to bundle "%s": ' + 'patch is already in bundle' % (patch.name, bundle.name), + ) # all other actions require edit privs elif not editable: @@ -114,10 +123,12 @@ def patch_detail(request, project_id, msgid): if patch.related: related_same_project = patch.related.patches.only( - 'name', 'msgid', 'project', 'related') + 'name', 'msgid', 'project', 'related' + ) # avoid a second trip out to the db for info we already have related_different_project = [ - related_patch for related_patch in related_same_project + related_patch + for related_patch in related_same_project if related_patch.project_id != patch.project_id ] else: @@ -140,20 +151,21 @@ def patch_detail(request, project_id, msgid): def patch_raw(request, project_id, msgid): - db_msgid = ('<%s>' % msgid) + db_msgid = '<%s>' % msgid project = get_object_or_404(Project, linkname=project_id) patch = get_object_or_404(Patch, project_id=project.id, msgid=db_msgid) response = HttpResponse(content_type="text/x-patch") response.write(patch.diff) response['Content-Disposition'] = 'attachment; filename=%s.diff' % ( - patch.filename) + patch.filename + ) return response def patch_mbox(request, project_id, msgid): - db_msgid = ('<%s>' % msgid) + db_msgid = '<%s>' % msgid project = get_object_or_404(Project, linkname=project_id) patch = get_object_or_404(Patch, project_id=project.id, msgid=db_msgid) series_id = request.GET.get('series') @@ -164,7 +176,8 @@ def patch_mbox(request, project_id, msgid): else: response.write(patch_to_mbox(patch)) response['Content-Disposition'] = 'attachment; filename=%s.patch' % ( - patch.filename) + patch.filename + ) return response @@ -172,8 +185,13 @@ def patch_mbox(request, project_id, msgid): def patch_by_id(request, patch_id): patch = get_object_or_404(Patch, id=patch_id) - url = reverse('patch-detail', kwargs={'project_id': patch.project.linkname, - 'msgid': patch.url_msgid}) + url = reverse( + 'patch-detail', + kwargs={ + 'project_id': patch.project.linkname, + 'msgid': patch.url_msgid, + }, + ) return HttpResponseRedirect(url) @@ -181,8 +199,13 @@ def patch_by_id(request, patch_id): def patch_mbox_by_id(request, patch_id): patch = get_object_or_404(Patch, id=patch_id) - url = reverse('patch-mbox', kwargs={'project_id': patch.project.linkname, - 'msgid': patch.url_msgid}) + url = reverse( + 'patch-mbox', + kwargs={ + 'project_id': patch.project.linkname, + 'msgid': patch.url_msgid, + }, + ) return HttpResponseRedirect(url) @@ -190,7 +213,12 @@ def patch_mbox_by_id(request, patch_id): def patch_raw_by_id(request, patch_id): patch = get_object_or_404(Patch, id=patch_id) - url = reverse('patch-raw', kwargs={'project_id': patch.project.linkname, - 'msgid': patch.url_msgid}) + url = reverse( + 'patch-raw', + kwargs={ + 'project_id': patch.project.linkname, + 'msgid': patch.url_msgid, + }, + ) return HttpResponseRedirect(url) diff --git patchwork/views/project.py patchwork/views/project.py index 4c25f715..a993618a 100644 --- patchwork/views/project.py +++ patchwork/views/project.py @@ -19,8 +19,8 @@ def project_list(request): if projects.count() == 1: return HttpResponseRedirect( - reverse('patch-list', - kwargs={'project_id': projects[0].linkname})) + reverse('patch-list', kwargs={'project_id': projects[0].linkname}) + ) context = { 'projects': projects, @@ -35,7 +35,8 @@ def project_detail(request, project_id): context = { 'project': project, 'maintainers': User.objects.filter( - profile__maintainer_projects=project).select_related('profile'), + profile__maintainer_projects=project + ).select_related('profile'), 'n_patches': patches.filter(archived=False).count(), 'n_archived_patches': patches.filter(archived=True).count(), 'enable_xmlrpc': settings.ENABLE_XMLRPC, diff --git patchwork/views/pwclient.py patchwork/views/pwclient.py index 72ebcbbb..a8be425b 100644 --- patchwork/views/pwclient.py +++ patchwork/views/pwclient.py @@ -21,8 +21,9 @@ def pwclientrc(request, project_id): else: context['scheme'] = 'http' - response = render(request, 'patchwork/pwclientrc', context, - content_type='text/plain') + response = render( + request, 'patchwork/pwclientrc', context, content_type='text/plain' + ) response['Content-Disposition'] = 'attachment; filename=.pwclientrc' return response diff --git patchwork/views/series.py patchwork/views/series.py index e0df3adf..a8892ae6 100644 --- patchwork/views/series.py +++ patchwork/views/series.py @@ -16,6 +16,7 @@ def series_mbox(request, series_id): response = HttpResponse(content_type='text/plain') response.write(series_to_mbox(series)) response['Content-Disposition'] = 'attachment; filename=%s.patch' % ( - series.filename) + series.filename + ) return response diff --git patchwork/views/user.py patchwork/views/user.py index 6b09adb2..7bf6377e 100644 --- patchwork/views/user.py +++ patchwork/views/user.py @@ -38,35 +38,41 @@ def register(request): data = form.cleaned_data # create inactive user - user = auth.models.User.objects.create_user(data['username'], - data['email'], - data['password']) + user = auth.models.User.objects.create_user( + data['username'], data['email'], data['password'] + ) user.is_active = False user.first_name = data.get('first_name', '') user.last_name = data.get('last_name', '') user.save() # create confirmation - conf = EmailConfirmation(type='registration', user=user, - email=user.email) + conf = EmailConfirmation( + type='registration', user=user, email=user.email + ) conf.save() context['confirmation'] = conf # send email subject = render_to_string( - 'patchwork/mails/activation-subject.txt') + 'patchwork/mails/activation-subject.txt' + ) message = render_to_string( 'patchwork/mails/activation.txt', - {'site': Site.objects.get_current(), 'confirmation': conf}) + {'site': Site.objects.get_current(), 'confirmation': conf}, + ) try: - send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, - [conf.email]) + send_mail( + subject, message, settings.DEFAULT_FROM_EMAIL, [conf.email] + ) except smtplib.SMTPException: context['confirmation'] = None - context['error'] = ('An error occurred during registration. ' - 'Please try again later') + context['error'] = ( + 'An error occurred during registration. ' + 'Please try again later' + ) else: form = RegistrationForm() @@ -83,8 +89,7 @@ def register_confirm(request, conf): try: person = Person.objects.get(email__iexact=conf.user.email) except Person.DoesNotExist: - person = Person(email=conf.user.email, - name=conf.user.profile.name) + person = Person(email=conf.user.email, name=conf.user.profile.name) person.user = conf.user person.save() @@ -94,8 +99,9 @@ def register_confirm(request, conf): @login_required def profile(request): if request.method == 'POST': - form = UserProfileForm(instance=request.user.profile, - data=request.POST) + form = UserProfileForm( + instance=request.user.profile, data=request.POST + ) if form.is_valid(): form.save() else: @@ -115,9 +121,11 @@ def profile(request): Person._meta.db_table, Person._meta.get_field('email').column, EmailOptout._meta.get_field('email').column, - EmailOptout._meta.db_table) - people = Person.objects.filter(user=request.user) \ - .extra(select={'is_optout': optout_query}) + EmailOptout._meta.db_table, + ) + people = Person.objects.filter(user=request.user).extra( + select={'is_optout': optout_query} + ) context['linked_emails'] = people context['linkform'] = EmailForm() context['api_token'] = request.user.profile.token @@ -134,25 +142,32 @@ def link(request): if request.method == 'POST': form = EmailForm(request.POST) if form.is_valid(): - conf = EmailConfirmation(type='userperson', - user=request.user, - email=form.cleaned_data['email']) + conf = EmailConfirmation( + type='userperson', + user=request.user, + email=form.cleaned_data['email'], + ) conf.save() context['confirmation'] = conf subject = render_to_string('patchwork/mails/user-link-subject.txt') - message = render_to_string('patchwork/mails/user-link.txt', - context, request=request) + message = render_to_string( + 'patchwork/mails/user-link.txt', context, request=request + ) try: - send_mail(subject, - message, - settings.DEFAULT_FROM_EMAIL, - [form.cleaned_data['email']]) + send_mail( + subject, + message, + settings.DEFAULT_FROM_EMAIL, + [form.cleaned_data['email']], + ) except smtplib.SMTPException: context['confirmation'] = None - context['error'] = ('An error occurred during confirmation. ' - 'Please try again later') + context['error'] = ( + 'An error occurred during confirmation. ' + 'Please try again later' + ) else: form = EmailForm() @@ -205,7 +220,9 @@ def todo_lists(request): return HttpResponseRedirect( reverse( 'user-todo', - kwargs={'project_id': todo_lists[0]['project'].linkname})) + kwargs={'project_id': todo_lists[0]['project'].linkname}, + ) + ) context = { 'todo_lists': todo_lists, @@ -218,19 +235,22 @@ def todo_lists(request): def todo_list(request, project_id): project = get_object_or_404(Project, linkname=project_id) patches = request.user.profile.todo_patches(project=project) - filter_settings = [(DelegateFilter, - {'delegate': request.user})] + filter_settings = [(DelegateFilter, {'delegate': request.user})] # TODO(stephenfin): Build the context dict here - context = generic_list(request, project, - 'user-todo', - view_args={'project_id': project.linkname}, - filter_settings=filter_settings, - patches=patches) + context = generic_list( + request, + project, + 'user-todo', + view_args={'project_id': project.linkname}, + filter_settings=filter_settings, + patches=patches, + ) context['bundles'] = request.user.bundles.all() context['action_required_states'] = State.objects.filter( - action_required=True).all() + action_required=True + ).all() return render(request, 'patchwork/todo-list.html', context) diff --git patchwork/views/utils.py patchwork/views/utils.py index 4631229b..1f7ee0da 100644 --- patchwork/views/utils.py +++ patchwork/views/utils.py @@ -28,8 +28,9 @@ class PatchMbox(MIMENonMultipart): patch_charset = 'utf-8' def __init__(self, _text): - MIMENonMultipart.__init__(self, 'text', 'plain', - **{'charset': self.patch_charset}) + MIMENonMultipart.__init__( + self, 'text', 'plain', **{'charset': self.patch_charset} + ) self.set_payload(_text.encode(self.patch_charset)) encode_7or8bit(self) @@ -79,9 +80,12 @@ def _submission_to_mbox(submission): utc_timestamp = delta.seconds + delta.days * 24 * 3600 mail = PatchMbox(body) - mail['X-Patchwork-Submitter'] = email.utils.formataddr(( - str(Header(submission.submitter.name, mail.patch_charset)), - submission.submitter.email)) + mail['X-Patchwork-Submitter'] = email.utils.formataddr( + ( + str(Header(submission.submitter.name, mail.patch_charset)), + submission.submitter.email, + ) + ) mail['X-Patchwork-Id'] = str(submission.id) if is_patch and submission.delegate: mail['X-Patchwork-Delegate'] = str(submission.delegate.email) @@ -152,13 +156,15 @@ def series_patch_to_mbox(patch, series_id): 'Patch does not have an associated series. This is ' 'because the patch was processed with an older ' 'version of Patchwork. It is not possible to ' - 'provide dependencies for this patch.') + 'provide dependencies for this patch.' + ) else: try: series_id = int(series_id) except ValueError: - raise Http404('Expected integer series value or *. Received: %r' % - series_id) + raise Http404( + 'Expected integer series value or *. Received: %r' % series_id + ) if patch.series.id != series_id: raise Http404('Patch does not belong to series %d' % series_id) @@ -166,8 +172,9 @@ def series_patch_to_mbox(patch, series_id): mbox = [] # get the series-ified patch - for dep in patch.series.patches.filter( - number__lt=patch.number).order_by('number'): + for dep in patch.series.patches.filter(number__lt=patch.number).order_by( + 'number' + ): mbox.append(patch_to_mbox(dep)) mbox.append(patch_to_mbox(patch)) diff --git patchwork/views/xmlrpc.py patchwork/views/xmlrpc.py index 6701bf20..2998729f 100644 --- patchwork/views/xmlrpc.py +++ patchwork/views/xmlrpc.py @@ -24,15 +24,13 @@ from patchwork.models import State from patchwork.views.utils import patch_to_mbox -class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher, - XMLRPCDocGenerator): +class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher, XMLRPCDocGenerator): server_name = 'Patchwork XML-RPC API' server_title = 'Patchwork XML-RPC API v1 Documentation' def __init__(self): - SimpleXMLRPCDispatcher.__init__(self, allow_none=False, - encoding=None) + SimpleXMLRPCDispatcher.__init__(self, allow_none=False, encoding=None) XMLRPCDocGenerator.__init__(self) def _dumps(obj, *args, **kwargs): @@ -65,7 +63,7 @@ class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher, if not header.startswith('Basic '): raise Exception('Authentication scheme not supported') - header = header[len('Basic '):].strip() + header = header[len('Basic ') :].strip() try: decoded = base64.b64decode(header.encode('ascii')).decode('ascii') @@ -104,7 +102,8 @@ class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher, # report exception back to server response = self.dumps( xmlrpc_client.Fault( - 1, '%s:%s' % (sys.exc_info()[0], sys.exc_info()[1])), + 1, '%s:%s' % (sys.exc_info()[0], sys.exc_info()[1]) + ), ) return response @@ -134,6 +133,7 @@ def xmlrpc(request): return response + # decorator for XMLRPC methods. Setting login_required to true will call # the decorated function with a non-optional user as the first argument. @@ -147,15 +147,31 @@ def xmlrpc_method(login_required=False): # We allow most of the Django field lookup types for remote queries -LOOKUP_TYPES = ['iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', - 'in', 'startswith', 'istartswith', 'endswith', - 'iendswith', 'range', 'year', 'month', 'day', 'isnull'] +LOOKUP_TYPES = [ + 'iexact', + 'contains', + 'icontains', + 'gt', + 'gte', + 'lt', + 'in', + 'startswith', + 'istartswith', + 'endswith', + 'iendswith', + 'range', + 'year', + 'month', + 'day', + 'isnull', +] ####################################################################### # Helper functions ####################################################################### + def project_to_dict(obj): """Serialize a project object. @@ -312,7 +328,7 @@ def patch_check_to_dict(obj): return { 'state': obj.combined_check_state, 'total': len(obj.checks), - 'checks': [check_to_dict(check) for check in obj.checks] + 'checks': [check_to_dict(check) for check in obj.checks], } @@ -320,6 +336,7 @@ def patch_check_to_dict(obj): # Public XML-RPC methods ####################################################################### + def _get_objects(serializer, objects, max_count): if max_count > 0: return [serializer(x) for x in objects[:max_count]] @@ -416,8 +433,9 @@ def person_list(search_str=None, max_count=0): of all persons if no filter given. """ if search_str: - people = (Person.objects.filter(name__icontains=search_str) | - Person.objects.filter(email__icontains=search_str)) + people = Person.objects.filter( + name__icontains=search_str + ) | Person.objects.filter(email__icontains=search_str) else: people = Person.objects.all() @@ -625,8 +643,7 @@ def patch_get_by_project_hash(project, hash): if any, else an empty dict. """ try: - patch = Patch.objects.get(project__linkname=project, - hash=hash) + patch = Patch.objects.get(project__linkname=project, hash=hash) return patch_to_dict(patch) except Patch.DoesNotExist: return {} @@ -860,8 +877,7 @@ def check_list(filt=None): if parts[0] == 'user_id': dfilter['user'] = Person.objects.filter(id=filt[key])[0] if parts[0] == 'project_id': - dfilter['patch__project'] = Project.objects.filter( - id=filt[key])[0] + dfilter['patch__project'] = Project.objects.filter(id=filt[key])[0] elif parts[0] == 'patch_id': dfilter['patch'] = Patch.objects.filter(id=filt[key])[0] elif parts[0] == 'max_count': @@ -895,8 +911,9 @@ def check_get(check_id): @xmlrpc_method(login_required=True) -def check_create(user, patch_id, context, state, target_url="", - description=""): +def check_create( + user, patch_id, context, state, target_url="", description="" +): """Add a Check to a patch. **NOTE:** Authentication is required for this method. @@ -920,8 +937,14 @@ def check_create(user, patch_id, context, state, target_url="", break else: raise Exception("Invalid check state: %s" % state) - Check.objects.create(patch=patch, context=context, state=state, user=user, - target_url=target_url, description=description) + Check.objects.create( + patch=patch, + context=context, + state=state, + user=user, + target_url=target_url, + description=description, + ) return True -- 2.31.1 _______________________________________________ Patchwork mailing list Patchwork@lists.ozlabs.org https://lists.ozlabs.org/listinfo/patchwork