Merge pull request #723 from ostcar/issue_721

Fixes #721
This commit is contained in:
Oskar Hahn 2013-06-09 09:58:06 -07:00
commit 5889bcda2c
4 changed files with 34 additions and 5 deletions

View File

@ -13,6 +13,7 @@
from datetime import datetime from datetime import datetime
from django.db import models from django.db import models
from django.contrib.auth.models import AnonymousUser
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy, ugettext_noop, ugettext as _ from django.utils.translation import ugettext_lazy, ugettext_noop, ugettext as _
@ -324,7 +325,12 @@ class Item(MPTTModel, SlideMixin):
class SpeakerManager(models.Manager): class SpeakerManager(models.Manager):
def add(self, person, item): def add(self, person, item):
if self.filter(person=person, item=item, begin_time=None).exists(): if self.filter(person=person, item=item, begin_time=None).exists():
raise OpenSlidesError(_('%(person)s is already on the list of speakers of item %(id)s.') % {'person': person, 'id': item.id}) raise OpenSlidesError(_(
'%(person)s is already on the list of speakers of item %(id)s.')
% {'person': person, 'id': item.id})
if isinstance(person, AnonymousUser):
raise OpenSlidesError(
_('An anonymous user can not be on the list of speakers.'))
weight = (self.filter(item=item).aggregate( weight = (self.filter(item=item).aggregate(
models.Max('weight'))['weight__max'] or 0) models.Max('weight'))['weight__max'] or 0)
return self.create(item=item, person=person, weight=weight + 1) return self.create(item=item, person=person, weight=weight + 1)

View File

@ -491,8 +491,8 @@ class CurrentListOfSpeakersView(RedirectView):
if self.request.user.has_perm('agenda.can_be_speaker'): if self.request.user.has_perm('agenda.can_be_speaker'):
try: try:
Speaker.objects.add(self.request.user, item) Speaker.objects.add(self.request.user, item)
except OpenSlidesError: except OpenSlidesError, e:
messages.error(request, _('You are already on the list of speakers.')) messages.error(request, e)
finally: finally:
reverse_to_dashboard = False reverse_to_dashboard = False
else: else:

View File

@ -10,6 +10,7 @@
:license: GNU GPL, see LICENSE for more details. :license: GNU GPL, see LICENSE for more details.
""" """
from django.db import models from django.db import models
from django.contrib.auth.models import AnonymousUser
from openslides.utils.exceptions import OpenSlidesError from openslides.utils.exceptions import OpenSlidesError
@ -29,14 +30,23 @@ class PersonField(models.fields.Field):
def to_python(self, value): def to_python(self, value):
""" """
Convert string value to a User Object. Convert an object to an user Object.
'value' has to be an object derivated from PersonMixin, None or has to
have an attribute 'person_id'.
""" """
if isinstance(value, PersonMixin): if isinstance(value, PersonMixin):
return value return value
elif value is None: elif value is None:
return None return None
elif isinstance(value, AnonymousUser):
raise AttributeError('An AnonymousUser can not be saved into the database.')
else: else:
return get_person(value) try:
return get_person(value)
except AttributeError:
raise AttributeError('You can not save \'%s\' into a person field.'
% type(value))
def get_prep_value(self, value): def get_prep_value(self, value):
""" """

View File

@ -12,6 +12,7 @@
from django.test.client import Client from django.test.client import Client
from django.db.models.query import EmptyQuerySet from django.db.models.query import EmptyQuerySet
from django.contrib.auth.models import AnonymousUser
from openslides.utils.test import TestCase from openslides.utils.test import TestCase
@ -32,3 +33,15 @@ class ItemTest(TestCase):
# update person field # update person field
test_object.save() test_object.save()
self.assertEqual(TestModel.objects.get(pk=test_object.pk).person, self.person1) self.assertEqual(TestModel.objects.get(pk=test_object.pk).person, self.person1)
def test_save_anonymous_user_in_person_field(self):
with self.assertRaisesRegexp(
AttributeError,
'An AnonymousUser can not be saved into the database.'):
TestModel.objects.create(person=AnonymousUser())
def test_save_unsupported_object_in_person_field(self):
with self.assertRaisesRegexp(
AttributeError,
'You can not save \'<type \'int\'>\' into a person field.'):
TestModel.objects.create(person=5)