From 1c6ec90fbf28043911b59700fbd6c7c72882efdb Mon Sep 17 00:00:00 2001 From: Anatoly Date: Tue, 4 Feb 2020 17:01:46 +0300 Subject: [PATCH] added an endpoint that return an array of country calling codes `/api/back/location/countries/calling-codes/` --- apps/establishment/serializers/back.py | 3 +- apps/location/models.py | 16 ++++++ apps/location/urls/back.py | 2 + apps/location/views/back.py | 69 ++++++++++++++++++++------ 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/apps/establishment/serializers/back.py b/apps/establishment/serializers/back.py index 841f0b80..295e1208 100644 --- a/apps/establishment/serializers/back.py +++ b/apps/establishment/serializers/back.py @@ -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) diff --git a/apps/location/models.py b/apps/location/models.py index a7585511..16841e5c 100644 --- a/apps/location/models.py +++ b/apps/location/models.py @@ -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): diff --git a/apps/location/urls/back.py b/apps/location/urls/back.py index b1c0d5ca..1bb3d0a8 100644 --- a/apps/location/urls/back.py +++ b/apps/location/urls/back.py @@ -14,6 +14,8 @@ urlpatterns = [ path('cities//', 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//', views.CountryRUDView.as_view(), name='country-retrieve'), path('regions/', views.RegionListCreateView.as_view(), name='region-list-create'), diff --git a/apps/location/views/back.py b/apps/location/views/back.py index 2085b95b..94646244 100644 --- a/apps/location/views/back.py +++ b/apps/location/views/back.py @@ -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)