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.models
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
Models and ModelFields 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.
|
|
|
|
"""
|
2013-06-09 17:51:30 +02:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.db import models
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2013-04-24 09:28:58 +02:00
|
|
|
from openslides.utils.exceptions import OpenSlidesError
|
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
from .api import generate_person_id, get_person
|
2013-02-02 21:19:37 +01:00
|
|
|
from .forms import PersonFormField
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
class PersonField(models.fields.Field):
|
2012-08-04 14:47:09 +02:00
|
|
|
__metaclass__ = models.SubfieldBase
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2012-08-04 14:47:09 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2013-02-02 21:19:37 +01:00
|
|
|
kwargs['max_length'] = 255
|
|
|
|
super(PersonField, self).__init__(*args, **kwargs)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2012-08-04 14:47:09 +02:00
|
|
|
def get_internal_type(self):
|
|
|
|
return "CharField"
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
def to_python(self, value):
|
2012-08-04 14:47:09 +02:00
|
|
|
"""
|
2013-06-09 17:51:30 +02:00
|
|
|
Convert an object to an user Object.
|
|
|
|
|
|
|
|
'value' has to be an object derivated from PersonMixin, None or has to
|
|
|
|
have an attribute 'person_id'.
|
2012-08-04 14:47:09 +02:00
|
|
|
"""
|
2013-03-14 22:18:56 +01:00
|
|
|
if isinstance(value, PersonMixin):
|
2013-02-02 21:19:37 +01:00
|
|
|
return value
|
|
|
|
elif value is None:
|
|
|
|
return None
|
2013-06-09 17:51:30 +02:00
|
|
|
elif isinstance(value, AnonymousUser):
|
|
|
|
raise AttributeError('An AnonymousUser can not be saved into the database.')
|
2012-08-04 14:47:09 +02:00
|
|
|
else:
|
2013-06-09 17:51:30 +02:00
|
|
|
try:
|
|
|
|
return get_person(value)
|
|
|
|
except AttributeError:
|
|
|
|
raise AttributeError('You can not save \'%s\' into a person field.'
|
|
|
|
% type(value))
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2012-08-04 14:47:09 +02:00
|
|
|
def get_prep_value(self, value):
|
2013-02-02 21:19:37 +01:00
|
|
|
"""
|
|
|
|
Convert a person object to a string, to store it in the database.
|
|
|
|
"""
|
|
|
|
if value is None:
|
2013-04-24 09:28:58 +02:00
|
|
|
# For Fields with null=True
|
2013-02-02 21:19:37 +01:00
|
|
|
return None
|
2013-03-18 12:34:47 +01:00
|
|
|
elif isinstance(value, basestring):
|
2013-04-24 09:28:58 +02:00
|
|
|
# The object is already a a person_id
|
2013-03-18 12:34:47 +01:00
|
|
|
return value
|
2013-04-22 19:59:05 +02:00
|
|
|
|
2013-04-24 09:28:58 +02:00
|
|
|
elif hasattr(value, 'person_id'):
|
|
|
|
# The object is a person
|
2013-02-02 21:19:37 +01:00
|
|
|
return value.person_id
|
2013-04-24 09:28:58 +02:00
|
|
|
else:
|
|
|
|
OpenSlidesError('%s (%s) is no person' % (value, type(value)))
|
2012-08-04 13:18:06 +02:00
|
|
|
|
2012-08-04 14:47:09 +02:00
|
|
|
def formfield(self, **kwargs):
|
2012-08-07 22:43:57 +02:00
|
|
|
defaults = {'form_class': PersonFormField}
|
2012-08-04 14:47:09 +02:00
|
|
|
defaults.update(kwargs)
|
2012-08-07 22:43:57 +02:00
|
|
|
return super(PersonField, self).formfield(**defaults)
|
2012-08-04 13:18:06 +02:00
|
|
|
|
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
class PersonMixin(object):
|
2012-08-04 14:47:09 +02:00
|
|
|
@property
|
2012-08-07 22:43:57 +02:00
|
|
|
def person_id(self):
|
2012-08-04 14:47:09 +02:00
|
|
|
try:
|
2012-08-07 22:43:57 +02:00
|
|
|
return generate_person_id(self.person_prefix, self.pk)
|
2012-08-04 14:47:09 +02:00
|
|
|
except AttributeError:
|
2012-08-12 12:52:38 +02:00
|
|
|
raise AttributeError("%s has to have a attribute 'person_prefix'"
|
2012-08-04 14:47:09 +02:00
|
|
|
% self)
|
|
|
|
|
2013-03-14 22:18:56 +01:00
|
|
|
def __unicode__(self):
|
2013-08-03 08:58:16 +02:00
|
|
|
return self.person_id
|
2013-03-14 22:18:56 +01:00
|
|
|
|
|
|
|
def prepare_database_save(self, field):
|
|
|
|
if type(field) is PersonField:
|
|
|
|
return self.person_id
|
|
|
|
else:
|
|
|
|
return super(PersonMixin, self).prepare_database_save(field)
|