EvilLogitech/python-project-52

View on GitHub
task_manager/tasks/filters.py

Summary

Maintainability
A
0 mins
Test Coverage
A
92%
import django_filters
from django.forms import CheckboxInput
from django.utils.translation import gettext as _
from task_manager.labels.models import Label
from .models import Task


class TaskFilter(django_filters.FilterSet):

    labels = django_filters.ModelChoiceFilter(
        queryset=Label.objects.all(),
        label=_("Метка")
    )
    own_tasks = django_filters.BooleanFilter(
        label=_('Только свои задачи'),
        method='get_authenticated_user_tasks', widget=CheckboxInput
    )

    class Meta:
        model = Task
        fields = ['status', 'executor']

    def get_authenticated_user_tasks(self, queryset, name, value):
        return queryset.filter(author=self.request.user)