2015-02-12 18:48:14 +01:00
|
|
|
from openslides.utils.rest_api import ModelSerializer, RelatedField
|
2015-01-06 00:11:22 +01:00
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
from .models import Group, User # TODO: Don't import Group from models but from core.models.
|
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 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',)
|