Handel null=True in PersonField

This commit is contained in:
Oskar Hahn 2013-02-02 21:19:37 +01:00
parent 2e430b4406
commit 15b13c2bcd
2 changed files with 17 additions and 17 deletions

View File

@ -10,7 +10,7 @@
:license: GNU GPL, see LICENSE for more details. :license: GNU GPL, see LICENSE for more details.
""" """
from openslides.utils.person.signals import receive_persons from .signals import receive_persons
class Person(object): class Person(object):

View File

@ -11,16 +11,16 @@
""" """
from django.db import models from django.db import models
from openslides.utils.person.forms import PersonFormField from .forms import PersonFormField
from openslides.utils.person.api import get_person, generate_person_id from .api import get_person, generate_person_id, Person
class PersonField(models.fields.Field): class PersonField(models.fields.Field):
__metaclass__ = models.SubfieldBase __metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(PersonField, self).__init__(max_length=255, *args, **kwargs) kwargs['max_length'] = 255
# TODO: Validate the uid super(PersonField, self).__init__(*args, **kwargs)
def get_internal_type(self): def get_internal_type(self):
return "CharField" return "CharField"
@ -29,21 +29,21 @@ class PersonField(models.fields.Field):
""" """
Convert string value to a User Object. Convert string value to a User Object.
""" """
if hasattr(value, 'person_id'): if isinstance(value, Person):
person = value return value
elif value is None:
return None
else: else:
person = get_person(value) return get_person(value)
person.prepare_database_save = (
lambda unused: PersonField().get_prep_value(person))
return person
def get_prep_value(self, value): def get_prep_value(self, value):
return value.person_id """
Convert a person object to a string, to store it in the database.
def value_to_string(self, obj): """
value = self._get_val_from_obj(obj) if value is None:
return self.get_prep_value(value) return None
else:
return value.person_id
def formfield(self, **kwargs): def formfield(self, **kwargs):
defaults = {'form_class': PersonFormField} defaults = {'form_class': PersonFormField}