Add filter
Add queryset Add settings
This commit is contained in:
parent
b6d0815ed0
commit
93056cf524
|
|
@ -1,3 +1,3 @@
|
||||||
FROM mdillon/postgis:9.5
|
FROM mdillon/postgis:latest
|
||||||
RUN localedef -i ru_RU -c -f UTF-8 -A /usr/share/locale/locale.alias ru_RU.UTF-8
|
RUN localedef -i ru_RU -c -f UTF-8 -A /usr/share/locale/locale.alias ru_RU.UTF-8
|
||||||
ENV LANG ru_RU.utf8
|
ENV LANG ru_RU.utf8
|
||||||
|
|
|
||||||
24
apps/location/filters.py
Normal file
24
apps/location/filters.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
from django.core.validators import EMPTY_VALUES
|
||||||
|
from django_filters import rest_framework as filters
|
||||||
|
|
||||||
|
from location import models
|
||||||
|
|
||||||
|
|
||||||
|
class CityBackFilter(filters.FilterSet):
|
||||||
|
"""Employee filter set."""
|
||||||
|
|
||||||
|
search = filters.CharFilter(method='search_by_name')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta class."""
|
||||||
|
|
||||||
|
model = models.City
|
||||||
|
fields = (
|
||||||
|
'search',
|
||||||
|
)
|
||||||
|
|
||||||
|
def search_by_name(self, queryset, name, value):
|
||||||
|
"""Search by name or last name."""
|
||||||
|
if value not in EMPTY_VALUES:
|
||||||
|
return queryset.search_by_name(value)
|
||||||
|
return queryset
|
||||||
|
|
@ -5,6 +5,9 @@ from django.db.models.signals import post_save
|
||||||
from django.db.transaction import on_commit
|
from django.db.transaction import on_commit
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from functools import reduce
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
from translation.models import Language
|
from translation.models import Language
|
||||||
from utils.models import (ProjectBaseMixin, SVGImageMixin, TJSONField,
|
from utils.models import (ProjectBaseMixin, SVGImageMixin, TJSONField,
|
||||||
|
|
@ -92,6 +95,18 @@ class Region(models.Model):
|
||||||
class CityQuerySet(models.QuerySet):
|
class CityQuerySet(models.QuerySet):
|
||||||
"""Extended queryset for City model."""
|
"""Extended queryset for City model."""
|
||||||
|
|
||||||
|
def _generic_search(self, value, filter_fields_names: List[str]):
|
||||||
|
"""Generic method for searching value in specified fields"""
|
||||||
|
filters = [
|
||||||
|
{f'{field}__icontains': value}
|
||||||
|
for field in filter_fields_names
|
||||||
|
]
|
||||||
|
return self.filter(reduce(lambda x, y: x | y, [models.Q(**i) for i in filters]))
|
||||||
|
|
||||||
|
def search_by_name(self, value):
|
||||||
|
"""Search by name or last_name."""
|
||||||
|
return self._generic_search(value, ['name', 'code', 'postal_code'])
|
||||||
|
|
||||||
def by_country_code(self, code):
|
def by_country_code(self, code):
|
||||||
"""Return establishments by country code"""
|
"""Return establishments by country code"""
|
||||||
return self.filter(country__code=code)
|
return self.filter(country__code=code)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ 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 import filters
|
||||||
|
|
||||||
# Address
|
# Address
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -31,6 +33,8 @@ class CityListCreateView(common.CityViewMixin, generics.ListCreateAPIView):
|
||||||
"""Create view for model City."""
|
"""Create view for model City."""
|
||||||
serializer_class = serializers.CitySerializer
|
serializer_class = serializers.CitySerializer
|
||||||
permission_classes = [IsAuthenticatedOrReadOnly|IsCountryAdmin]
|
permission_classes = [IsAuthenticatedOrReadOnly|IsCountryAdmin]
|
||||||
|
queryset = models.City.objects.all()
|
||||||
|
filter_class = filters.CityBackFilter
|
||||||
|
|
||||||
|
|
||||||
class CityRUDView(common.CityViewMixin, generics.RetrieveUpdateDestroyAPIView):
|
class CityRUDView(common.CityViewMixin, generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ services:
|
||||||
- "5436:5432"
|
- "5436:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- gm-db:/var/lib/postgresql/data/
|
- gm-db:/var/lib/postgresql/data/
|
||||||
|
- .:/code
|
||||||
|
|
||||||
|
|
||||||
elasticsearch:
|
elasticsearch:
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,19 @@ DATABASES.update({
|
||||||
'PORT': 3306,
|
'PORT': 3306,
|
||||||
'NAME': 'dev',
|
'NAME': 'dev',
|
||||||
'USER': 'dev',
|
'USER': 'dev',
|
||||||
'PASSWORD': 'octosecret123'}})
|
'PASSWORD': 'octosecret123'},
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.contrib.gis.db.backends.postgis',
|
||||||
|
'NAME': os.environ.get('DB_NAME'),
|
||||||
|
'USER': os.environ.get('DB_USERNAME'),
|
||||||
|
'PASSWORD': os.environ.get('DB_PASSWORD'),
|
||||||
|
'HOST': os.environ.get('DB_HOSTNAME'),
|
||||||
|
'PORT': os.environ.get('DB_PORT'),
|
||||||
|
'OPTIONS': {
|
||||||
|
'options': '-c search_path=gm'
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
# LOGGING
|
# LOGGING
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user