OpenSlides/openslides/participant/models.py

148 lines
4.8 KiB
Python
Raw Normal View History

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-03 00:11:53 +02:00
from django.contrib.auth.models import User, Group
2011-07-31 10:46:29 +02:00
from django.db import models
from django.db.models import Q, signals
from django.dispatch import receiver
2012-07-10 01:33:03 +02:00
from django.utils.translation import ugettext_lazy as _, ugettext_noop
from openslides.utils.person import PersonMixin
from openslides.utils.person.signals import receiv_persons
2012-08-03 00:11:53 +02:00
from openslides.config.signals import default_config_value
2011-07-31 10:46:29 +02:00
class OpenSlidesUser(models.Model, PersonMixin):
person_prefix = 'openslides_user'
2011-07-31 10:46:29 +02:00
GENDER_CHOICES = (
('male', _('Male')),
('female', _('Female')),
)
TYPE_CHOICE = (
('delegate', _('Delegate')),
('observer', _('Observer')),
('staff', _('Staff')),
2011-09-07 22:46:47 +02:00
('guest', _('Guest')),
2011-07-31 10:46:29 +02:00
)
user = models.OneToOneField(User, unique=True, editable=False)
name_surfix = models.CharField(max_length=100, null=True, blank=True,
verbose_name = _("Name Surfix"), help_text=_('Shown behind the name.'))
gender = models.CharField(max_length=50, choices=GENDER_CHOICES, blank=True,
verbose_name = _("Gender"),
help_text=_('Only for filter the userlist.'))
type = models.CharField(max_length=100, choices=TYPE_CHOICE, blank=True,
verbose_name = _("Typ"), help_text=_('Only for filter the userlist.'))
committee = models.CharField(max_length=100, null=True, blank=True,
verbose_name = _("Committee"),
help_text=_('Only for filter the userlist.'))
comment = models.TextField(null=True, blank=True,
verbose_name = _('Comment'), help_text=_('Only for notes.'))
firstpassword = models.CharField(max_length=100, null=True, blank=True,
verbose_name = _("First Password"))
2011-09-03 17:17:29 +02:00
def reset_password(self):
"""
Reset the password for the user to his default-password.
"""
2011-09-03 17:17:29 +02:00
self.user.set_password(self.firstpassword)
2011-09-07 14:27:36 +02:00
self.user.save()
2011-07-31 10:46:29 +02:00
def has_perm(self, perm):
return self.user.has_perm(perm)
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':
return ('user_edit', [str(self.user.id)])
if link == 'delete':
return ('user_delete', [str(self.user.id)])
2011-07-31 10:46:29 +02:00
def __unicode__(self):
if self.name_surfix:
return "%s (%s)" % (self.user.get_full_name(), self.name_surfix)
2011-07-31 10:46:29 +02:00
return "%s" % self.user.get_full_name()
class Meta:
# Rename permissions
2011-07-31 10:46:29 +02:00
permissions = (
('can_see_participant', ugettext_noop("Can see participant")),
('can_manage_participant', ugettext_noop("Can manage participant")),
2011-07-31 10:46:29 +02:00
)
class OpenSlidesGroup(models.Model, PersonMixin):
person_prefix = 'openslides_group'
2012-08-03 00:11:53 +02:00
group = models.OneToOneField(Group)
group_as_person = models.BooleanField(default=False)
2012-08-03 00:11:53 +02:00
def __unicode__(self):
return unicode(self.group)
class OpenSlidesUsersConnecter(object):
def __init__(self, person_prefix=None, id=None):
self.person_prefix = person_prefix
self.id = id
2012-08-03 00:11:53 +02:00
def __iter__(self):
if not self.person_prefix or self.person_prefix == OpenSlidesUser.person_prefix:
if self.id:
yield OpenSlidesUser.objects.get(pk=self.id)
else:
for user in OpenSlidesUser.objects.all():
yield user
if not self.person_prefix or self.person_prefix == OpenSlidesGroup.person_prefix:
if self.id:
yield OpenSlidesObject.objects.get(pk=self.id)
else:
for group in OpenSlidesGroup.objects.all():
yield group
2012-08-03 00:11:53 +02:00
def __getitem__(self, key):
return OpenSlidesUser.objects.get(pk=key)
2012-08-03 00:11:53 +02:00
@receiver(receiv_persons, dispatch_uid="participant")
def receiv_persons(sender, **kwargs):
return OpenSlidesUsersConnecter(person_prefix=kwargs['person_prefix'], id=kwargs['id'])
2012-08-03 00:11:53 +02:00
@receiver(default_config_value, dispatch_uid="participant_default_config")
def default_config(sender, key, **kwargs):
"""
Default values for the participant app.
"""
# TODO: Rename config-vars
return {
'participant_pdf_system_url': 'http://example.com:8000',
2012-04-22 16:51:22 +02:00
'participant_pdf_welcometext': _('Welcome to OpenSlides!'),
'admin_password': None,
}.get(key)
@receiver(signals.post_save, sender=User)
def user_post_save(sender, instance, signal, *args, **kwargs):
# Creates OpenSlidesUser
profile, new = OpenSlidesUser.objects.get_or_create(user=instance)