2012-08-04 13:18:06 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2012-08-07 22:43:57 +02:00
|
|
|
openslides.utils.person.forms
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
Forms and FormFields for the OpenSlides person api.
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
from openslides.utils.person.api import get_person, Persons
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2012-08-04 14:47:09 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
class PersonChoices(object):
|
2012-08-04 13:18:06 +02:00
|
|
|
def __init__(self, field):
|
|
|
|
self.field = field
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
if self.field.empty_label is not None:
|
2013-02-16 16:19:20 +01:00
|
|
|
yield (u'', self.field.empty_label)
|
2012-11-22 16:54:56 +01:00
|
|
|
for person in sorted(Persons(), key=lambda person: person.sort_name):
|
2012-08-07 22:43:57 +02:00
|
|
|
yield (person.person_id, person)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
class PersonFormField(forms.fields.ChoiceField):
|
2012-08-04 13:18:06 +02:00
|
|
|
def __init__(self, required=True, initial=None, empty_label=u"---------",
|
|
|
|
*args, **kwargs):
|
|
|
|
if required and (initial is not None):
|
|
|
|
self.empty_label = None
|
|
|
|
else:
|
|
|
|
self.empty_label = empty_label
|
|
|
|
forms.fields.Field.__init__(self, required=required, initial=initial,
|
|
|
|
*args, **kwargs)
|
|
|
|
self.widget.choices = self.choices
|
|
|
|
|
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
result = super(forms.fields.ChoiceField, self).__deepcopy__(memo)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _get_choices(self):
|
|
|
|
# If self._choices is set, then somebody must have manually set
|
|
|
|
# the property self.choices. In this case, just return self._choices.
|
|
|
|
if hasattr(self, '_choices'):
|
|
|
|
return self._choices
|
2012-08-07 22:43:57 +02:00
|
|
|
return PersonChoices(self)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
choices = property(_get_choices, forms.fields.ChoiceField._set_choices)
|
|
|
|
|
|
|
|
def to_python(self, value):
|
2013-02-16 16:19:20 +01:00
|
|
|
if value == u'':
|
|
|
|
return u''
|
2012-08-07 22:43:57 +02:00
|
|
|
return get_person(value)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
def valid_value(self, value):
|
2012-08-07 22:43:57 +02:00
|
|
|
return super(PersonFormField, self).valid_value(value.person_id)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
class MultiplePersonFormField(PersonFormField):
|
2012-08-04 13:18:06 +02:00
|
|
|
widget = forms.widgets.SelectMultiple
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2012-11-24 14:01:21 +01:00
|
|
|
super(MultiplePersonFormField, self).__init__(
|
|
|
|
empty_label=None, *args, **kwargs)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if hasattr(value, '__iter__'):
|
2012-08-07 22:43:57 +02:00
|
|
|
return [super(MultiplePersonFormField, self).to_python(v)
|
2012-08-04 13:18:06 +02:00
|
|
|
for v in value]
|
2012-08-07 22:43:57 +02:00
|
|
|
return super(MultiplePersonFormField, self).to_python(value)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
def valid_value(self, value):
|
|
|
|
if hasattr(value, '__iter__'):
|
2012-08-07 22:43:57 +02:00
|
|
|
return [super(MultiplePersonFormField, self).valid_value(v)
|
2012-08-04 13:18:06 +02:00
|
|
|
for v in value]
|
2012-08-07 22:43:57 +02:00
|
|
|
return super(MultiplePersonFormField, self).valid_value(value)
|