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 django.db import models
from django.contrib.auth.models import AnonymousUser
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy, ugettext_noop, ugettext as _
@ -324,7 +325,12 @@ class Item(MPTTModel, SlideMixin):
class SpeakerManager(models.Manager):
def add(self, person, item):
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(
models.Max('weight'))['weight__max'] or 0)
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'):
try:
Speaker.objects.add(self.request.user, item)
except OpenSlidesError:
messages.error(request, _('You are already on the list of speakers.'))
except OpenSlidesError, e:
messages.error(request, e)
finally:
reverse_to_dashboard = False
else:

View File

@ -10,6 +10,7 @@
:license: GNU GPL, see LICENSE for more details.
"""
from django.db import models
from django.contrib.auth.models import AnonymousUser
from openslides.utils.exceptions import OpenSlidesError
@ -29,14 +30,23 @@ class PersonField(models.fields.Field):
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):
return value
elif value is None:
return None
elif isinstance(value, AnonymousUser):
raise AttributeError('An AnonymousUser can not be saved into the database.')
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):
"""

View File

@ -12,6 +12,7 @@
from django.test.client import Client
from django.db.models.query import EmptyQuerySet
from django.contrib.auth.models import AnonymousUser
from openslides.utils.test import TestCase
@ -32,3 +33,15 @@ class ItemTest(TestCase):
# update person field
test_object.save()
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)