src/notes/api/filters.py
author ymh <ymh.work@gmail.com>
Fri, 30 Nov 2018 10:53:15 +0100
changeset 183 f8f3af9e5c83
parent 128 34a75bd8d0b9
permissions -rw-r--r--
Change the settings to avoid using Session authentication for rest framework as it raise exceptions in case client and backend are on the same domain On the filter, adapt to take into account new version of django_filters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
128
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
'''
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
Inspired by https://stackoverflow.com/a/36289332
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
'''
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from django_filters.rest_framework import (BaseInFilter, Filter, FilterSet,
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
                                           UUIDFilter)
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from ..models import Note, Session
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
class ExtIdFilter(BaseInFilter, UUIDFilter):
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
    pass
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
class CoreFilterSet(FilterSet):
183
f8f3af9e5c83 Change the settings to avoid using Session authentication for rest framework as it raise exceptions in case client and backend are on the same domain
ymh <ymh.work@gmail.com>
parents: 128
diff changeset
    13
    ext_id__in = ExtIdFilter(field_name='ext_id')
128
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class SessionFilterSet(CoreFilterSet):
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    class Meta:
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        model = Session
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        fields = ['ext_id__in']
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
class NoteFilterSet(CoreFilterSet):
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    class Meta:
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        model = Note
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        fields = ['ext_id__in']
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24