2011-07-31 10:46:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
openslides.participant.models
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Models for the participant app.
|
|
|
|
|
2012-04-25 22:29:19 +02:00
|
|
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
from django.contrib.auth.models import User as DjangoUser, Group as DjangoGroup
|
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
|
2012-07-10 01:33:03 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _, ugettext_noop
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
from openslides.utils.person import PersonMixin
|
2012-08-13 14:37:49 +02:00
|
|
|
from openslides.utils.person.signals import receive_persons
|
2012-08-03 00:11:53 +02:00
|
|
|
|
2012-09-14 02:46:43 +02:00
|
|
|
from openslides.config.models import config
|
2012-07-07 15:26:00 +02:00
|
|
|
from openslides.config.signals import default_config_value
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
class User(DjangoUser, PersonMixin):
|
|
|
|
person_prefix = 'user'
|
2011-07-31 10:46:29 +02:00
|
|
|
GENDER_CHOICES = (
|
|
|
|
('male', _('Male')),
|
|
|
|
('female', _('Female')),
|
|
|
|
)
|
2012-09-14 01:52:20 +02:00
|
|
|
TYPE_CHOICES = (
|
2011-07-31 10:46:29 +02:00
|
|
|
('delegate', _('Delegate')),
|
|
|
|
('observer', _('Observer')),
|
|
|
|
('staff', _('Staff')),
|
2011-09-07 22:46:47 +02:00
|
|
|
('guest', _('Guest')),
|
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-08-10 11:14:11 +02:00
|
|
|
category = models.CharField(
|
2012-08-10 11:51:45 +02:00
|
|
|
max_length=100, null=True, blank=True, verbose_name=_("Category"),
|
2012-08-10 13:11:10 +02:00
|
|
|
help_text=_('Will be shown behind the name.'))
|
2012-08-08 10:34:23 +02:00
|
|
|
gender = models.CharField(
|
|
|
|
max_length=50, choices=GENDER_CHOICES, blank=True,
|
|
|
|
verbose_name=_("Gender"), help_text=_('Only for filter the userlist.'))
|
|
|
|
type = models.CharField(
|
2012-09-14 01:52:20 +02:00
|
|
|
max_length=100, choices=TYPE_CHOICES, blank=True,
|
2012-08-08 10:34:23 +02:00
|
|
|
verbose_name=_("Typ"), help_text=_('Only for filter the userlist.'))
|
|
|
|
committee = models.CharField(
|
|
|
|
max_length=100, null=True, blank=True, verbose_name=_("Committee"),
|
2012-07-07 15:26:00 +02:00
|
|
|
help_text=_('Only for filter the userlist.'))
|
2012-08-08 10:34:23 +02:00
|
|
|
comment = models.TextField(
|
|
|
|
null=True, blank=True, verbose_name=_('Comment'),
|
|
|
|
help_text=_('Only for notes.'))
|
2012-08-13 14:37:49 +02:00
|
|
|
default_password = models.CharField(
|
2012-08-08 10:34:23 +02:00
|
|
|
max_length=100, null=True, blank=True,
|
2012-08-13 14:37:49 +02:00
|
|
|
verbose_name=_("Default password"))
|
2011-09-03 17:17:29 +02:00
|
|
|
|
2012-08-13 14:37:49 +02:00
|
|
|
def get_name_suffix(self):
|
2012-08-10 11:14:11 +02:00
|
|
|
return self.category
|
|
|
|
|
2012-08-13 14:37:49 +02:00
|
|
|
def set_name_suffix(self, value):
|
2012-08-10 11:14:11 +02:00
|
|
|
self.category = value
|
|
|
|
|
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-04-13 11:35:53 +02:00
|
|
|
@models.permalink
|
|
|
|
def get_absolute_url(self, link='edit'):
|
|
|
|
"""
|
|
|
|
Return the URL to this user.
|
|
|
|
|
|
|
|
link can be:
|
|
|
|
* edit
|
|
|
|
* delete
|
|
|
|
"""
|
|
|
|
if link == 'edit':
|
2012-08-11 10:09:54 +02:00
|
|
|
return ('user_edit', [str(self.id)])
|
2012-04-13 11:35:53 +02:00
|
|
|
if link == 'delete':
|
2012-08-11 10:09:54 +02:00
|
|
|
return ('user_delete', [str(self.id)])
|
2012-04-13 11:35:53 +02:00
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
def __unicode__(self):
|
2012-08-13 14:37:49 +02:00
|
|
|
name = self.get_full_name() or self.username
|
|
|
|
if self.name_suffix:
|
|
|
|
return u"%s (%s)" % (name, self.name_suffix)
|
2012-08-11 11:36:55 +02:00
|
|
|
return u"%s" % name
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
class Meta:
|
2012-08-07 22:43:57 +02:00
|
|
|
# Rename permissions
|
2011-07-31 10:46:29 +02:00
|
|
|
permissions = (
|
2012-07-07 15:26:00 +02:00
|
|
|
('can_see_participant', ugettext_noop("Can see participant")),
|
2012-08-08 10:34:23 +02:00
|
|
|
('can_manage_participant',
|
|
|
|
ugettext_noop("Can manage participant")),
|
2011-07-31 10:46:29 +02:00
|
|
|
)
|
2012-09-14 03:02:00 +02:00
|
|
|
ordering = ('last_name',)
|
2012-04-15 12:39:28 +02:00
|
|
|
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
class Group(DjangoGroup, PersonMixin):
|
|
|
|
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-08-07 22:43:57 +02:00
|
|
|
group_as_person = models.BooleanField(default=False)
|
2012-08-10 11:51:45 +02:00
|
|
|
description = models.TextField(blank=True)
|
2012-08-03 00:11:53 +02:00
|
|
|
|
2012-08-11 10:09:54 +02:00
|
|
|
@models.permalink
|
|
|
|
def get_absolute_url(self, link='edit'):
|
|
|
|
"""
|
|
|
|
Return the URL to this user.
|
|
|
|
|
|
|
|
link can be:
|
|
|
|
* edit
|
|
|
|
* delete
|
|
|
|
"""
|
|
|
|
if link == 'edit':
|
|
|
|
return ('user_group_edit', [str(self.id)])
|
|
|
|
if link == 'delete':
|
|
|
|
return ('user_group_delete', [str(self.id)])
|
|
|
|
|
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
|
|
|
|
2012-09-14 03:02:00 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-03 00:11:53 +02: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-09-14 03:02:00 +02:00
|
|
|
self.users = User.objects.all()
|
|
|
|
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-08-13 19:16:42 +02:00
|
|
|
yield self.users.get(pk=self.id_filter)
|
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-08-13 19:16:42 +02:00
|
|
|
yield self.groups.get(pk=self.id_filter)
|
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-09-14 02:24:02 +02:00
|
|
|
# Are the following two lines superfluous? They only return Users not Groups.
|
2012-08-03 00:11:53 +02:00
|
|
|
def __getitem__(self, key):
|
2012-08-12 12:52:38 +02:00
|
|
|
return User.objects.get(pk=key)
|
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
|
|
|
|
"""
|
|
|
|
return UsersAndGroupsToPersons(person_prefix_filter=kwargs['person_prefix_filter'],
|
2012-08-13 19:16:42 +02:00
|
|
|
id_filter=kwargs['id_filter'])
|
2012-08-03 00:11:53 +02:00
|
|
|
|
|
|
|
|
2012-04-15 12:39:28 +02:00
|
|
|
@receiver(default_config_value, dispatch_uid="participant_default_config")
|
|
|
|
def default_config(sender, key, **kwargs):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
|
|
|
Default values for the participant app.
|
|
|
|
"""
|
2012-08-07 22:43:57 +02:00
|
|
|
# TODO: Rename config-vars
|
2012-04-15 12:39:28 +02:00
|
|
|
return {
|
|
|
|
'participant_pdf_system_url': 'http://example.com:8000',
|
2012-04-22 16:51:22 +02:00
|
|
|
'participant_pdf_welcometext': _('Welcome to OpenSlides!'),
|
2012-09-14 02:40:40 +02:00
|
|
|
'participant_sort_users_by_first_name': False,
|
2012-04-15 12:39:28 +02:00
|
|
|
}.get(key)
|
2012-08-07 22:43:57 +02:00
|
|
|
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
@receiver(signals.post_save, sender=DjangoUser)
|
2012-08-07 22:43:57 +02:00
|
|
|
def user_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-08-10 11:51:45 +02:00
|
|
|
def group_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)
|