Generateurv2/backend/api/exercices/filters.py

29 lines
794 B
Python

import django_filters
from .models import Exercice
class ExosFilter(django_filters.FilterSet):
class Meta:
model = Exercice
fields = ['name']
@property
def qs(self):
parent = super().qs
search = self.request.get('search', '')
tags = self.request.getlist('tags[]', [])
if len(tags) == 0:
withT = []
withAllTags = []
withAllTagsQ = parent
else:
withT = parent.filter(tags__id_code__in=tags).distinct() #les exos possedants au moins un des tags
withAllTags = list(filter(lambda t: all(
i in [t.id_code for t in t.tags.all()] for i in tags ), withT))
withAllTagsQ = parent.filter(id_code__in = [e.id_code for e in withAllTags])
return withAllTagsQ.filter(name__icontains=search)