From d8949a993a30640ac8799a9eb9928f2b907ec6a8 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sat, 28 Mar 2020 17:42:29 +0100 Subject: [PATCH] get alias and note infos together --- apps/note/api/serializers.py | 14 ++++++++++++++ apps/note/api/urls.py | 5 +++-- apps/note/api/views.py | 27 +++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/apps/note/api/serializers.py b/apps/note/api/serializers.py index a51b4263..27ff6d65 100644 --- a/apps/note/api/serializers.py +++ b/apps/note/api/serializers.py @@ -92,6 +92,20 @@ class NotePolymorphicSerializer(PolymorphicSerializer): class Meta: model = Note +class ConsumerSerializer(serializers.ModelSerializer): + """ + REST API Nested Serializer for Consumers. + return Alias, and the note Associated to it in + """ + note = NotePolymorphicSerializer() + class Meta: + model = Alias + fields = '__all__' + + @staticmethod + def setup_eager_loading(queryset): + queryset = queryset.select_related('note') + class TemplateCategorySerializer(serializers.ModelSerializer): """ diff --git a/apps/note/api/urls.py b/apps/note/api/urls.py index 796a397f..57909080 100644 --- a/apps/note/api/urls.py +++ b/apps/note/api/urls.py @@ -1,7 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from .views import NotePolymorphicViewSet, AliasViewSet, \ +from .views import NotePolymorphicViewSet, AliasViewSet, ConsumerViewSet, \ TemplateCategoryViewSet, TransactionViewSet, TransactionTemplateViewSet @@ -11,7 +11,8 @@ def register_note_urls(router, path): """ router.register(path + '/note', NotePolymorphicViewSet) router.register(path + '/alias', AliasViewSet) - + router.register(path + '/consumer', ConsumerViewSet) + router.register(path + '/transaction/category', TemplateCategoryViewSet) router.register(path + '/transaction/transaction', TransactionViewSet) router.register(path + '/transaction/template', TransactionTemplateViewSet) diff --git a/apps/note/api/views.py b/apps/note/api/views.py index f230a646..af02dc8b 100644 --- a/apps/note/api/views.py +++ b/apps/note/api/views.py @@ -6,8 +6,8 @@ from django_filters.rest_framework import DjangoFilterBackend from rest_framework.filters import OrderingFilter, SearchFilter from api.viewsets import ReadProtectedModelViewSet, ReadOnlyProtectedModelViewSet -from .serializers import NotePolymorphicSerializer, AliasSerializer, TemplateCategorySerializer, \ - TransactionTemplateSerializer, TransactionPolymorphicSerializer +from .serializers import NotePolymorphicSerializer, AliasSerializer, ConsumerSerializer,\ + TemplateCategorySerializer, TransactionTemplateSerializer, TransactionPolymorphicSerializer from ..models.notes import Note, Alias from ..models.transactions import TransactionTemplate, Transaction, TemplateCategory @@ -68,6 +68,29 @@ class AliasViewSet(ReadProtectedModelViewSet): return queryset +class ConsumerViewSet(ReadOnlyProtectedModelViewSet): + queryset = Alias.objects.all() + serializer_class = ConsumerSerializer + filter_backends = [SearchFilter, OrderingFilter] + search_fields = ['$normalized_name', '$name', '$note__polymorphic_ctype__model', ] + ordering_fields = ['name', 'normalized_name'] + + def get_queryset(self): + """ + Parse query and apply filters. + :return: The filtered set of requested aliases + """ + + queryset = super().get_queryset() + + alias = self.request.query_params.get("alias", ".*") + queryset = queryset.filter( + Q(name__regex="^" + alias) + | Q(normalized_name__regex="^" + Alias.normalize(alias)) + | Q(normalized_name__regex="^" + alias.lower())) + + return queryset + class TemplateCategoryViewSet(ReadProtectedModelViewSet): """