nk20/apps/note/api/serializers.py

280 lines
9.1 KiB
Python
Raw Permalink Normal View History

Update 131 files - /apps/activity/api/serializers.py - /apps/activity/api/urls.py - /apps/activity/api/views.py - /apps/activity/tests/test_activities.py - /apps/activity/__init__.py - /apps/activity/admin.py - /apps/activity/apps.py - /apps/activity/forms.py - /apps/activity/tables.py - /apps/activity/urls.py - /apps/activity/views.py - /apps/api/__init__.py - /apps/api/apps.py - /apps/api/serializers.py - /apps/api/tests.py - /apps/api/urls.py - /apps/api/views.py - /apps/api/viewsets.py - /apps/logs/signals.py - /apps/logs/apps.py - /apps/logs/__init__.py - /apps/logs/api/serializers.py - /apps/logs/api/urls.py - /apps/logs/api/views.py - /apps/member/api/serializers.py - /apps/member/api/urls.py - /apps/member/api/views.py - /apps/member/templatetags/memberinfo.py - /apps/member/__init__.py - /apps/member/admin.py - /apps/member/apps.py - /apps/member/auth.py - /apps/member/forms.py - /apps/member/hashers.py - /apps/member/signals.py - /apps/member/tables.py - /apps/member/urls.py - /apps/member/views.py - /apps/note/api/serializers.py - /apps/note/api/urls.py - /apps/note/api/views.py - /apps/note/models/__init__.py - /apps/note/static/note/js/consos.js - /apps/note/templates/note/mails/negative_balance.txt - /apps/note/templatetags/getenv.py - /apps/note/templatetags/pretty_money.py - /apps/note/tests/test_transactions.py - /apps/note/__init__.py - /apps/note/admin.py - /apps/note/apps.py - /apps/note/forms.py - /apps/note/signals.py - /apps/note/tables.py - /apps/note/urls.py - /apps/note/views.py - /apps/permission/api/serializers.py - /apps/permission/api/urls.py - /apps/permission/api/views.py - /apps/permission/templatetags/perms.py - /apps/permission/tests/test_oauth2.py - /apps/permission/tests/test_permission_denied.py - /apps/permission/tests/test_permission_queries.py - /apps/permission/tests/test_rights_page.py - /apps/permission/__init__.py - /apps/permission/admin.py - /apps/permission/backends.py - /apps/permission/apps.py - /apps/permission/decorators.py - /apps/permission/permissions.py - /apps/permission/scopes.py - /apps/permission/signals.py - /apps/permission/tables.py - /apps/permission/urls.py - /apps/permission/views.py - /apps/registration/tests/test_registration.py - /apps/registration/__init__.py - /apps/registration/apps.py - /apps/registration/forms.py - /apps/registration/tables.py - /apps/registration/tokens.py - /apps/registration/urls.py - /apps/registration/views.py - /apps/treasury/api/serializers.py - /apps/treasury/api/urls.py - /apps/treasury/api/views.py - /apps/treasury/templatetags/escape_tex.py - /apps/treasury/tests/test_treasury.py - /apps/treasury/__init__.py - /apps/treasury/admin.py - /apps/treasury/apps.py - /apps/treasury/forms.py - /apps/treasury/signals.py - /apps/treasury/tables.py - /apps/treasury/urls.py - /apps/treasury/views.py - /apps/wei/api/serializers.py - /apps/wei/api/urls.py - /apps/wei/api/views.py - /apps/wei/forms/surveys/__init__.py - /apps/wei/forms/surveys/base.py - /apps/wei/forms/surveys/wei2021.py - /apps/wei/forms/surveys/wei2022.py - /apps/wei/forms/surveys/wei2023.py - /apps/wei/forms/__init__.py - /apps/wei/forms/registration.py - /apps/wei/management/commands/export_wei_registrations.py - /apps/wei/management/commands/import_scores.py - /apps/wei/management/commands/wei_algorithm.py - /apps/wei/templates/wei/weilist_sample.tex - /apps/wei/tests/test_wei_algorithm_2021.py - /apps/wei/tests/test_wei_algorithm_2022.py - /apps/wei/tests/test_wei_algorithm_2023.py - /apps/wei/tests/test_wei_registration.py - /apps/wei/__init__.py - /apps/wei/admin.py - /apps/wei/apps.py - /apps/wei/tables.py - /apps/wei/urls.py - /apps/wei/views.py - /note_kfet/settings/__init__.py - /note_kfet/settings/base.py - /note_kfet/settings/development.py - /note_kfet/settings/secrets_example.py - /note_kfet/static/js/base.js - /note_kfet/admin.py - /note_kfet/inputs.py - /note_kfet/middlewares.py - /note_kfet/urls.py - /note_kfet/views.py - /note_kfet/wsgi.py - /entrypoint.sh
2024-02-07 01:26:49 +00:00
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
2020-02-06 23:12:00 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
2020-08-06 10:50:24 +00:00
2020-09-01 13:54:56 +00:00
from django.conf import settings
2020-08-31 18:15:48 +00:00
from django.utils.translation import gettext_lazy as _
2020-02-07 16:02:07 +00:00
from rest_framework import serializers
2020-08-31 18:15:48 +00:00
from rest_framework.exceptions import ValidationError
2020-02-07 19:47:49 +00:00
from rest_polymorphic.serializers import PolymorphicSerializer
from member.api.serializers import MembershipSerializer
from member.models import Membership
from note_kfet.middlewares import get_current_request
from permission.backends import PermissionBackend
from rest_framework.utils import model_meta
from rest_framework.validators import UniqueTogetherValidator
2020-02-07 16:02:07 +00:00
from ..models.notes import Note, NoteClub, NoteSpecial, NoteUser, Alias, Trust
2020-03-11 10:15:03 +00:00
from ..models.transactions import TransactionTemplate, Transaction, MembershipTransaction, TemplateCategory, \
RecurrentTransaction, SpecialTransaction
2020-02-18 20:14:29 +00:00
2020-02-06 23:12:00 +00:00
2020-02-07 19:47:49 +00:00
class NoteSerializer(serializers.ModelSerializer):
2020-02-06 23:29:04 +00:00
"""
REST API Serializer for Notes.
The djangorestframework plugin will analyse the model `Note` and parse all fields in the API.
"""
2020-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = Note
2020-02-07 19:47:49 +00:00
fields = '__all__'
2020-08-31 18:15:48 +00:00
read_only_fields = ('balance', 'last_negative', 'created_at', ) # Note balances are read-only protected
2020-02-06 23:12:00 +00:00
2020-02-07 19:47:49 +00:00
class NoteClubSerializer(serializers.ModelSerializer):
2020-02-06 23:29:04 +00:00
"""
REST API Serializer for Club's notes.
The djangorestframework plugin will analyse the model `NoteClub` and parse all fields in the API.
"""
2020-03-12 10:12:21 +00:00
name = serializers.SerializerMethodField()
2020-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = NoteClub
2020-02-07 19:47:49 +00:00
fields = '__all__'
2020-08-31 18:15:48 +00:00
read_only_fields = ('note', 'club', 'balance', 'last_negative', 'created_at', )
2020-02-06 23:12:00 +00:00
2020-03-12 10:12:21 +00:00
def get_name(self, obj):
return str(obj)
2020-02-06 23:12:00 +00:00
2020-02-07 19:47:49 +00:00
class NoteSpecialSerializer(serializers.ModelSerializer):
2020-02-06 23:29:04 +00:00
"""
REST API Serializer for special notes.
The djangorestframework plugin will analyse the model `NoteSpecial` and parse all fields in the API.
"""
2020-03-12 10:12:21 +00:00
name = serializers.SerializerMethodField()
2020-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = NoteSpecial
2020-02-07 19:47:49 +00:00
fields = '__all__'
2020-08-31 18:15:48 +00:00
read_only_fields = ('note', 'balance', 'last_negative', 'created_at', )
2020-02-06 23:12:00 +00:00
2020-03-12 10:12:21 +00:00
def get_name(self, obj):
return str(obj)
2020-02-06 23:12:00 +00:00
2020-02-07 19:47:49 +00:00
class NoteUserSerializer(serializers.ModelSerializer):
2020-02-06 23:29:04 +00:00
"""
REST API Serializer for User's notes.
The djangorestframework plugin will analyse the model `NoteUser` and parse all fields in the API.
"""
2020-03-12 10:12:21 +00:00
name = serializers.SerializerMethodField()
2020-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = NoteUser
2020-02-07 19:47:49 +00:00
fields = '__all__'
2020-08-31 18:15:48 +00:00
read_only_fields = ('note', 'user', 'balance', 'last_negative', 'created_at', )
2020-02-06 23:12:00 +00:00
2020-03-12 10:12:21 +00:00
def get_name(self, obj):
return str(obj)
class TrustSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Trusts.
The djangorestframework plugin will analyse the model `Trust` and parse all fields in the API.
"""
class Meta:
model = Trust
fields = '__all__'
validators = [UniqueTogetherValidator(
queryset=Trust.objects.all(), fields=('trusting', 'trusted'),
message=_("This friendship already exists"))]
2020-02-08 14:08:55 +00:00
class AliasSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Aliases.
The djangorestframework plugin will analyse the model `Alias` and parse all fields in the API.
"""
2020-03-07 21:28:59 +00:00
2020-02-08 14:08:55 +00:00
class Meta:
model = Alias
fields = '__all__'
2020-03-26 16:45:24 +00:00
def validate(self, attrs):
instance = Alias(**attrs)
instance.clean()
return attrs
2020-02-08 14:08:55 +00:00
2020-02-07 19:47:49 +00:00
class NotePolymorphicSerializer(PolymorphicSerializer):
model_serializer_mapping = {
Note: NoteSerializer,
NoteUser: NoteUserSerializer,
NoteClub: NoteClubSerializer,
2020-03-30 23:03:30 +00:00
NoteSpecial: NoteSpecialSerializer,
2020-02-07 19:47:49 +00:00
}
2020-02-06 23:12:00 +00:00
class Meta:
model = Note
2020-03-28 16:42:29 +00:00
class ConsumerSerializer(serializers.ModelSerializer):
"""
REST API Nested Serializer for Consumers.
return Alias, and the note Associated to it in
2020-03-28 16:42:29 +00:00
"""
note = serializers.SerializerMethodField()
email_confirmed = serializers.SerializerMethodField()
membership = serializers.SerializerMethodField()
2020-03-28 16:42:29 +00:00
class Meta:
model = Alias
fields = '__all__'
def get_note(self, obj):
2020-04-09 22:09:10 +00:00
"""
Display information about the associated note
"""
# If the user has no right to see the note, then we only display the note identifier
2020-09-01 13:54:56 +00:00
return NotePolymorphicSerializer().to_representation(obj.note)\
if PermissionBackend.check_perm(get_current_request(), "note.view_note", obj.note)\
else dict(
id=obj.note.id,
name=str(obj.note),
is_active=obj.note.is_active,
display_image=obj.note.display_image.url,
)
def get_email_confirmed(self, obj):
if isinstance(obj.note, NoteUser):
return obj.note.user.profile.email_confirmed
return True
def get_membership(self, obj):
if isinstance(obj.note, NoteUser):
memberships = Membership.objects.filter(
PermissionBackend.filter_queryset(get_current_request(), Membership, "view")).filter(
user=obj.note.user,
club=2, # Kfet
).order_by("-date_start")
if memberships.exists():
return MembershipSerializer().to_representation(memberships.first())
return None
2020-02-18 11:31:15 +00:00
2020-03-11 00:03:15 +00:00
class TemplateCategorySerializer(serializers.ModelSerializer):
"""
REST API Serializer for Transaction templates.
The djangorestframework plugin will analyse the model `TemplateCategory` and parse all fields in the API.
"""
class Meta:
model = TemplateCategory
fields = '__all__'
2020-02-07 19:47:49 +00:00
class TransactionTemplateSerializer(serializers.ModelSerializer):
2020-02-06 23:29:04 +00:00
"""
REST API Serializer for Transaction templates.
The djangorestframework plugin will analyse the model `TransactionTemplate` and parse all fields in the API.
"""
2020-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = TransactionTemplate
fields = '__all__'
2020-02-07 19:47:49 +00:00
class TransactionSerializer(serializers.ModelSerializer):
2020-02-06 23:29:04 +00:00
"""
REST API Serializer for Transactions.
The djangorestframework plugin will analyse the model `Transaction` and parse all fields in the API.
"""
2020-08-31 18:15:48 +00:00
def validate_source(self, value):
if not value.is_active:
2020-08-31 18:15:48 +00:00
raise ValidationError(_("The transaction can't be saved since the source note "
"or the destination note is not active."))
return value
2020-08-31 18:15:48 +00:00
def validate_destination(self, value):
if not value.is_active:
2020-08-31 18:15:48 +00:00
raise ValidationError(_("The transaction can't be saved since the source note "
"or the destination note is not active."))
return value
2020-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = Transaction
fields = '__all__'
2020-08-31 18:15:48 +00:00
class RecurrentTransactionSerializer(TransactionSerializer):
2020-03-11 10:15:03 +00:00
"""
REST API Serializer for Transactions.
The djangorestframework plugin will analyse the model `RecurrentTransaction` and parse all fields in the API.
2020-03-11 10:15:03 +00:00
"""
class Meta:
model = RecurrentTransaction
2020-03-11 10:15:03 +00:00
fields = '__all__'
2020-08-31 18:15:48 +00:00
class MembershipTransactionSerializer(TransactionSerializer):
2020-02-06 23:29:04 +00:00
"""
REST API Serializer for Membership transactions.
The djangorestframework plugin will analyse the model `MembershipTransaction` and parse all fields in the API.
"""
2020-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = MembershipTransaction
fields = '__all__'
2020-03-11 10:15:03 +00:00
2020-08-31 18:15:48 +00:00
class SpecialTransactionSerializer(TransactionSerializer):
2020-03-14 14:13:58 +00:00
"""
REST API Serializer for Special transactions.
The djangorestframework plugin will analyse the model `SpecialTransaction` and parse all fields in the API.
"""
class Meta:
model = SpecialTransaction
fields = '__all__'
# noinspection PyUnresolvedReferences
2020-03-11 10:15:03 +00:00
class TransactionPolymorphicSerializer(PolymorphicSerializer):
model_serializer_mapping = {
Transaction: TransactionSerializer,
RecurrentTransaction: RecurrentTransactionSerializer,
2020-03-11 10:15:03 +00:00
MembershipTransaction: MembershipTransactionSerializer,
2020-03-14 14:13:58 +00:00
SpecialTransaction: SpecialTransactionSerializer,
2020-03-11 10:15:03 +00:00
}
2020-09-01 13:54:56 +00:00
if "activity" in settings.INSTALLED_APPS:
from activity.models import GuestTransaction
from activity.api.serializers import GuestTransactionSerializer
model_serializer_mapping[GuestTransaction] = GuestTransactionSerializer
def validate(self, attrs):
resource_type = attrs.pop(self.resource_type_field_name)
serializer = self._get_serializer_from_resource_type(resource_type)
if self.instance:
instance = self.instance
info = model_meta.get_field_info(instance)
for attr, value in attrs.items():
if attr in info.relations and info.relations[attr].to_many:
field = getattr(instance, attr)
field.set(value)
else:
setattr(instance, attr, value)
2020-08-09 10:31:06 +00:00
instance.validate()
else:
2020-08-09 10:31:06 +00:00
serializer.Meta.model(**attrs).validate()
attrs[self.resource_type_field_name] = resource_type
return super().validate(attrs)
class Meta:
model = Transaction