2011-07-31 10:46:29 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.participant.models
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Models for the participant app.
|
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
|
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from django.contrib.auth.models import Group as DjangoGroup
|
|
|
|
|
from django.contrib.auth.models import User as DjangoUser
|
|
|
|
|
from django.contrib.auth.models import Permission
|
2013-06-03 20:13:06 +02:00
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from django.core.urlresolvers import reverse
|
2011-07-31 10:46:29 +02:00
|
|
|
|
from django.db import models
|
2012-08-11 10:51:52 +02:00
|
|
|
|
from django.db.models import signals
|
2012-07-07 15:26:00 +02:00
|
|
|
|
from django.dispatch import receiver
|
2013-04-22 19:59:05 +02:00
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext_noop
|
2012-07-07 15:26:00 +02:00
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
|
from openslides.config.api import config
|
2013-08-04 12:59:11 +02:00
|
|
|
|
from openslides.projector.models import SlideMixin
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from openslides.utils.person import Person, PersonMixin
|
|
|
|
|
from openslides.utils.person.signals import receive_persons
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
class User(SlideMixin, PersonMixin, Person, DjangoUser):
|
|
|
|
|
slide_callback_name = 'user'
|
2012-08-12 12:52:38 +02:00
|
|
|
|
person_prefix = 'user'
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
GENDER_CHOICES = (
|
2013-04-22 19:59:05 +02:00
|
|
|
|
('male', ugettext_lazy('Male')),
|
|
|
|
|
('female', ugettext_lazy('Female')),
|
2011-07-31 10:46:29 +02:00
|
|
|
|
)
|
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
|
django_user = models.OneToOneField(DjangoUser, editable=False, parent_link=True)
|
2012-11-19 23:30:09 +01:00
|
|
|
|
structure_level = models.CharField(
|
2013-04-22 19:59:05 +02:00
|
|
|
|
max_length=255, blank=True, default='', verbose_name=ugettext_lazy("Structure level"),
|
|
|
|
|
help_text=ugettext_lazy('Will be shown after the name.'))
|
2013-04-08 21:47:33 +02:00
|
|
|
|
title = models.CharField(
|
2013-04-22 19:59:05 +02:00
|
|
|
|
max_length=50, blank=True, default='', verbose_name=ugettext_lazy("Title"),
|
|
|
|
|
help_text=ugettext_lazy('Will be shown before the name.'))
|
2012-08-08 10:34:23 +02:00
|
|
|
|
gender = models.CharField(
|
|
|
|
|
max_length=50, choices=GENDER_CHOICES, blank=True,
|
2013-04-22 19:59:05 +02:00
|
|
|
|
verbose_name=ugettext_lazy("Gender"), help_text=ugettext_lazy('Only for filtering the participant list.'))
|
2012-08-08 10:34:23 +02:00
|
|
|
|
committee = models.CharField(
|
2013-04-22 19:59:05 +02:00
|
|
|
|
max_length=255, blank=True, default='', verbose_name=ugettext_lazy("Committee"),
|
|
|
|
|
help_text=ugettext_lazy('Only for filtering the participant list.'))
|
2012-11-03 09:34:42 +01:00
|
|
|
|
about_me = models.TextField(
|
2013-04-22 19:59:05 +02:00
|
|
|
|
blank=True, default='', verbose_name=ugettext_lazy('About me'),
|
|
|
|
|
help_text=ugettext_lazy('Your profile text'))
|
2012-08-08 10:34:23 +02:00
|
|
|
|
comment = models.TextField(
|
2013-04-22 19:59:05 +02:00
|
|
|
|
blank=True, default='', verbose_name=ugettext_lazy('Comment'),
|
|
|
|
|
help_text=ugettext_lazy('Only for notes.'))
|
2012-08-13 14:37:49 +02:00
|
|
|
|
default_password = models.CharField(
|
2012-10-24 12:30:45 +02:00
|
|
|
|
max_length=100, blank=True, default='',
|
2013-04-22 19:59:05 +02:00
|
|
|
|
verbose_name=ugettext_lazy("Default password"))
|
2011-09-03 17:17:29 +02:00
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
class Meta:
|
|
|
|
|
permissions = (
|
|
|
|
|
('can_see_participant', ugettext_noop('Can see participants')),
|
|
|
|
|
('can_manage_participant', ugettext_noop('Can manage participants')),
|
|
|
|
|
)
|
|
|
|
|
ordering = ('last_name',)
|
|
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
|
if self.name_suffix:
|
|
|
|
|
return u"%s (%s)" % (self.clean_name, self.name_suffix)
|
|
|
|
|
return u"%s" % self.clean_name
|
|
|
|
|
|
|
|
|
|
def get_absolute_url(self, link='detail'):
|
|
|
|
|
"""
|
|
|
|
|
Return the URL to the user.
|
|
|
|
|
"""
|
|
|
|
|
if link == 'detail' or link == 'view':
|
|
|
|
|
return reverse('user_view', args=[str(self.id)])
|
|
|
|
|
if link == 'edit':
|
|
|
|
|
return reverse('user_edit', args=[str(self.id)])
|
|
|
|
|
if link == 'delete':
|
|
|
|
|
return reverse('user_delete', args=[str(self.id)])
|
|
|
|
|
return super(User, self).get_absolute_url(link)
|
|
|
|
|
|
2013-10-03 21:49:51 +02:00
|
|
|
|
def get_slide_context(self, **context):
|
|
|
|
|
# Does not call super. In this case the context would override the name
|
|
|
|
|
# 'user'.
|
|
|
|
|
return {'shown_user': self}
|
|
|
|
|
|
2012-10-24 12:15:29 +02:00
|
|
|
|
@property
|
|
|
|
|
def clean_name(self):
|
2013-04-08 21:47:33 +02:00
|
|
|
|
if self.title:
|
|
|
|
|
name = "%s %s" % (self.title, self.get_full_name())
|
|
|
|
|
else:
|
|
|
|
|
name = self.get_full_name()
|
|
|
|
|
return name or self.username
|
2012-10-24 12:15:29 +02:00
|
|
|
|
|
2012-08-13 14:37:49 +02:00
|
|
|
|
def get_name_suffix(self):
|
2012-11-19 23:30:09 +01:00
|
|
|
|
return self.structure_level
|
2012-08-10 11:14:11 +02:00
|
|
|
|
|
2012-08-13 14:37:49 +02:00
|
|
|
|
def set_name_suffix(self, value):
|
2012-11-19 23:30:09 +01:00
|
|
|
|
self.structure_level = value
|
2012-08-10 11:14:11 +02:00
|
|
|
|
|
2012-08-13 14:37:49 +02:00
|
|
|
|
name_suffix = property(get_name_suffix, set_name_suffix)
|
2012-08-10 11:14:11 +02:00
|
|
|
|
|
2012-08-08 10:07:36 +02:00
|
|
|
|
def reset_password(self, password=None):
|
2012-07-07 15:26:00 +02:00
|
|
|
|
"""
|
|
|
|
|
Reset the password for the user to his default-password.
|
|
|
|
|
"""
|
2012-08-08 10:07:36 +02:00
|
|
|
|
if password is None:
|
2012-08-13 14:37:49 +02:00
|
|
|
|
password = self.default_password
|
2012-08-12 12:52:38 +02:00
|
|
|
|
self.set_password(password)
|
|
|
|
|
self.save()
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-11-22 16:19:09 +01:00
|
|
|
|
@property
|
|
|
|
|
def sort_name(self):
|
|
|
|
|
if config['participant_sort_users_by_first_name']:
|
2012-11-22 16:54:56 +01:00
|
|
|
|
return self.first_name.lower()
|
|
|
|
|
return self.last_name.lower()
|
2012-11-22 16:19:09 +01:00
|
|
|
|
|
2012-04-15 12:39:28 +02:00
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
class Group(SlideMixin, PersonMixin, Person, DjangoGroup):
|
|
|
|
|
slide_callback_name = 'group'
|
2012-08-12 12:52:38 +02:00
|
|
|
|
person_prefix = 'group'
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
|
django_group = models.OneToOneField(DjangoGroup, editable=False, parent_link=True)
|
2012-11-26 10:11:36 +01:00
|
|
|
|
group_as_person = models.BooleanField(
|
2013-04-22 19:59:05 +02:00
|
|
|
|
default=False, verbose_name=ugettext_lazy("Use this group as participant"),
|
|
|
|
|
help_text=ugettext_lazy('For example as submitter of a motion.'))
|
|
|
|
|
description = models.TextField(blank=True, verbose_name=ugettext_lazy("Description"))
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
class Meta:
|
|
|
|
|
ordering = ('name',)
|
2012-08-11 10:09:54 +02:00
|
|
|
|
|
2012-08-03 00:11:53 +02:00
|
|
|
|
def __unicode__(self):
|
2012-08-12 12:52:38 +02:00
|
|
|
|
return unicode(self.name)
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
def get_absolute_url(self, link='detail'):
|
2012-11-21 14:22:30 +01:00
|
|
|
|
"""
|
2013-08-04 12:59:11 +02:00
|
|
|
|
Return the URL to the user group.
|
2012-11-21 14:22:30 +01:00
|
|
|
|
"""
|
2013-08-04 12:59:11 +02:00
|
|
|
|
if link == 'detail' or link == 'view':
|
2013-09-25 12:53:44 +02:00
|
|
|
|
return reverse('user_group_view', args=[str(self.pk)])
|
2013-08-04 12:59:11 +02:00
|
|
|
|
if link == 'update' or link == 'edit':
|
2013-09-25 12:53:44 +02:00
|
|
|
|
return reverse('user_group_edit', args=[str(self.pk)])
|
2013-08-04 12:59:11 +02:00
|
|
|
|
if link == 'delete':
|
2013-09-25 12:53:44 +02:00
|
|
|
|
return reverse('user_group_delete', args=[str(self.pk)])
|
2013-08-04 12:59:11 +02:00
|
|
|
|
return super(Group, self).get_absolute_url(link)
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
|
2012-09-14 02:24:02 +02:00
|
|
|
|
class UsersAndGroupsToPersons(object):
|
|
|
|
|
"""
|
|
|
|
|
Object to send all Users and Groups or a special User or Group to
|
|
|
|
|
the Person-API via receice_persons()
|
|
|
|
|
"""
|
2012-08-13 14:37:49 +02:00
|
|
|
|
def __init__(self, person_prefix_filter=None, id_filter=None):
|
|
|
|
|
self.person_prefix_filter = person_prefix_filter
|
|
|
|
|
self.id_filter = id_filter
|
2012-09-14 02:46:43 +02:00
|
|
|
|
if config['participant_sort_users_by_first_name']:
|
|
|
|
|
self.users = User.objects.all().order_by('first_name')
|
|
|
|
|
else:
|
2012-10-30 23:00:24 +01:00
|
|
|
|
self.users = User.objects.all().order_by('last_name')
|
2012-08-13 14:37:49 +02:00
|
|
|
|
self.groups = Group.objects.filter(group_as_person=True)
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
2012-08-13 14:37:49 +02:00
|
|
|
|
if (not self.person_prefix_filter or
|
|
|
|
|
self.person_prefix_filter == User.person_prefix):
|
|
|
|
|
if self.id_filter:
|
2012-09-15 09:11:56 +02:00
|
|
|
|
try:
|
|
|
|
|
yield self.users.get(pk=self.id_filter)
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
pass
|
2012-08-03 14:23:13 +02:00
|
|
|
|
else:
|
2012-08-13 14:37:49 +02:00
|
|
|
|
for user in self.users:
|
2012-08-07 22:43:57 +02:00
|
|
|
|
yield user
|
2012-08-03 14:23:13 +02:00
|
|
|
|
|
2012-08-13 14:37:49 +02:00
|
|
|
|
if (not self.person_prefix_filter or
|
|
|
|
|
self.person_prefix_filter == Group.person_prefix):
|
|
|
|
|
if self.id_filter:
|
2012-09-15 09:11:56 +02:00
|
|
|
|
try:
|
|
|
|
|
yield self.groups.get(pk=self.id_filter)
|
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
|
pass
|
2012-08-03 14:23:13 +02:00
|
|
|
|
else:
|
2012-08-13 14:37:49 +02:00
|
|
|
|
for group in self.groups:
|
2012-08-03 14:23:13 +02:00
|
|
|
|
yield group
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-13 14:37:49 +02:00
|
|
|
|
@receiver(receive_persons, dispatch_uid="participant")
|
|
|
|
|
def receive_persons(sender, **kwargs):
|
2012-09-14 02:24:02 +02:00
|
|
|
|
"""
|
|
|
|
|
Answers to the Person-API
|
|
|
|
|
"""
|
2012-11-24 14:01:21 +01:00
|
|
|
|
return UsersAndGroupsToPersons(
|
|
|
|
|
person_prefix_filter=kwargs['person_prefix_filter'],
|
|
|
|
|
id_filter=kwargs['id_filter'])
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
|
@receiver(signals.post_save, sender=DjangoUser)
|
2012-11-22 18:39:54 +01:00
|
|
|
|
def djangouser_post_save(sender, instance, signal, *args, **kwargs):
|
2012-08-10 13:11:10 +02:00
|
|
|
|
try:
|
2012-08-12 12:52:38 +02:00
|
|
|
|
instance.user
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
User(django_user=instance).save_base(raw=True)
|
2012-08-10 11:51:45 +02:00
|
|
|
|
|
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
|
@receiver(signals.post_save, sender=DjangoGroup)
|
2012-11-22 18:39:54 +01:00
|
|
|
|
def djangogroup_post_save(sender, instance, signal, *args, **kwargs):
|
2012-08-11 10:09:54 +02:00
|
|
|
|
try:
|
2012-08-12 12:52:38 +02:00
|
|
|
|
instance.group
|
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
|
Group(django_group=instance).save_base(raw=True)
|
2012-11-22 18:39:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(signals.post_save, sender=User)
|
|
|
|
|
def user_post_save(sender, instance, *args, **kwargs):
|
|
|
|
|
if not kwargs['created']:
|
|
|
|
|
return
|
2013-03-12 20:58:22 +01:00
|
|
|
|
from openslides.participant.api import get_registered_group # TODO: Test, if global import is possible
|
|
|
|
|
registered = get_registered_group()
|
2012-11-22 18:39:54 +01:00
|
|
|
|
instance.groups.add(registered)
|
|
|
|
|
instance.save()
|
2013-06-03 20:13:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_protected_perm():
|
|
|
|
|
"""
|
|
|
|
|
Returns the permission to manage participants. This function is a helper
|
|
|
|
|
function used to protect manager users from locking out themselves.
|
|
|
|
|
"""
|
|
|
|
|
return Permission.objects.get(
|
|
|
|
|
content_type=ContentType.objects.get(app_label='participant', model='user'),
|
|
|
|
|
codename='can_manage_participant')
|