2012-08-07 22:43:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-02-02 21:19:37 +01:00
|
|
|
from .signals import receive_persons
|
2012-08-07 22:43:57 +02:00
|
|
|
|
|
|
|
|
2012-10-24 12:15:29 +02:00
|
|
|
class Person(object):
|
|
|
|
"""
|
|
|
|
Meta-class for all person objects
|
|
|
|
"""
|
|
|
|
def person_id(self):
|
|
|
|
"""
|
|
|
|
Return an id for representation of ths person. Has to be unique.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError('Any person object needs a person_id')
|
|
|
|
|
2013-03-18 12:34:47 +01:00
|
|
|
def __unicode__(self):
|
2012-10-24 12:15:29 +02:00
|
|
|
"""
|
|
|
|
Return a string for this person.
|
|
|
|
"""
|
|
|
|
return str(self.person_id)
|
|
|
|
|
2012-11-22 16:19:09 +01:00
|
|
|
@property
|
|
|
|
def sort_name(self):
|
|
|
|
"""
|
|
|
|
Return the part of the name, which is used for sorting.
|
|
|
|
For example the pre-name or the last-name
|
|
|
|
"""
|
2012-11-22 16:54:56 +01:00
|
|
|
return self.clean_name.lower()
|
2012-11-22 16:19:09 +01:00
|
|
|
|
2012-10-24 12:15:29 +02:00
|
|
|
@property
|
|
|
|
def clean_name(self):
|
|
|
|
"""
|
|
|
|
Return the name of this person without a suffix
|
|
|
|
"""
|
|
|
|
return unicode(self)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name_suffix(self):
|
|
|
|
"""
|
|
|
|
Return a suffix for the person-name.
|
|
|
|
"""
|
|
|
|
return ''
|
|
|
|
|
2013-08-03 08:58:16 +02:00
|
|
|
def get_absolute_url(self, link='detail'):
|
|
|
|
"""
|
|
|
|
Return an absolute url for the person.
|
|
|
|
|
|
|
|
The argument 'link' affects which url it is. Typically it is one of:
|
|
|
|
* detail
|
|
|
|
* edit
|
|
|
|
* delete
|
|
|
|
|
|
|
|
You should raise an 'ValueError', if your person does not have
|
2014-01-11 17:57:24 +01:00
|
|
|
one of this links, or use the AbsoluteURLMixin, which raises the
|
|
|
|
ValueError for you.
|
2013-08-03 08:58:16 +02:00
|
|
|
"""
|
2014-01-11 17:57:24 +01:00
|
|
|
try:
|
|
|
|
url = super(Person, self).get_absolute_url(link)
|
|
|
|
except AttributeError:
|
|
|
|
raise ValueError('This person object has no url.')
|
|
|
|
return url
|
2013-08-03 08:58:16 +02:00
|
|
|
|
2012-10-24 12:15:29 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
class Persons(object):
|
|
|
|
"""
|
|
|
|
A Storage for a multiplicity of different Person-Objects.
|
|
|
|
"""
|
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-08-07 22:43:57 +02:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
try:
|
|
|
|
return iter(self._cache)
|
|
|
|
except AttributeError:
|
2012-09-15 09:11:56 +02:00
|
|
|
return self.iter_persons()
|
2012-08-07 22:43:57 +02:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(list(self.__iter__()))
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
2012-09-15 09:11:56 +02:00
|
|
|
try:
|
|
|
|
return list(self)[key]
|
|
|
|
except IndexError:
|
|
|
|
from openslides.utils.person import EmptyPerson
|
|
|
|
return EmptyPerson()
|
2012-08-07 22:43:57 +02:00
|
|
|
|
|
|
|
def iter_persons(self):
|
|
|
|
self._cache = list()
|
2012-08-13 14:37:49 +02:00
|
|
|
for receiver, persons in receive_persons.send(
|
|
|
|
sender='persons', person_prefix_filter=self.person_prefix_filter, id_filter=self.id_filter):
|
2012-08-07 22:43:57 +02:00
|
|
|
for person in persons:
|
|
|
|
self._cache.append(person)
|
|
|
|
yield person
|
|
|
|
|
|
|
|
|
|
|
|
def generate_person_id(prefix, id):
|
2012-08-12 12:52:38 +02:00
|
|
|
assert prefix is not None
|
|
|
|
assert id is not None
|
2012-08-07 22:43:57 +02:00
|
|
|
if ':' in prefix:
|
|
|
|
raise ValueError("':' is not allowed in a the 'person_prefix'")
|
|
|
|
return "%s:%d" % (prefix, id)
|
|
|
|
|
|
|
|
|
|
|
|
def split_person_id(person_id):
|
|
|
|
data = person_id.split(':', 1)
|
|
|
|
if len(data) == 2 and data[0] and data[1]:
|
|
|
|
return data
|
|
|
|
raise TypeError("Invalid person_id: '%s'" % person_id)
|
|
|
|
|
|
|
|
|
|
|
|
def get_person(person_id):
|
|
|
|
try:
|
|
|
|
person_prefix, id = split_person_id(person_id)
|
|
|
|
except TypeError:
|
2012-09-15 09:11:56 +02:00
|
|
|
from openslides.utils.person import EmptyPerson
|
|
|
|
return EmptyPerson()
|
2012-08-13 14:37:49 +02:00
|
|
|
return Persons(person_prefix_filter=person_prefix, id_filter=id)[0]
|