added an endpoint that return an array of country calling codes /api/back/location/countries/calling-codes/

This commit is contained in:
Anatoly 2020-02-04 17:01:46 +03:00
parent 28d4122840
commit 1c6ec90fbf
4 changed files with 74 additions and 16 deletions

View File

@ -133,7 +133,7 @@ class EstablishmentListCreateSerializer(model_serializers.EstablishmentBaseSeria
emails_list = validated_data.pop('contact_emails')
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(
index_name,
word_boundary=True
@ -143,6 +143,7 @@ class EstablishmentListCreateSerializer(model_serializers.EstablishmentBaseSeria
f'{index_name} {string_random()}',
word_boundary=True
)
validated_data['slug'] = slug
instance = super().create(validated_data)
phones_handler(phones_list, instance)

View File

@ -23,6 +23,22 @@ class CountryQuerySet(models.QuerySet):
"""Filter only active users."""
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,
SVGImageMixin, ProjectBaseMixin):

View File

@ -14,6 +14,8 @@ urlpatterns = [
path('cities/<int:pk>/', views.CityRUDView.as_view(), name='city-retrieve'),
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('regions/', views.RegionListCreateView.as_view(), name='region-list-create'),

View File

@ -1,6 +1,7 @@
"""Location app views."""
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 models, serializers
@ -107,26 +108,64 @@ class RegionRUDView(common.RegionViewMixin, generics.RetrieveUpdateDestroyAPIVie
# 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."""
queryset = models.Country.objects.all()\
.annotate(locale_name=KeyTextTransform(get_current_locale(), 'name'))\
.order_by('locale_name')
queryset = (models.Country.objects.annotate(
locale_name=KeyTextTransform(get_current_locale(), 'name')).order_by('locale_name'))
serializer_class = serializers.CountryBackSerializer
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(
IsEstablishmentManager,
IsEstablishmentAdministrator,
IsGuest,
)
class CountryRUDView(generics.RetrieveUpdateDestroyAPIView):
"""RUD view for model Country."""
serializer_class = serializers.CountryBackSerializer
queryset = models.Country.objects.all()
permission_classes = get_permission_classes(
IsEstablishmentManager,
IsEstablishmentAdministrator,
IsGuest,
)
def get(self, request, *args, **kwargs):
"""
## Country codes view.
### Response
```
[
"+7",
"+386",
"+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)