mirror of https://gitlab.crans.org/bde/nk20
Filter notes and aliases by aliases (regexp) or note type
This commit is contained in:
parent
f54e2ed145
commit
dc87a5d77b
|
@ -2,6 +2,7 @@
|
||||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
from django.db.models import Q
|
||||||
from note.models.notes import Note, NoteClub, NoteSpecial, NoteUser, Alias
|
from note.models.notes import Note, NoteClub, NoteSpecial, NoteUser, Alias
|
||||||
from note.models.transactions import TransactionTemplate, Transaction, MembershipTransaction
|
from note.models.transactions import TransactionTemplate, Transaction, MembershipTransaction
|
||||||
from .serializers import NoteSerializer, NotePolymorphicSerializer, NoteClubSerializer, NoteSpecialSerializer, NoteUserSerializer, AliasSerializer, \
|
from .serializers import NoteSerializer, NotePolymorphicSerializer, NoteClubSerializer, NoteSpecialSerializer, NoteUserSerializer, AliasSerializer, \
|
||||||
|
@ -52,12 +53,40 @@ class NoteUserViewSet(viewsets.ModelViewSet):
|
||||||
class NotePolymorphicViewSet(viewsets.ModelViewSet):
|
class NotePolymorphicViewSet(viewsets.ModelViewSet):
|
||||||
"""
|
"""
|
||||||
REST API View set.
|
REST API View set.
|
||||||
The djangorestframework plugin will get all `NoteUser` objects, serialize it to JSON with the given serializer,
|
The djangorestframework plugin will get all `Note` objects (with polymorhism), serialize it to JSON with the given serializer,
|
||||||
then render it on /api/note/user/
|
then render it on /api/note/note/
|
||||||
"""
|
"""
|
||||||
queryset = Note.objects.all()
|
queryset = Note.objects.all()
|
||||||
serializer_class = NotePolymorphicSerializer
|
serializer_class = NotePolymorphicSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""
|
||||||
|
Parse query and apply filters.
|
||||||
|
:return: The filtered set of requested notes
|
||||||
|
"""
|
||||||
|
queryset = Note.objects.all()
|
||||||
|
|
||||||
|
alias = self.request.query_params.get("alias", ".*")
|
||||||
|
queryset = queryset.filter(Q(alias__name__regex=alias) | Q(alias__normalized_name__regex=alias))
|
||||||
|
|
||||||
|
note_id = self.request.query_params.get("id", None)
|
||||||
|
if note_id:
|
||||||
|
queryset = queryset.filter(id=note_id)
|
||||||
|
|
||||||
|
note_type = self.request.query_params.get("type", None)
|
||||||
|
if note_type:
|
||||||
|
l = str(note_type).lower()
|
||||||
|
if "user" in l:
|
||||||
|
queryset = queryset.filter(polymorphic_ctype__model="noteuser")
|
||||||
|
elif "club" in l:
|
||||||
|
queryset = queryset.filter(polymorphic_ctype__model="noteclub")
|
||||||
|
elif "special" in l:
|
||||||
|
queryset = queryset.filter(polymorphic_ctype__model="notespecial")
|
||||||
|
else:
|
||||||
|
queryset = queryset.none()
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
class AliasViewSet(viewsets.ModelViewSet):
|
class AliasViewSet(viewsets.ModelViewSet):
|
||||||
"""
|
"""
|
||||||
|
@ -68,6 +97,35 @@ class AliasViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Alias.objects.all()
|
queryset = Alias.objects.all()
|
||||||
serializer_class = AliasSerializer
|
serializer_class = AliasSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""
|
||||||
|
Parse query and apply filters.
|
||||||
|
:return: The filtered set of requested aliases
|
||||||
|
"""
|
||||||
|
|
||||||
|
queryset = Alias.objects.all()
|
||||||
|
|
||||||
|
alias = self.request.query_params.get("alias", ".*")
|
||||||
|
queryset = queryset.filter(Q(name__regex=alias) | Q(normalized_name__regex=alias))
|
||||||
|
|
||||||
|
note_id = self.request.query_params.get("note", None)
|
||||||
|
if note_id:
|
||||||
|
queryset = queryset.filter(id=note_id)
|
||||||
|
|
||||||
|
note_type = self.request.query_params.get("type", None)
|
||||||
|
if note_type:
|
||||||
|
l = str(note_type).lower()
|
||||||
|
if "user" in l:
|
||||||
|
queryset = queryset.filter(note__polymorphic_ctype__model="noteuser")
|
||||||
|
elif "club" in l:
|
||||||
|
queryset = queryset.filter(note__polymorphic_ctype__model="noteclub")
|
||||||
|
elif "special" in l:
|
||||||
|
queryset = queryset.filter(note__polymorphic_ctype__model="notespecial")
|
||||||
|
else:
|
||||||
|
queryset = queryset.none()
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
class TransactionTemplateViewSet(viewsets.ModelViewSet):
|
class TransactionTemplateViewSet(viewsets.ModelViewSet):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -6,7 +6,6 @@ from django.conf.urls import url, include
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from rest_framework import routers, serializers, viewsets
|
from rest_framework import routers, serializers, viewsets
|
||||||
|
|
||||||
from note.models import Alias
|
|
||||||
from .activity.urls import register_activity_urls
|
from .activity.urls import register_activity_urls
|
||||||
from .members.urls import register_members_urls
|
from .members.urls import register_members_urls
|
||||||
from .note.urls import register_note_urls
|
from .note.urls import register_note_urls
|
||||||
|
@ -18,7 +17,7 @@ class UserSerializer(serializers.ModelSerializer):
|
||||||
"""
|
"""
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = '__all__'
|
exclude = ('password', 'groups', 'user_permissions',)
|
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
"""
|
"""
|
||||||
|
@ -34,17 +33,17 @@ router = routers.DefaultRouter()
|
||||||
router.register('user', UserViewSet)
|
router.register('user', UserViewSet)
|
||||||
|
|
||||||
# Routers for members app
|
# Routers for members app
|
||||||
register_members_urls(router, r'members')
|
register_members_urls(router, 'members')
|
||||||
|
|
||||||
# Routers for activity app
|
# Routers for activity app
|
||||||
register_activity_urls(router, r'activity')
|
register_activity_urls(router, 'activity')
|
||||||
|
|
||||||
# Routers for note app
|
# Routers for note app
|
||||||
register_note_urls(router, r'note')
|
register_note_urls(router, 'note')
|
||||||
|
|
||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browsable API.
|
# Additionally, we include login URLs for the browsable API.
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^', include(router.urls)),
|
url('^', include(router.urls)),
|
||||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
url('^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||||
]
|
]
|
Loading…
Reference in New Issue