From 8c795c38dc26af3bf6643657d59950e0e3c56928 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 13 Mar 2020 10:29:27 +0100 Subject: [PATCH] Minor fixes on consumptions page, research is now faster --- apps/note/api/serializers.py | 4 ++++ apps/note/api/views.py | 8 ++++++-- apps/note/tables.py | 1 - apps/note/views.py | 4 ++-- static/js/base.js | 6 ++---- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/apps/note/api/serializers.py b/apps/note/api/serializers.py index b2d0ab66..2d85cc41 100644 --- a/apps/note/api/serializers.py +++ b/apps/note/api/serializers.py @@ -70,11 +70,15 @@ class AliasSerializer(serializers.ModelSerializer): REST API Serializer for Aliases. The djangorestframework plugin will analyse the model `Alias` and parse all fields in the API. """ + note = serializers.SerializerMethodField() class Meta: model = Alias fields = '__all__' + def get_note(self, alias): + return NotePolymorphicSerializer().to_representation(alias.note) + class NotePolymorphicSerializer(PolymorphicSerializer): model_serializer_mapping = { diff --git a/apps/note/api/views.py b/apps/note/api/views.py index 2745a034..29c79bd8 100644 --- a/apps/note/api/views.py +++ b/apps/note/api/views.py @@ -4,7 +4,7 @@ from django.db.models import Q from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets -from rest_framework.filters import SearchFilter +from rest_framework.filters import OrderingFilter, SearchFilter from .serializers import NoteSerializer, NotePolymorphicSerializer, NoteClubSerializer, NoteSpecialSerializer, \ NoteUserSerializer, AliasSerializer, \ @@ -61,6 +61,9 @@ class NotePolymorphicViewSet(viewsets.ModelViewSet): """ queryset = Note.objects.all() serializer_class = NotePolymorphicSerializer + filter_backends = [SearchFilter, OrderingFilter] + search_fields = ['$alias__normalized_name', '$alias__name', '$polymorphic_ctype__model', ] + ordering_fields = ['alias__name', 'alias__normalized_name'] def get_queryset(self): """ @@ -97,8 +100,9 @@ class AliasViewSet(viewsets.ModelViewSet): """ queryset = Alias.objects.all() serializer_class = AliasSerializer - filter_backends = [SearchFilter] + filter_backends = [SearchFilter, OrderingFilter] search_fields = ['$normalized_name', '$name', '$note__polymorphic_ctype__model', ] + ordering_fields = ['name', 'normalized_name'] def get_queryset(self): """ diff --git a/apps/note/tables.py b/apps/note/tables.py index cea2aa8e..496ae335 100644 --- a/apps/note/tables.py +++ b/apps/note/tables.py @@ -18,7 +18,6 @@ class HistoryTable(tables.Table): } model = Transaction exclude = ("id", "polymorphic_ctype", ) - order_by = ('-created_at', ) template_name = 'django_tables2/bootstrap4.html' sequence = ('...', 'total', 'valid') diff --git a/apps/note/views.py b/apps/note/views.py index 9b60a642..74da6497 100644 --- a/apps/note/views.py +++ b/apps/note/views.py @@ -110,12 +110,12 @@ class ConsoView(LoginRequiredMixin, SingleTableView): """ Consume """ - model = Transaction + queryset = Transaction.objects.order_by("-id").all()[:50] template_name = "note/conso_form.html" # Transaction history table table_class = HistoryTable - table_pagination = {"per_page": 20} + table_pagination = {"per_page": 50} def get_context_data(self, **kwargs): """ diff --git a/static/js/base.js b/static/js/base.js index bc757d96..a35a5dc3 100644 --- a/static/js/base.js +++ b/static/js/base.js @@ -44,11 +44,9 @@ function refreshBalance() { * This function is synchronous. */ function getMatchedNotes(pattern, fun) { - getJSONSync("/api/note/alias/?format=json&alias=" + pattern + "&search=user|club", function(aliases) { + getJSONSync("/api/note/alias/?format=json&alias=" + pattern + "&search=user|club&ordering=normalized_name", function(aliases) { aliases.results.forEach(function(alias) { - getJSONSync("/api/note/note/" + alias.note + "/?format=json", function (note) { - fun(note, alias); - }); + fun(alias, alias.note); }); }); }