[PATCH 3/7] trivial: Cleanup of 'views/user.py'

Stephen Finucane stephen.finucane at intel.com
Wed Sep 9 22:59:10 AEST 2015


Resolve some issues with the aforementioned file. These are mostly
stylistic changes.

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/views/user.py | 147 +++++++++++++++++++++++++-----------------------
 1 file changed, 78 insertions(+), 69 deletions(-)

diff --git a/patchwork/views/user.py b/patchwork/views/user.py
index 126ecc9..8f1e596 100644
--- a/patchwork/views/user.py
+++ b/patchwork/views/user.py
@@ -17,109 +17,111 @@
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-
-from django.contrib.auth.decorators import login_required
-from patchwork.requestcontext import PatchworkRequestContext
-from django.shortcuts import render_to_response, get_object_or_404
+from django.conf import settings
 from django.contrib import auth
+from django.contrib.auth.decorators import login_required
 from django.contrib.sites.models import Site
+from django.core.mail import send_mail
 from django.http import HttpResponseRedirect
-from patchwork.models import Project, Bundle, Person, EmailConfirmation, \
-         State, EmailOptout
-from patchwork.forms import UserProfileForm, UserPersonLinkForm, \
-         RegistrationForm
-from patchwork.filters import DelegateFilter
-from patchwork.views import generic_list
+from django.shortcuts import render_to_response, get_object_or_404
 from django.template.loader import render_to_string
-from django.conf import settings
-from django.core.mail import send_mail
 import django.core.urlresolvers
 
+from patchwork.models import (
+    Project, Bundle, Person, EmailConfirmation, State, EmailOptout)
+from patchwork.forms import (
+    UserProfileForm, UserPersonLinkForm, RegistrationForm)
+from patchwork.filters import DelegateFilter
+from patchwork.requestcontext import PatchworkRequestContext
+from patchwork.views import generic_list
+
+
 def register(request):
     context = PatchworkRequestContext(request)
     if request.method == 'POST':
         form = RegistrationForm(request.POST)
         if form.is_valid():
             data = form.cleaned_data
+
             # create inactive user
             user = auth.models.User.objects.create_user(data['username'],
                                                         data['email'],
                                                         data['password'])
-            user.is_active = False;
+            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()
 
             # send email
             mail_ctx = {'site': Site.objects.get_current(),
                         'confirmation': conf}
-
-            subject = render_to_string('patchwork/activation_email_subject.txt',
-                                mail_ctx).replace('\n', ' ').strip()
-            
+            subject = render_to_string(
+                'patchwork/activation_email_subject.txt', mail_ctx).replace(
+                    '\n', ' ').strip()
             message = render_to_string('patchwork/activation_email.txt',
-                                    mail_ctx)
-            
+                                       mail_ctx)
             send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
-                            [conf.email])
+                      [conf.email])
 
             # setting 'confirmation' in the template indicates success
             context['confirmation'] = conf
-
     else:
         form = RegistrationForm()
-    
+
     return render_to_response('patchwork/registration_form.html',
-                              { 'form': form },
+                              {'form': form},
                               context_instance=context)
 
+
 def register_confirm(request, conf):
     conf.user.is_active = True
     conf.user.save()
     conf.deactivate()
     try:
-        person = Person.objects.get(email__iexact = conf.user.email)
+        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()
 
     return render_to_response('patchwork/registration-confirm.html')
 
+
 @login_required
 def profile(request):
     context = PatchworkRequestContext(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:
-        form = UserProfileForm(instance = request.user.profile)
+        form = UserProfileForm(instance=request.user.profile)
 
     context.project = request.user.profile.primary_project
-    context['bundles'] = Bundle.objects.filter(owner = request.user)
+    context['bundles'] = Bundle.objects.filter(owner=request.user)
     context['profileform'] = form
 
     optout_query = '%s.%s IN (SELECT %s FROM %s)' % (
-                        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})
+        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})
     context['linked_emails'] = people
     context['linkform'] = UserPersonLinkForm()
 
     return render_to_response('patchwork/profile.html', context)
 
+
 @login_required
 def link(request):
     context = PatchworkRequestContext(request)
@@ -127,36 +129,37 @@ def link(request):
     if request.method == 'POST':
         form = UserPersonLinkForm(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
 
             try:
                 send_mail('Patchwork email address confirmation',
-                            render_to_string('patchwork/user-link.mail',
-                                context),
-                            settings.DEFAULT_FROM_EMAIL,
-                            [form.cleaned_data['email']])
+                          render_to_string('patchwork/user-link.mail',
+                                           context),
+                          settings.DEFAULT_FROM_EMAIL,
+                          [form.cleaned_data['email']])
             except Exception:
                 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 = UserPersonLinkForm()
     context['linkform'] = form
 
     return render_to_response('patchwork/user-link.html', context)
 
+
 @login_required
 def link_confirm(request, conf):
     context = PatchworkRequestContext(request)
 
     try:
-        person = Person.objects.get(email__iexact = conf.email)
+        person = Person.objects.get(email__iexact=conf.email)
     except Person.DoesNotExist:
-        person = Person(email = conf.email)
+        person = Person(email=conf.email)
 
     person.link_to_user(conf.user)
     person.save()
@@ -166,9 +169,10 @@ def link_confirm(request, conf):
 
     return render_to_response('patchwork/user-link-confirm.html', context)
 
+
 @login_required
 def unlink(request, person_id):
-    person = get_object_or_404(Person, id = person_id)
+    person = get_object_or_404(Person, id=person_id)
 
     if request.method == 'POST':
         if person.email != request.user.email:
@@ -176,41 +180,46 @@ def unlink(request, person_id):
             person.save()
 
     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
+
     return HttpResponseRedirect(url)
 
 
 @login_required
 def todo_lists(request):
-    todo_lists = []
+    todo_lists_ = []
 
     for project in Project.objects.all():
-        patches = request.user.profile.todo_patches(project = project)
+        patches = request.user.profile.todo_patches(project=project)
         if not patches.count():
             continue
 
-        todo_lists.append({'project': project, 'n_patches': patches.count()})
+        todo_lists_.append({'project': project, 'n_patches': patches.count()})
 
-    if len(todo_lists) == 1:
-        return todo_list(request, todo_lists[0]['project'].linkname)
+    if len(todo_lists_) == 1:
+        return todo_list(request, todo_lists_[0]['project'].linkname)
 
     context = PatchworkRequestContext(request)
-    context['todo_lists'] = todo_lists
+    context['todo_lists'] = todo_lists_
     context.project = request.user.profile.primary_project
+
     return render_to_response('patchwork/todo-lists.html', context)
 
+
 @login_required
 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})]
-
-    context = generic_list(request, project,
-            'patchwork.views.user.todo_list',
-            view_args = {'project_id': project.linkname},
-            filter_settings = filter_settings,
-            patches = patches)
-
-    context['action_required_states'] = \
-        State.objects.filter(action_required = True).all()
+    project = get_object_or_404(Project, linkname=project_id)
+    patches = request.user.profile.todo_patches(project=project)
+    filter_settings = [(DelegateFilter, {'delegate': request.user})]
+
+    context = generic_list(
+        request,
+        project,
+        'patchwork.views.user.todo_list',
+        view_args={'project_id': project.linkname},
+        filter_settings=filter_settings,
+        patches=patches)
+
+    context['action_required_states'] = State.objects.filter(
+        action_required=True).all()
+
     return render_to_response('patchwork/todo-list.html', context)
-- 
2.0.0



More information about the Patchwork mailing list