Skip to content

views

ListFilter

Bases: django_filters.Filter

Source code in tasks/views.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class ListFilter(django_filters.Filter):
    def __init__(self, filter_value=lambda x: x, **kwargs):
        super(ListFilter, self).__init__(**kwargs)
        self.filter_value_fn = filter_value

    def sanitize(self, value_list):
        return [v for v in value_list if v != u'']

    def filter(self, qs, value):
        if value:
            values = value.split(u",")
            values = self.sanitize(values)
            values = map(self.filter_value_fn, values)
            f = Q()
            for v in values:
                kwargs = {self.field_name: v}
                f = f | Q(**kwargs)
            return qs.filter(f)
        else:
            return qs

filter_value_fn = filter_value instance-attribute

__init__(filter_value=lambda x: x, **kwargs)

Source code in tasks/views.py
13
14
15
def __init__(self, filter_value=lambda x: x, **kwargs):
    super(ListFilter, self).__init__(**kwargs)
    self.filter_value_fn = filter_value

filter(qs, value)

Source code in tasks/views.py
20
21
22
23
24
25
26
27
28
29
30
31
def filter(self, qs, value):
    if value:
        values = value.split(u",")
        values = self.sanitize(values)
        values = map(self.filter_value_fn, values)
        f = Q()
        for v in values:
            kwargs = {self.field_name: v}
            f = f | Q(**kwargs)
        return qs.filter(f)
    else:
        return qs

sanitize(value_list)

Source code in tasks/views.py
17
18
def sanitize(self, value_list):
    return [v for v in value_list if v != u'']

TaskFilter

Bases: django_filters.FilterSet

Source code in tasks/views.py
34
35
36
37
38
39
class TaskFilter(django_filters.FilterSet):
    state__in = ListFilter(field_name="state")

    class Meta:
        model = Task
        fields = ['state', 'created_at']

state__in = ListFilter(field_name='state') class-attribute

Meta

Source code in tasks/views.py
37
38
39
class Meta:
    model = Task
    fields = ['state', 'created_at']

fields = ['state', 'created_at'] class-attribute

model = Task class-attribute

TaskViewSet

Bases: viewsets.ModelViewSet

API endpoint that allows users to be viewed or edited.

Source code in tasks/views.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class TaskViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Task.objects.all()
    serializer_class = TaskSerializer
    pagination_class = LimitOffsetPagination
    filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
    search_fields = ['object_id', 'name', 'state']
    filter_class = TaskFilter

    def get_queryset(self):
        if self.request.user.is_superuser:
            return super().get_queryset()

        return super().get_queryset().filter(
            Q(creator=self.request.user)
        )

filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter] class-attribute

filter_class = TaskFilter class-attribute

pagination_class = LimitOffsetPagination class-attribute

queryset = Task.objects.all() class-attribute

search_fields = ['object_id', 'name', 'state'] class-attribute

serializer_class = TaskSerializer class-attribute

get_queryset()

Source code in tasks/views.py
53
54
55
56
57
58
59
def get_queryset(self):
    if self.request.user.is_superuser:
        return super().get_queryset()

    return super().get_queryset().filter(
        Q(creator=self.request.user)
    )