OpenSlides/openslides/users/serializers.py

71 lines
1.6 KiB
Python
Raw Normal View History

from openslides.utils.rest_api import ModelSerializer, RelatedField
2015-01-06 00:11:22 +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
class UserShortSerializer(ModelSerializer):
2015-01-06 00:11:22 +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 = (
'id',
2015-01-06 00:11:22 +01:00
'username',
'title',
2015-01-06 00:11:22 +01:00
'first_name',
'last_name',
'structure_level',
'groups',)
class UserFullSerializer(ModelSerializer):
"""
Serializer for users.models.User objects.
Serializes all relevant fields.
"""
class Meta:
model = User
fields = (
'id',
'is_present',
'username',
'title',
'first_name',
'last_name',
'structure_level',
'about_me',
'comment',
'groups',
'default_password',
'last_login',
'is_active',)
class PermissionRelatedField(RelatedField):
"""
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,))
class GroupSerializer(ModelSerializer):
"""
Serializer for django.contrib.auth.models.Group objects.
"""
permissions = PermissionRelatedField(many=True, read_only=True)
class Meta:
model = Group
fields = (
'id',
'name',
'permissions',)