8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-24 20:33:11 +00:00
re2o/users/api/serializers.py

169 lines
5.3 KiB
Python

# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2018 Maël Kervella
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Defines the serializers of the API
For app Users
"""
import datetime
from rest_framework import serializers
import users.models as users
from re2o.serializers import NamespacedHRField, NamespacedHIField, NamespacedHMSerializer
# USERS
class UserSerializer(NamespacedHMSerializer):
"""Serialize `users.models.User` objects.
"""
access = serializers.BooleanField(source='has_access')
uid = serializers.IntegerField(source='uid_number')
class Meta:
model = users.User
fields = ('surname', 'pseudo', 'email', 'local_email_redirect',
'local_email_enabled', 'school', 'shell', 'comment',
'state', 'registered', 'telephone', 'solde', 'access',
'end_access', 'uid', 'class_name', 'api_url')
extra_kwargs = {
'shell': {'view_name': 'shell-detail'}
}
class ClubSerializer(NamespacedHMSerializer):
"""Serialize `users.models.Club` objects.
"""
name = serializers.CharField(source='surname')
access = serializers.BooleanField(source='has_access')
uid = serializers.IntegerField(source='uid_number')
class Meta:
model = users.Club
fields = ('name', 'pseudo', 'email', 'local_email_redirect',
'local_email_enabled', 'school', 'shell', 'comment',
'state', 'registered', 'telephone', 'solde', 'room',
'access', 'end_access', 'administrators', 'members',
'mailing', 'uid', 'api_url')
extra_kwargs = {
'shell': {'view_name': 'shell-detail'}
}
class AdherentSerializer(NamespacedHMSerializer):
"""Serialize `users.models.Adherent` objects.
"""
access = serializers.BooleanField(source='has_access')
uid = serializers.IntegerField(source='uid_number')
class Meta:
model = users.Adherent
fields = ('name', 'surname', 'pseudo', 'email', 'local_email_redirect',
'local_email_enabled', 'school', 'shell', 'comment',
'state', 'registered', 'telephone', 'room', 'solde',
'access', 'end_access', 'uid', 'api_url', 'gid')
extra_kwargs = {
'shell': {'view_name': 'shell-detail'}
}
class BasicUserSerializer(NamespacedHMSerializer):
"""Serialize 'users.models.User' minimal infos"""
uid = serializers.IntegerField(source='uid_number')
gid = serializers.IntegerField(source='gid_number')
class Meta:
model = users.User
fields = ('pseudo', 'uid', 'gid')
class ServiceUserSerializer(NamespacedHMSerializer):
"""Serialize `users.models.ServiceUser` objects.
"""
class Meta:
model = users.ServiceUser
fields = ('pseudo', 'access_group', 'comment', 'api_url')
class SchoolSerializer(NamespacedHMSerializer):
"""Serialize `users.models.School` objects.
"""
class Meta:
model = users.School
fields = ('name', 'api_url')
class ListRightSerializer(NamespacedHMSerializer):
"""Serialize `users.models.ListRight` objects.
"""
class Meta:
model = users.ListRight
fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
class ShellSerializer(NamespacedHMSerializer):
"""Serialize `users.models.ListShell` objects.
"""
class Meta:
model = users.ListShell
fields = ('shell', 'api_url')
extra_kwargs = {
'api_url': {'view_name': 'shell-detail'}
}
class BanSerializer(NamespacedHMSerializer):
"""Serialize `users.models.Ban` objects.
"""
active = serializers.BooleanField(source='is_active')
class Meta:
model = users.Ban
fields = ('user', 'raison', 'date_start', 'date_end', 'state',
'active', 'api_url')
class WhitelistSerializer(NamespacedHMSerializer):
"""Serialize `users.models.Whitelist` objects.
"""
active = serializers.BooleanField(source='is_active')
class Meta:
model = users.Whitelist
fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
class EMailAddressSerializer(NamespacedHMSerializer):
"""Serialize `users.models.EMailAddress` objects.
"""
user = serializers.CharField(source='user.pseudo', read_only=True)
class Meta:
model = users.EMailAddress
fields = ('user', 'local_part', 'complete_email_address', 'api_url')