1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-21 01:48:21 +02:00

Test and cover note app

This commit is contained in:
Yohann D'ANELLO
2020-09-01 15:54:56 +02:00
parent c6abad107a
commit 7c9287e387
7 changed files with 407 additions and 31 deletions

View File

@ -1,6 +1,7 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
@ -124,9 +125,9 @@ class ConsumerSerializer(serializers.ModelSerializer):
Display information about the associated note
"""
# If the user has no right to see the note, then we only display the note identifier
if PermissionBackend.check_perm(get_current_authenticated_user(), "note.view_note", obj.note):
return NotePolymorphicSerializer().to_representation(obj.note)
return dict(id=obj.note.id, name=str(obj.note))
return NotePolymorphicSerializer().to_representation(obj.note)\
if PermissionBackend.check_perm(get_current_authenticated_user(), "note.view_note", obj.note)\
else dict(id=obj.note.id, name=str(obj.note))
def get_email_confirmed(self, obj):
if isinstance(obj.note, NoteUser):
@ -231,12 +232,10 @@ class TransactionPolymorphicSerializer(PolymorphicSerializer):
SpecialTransaction: SpecialTransactionSerializer,
}
try:
if "activity" in settings.INSTALLED_APPS:
from activity.models import GuestTransaction
from activity.api.serializers import GuestTransactionSerializer
model_serializer_mapping[GuestTransaction] = GuestTransactionSerializer
except ImportError: # Activity app is not loaded
pass
def validate(self, attrs):
resource_type = attrs.pop(self.resource_type_field_name)

View File

@ -45,7 +45,7 @@ class NotePolymorphicViewSet(ReadProtectedModelViewSet):
| Q(alias__normalized_name__iregex="^" + alias.lower())
)
return queryset
return queryset.order_by("id")
class AliasViewSet(ReadProtectedModelViewSet):
@ -72,7 +72,6 @@ class AliasViewSet(ReadProtectedModelViewSet):
try:
self.perform_destroy(instance)
except ValidationError as e:
print(e)
return Response({e.code: e.message}, status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_204_NO_CONTENT)
@ -101,7 +100,7 @@ class AliasViewSet(ReadProtectedModelViewSet):
),
all=True)
return queryset
return queryset.order_by("name")
class ConsumerViewSet(ReadOnlyProtectedModelViewSet):
@ -120,7 +119,7 @@ class ConsumerViewSet(ReadOnlyProtectedModelViewSet):
queryset = super().get_queryset()
alias = self.request.query_params.get("alias", ".*")
queryset = queryset.order_by('name').prefetch_related('note')
queryset = queryset.prefetch_related('note')
# We match first an alias if it is matched without normalization,
# then if the normalized pattern matches a normalized alias.
queryset = queryset.filter(
@ -138,7 +137,7 @@ class ConsumerViewSet(ReadOnlyProtectedModelViewSet):
),
all=True)
return queryset.distinct()
return queryset.order_by('name').distinct()
class TemplateCategoryViewSet(ReadProtectedModelViewSet):
@ -147,7 +146,7 @@ class TemplateCategoryViewSet(ReadProtectedModelViewSet):
The djangorestframework plugin will get all `TemplateCategory` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/category/
"""
queryset = TemplateCategory.objects.all()
queryset = TemplateCategory.objects.order_by("name").all()
serializer_class = TemplateCategorySerializer
filter_backends = [SearchFilter]
search_fields = ['$name', ]
@ -159,7 +158,7 @@ class TransactionTemplateViewSet(viewsets.ModelViewSet):
The djangorestframework plugin will get all `TransactionTemplate` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/template/
"""
queryset = TransactionTemplate.objects.all()
queryset = TransactionTemplate.objects.order_by("name").all()
serializer_class = TransactionTemplateSerializer
filter_backends = [SearchFilter, DjangoFilterBackend]
filterset_fields = ['name', 'amount', 'display', 'category', ]
@ -172,7 +171,7 @@ class TransactionViewSet(ReadProtectedModelViewSet):
The djangorestframework plugin will get all `Transaction` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/transaction/
"""
queryset = Transaction.objects.all()
queryset = Transaction.objects.order_by("-created_at").all()
serializer_class = TransactionPolymorphicSerializer
filter_backends = [SearchFilter]
search_fields = ['$reason', ]