some pep8 changes for the user-api

This commit is contained in:
Oskar Hahn 2012-08-04 14:47:09 +02:00
parent 72e41fe286
commit 465497ab30
5 changed files with 52 additions and 125 deletions

View File

@ -13,7 +13,7 @@
from openslides.utils.user.signals import receiv_users from openslides.utils.user.signals import receiv_users
from openslides.utils.user.api import generate_uid, split_uid, get_user, Users from openslides.utils.user.api import generate_uid, split_uid, get_user, Users
from openslides.utils.user.forms import UserFormField, MultipleUserFormField from openslides.utils.user.forms import UserFormField, MultipleUserFormField
from openslides.utils.user.model import UserField, UserMixin from openslides.utils.user.models import UserField, UserMixin
class EmtyUser(UserMixin): class EmtyUser(UserMixin):

View File

@ -12,6 +12,7 @@
from openslides.utils.user.signals import receiv_users from openslides.utils.user.signals import receiv_users
class Users(object): class Users(object):
""" """
A Storage for a multiplicity of different User-Objects. A Storage for a multiplicity of different User-Objects.
@ -48,6 +49,6 @@ def get_user(uid):
try: try:
user_prefix, id = split_uid(uid) user_prefix, id = split_uid(uid)
except TypeError: except TypeError:
from openslides.utils.user import EmtyUser
return EmtyUser() return EmtyUser()
return Users(user_prefix=user_prefix, id=id)[0] return Users(user_prefix=user_prefix, id=id)[0]

View File

@ -14,6 +14,7 @@ from django import forms
from openslides.utils.user.api import Users, get_user from openslides.utils.user.api import Users, get_user
class UserChoices(object): class UserChoices(object):
def __init__(self, field): def __init__(self, field):
self.field = field self.field = field
@ -40,7 +41,6 @@ class UserFormField(forms.fields.ChoiceField):
result = super(forms.fields.ChoiceField, self).__deepcopy__(memo) result = super(forms.fields.ChoiceField, self).__deepcopy__(memo)
return result return result
def _get_choices(self): def _get_choices(self):
# If self._choices is set, then somebody must have manually set # If self._choices is set, then somebody must have manually set
# the property self.choices. In this case, just return self._choices. # the property self.choices. In this case, just return self._choices.

View File

@ -1,65 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.utils.user.models
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Models and ModelFields for the OpenSlides user api.
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from django.db import models
from openslides.utils.user.forms import UserFormField
from openslides.utils.user.api import get_user, generate_uid
class UserField(models.fields.Field):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
super(UserField, self).__init__(max_length=255, *args, **kwargs)
# TODO: Validate the uid
def get_internal_type(self):
return "CharField"
def to_python(self, value):
"""
Convert string value to a User Object.
"""
if hasattr(value, 'uid'):
user = value
else:
user = get_user(value)
user.prepare_database_save = lambda unused: UserField() \
.get_prep_value(user)
return user
def get_prep_value(self, value):
return value.uid
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)
def formfield(self, **kwargs):
defaults = {'form_class': UserFormField}
defaults.update(kwargs)
return super(UserField, self).formfield(**defaults)
class UserMixin(object):
@property
def uid(self):
try:
return generate_uid(self.user_prefix, self.pk)
except AttributeError:
raise AttributeError("%s has to have a attribute 'user_prefix'"
% self)
def __repr__(self):
return 'User: %s' % self.uid

View File

@ -1,73 +1,64 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
openslides.utils.user.forms openslides.utils.user.models
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Forms and FormFields for the OpenSlides user api Models and ModelFields for the OpenSlides user api.
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS. :copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details. :license: GNU GPL, see LICENSE for more details.
""" """
from django.db import models
class UserChoices(object): from openslides.utils.user.forms import UserFormField
def __init__(self, field): from openslides.utils.user.api import get_user, generate_uid
self.field = field
def __iter__(self):
if self.field.empty_label is not None:
yield (u"", self.field.empty_label)
for user in Users():
yield (user.uid, user)
class UserFormField(forms.fields.ChoiceField): class UserField(models.fields.Field):
def __init__(self, required=True, initial=None, empty_label=u"---------", __metaclass__ = models.SubfieldBase
*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
return UserChoices(self)
choices = property(_get_choices, forms.fields.ChoiceField._set_choices)
def to_python(self, value):
return get_user(value)
def valid_value(self, value):
return super(UserFormField, self).valid_value(value.uid)
class MultipleUserFormField(UserFormField):
widget = forms.widgets.SelectMultiple
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(MultipleUserFormField, self).__init__(empty_label=None, super(UserField, self).__init__(max_length=255, *args, **kwargs)
*args, **kwargs) # TODO: Validate the uid
def get_internal_type(self):
return "CharField"
def to_python(self, value): def to_python(self, value):
if hasattr(value, '__iter__'): """
return [super(MultipleUserFormField, self).to_python(v) Convert string value to a User Object.
for v in value] """
return super(MultipleUserFormField, self).to_python(value) if hasattr(value, 'uid'):
user = value
else:
user = get_user(value)
def valid_value(self, value): user.prepare_database_save = (
if hasattr(value, '__iter__'): lambda unused: UserField().get_prep_value(user))
return [super(MultipleUserFormField, self).valid_value(v) return user
for v in value]
return super(MultipleUserFormField, self).valid_value(value) def get_prep_value(self, value):
return value.uid
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)
def formfield(self, **kwargs):
defaults = {'form_class': UserFormField}
defaults.update(kwargs)
return super(UserField, self).formfield(**defaults)
class UserMixin(object):
@property
def uid(self):
try:
return generate_uid(self.user_prefix, self.pk)
except AttributeError:
raise AttributeError("%s has to have a attribute 'user_prefix'"
% self)
def __repr__(self):
return 'User: %s' % self.uid