add api locations for back office

This commit is contained in:
Dmitriy Kuzmenko 2019-09-18 11:39:46 +03:00
parent ccc1660e1f
commit e9bf129f82
13 changed files with 146 additions and 24 deletions

View File

@ -0,0 +1,2 @@
from location.serializers.common import *
from location.serializers.back import *

View File

@ -0,0 +1,22 @@
from django.contrib.gis.geos import Point
from rest_framework import serializers
from location import models
from location.serializers import common
class AddressCreateSerializer(common.AddressSerializer):
"""Address create serializer."""
class CountryBackSerializer(common.CountrySerializer):
"""Country back-office serializer."""
class Meta:
model = models.Country
fields = [
'id',
'code',
'svg_image',
'name',
]

View File

@ -23,7 +23,12 @@ class CountrySerializer(serializers.ModelSerializer):
class RegionSerializer(serializers.ModelSerializer):
"""Region serializer"""
country = CountrySerializer()
country = CountrySerializer(read_only=True)
country_id = serializers.PrimaryKeyRelatedField(
source='country',
queryset=models.Country.objects.all(),
write_only=True
)
class Meta:
model = models.Region
@ -32,13 +37,24 @@ class RegionSerializer(serializers.ModelSerializer):
'name',
'code',
'parent_region',
'country'
'country',
'country_id'
]
class CitySerializer(serializers.ModelSerializer):
"""City serializer."""
region = RegionSerializer()
region = RegionSerializer(read_only=True)
region_id = serializers.PrimaryKeyRelatedField(
source='region',
queryset=models.Region.objects.all(),
write_only=True
)
country_id = serializers.PrimaryKeyRelatedField(
source='country',
queryset=models.Country.objects.all(),
write_only=True
)
class Meta:
model = models.City
@ -47,6 +63,8 @@ class CitySerializer(serializers.ModelSerializer):
'name',
'code',
'region',
'region_id',
'country_id',
'postal_code',
'is_island',
]
@ -54,13 +72,18 @@ class CitySerializer(serializers.ModelSerializer):
class AddressSerializer(serializers.ModelSerializer):
"""Address serializer."""
city = CitySerializer()
city_id = serializers.PrimaryKeyRelatedField(
source='city',
queryset=models.City.objects.all())
city = CitySerializer(read_only=True)
geo_lon = serializers.FloatField(allow_null=True)
geo_lat = serializers.FloatField(allow_null=True)
class Meta:
model = models.Address
fields = [
'id',
'city_id',
'city',
'street_name_1',
'street_name_2',
@ -74,9 +97,10 @@ class AddressSerializer(serializers.ModelSerializer):
# if geo_lat and geo_lon was sent
geo_lat = attrs.pop('geo_lat') if 'geo_lat' in attrs else None
geo_lon = attrs.pop('geo_lon') if 'geo_lon' in attrs else None
if geo_lat and geo_lon:
# Point(longitude, latitude)
attrs['location'] = Point(geo_lat, geo_lon)
attrs['coordinates'] = Point(geo_lat, geo_lon)
return attrs
def to_representation(self, instance):

View File

View File

@ -0,0 +1,20 @@
"""Location app back-office urlconf."""
from django.urls import path
from location import views
app_name = 'location'
urlpatterns = [
path('addresses/', views.AddressListCreateView.as_view(), name='address-list-create'),
path('addresses/<int:pk>/', views.AddressRUDView.as_view(), name='address-RUD'),
path('cities/', views.CityListCreateView.as_view(), name='city-list-create'),
path('cities/<int:pk>/', views.CityRUDView.as_view(), name='city-retrieve'),
path('countries/', views.CountryListCreateView.as_view(), name='country-list-create'),
path('countries/<int:pk>/', views.CountryRUDView.as_view(), name='country-retrieve'),
path('regions/', views.RegionListCreateView.as_view(), name='region-list-create'),
path('regions/<int:pk>/', views.RegionRUDView.as_view(), name='region-retrieve'),
]

View File

@ -1,4 +1,4 @@
"""Location app urlconf."""
"""Location app common urlconf."""
from django.urls import path
from location import views

View File

@ -0,0 +1,7 @@
"""Location app mobile urlconf."""
from location.urls.common import urlpatterns as common_urlpatterns
urlpatterns = []
urlpatterns.extend(common_urlpatterns)

View File

@ -0,0 +1,7 @@
"""Location app web urlconf."""
from location.urls.common import urlpatterns as common_urlpatterns
urlpatterns = []
urlpatterns.extend(common_urlpatterns)

View File

@ -0,0 +1,2 @@
from location.views.common import *
from location.views.back import *

View File

@ -0,0 +1,53 @@
"""Location app views."""
from rest_framework import generics
from rest_framework import permissions
from location import models, serializers
from location.views import common
# Address
class AddressListCreateView(common.AddressViewMixin, generics.ListCreateAPIView):
"""Create view for model Address."""
serializer_class = serializers.AddressSerializer
queryset = models.Address.objects.all()
class AddressRUDView(common.AddressViewMixin, generics.RetrieveUpdateDestroyAPIView):
"""RUD view for model Address."""
serializer_class = serializers.AddressSerializer
queryset = models.Address.objects.all()
# City
class CityListCreateView(common.CityViewMixin, generics.ListCreateAPIView):
"""Create view for model City."""
serializer_class = serializers.CitySerializer
class CityRUDView(common.CityViewMixin, generics.RetrieveUpdateDestroyAPIView):
"""RUD view for model City."""
serializer_class = serializers.CitySerializer
# Region
class RegionListCreateView(common.RegionViewMixin, generics.ListCreateAPIView):
"""Create view for model Region"""
serializer_class = serializers.RegionSerializer
class RegionRUDView(common.RegionViewMixin, generics.RetrieveUpdateDestroyAPIView):
"""Retrieve view for model Region"""
serializer_class = serializers.RegionSerializer
# Country
class CountryListCreateView(common.CountryViewMixin, generics.ListCreateAPIView):
"""List/Create view for model Country."""
serializer_class = serializers.CountryBackSerializer
pagination_class = None
class CountryRUDView(common.CountryViewMixin, generics.RetrieveUpdateDestroyAPIView):
"""RUD view for model Country."""
serializer_class = serializers.CountryBackSerializer

View File

@ -3,11 +3,10 @@ from rest_framework import generics
from rest_framework import permissions
from location import models, serializers
from utils.views import JWTGenericViewMixin
# Mixins
class CountryViewMixin(JWTGenericViewMixin, generics.GenericAPIView):
class CountryViewMixin(generics.GenericAPIView):
"""View Mixin for model Country"""
serializer_class = serializers.CountrySerializer
@ -115,13 +114,3 @@ class AddressListView(AddressViewMixin, generics.ListAPIView):
serializer_class = serializers.AddressSerializer
class AddressDestroyView(AddressViewMixin, generics.DestroyAPIView):
"""Destroy view for model Address"""
serializer_class = serializers.AddressSerializer
class AddressUpdateView(AddressViewMixin, generics.UpdateAPIView):
"""Update view for model Address"""
serializer_class = serializers.AddressSerializer

View File

@ -5,10 +5,6 @@ app_name = 'back'
urlpatterns = [
path('gallery/', include(('gallery.urls', 'gallery'),
namespace='gallery')),
# path('account/', include('account.urls.web')),
# path('advertisement/', include('advertisement.urls.web')),
# path('collection/', include('collection.urls.web')),
path('establishments/', include('establishment.urls.back')),
# path('news/', include('news.urls.web')),
# path('partner/', include('partner.urls.web')),
path('location/', include('location.urls.back')),
]

View File

@ -25,7 +25,7 @@ urlpatterns = [
path('news/', include('news.urls.web')),
path('notifications/', include('notification.urls.web')),
path('partner/', include('partner.urls.web')),
path('location/', include('location.urls')),
path('location/', include('location.urls.web')),
path('main/', include('main.urls')),
path('translation/', include('translation.urls')),
path('comments/', include('comment.urls.web')),