From c8dfb9437b08d0350073a3d7e8d065e20de868e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Mon, 7 Sep 2015 17:09:29 +0200 Subject: [PATCH] Removed imports from openslides.users in other apps: Cleaned up agenda, assignments and motions. --- openslides/agenda/models.py | 20 ++++++++++++++------ openslides/assignments/models.py | 8 ++++---- openslides/assignments/views.py | 21 +++++++++++++-------- openslides/motions/models.py | 5 ++--- openslides/motions/pdf.py | 19 +++++++++++-------- openslides/users/models.py | 2 -- 6 files changed, 44 insertions(+), 31 deletions(-) diff --git a/openslides/agenda/models.py b/openslides/agenda/models.py index f65d595e1..b6f6bff17 100644 --- a/openslides/agenda/models.py +++ b/openslides/agenda/models.py @@ -1,6 +1,7 @@ from collections import defaultdict from datetime import datetime +from django.conf import settings from django.contrib.auth.models import AnonymousUser from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType @@ -12,7 +13,6 @@ from django.utils.translation import ugettext_lazy, ugettext_noop from openslides.core.config import config from openslides.core.models import Tag from openslides.core.projector import Countdown -from openslides.users.models import User from openslides.utils.exceptions import OpenSlidesError from openslides.utils.models import RESTModelMixin from openslides.utils.utils import to_roman @@ -304,7 +304,7 @@ class Item(RESTModelMixin, models.Model): def get_next_speaker(self): """ - Returns the speaker object of the user who is next. + Returns the speaker object of the speaker who is next. """ try: return self.speakers.filter(begin_time=None).order_by('weight')[0] @@ -352,10 +352,18 @@ class Item(RESTModelMixin, models.Model): class SpeakerManager(models.Manager): + """ + Manager for Speaker model. Provides a customized add method. + """ def add(self, user, item): + """ + Customized manager method to prevent anonymous users to be on the + list of speakers and that someone is twice on one list (off coming + speakers). Cares also initial sorting of the coming speakers. + """ if self.filter(user=user, item=item, begin_time=None).exists(): - raise OpenSlidesError(_( - '%(user)s is already on the list of speakers of item %(id)s.') + raise OpenSlidesError( + _('%(user)s is already on the list of speakers of item %(id)s.') % {'user': user, 'id': item.id}) if isinstance(user, AnonymousUser): raise OpenSlidesError( @@ -372,14 +380,14 @@ class Speaker(RESTModelMixin, models.Model): objects = SpeakerManager() - user = models.ForeignKey(User) + user = models.ForeignKey(settings.AUTH_USER_MODEL) """ ForeinKey to the user who speaks. """ item = models.ForeignKey(Item, related_name='speakers') """ - ForeinKey to the AgendaItem to which the user want to speak. + ForeinKey to the agenda item to which the user want to speak. """ begin_time = models.DateTimeField(null=True) diff --git a/openslides/assignments/models.py b/openslides/assignments/models.py index b2160090e..83d4ab9b3 100644 --- a/openslides/assignments/models.py +++ b/openslides/assignments/models.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.contrib.contenttypes.fields import GenericRelation from django.db import models from django.utils.datastructures import SortedDict @@ -14,7 +15,6 @@ from openslides.poll.models import ( CollectDefaultVotesMixin, PublishPollMixin, ) -from openslides.users.models import User from openslides.utils.exceptions import OpenSlidesError from openslides.utils.models import RESTModelMixin @@ -36,7 +36,7 @@ class AssignmentRelatedUser(RESTModelMixin, models.Model): 'Assignment', db_index=True, related_name='assignment_related_users') - user = models.ForeignKey(User, db_index=True) + user = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True) status = models.IntegerField( choices=STATUSES, default=STATUS_CANDIDATE) @@ -103,7 +103,7 @@ class Assignment(RESTModelMixin, models.Model): """ related_users = models.ManyToManyField( - User, + settings.AUTH_USER_MODEL, through='AssignmentRelatedUser') """ Users that a candidates, elected or blocked as candidate. @@ -324,7 +324,7 @@ class AssignmentVote(RESTModelMixin, BaseVote): class AssignmentOption(RESTModelMixin, BaseOption): poll = models.ForeignKey('AssignmentPoll') - candidate = models.ForeignKey(User) + candidate = models.ForeignKey(settings.AUTH_USER_MODEL) vote_class = AssignmentVote def __str__(self): diff --git a/openslides/assignments/views.py b/openslides/assignments/views.py index 2e760fa4b..0c291688a 100644 --- a/openslides/assignments/views.py +++ b/openslides/assignments/views.py @@ -1,5 +1,7 @@ from cgi import escape +from django.conf import settings +from django.contrib.auth import get_user_model from django.db import transaction from django.utils.translation import ugettext as _ from django.utils.translation import ungettext @@ -16,7 +18,6 @@ from reportlab.platypus import ( ) from openslides.core.config import config -from openslides.users.models import Group, User # TODO: remove this from openslides.utils.pdf import stylesheet from openslides.utils.rest_api import ( DestroyModelMixin, @@ -134,8 +135,8 @@ class AssignmentViewSet(ModelViewSet): except ValueError: raise ValidationError({'detail': _('Invalid data. Expected something like {"user": }.')}) try: - user = User.objects.get(pk=user_pk) - except User.DoesNotExist: + user = get_user_model().objects.get(pk=user_pk) + except get_user_model().DoesNotExist: raise ValidationError({'detail': _('Invalid data. User %d does not exist.') % user_pk}) return user @@ -480,13 +481,17 @@ class AssignmentPollPDF(PDFView): # set number of ballot papers if ballot_papers_selection == "NUMBER_OF_DELEGATES": - try: - if Group.objects.get(pk=3): - number = User.objects.filter(groups__pk=3).count() - except Group.DoesNotExist: + if 'openslides.users' in settings.INSTALLED_APPS: + from openslides.users.models import Group + try: + if Group.objects.get(pk=3): + number = get_user_model().objects.filter(groups__pk=3).count() + except Group.DoesNotExist: + number = 0 + else: number = 0 elif ballot_papers_selection == "NUMBER_OF_ALL_PARTICIPANTS": - number = int(User.objects.count()) + number = int(get_user_model().objects.count()) else: # ballot_papers_selection == "CUSTOM_NUMBER" number = int(ballot_papers_number) number = max(1, number) diff --git a/openslides/motions/models.py b/openslides/motions/models.py index 74c11879e..3948fc577 100644 --- a/openslides/motions/models.py +++ b/openslides/motions/models.py @@ -15,7 +15,6 @@ from openslides.poll.models import ( BaseVote, CollectDefaultVotesMixin, ) -from openslides.users.models import User from openslides.utils.models import RESTModelMixin from .exceptions import WorkflowError @@ -599,8 +598,8 @@ class MotionLog(RESTModelMixin, models.Model): The log message. It should be a list of strings in English. """ - person = models.ForeignKey(User, null=True) - """A person object, who created the log message. Optional.""" + person = models.ForeignKey(settings.AUTH_USER_MODEL, null=True) + """A user object, who created the log message. Optional.""" time = models.DateTimeField(auto_now=True) """The Time, when the loged action was performed.""" diff --git a/openslides/motions/pdf.py b/openslides/motions/pdf.py index 8c23db29d..eb1797c1b 100644 --- a/openslides/motions/pdf.py +++ b/openslides/motions/pdf.py @@ -3,6 +3,8 @@ from cgi import escape from operator import attrgetter from bs4 import BeautifulSoup +from django.conf import settings +from django.contrib.auth import get_user_model from django.utils.translation import ugettext as _ from natsort import natsorted from reportlab.lib import colors @@ -10,7 +12,6 @@ from reportlab.lib.units import cm from reportlab.platypus import PageBreak, Paragraph, Spacer, Table, TableStyle from openslides.core.config import config -from openslides.users.models import Group, User # TODO: remove this line from openslides.utils.pdf import stylesheet from .models import Category @@ -278,15 +279,17 @@ def motion_poll_to_pdf(pdf, poll): # set number of ballot papers if ballot_papers_selection == "NUMBER_OF_DELEGATES": - # TODO: get this number from users - try: - if Group.objects.get(pk=3): # TODO: Find a better way - number = User.objects.filter(groups__pk=3).count() - except Group.DoesNotExist: + if 'openslides.users' in settings.INSTALLED_APPS: + from openslides.users.models import Group + try: + if Group.objects.get(pk=3): + number = get_user_model().objects.filter(groups__pk=3).count() + except Group.DoesNotExist: + number = 0 + else: number = 0 elif ballot_papers_selection == "NUMBER_OF_ALL_PARTICIPANTS": - # TODO: get the number from the persons - number = int(User.objects.count()) + number = int(get_user_model().objects.count()) else: # ballot_papers_selection == "CUSTOM_NUMBER" number = int(ballot_papers_number) number = max(1, number) diff --git a/openslides/users/models.py b/openslides/users/models.py index 534dd4be5..d2093c5eb 100644 --- a/openslides/users/models.py +++ b/openslides/users/models.py @@ -1,5 +1,3 @@ -# TODO: Check every app, that they do not import Group or User from here. - from random import choice from django.contrib.auth.hashers import make_password