2015-02-12 20:57:05 +01:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.contrib.auth.hashers import make_password
|
|
|
|
from django.utils.translation import ugettext as _, ugettext_lazy
|
2015-01-06 00:11:22 +01:00
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
from openslides.utils.rest_api import ModelSerializer, PrimaryKeyRelatedField, RelatedField, ValidationError
|
|
|
|
|
|
|
|
from .models import Group, User
|
2015-01-06 00:11:22 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class UserShortSerializer(ModelSerializer):
|
2015-01-06 00:11:22 +01:00
|
|
|
"""
|
2015-01-17 14:25:05 +01:00
|
|
|
Serializer for users.models.User objects.
|
|
|
|
|
|
|
|
Serializes only name fields.
|
2015-01-06 00:11:22 +01:00
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = (
|
2015-02-04 00:08:38 +01:00
|
|
|
'id',
|
2015-01-06 00:11:22 +01:00
|
|
|
'username',
|
2015-01-17 14:25:05 +01:00
|
|
|
'title',
|
2015-01-06 00:11:22 +01:00
|
|
|
'first_name',
|
2015-01-17 14:25:05 +01:00
|
|
|
'last_name',
|
2015-02-04 00:08:38 +01:00
|
|
|
'structure_level',
|
|
|
|
'groups',)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class UserFullSerializer(ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2015-01-17 14:25:05 +01:00
|
|
|
Serializer for users.models.User objects.
|
|
|
|
|
|
|
|
Serializes all relevant fields.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = (
|
2015-02-04 00:08:38 +01:00
|
|
|
'id',
|
2015-01-17 14:25:05 +01:00
|
|
|
'is_present',
|
|
|
|
'username',
|
|
|
|
'title',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'structure_level',
|
|
|
|
'about_me',
|
|
|
|
'comment',
|
2015-02-04 00:08:38 +01:00
|
|
|
'groups',
|
2015-01-17 14:25:05 +01:00
|
|
|
'default_password',
|
2015-02-04 00:08:38 +01:00
|
|
|
'last_login',
|
|
|
|
'is_active',)
|
|
|
|
|
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
class UserCreateUpdateSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for users.models.User objects.
|
|
|
|
|
|
|
|
Serializes data to create new users or update users.
|
|
|
|
|
|
|
|
Do not use this for list or retrieve requests.
|
|
|
|
"""
|
|
|
|
groups = PrimaryKeyRelatedField(
|
|
|
|
many=True,
|
|
|
|
queryset=Group.objects.exclude(pk__in=(1, 2)),
|
|
|
|
help_text=ugettext_lazy('The groups this user belongs to. A user will '
|
|
|
|
'get all permissions granted to each of '
|
|
|
|
'his/her groups.'))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = (
|
|
|
|
'is_present',
|
|
|
|
'username',
|
|
|
|
'title',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'structure_level',
|
|
|
|
'about_me',
|
|
|
|
'comment',
|
|
|
|
'groups',
|
|
|
|
'default_password',
|
|
|
|
'is_active',)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Overridden to add read_only flag to username field in create requests.
|
|
|
|
"""
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if self.context['view'].action == 'create':
|
|
|
|
self.fields['username'].read_only = True
|
|
|
|
elif self.context['view'].action == 'update':
|
|
|
|
# Everything is fine. Do nothing.
|
|
|
|
pass
|
|
|
|
else: # Other action than 'create' or 'update'.
|
|
|
|
raise ImproperlyConfigured('This serializer can only be used in create and update requests.')
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
"""
|
|
|
|
Checks that first_name or last_name is given.
|
|
|
|
"""
|
|
|
|
if not (data.get('username') or data.get('first_name') or data.get('last_name')):
|
|
|
|
raise ValidationError(_('Username, first name and last name can not all be empty.'))
|
|
|
|
return data
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
"""
|
|
|
|
Creates user with generated username and sets the default_password.
|
|
|
|
Adds the new user to the registered group.
|
|
|
|
"""
|
|
|
|
# Generate username if neccessary.
|
|
|
|
if not validated_data.get('username'):
|
|
|
|
validated_data['username'] = User.objects.generate_username(
|
|
|
|
validated_data.get('first_name', ''),
|
|
|
|
validated_data.get('last_name', ''))
|
|
|
|
# Prepare setup password.
|
|
|
|
if not validated_data.get('default_password'):
|
|
|
|
validated_data['default_password'] = User.objects.generate_password()
|
|
|
|
validated_data['password'] = make_password(validated_data['default_password'], '', 'md5')
|
|
|
|
# Perform creation in the database and return new user.
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class PermissionRelatedField(RelatedField):
|
2015-02-04 00:08:38 +01:00
|
|
|
"""
|
|
|
|
A custom field to use for the permission relationship.
|
|
|
|
"""
|
|
|
|
def to_representation(self, value):
|
|
|
|
"""
|
|
|
|
Returns the permission name (app_label.codename).
|
|
|
|
"""
|
|
|
|
return '.'.join((value.content_type.app_label, value.codename,))
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class GroupSerializer(ModelSerializer):
|
2015-02-04 00:08:38 +01:00
|
|
|
"""
|
|
|
|
Serializer for django.contrib.auth.models.Group objects.
|
|
|
|
"""
|
|
|
|
permissions = PermissionRelatedField(many=True, read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Group
|
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'permissions',)
|