added an endpoint that return an array of country calling codes /api/back/location/countries/calling-codes/
This commit is contained in:
parent
28d4122840
commit
1c6ec90fbf
|
|
@ -133,7 +133,7 @@ class EstablishmentListCreateSerializer(model_serializers.EstablishmentBaseSeria
|
||||||
emails_list = validated_data.pop('contact_emails')
|
emails_list = validated_data.pop('contact_emails')
|
||||||
|
|
||||||
index_name = validated_data.get('index_name')
|
index_name = validated_data.get('index_name')
|
||||||
if 'slug' in validated_data and index_name:
|
if 'slug' not in validated_data and index_name:
|
||||||
slug = slugify(
|
slug = slugify(
|
||||||
index_name,
|
index_name,
|
||||||
word_boundary=True
|
word_boundary=True
|
||||||
|
|
@ -143,6 +143,7 @@ class EstablishmentListCreateSerializer(model_serializers.EstablishmentBaseSeria
|
||||||
f'{index_name} {string_random()}',
|
f'{index_name} {string_random()}',
|
||||||
word_boundary=True
|
word_boundary=True
|
||||||
)
|
)
|
||||||
|
validated_data['slug'] = slug
|
||||||
|
|
||||||
instance = super().create(validated_data)
|
instance = super().create(validated_data)
|
||||||
phones_handler(phones_list, instance)
|
phones_handler(phones_list, instance)
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,22 @@ class CountryQuerySet(models.QuerySet):
|
||||||
"""Filter only active users."""
|
"""Filter only active users."""
|
||||||
return self.filter(is_active=switcher)
|
return self.filter(is_active=switcher)
|
||||||
|
|
||||||
|
def by_country_code(self, code: str):
|
||||||
|
"""Filter QuerySet by country code."""
|
||||||
|
return self.filter(code__iexact=code)
|
||||||
|
|
||||||
|
def aggregate_country_codes(self):
|
||||||
|
"""Aggregate country codes."""
|
||||||
|
calling_codes = list(
|
||||||
|
self.model.objects.exclude(calling_code__isnull=True)
|
||||||
|
.exclude(code__iexact='aa')
|
||||||
|
.distinct()
|
||||||
|
.values_list('calling_code', flat=True)
|
||||||
|
)
|
||||||
|
# extend country calling code hardcoded codes
|
||||||
|
calling_codes.extend(settings.CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES)
|
||||||
|
return [self.model.CALLING_NUMBER_MASK % i for i in calling_codes]
|
||||||
|
|
||||||
|
|
||||||
class Country(TranslatedFieldsMixin,
|
class Country(TranslatedFieldsMixin,
|
||||||
SVGImageMixin, ProjectBaseMixin):
|
SVGImageMixin, ProjectBaseMixin):
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ urlpatterns = [
|
||||||
path('cities/<int:pk>/', views.CityRUDView.as_view(), name='city-retrieve'),
|
path('cities/<int:pk>/', views.CityRUDView.as_view(), name='city-retrieve'),
|
||||||
|
|
||||||
path('countries/', views.CountryListCreateView.as_view(), name='country-list-create'),
|
path('countries/', views.CountryListCreateView.as_view(), name='country-list-create'),
|
||||||
|
path('countries/calling-codes/', views.CountryCallingCodeListView.as_view(),
|
||||||
|
name='country-calling-code-list'),
|
||||||
path('countries/<int:pk>/', views.CountryRUDView.as_view(), name='country-retrieve'),
|
path('countries/<int:pk>/', views.CountryRUDView.as_view(), name='country-retrieve'),
|
||||||
|
|
||||||
path('regions/', views.RegionListCreateView.as_view(), name='region-list-create'),
|
path('regions/', views.RegionListCreateView.as_view(), name='region-list-create'),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"""Location app views."""
|
"""Location app views."""
|
||||||
from django.contrib.postgres.fields.jsonb import KeyTextTransform
|
from django.contrib.postgres.fields.jsonb import KeyTextTransform
|
||||||
from rest_framework import generics
|
from rest_framework import generics, status
|
||||||
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from location import filters
|
from location import filters
|
||||||
from location import models, serializers
|
from location import models, serializers
|
||||||
|
|
@ -107,26 +108,64 @@ class RegionRUDView(common.RegionViewMixin, generics.RetrieveUpdateDestroyAPIVie
|
||||||
|
|
||||||
|
|
||||||
# Country
|
# Country
|
||||||
class CountryListCreateView(generics.ListCreateAPIView):
|
class CountryBaseViewMixin:
|
||||||
|
"""Mixin for Country views."""
|
||||||
|
queryset = models.Country.objects.all()
|
||||||
|
permission_classes = get_permission_classes(
|
||||||
|
IsEstablishmentManager,
|
||||||
|
IsEstablishmentAdministrator,
|
||||||
|
IsGuest,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CountryListCreateView(CountryBaseViewMixin, generics.ListCreateAPIView):
|
||||||
"""List/Create view for model Country."""
|
"""List/Create view for model Country."""
|
||||||
queryset = models.Country.objects.all()\
|
queryset = (models.Country.objects.annotate(
|
||||||
.annotate(locale_name=KeyTextTransform(get_current_locale(), 'name'))\
|
locale_name=KeyTextTransform(get_current_locale(), 'name')).order_by('locale_name'))
|
||||||
.order_by('locale_name')
|
|
||||||
serializer_class = serializers.CountryBackSerializer
|
serializer_class = serializers.CountryBackSerializer
|
||||||
pagination_class = None
|
pagination_class = None
|
||||||
|
|
||||||
|
|
||||||
|
class CountryRUDView(CountryBaseViewMixin, generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
"""RUD view for model Country."""
|
||||||
|
serializer_class = serializers.CountryBackSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class CountryCallingCodeListView(generics.ListAPIView):
|
||||||
|
"""
|
||||||
|
## Country codes view.
|
||||||
|
### Response
|
||||||
|
```
|
||||||
|
[
|
||||||
|
"+7",
|
||||||
|
"+386",
|
||||||
|
"+352"
|
||||||
|
]
|
||||||
|
```
|
||||||
|
### Description
|
||||||
|
Return an array of unique country code for all countries in a database.
|
||||||
|
"""
|
||||||
|
pagination_class = None
|
||||||
permission_classes = get_permission_classes(
|
permission_classes = get_permission_classes(
|
||||||
IsEstablishmentManager,
|
IsEstablishmentManager,
|
||||||
IsEstablishmentAdministrator,
|
IsEstablishmentAdministrator,
|
||||||
IsGuest,
|
IsGuest,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
class CountryRUDView(generics.RetrieveUpdateDestroyAPIView):
|
"""
|
||||||
"""RUD view for model Country."""
|
## Country codes view.
|
||||||
serializer_class = serializers.CountryBackSerializer
|
### Response
|
||||||
queryset = models.Country.objects.all()
|
```
|
||||||
permission_classes = get_permission_classes(
|
[
|
||||||
IsEstablishmentManager,
|
"+7",
|
||||||
IsEstablishmentAdministrator,
|
"+386",
|
||||||
IsGuest,
|
"+352"
|
||||||
)
|
]
|
||||||
|
```
|
||||||
|
### Description
|
||||||
|
Return an array of unique country code for all countries in a database.
|
||||||
|
"""
|
||||||
|
return Response(models.Country.objects.only('calling_code')
|
||||||
|
.aggregate_country_codes(),
|
||||||
|
status=status.HTTP_200_OK)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user