From f01fe5a8942861ad1b87b0aecd076bddd190e444 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 11 Aug 2012 10:51:52 +0200 Subject: [PATCH] removed unused imports from participant and did some pep8 changes --- openslides/participant/api.py | 5 +- openslides/participant/forms.py | 11 ++-- openslides/participant/models.py | 2 +- openslides/participant/tests.py | 31 ++++------- openslides/participant/views.py | 92 ++++++++++++++++---------------- 5 files changed, 64 insertions(+), 77 deletions(-) diff --git a/openslides/participant/api.py b/openslides/participant/api.py index 84077a0e4..9fdccc47b 100644 --- a/openslides/participant/api.py +++ b/openslides/participant/api.py @@ -21,7 +21,6 @@ from django.contrib.auth.models import User from django.db import transaction from openslides.utils import csv_ext -from openslides.utils.person import get_person from openslides.participant.models import OpenSlidesUser @@ -55,6 +54,7 @@ def gen_username(first_name, last_name): except User.DoesNotExist: return testname + def import_users(csv_file): error_messages = [] count_success = 0 @@ -68,7 +68,8 @@ def import_users(csv_file): dialect = csv_ext.patchup(dialect) csv_file.seek(0) - for (line_no, line) in enumerate(csv.reader(csv_file, dialect=dialect)): + for (line_no, line) in enumerate(csv.reader(csv_file, + dialect=dialect)): if line_no: try: (first_name, last_name, gender, category, type, committee, comment) = line[:7] diff --git a/openslides/participant/forms.py b/openslides/participant/forms.py index f766c247b..f37a28420 100644 --- a/openslides/participant/forms.py +++ b/openslides/participant/forms.py @@ -11,9 +11,8 @@ """ from django import forms -from django.contrib.auth.forms import AdminPasswordChangeForm -from django.contrib.auth.models import User, Group, Permission -from django.utils.translation import ugettext_lazy as _, ugettext_noop +from django.contrib.auth.models import Permission +from django.utils.translation import ugettext_lazy as _ from openslides.utils.forms import ( CssClassMixin, LocalizedModelMultipleChoiceField) @@ -25,7 +24,8 @@ class UserCreateForm(forms.ModelForm, CssClassMixin): first_name = forms.CharField(label=_("First name")) last_name = forms.CharField(label=_("Last name")) groups = forms.ModelMultipleChoiceField( - queryset=Group.objects.all(), label=_("User groups"), required=False) + queryset=OpenSlidesGroup.objects.all(), label=_("User groups"), + required=False) is_active = forms.BooleanField( label=_("Active"), required=False, initial=True) @@ -67,14 +67,13 @@ class GroupForm(forms.ModelForm, CssClassMixin): _('Group name "%s" is reserved for internal use.') % data) return data - class Meta: model = OpenSlidesGroup class UsersettingsForm(forms.ModelForm, CssClassMixin): class Meta: - model = User + model = OpenSlidesUser fields = ('username', 'first_name', 'last_name', 'email') diff --git a/openslides/participant/models.py b/openslides/participant/models.py index 625a82ec9..3bc597ad8 100644 --- a/openslides/participant/models.py +++ b/openslides/participant/models.py @@ -12,7 +12,7 @@ from django.contrib.auth.models import User, Group from django.db import models -from django.db.models import Q, signals +from django.db.models import signals from django.dispatch import receiver from django.utils.translation import ugettext_lazy as _, ugettext_noop diff --git a/openslides/participant/tests.py b/openslides/participant/tests.py index fc76ae0f3..99cfcb009 100644 --- a/openslides/participant/tests.py +++ b/openslides/participant/tests.py @@ -12,8 +12,6 @@ from django.test import TestCase from django.test.client import Client -from django.contrib.auth.models import User, Group -from django.db.models.query import EmptyQuerySet from django.contrib.auth.hashers import check_password from openslides.utils.person import get_person, Persons @@ -23,10 +21,11 @@ from openslides.participant.models import OpenSlidesUser, OpenSlidesGroup class OpenSlidesUserTest(TestCase): def setUp(self): - self.user1 = User(first_name=u'Max', last_name=u'Mustermann') - self.user1.username = gen_username(self.user1.first_name, self.user1.last_name) - self.user1.save() - self.openslidesuser1 = self.user1.openslidesuser + self.openslidesuser1 = OpenSlidesUser() + self.openslidesuser1.first_name = u'Max' + self.openslidesuser1.last_name = u'Mustermann' + self.openslidesuser1.username = gen_username( + self.openslidesuser1.first_name, self.openslidesuser1.last_name) self.openslidesuser1.firstpassword = gen_password() self.openslidesuser1.save() self.user1 = self.openslidesuser1.user @@ -57,27 +56,17 @@ class OpenSlidesUserTest(TestCase): self.assertEqual(get_person('openslides_user:1'), self.openslidesuser1) self.assertEqual(len(Persons(person_prefix='openslides_user')), 1) - def test_save_name(self): - self.assertEqual(self.openslidesuser1.first_name, self.user1.first_name) - self.assertEqual(self.openslidesuser1.last_name, self.user1.last_name) - self.openslidesuser1.first_name = 'foo' - self.openslidesuser1.last_name = 'bar' - self.openslidesuser1.save() - user1 = User.objects.get(pk=1) - self.assertEqual(user1.first_name, 'foo') - self.assertEqual(user1.last_name, 'bar') - self.assertEqual(user1.get_full_name(), 'foo bar') - class OpenSlidesGroupTest(TestCase): def setUp(self): - self.group1 = Group.objects.create(name='Test Group') - self.openslidesgroup1 = OpenSlidesGroup.objects.create(group=self.group1) + self.openslidesgroup1 = OpenSlidesGroup.objects.create(name='Test Group') + self.group1 = self.openslidesgroup1.group def test_group_openslidesgroup(self): self.assertEqual(self.openslidesgroup1.group, self.group1) def test_person_api(self): self.assertTrue(hasattr(self.openslidesgroup1, 'person_id')) - self.assertEqual(self.openslidesgroup1.person_id, 'openslides_group:1') - self.assertEqual(get_person('openslides_group:1'), self.openslidesgroup1) + person_id = "openslides_group:%d" % self.openslidesgroup1.id + self.assertEqual(self.openslidesgroup1.person_id, person_id) + self.assertEqual(get_person(person_id), self.openslidesgroup1) diff --git a/openslides/participant/views.py b/openslides/participant/views.py index 4854c29af..4ee452681 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -13,7 +13,6 @@ # for python 2.5 support from __future__ import with_statement -import csv from urllib import urlencode try: @@ -24,36 +23,31 @@ except ImportError: # python <= 2.5 grab it from cgi from reportlab.lib import colors from reportlab.lib.units import cm from reportlab.platypus import ( - SimpleDocTemplate, PageBreak, Paragraph, LongTable, Spacer, Table, - TableStyle) + SimpleDocTemplate, Paragraph, LongTable, Spacer, Table, TableStyle) -from django.db import transaction from django.contrib import messages from django.contrib.auth.decorators import login_required -from django.contrib.auth.models import User, Group from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.views import login as django_login from django.core.urlresolvers import reverse from django.shortcuts import redirect -from django.utils.translation import ugettext as _, ungettext, ugettext_lazy +from django.utils.translation import ugettext as _, ugettext_lazy -from openslides.utils import csv_ext from openslides.utils.pdf import stylesheet from openslides.utils.template import Tab from openslides.utils.utils import ( - template, permission_required, gen_confirm_form, ajax_request, decodedict, - encodedict, delete_default_permissions, html_strong) + template, decodedict, encodedict, delete_default_permissions, html_strong) from openslides.utils.views import ( - FormView, PDFView, TemplateView, CreateView, UpdateView, DeleteView, + FormView, PDFView, CreateView, UpdateView, DeleteView, RedirectView, SingleObjectMixin, ListView, QuestionMixin) from openslides.config.models import config -from openslides.participant.models import OpenSlidesUser, OpenSlidesGroup from openslides.participant.api import gen_username, gen_password, import_users from openslides.participant.forms import ( UserCreateForm, UserUpdateForm, UsersettingsForm, - UserImportForm, GroupForm, AdminPasswordChangeForm, ConfigForm) + UserImportForm, GroupForm, ConfigForm) +from openslides.participant.models import OpenSlidesUser, OpenSlidesGroup class Overview(ListView): @@ -152,7 +146,8 @@ class UserCreateView(CreateView): apply_url = 'participant_edit' def manipulate_object(self, form): - self.object.username = gen_username(form.cleaned_data['first_name'], form.cleaned_data['last_name']) + self.object.username = gen_username(form.cleaned_data['first_name'], + form.cleaned_data['last_name']) if not self.object.firstpassword: self.object.firstpassword = gen_password() @@ -216,7 +211,7 @@ class ParticipantsListPDF(PDFView): def append_to_pdf(self, story): data = [['#', _('Last Name'), _('First Name'), _('Group'), _('Type'), - _('Committee')]] + _('Committee')]] sort = 'last_name' counter = 0 for user in OpenSlidesUser.objects.all().order_by(sort): @@ -227,16 +222,14 @@ class ParticipantsListPDF(PDFView): Paragraph(user.first_name, stylesheet['Tablecell']), Paragraph(user.category, stylesheet['Tablecell']), Paragraph(user.type, stylesheet['Tablecell']), - Paragraph(user.committee, stylesheet['Tablecell']) - ]) - t = LongTable(data, - style=[ - ('VALIGN', (0, 0), (-1, -1), 'TOP'), - ('LINEABOVE', (0, 0), (-1, 0), 2, colors.black), - ('LINEABOVE', (0, 1), (-1, 1), 1, colors.black), - ('LINEBELOW', (0, -1), (-1, -1), 2, colors.black), - ('ROWBACKGROUNDS', (0, 1), (-1, -1), - (colors.white, (.9, .9, .9)))]) + Paragraph(user.committee, stylesheet['Tablecell'])]) + t = LongTable(data, style=[ + ('VALIGN', (0, 0), (-1, -1), 'TOP'), + ('LINEABOVE', (0, 0), (-1, 0), 2, colors.black), + ('LINEABOVE', (0, 1), (-1, 1), 1, colors.black), + ('LINEBELOW', (0, -1), (-1, -1), 2, colors.black), + ('ROWBACKGROUNDS', (0, 1), (-1, -1), + (colors.white, (.9, .9, .9)))]) t._argW[0] = 0.75 * cm story.append(t) @@ -250,7 +243,8 @@ class ParticipantsPasswordsPDF(PDFView): top_space = 0 def get_template(self, buffer): - return SimpleDocTemplate(buffer, topMargin=-6, bottomMargin=-6, + return SimpleDocTemplate( + buffer, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0, showBoundary=False) def build_document(self, pdf_document, story): @@ -270,12 +264,15 @@ class ParticipantsPasswordsPDF(PDFView): cell.append(Spacer(0, 0.5 * cm)) cell.append(Paragraph(_("User: %s") % (user.username), stylesheet['Monotype'])) - cell.append(Paragraph(_("Password: %s") - % (user.firstpassword), stylesheet['Monotype'])) + cell.append( + Paragraph( + _("Password: %s") + % (user.firstpassword), stylesheet['Monotype'])) cell.append(Spacer(0, 0.5 * cm)) - cell.append(Paragraph(_("URL: %s") - % (participant_pdf_system_url), - stylesheet['Ballot_option'])) + cell.append( + Paragraph( + _("URL: %s") % (participant_pdf_system_url), + stylesheet['Ballot_option'])) cell.append(Spacer(0, 0.5 * cm)) cell2 = [] cell2.append(Spacer(0, 0.8 * cm)) @@ -314,7 +311,9 @@ class UserImportView(FormView): for message in error_messages: messages.error(self.request, message) if success: - messages.success(self.request, _('%d new participants were successfully imported.') % success) + messages.success( + self.request, + _('%d new participants were successfully imported.') % success) return super(UserImportView, self).form_valid(form) @@ -341,7 +340,7 @@ class ResetPasswordView(RedirectView, SingleObjectMixin, QuestionMixin): if self.get_answer().lower() == 'yes': self.object.reset_password() messages.success(request, - _('The Password for %s was successfully reset.') % self.object) + _('The Password for %s was successfully reset.') % html_strong(self.object)) def get_answer_url(self): return reverse('user_reset_password', args=[self.object.id]) @@ -412,15 +411,15 @@ class Config(FormView): def get_initial(self): return { 'participant_pdf_system_url': config['participant_pdf_system_url'], - 'participant_pdf_welcometext': config['participant_pdf_welcometext'] - } + 'participant_pdf_welcometext': config['participant_pdf_welcometext']} def form_valid(self, form): - config['participant_pdf_system_url'] = \ - form.cleaned_data['participant_pdf_system_url'] - config['participant_pdf_welcometext'] = \ - form.cleaned_data['participant_pdf_welcometext'] - messages.success(self.request, + config['participant_pdf_system_url'] = ( + form.cleaned_data['participant_pdf_system_url']) + config['participant_pdf_welcometext'] = ( + form.cleaned_data['participant_pdf_welcometext']) + messages.success( + self.request, _('Participants settings successfully saved.')) return super(Config, self).form_valid(form) @@ -478,10 +477,10 @@ def login(request): "Installation was successfully! Use %(user)s " "(password: %(password)s) for first login.
" "Important: Please change the password after " - "first login! Otherwise this message still appears for everyone " - "and could be a security risk.") % { - 'user': html_strong(admin.username), - 'password': html_strong(config['admin_password'])} + "first login! Otherwise this message still appears for " + "everyone and could be a security risk.") % { + 'user': html_strong(admin.username), + 'password': html_strong(config['admin_password'])} extra_content['next'] = reverse('password_change') except User.DoesNotExist: pass @@ -496,7 +495,6 @@ def register_tab(request): return Tab( title=_('Participants'), url=reverse('user_overview'), - permission=request.user.has_perm('participant.can_see_participant') - or request.user.has_perm('participant.can_manage_participant'), - selected=selected, - ) + permission=request.user.has_perm('participant.can_see_participant') or + request.user.has_perm('participant.can_manage_participant'), + selected=selected)