added filters to location views
This commit is contained in:
parent
71fb3249b0
commit
36834835cd
|
|
@ -22,3 +22,34 @@ class CityBackFilter(filters.FilterSet):
|
||||||
if value not in EMPTY_VALUES:
|
if value not in EMPTY_VALUES:
|
||||||
return queryset.search_by_name(value)
|
return queryset.search_by_name(value)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
class RegionFilter(filters.FilterSet):
|
||||||
|
"""Region filter set."""
|
||||||
|
|
||||||
|
country_id = filters.CharFilter()
|
||||||
|
sub_regions_by_region_id = filters.CharFilter(method='by_region')
|
||||||
|
without_parent_region = filters.BooleanFilter(method='by_parent_region')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta class."""
|
||||||
|
model = models.Region
|
||||||
|
fields = (
|
||||||
|
'country_id',
|
||||||
|
'sub_regions_by_region_id',
|
||||||
|
'without_parent_region',
|
||||||
|
)
|
||||||
|
|
||||||
|
def by_region(self, queryset, name, value):
|
||||||
|
"""Search regions by sub region id."""
|
||||||
|
if value not in EMPTY_VALUES:
|
||||||
|
return queryset.sub_regions_by_region_id(value)
|
||||||
|
|
||||||
|
def by_parent_region(self, queryset, name, value):
|
||||||
|
"""
|
||||||
|
Search if region instance has a parent region..
|
||||||
|
If True then show only Regions
|
||||||
|
Otherwise show only Sub regions.
|
||||||
|
"""
|
||||||
|
if value not in EMPTY_VALUES:
|
||||||
|
return queryset.without_parent_region(value)
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,26 @@ class Country(TranslatedFieldsMixin, SVGImageMixin, ProjectBaseMixin):
|
||||||
return str_name
|
return str_name
|
||||||
|
|
||||||
|
|
||||||
|
class RegionQuerySet(models.QuerySet):
|
||||||
|
"""QuerySet for model Region."""
|
||||||
|
|
||||||
|
def without_parent_region(self, switcher: bool = True):
|
||||||
|
"""Filter regions by parent region."""
|
||||||
|
return self.filter(parent_region__isnull=switcher)
|
||||||
|
|
||||||
|
def by_region_id(self, region_id):
|
||||||
|
"""Filter regions by region id."""
|
||||||
|
return self.filter(id=region_id)
|
||||||
|
|
||||||
|
def by_sub_region_id(self, sub_region_id):
|
||||||
|
"""Filter sub regions by sub region id."""
|
||||||
|
return self.filter(parent_region_id=sub_region_id)
|
||||||
|
|
||||||
|
def sub_regions_by_region_id(self, region_id):
|
||||||
|
"""Filter regions by sub region id."""
|
||||||
|
return self.filter(parent_region_id=region_id)
|
||||||
|
|
||||||
|
|
||||||
class Region(models.Model):
|
class Region(models.Model):
|
||||||
"""Region model."""
|
"""Region model."""
|
||||||
|
|
||||||
|
|
@ -82,6 +102,8 @@ class Region(models.Model):
|
||||||
Country, verbose_name=_('country'), on_delete=models.CASCADE)
|
Country, verbose_name=_('country'), on_delete=models.CASCADE)
|
||||||
old_id = models.IntegerField(null=True, blank=True, default=None)
|
old_id = models.IntegerField(null=True, blank=True, default=None)
|
||||||
|
|
||||||
|
objects = RegionQuerySet.as_manager()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta class."""
|
"""Meta class."""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
"""Location app views."""
|
"""Location app views."""
|
||||||
from django_filters.rest_framework import DjangoFilterBackend
|
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
|
|
||||||
from location import models, serializers
|
from location import models, serializers
|
||||||
|
|
@ -9,6 +8,7 @@ from utils.views import CreateDestroyGalleryViewMixin
|
||||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from utils.serializers import ImageBaseSerializer
|
from utils.serializers import ImageBaseSerializer
|
||||||
|
from location.filters import RegionFilter
|
||||||
|
|
||||||
from location import filters
|
from location import filters
|
||||||
|
|
||||||
|
|
@ -109,11 +109,8 @@ class RegionListCreateView(common.RegionViewMixin, generics.ListCreateAPIView):
|
||||||
pagination_class = None
|
pagination_class = None
|
||||||
serializer_class = serializers.RegionSerializer
|
serializer_class = serializers.RegionSerializer
|
||||||
permission_classes = [IsAuthenticatedOrReadOnly | IsCountryAdmin]
|
permission_classes = [IsAuthenticatedOrReadOnly | IsCountryAdmin]
|
||||||
filter_backends = (DjangoFilterBackend,)
|
|
||||||
ordering_fields = '__all__'
|
ordering_fields = '__all__'
|
||||||
filterset_fields = (
|
filter_class = RegionFilter
|
||||||
'country',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class RegionRUDView(common.RegionViewMixin, generics.RetrieveUpdateDestroyAPIView):
|
class RegionRUDView(common.RegionViewMixin, generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user