refactoring guide and collection endpoints
This commit is contained in:
parent
aea5c9e96b
commit
ab82d9a2d6
|
|
@ -194,6 +194,17 @@ class Guide(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin):
|
||||||
"""String method."""
|
"""String method."""
|
||||||
return f'{self.name}'
|
return f'{self.name}'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def entities(self):
|
||||||
|
"""Return entities and its count."""
|
||||||
|
# todo: to work
|
||||||
|
return {
|
||||||
|
'Current': 0,
|
||||||
|
'Initial': 0,
|
||||||
|
'Restaurants': 0,
|
||||||
|
'Shops': 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class AdvertorialQuerySet(models.QuerySet):
|
class AdvertorialQuerySet(models.QuerySet):
|
||||||
"""QuerySet for model Advertorial."""
|
"""QuerySet for model Advertorial."""
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
from .back import *
|
||||||
|
from .web import *
|
||||||
|
from .common import *
|
||||||
|
|
@ -92,3 +92,54 @@ class CollectionBindObjectSerializer(serializers.Serializer):
|
||||||
raise RemovedBindingObjectNotFound()
|
raise RemovedBindingObjectNotFound()
|
||||||
attrs['related_object'] = product
|
attrs['related_object'] = product
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
|
class GuideBindObjectSerializer(serializers.Serializer):
|
||||||
|
"""Serializer for binding collection and objects"""
|
||||||
|
|
||||||
|
ESTABLISHMENT = 'establishment'
|
||||||
|
PRODUCT = 'product'
|
||||||
|
|
||||||
|
TYPE_CHOICES = (
|
||||||
|
(ESTABLISHMENT, 'Establishment'),
|
||||||
|
(PRODUCT, 'Product'),
|
||||||
|
)
|
||||||
|
|
||||||
|
type = serializers.ChoiceField(TYPE_CHOICES)
|
||||||
|
object_id = serializers.IntegerField()
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
view = self.context.get('view')
|
||||||
|
request = self.context.get('request')
|
||||||
|
|
||||||
|
obj_type = attrs.get('type')
|
||||||
|
obj_id = attrs.get('object_id')
|
||||||
|
|
||||||
|
collection = view.get_object()
|
||||||
|
attrs['collection'] = collection
|
||||||
|
|
||||||
|
if obj_type == self.ESTABLISHMENT:
|
||||||
|
establishment = Establishment.objects.filter(pk=obj_id).\
|
||||||
|
first()
|
||||||
|
if not establishment:
|
||||||
|
raise BindingObjectNotFound()
|
||||||
|
if request.method == 'POST' and collection.establishments.\
|
||||||
|
filter(pk=establishment.pk).exists():
|
||||||
|
raise ObjectAlreadyAdded()
|
||||||
|
if request.method == 'DELETE' and not collection.\
|
||||||
|
establishments.filter(pk=establishment.pk).\
|
||||||
|
exists():
|
||||||
|
raise RemovedBindingObjectNotFound()
|
||||||
|
attrs['related_object'] = establishment
|
||||||
|
elif obj_type == self.PRODUCT:
|
||||||
|
product = Product.objects.filter(pk=obj_id).first()
|
||||||
|
if not product:
|
||||||
|
raise BindingObjectNotFound()
|
||||||
|
if request.method == 'POST' and collection.products.\
|
||||||
|
filter(pk=product.pk).exists():
|
||||||
|
raise ObjectAlreadyAdded()
|
||||||
|
if request.method == 'DELETE' and not collection.products.\
|
||||||
|
filter(pk=product.pk).exists():
|
||||||
|
raise RemovedBindingObjectNotFound()
|
||||||
|
attrs['related_object'] = product
|
||||||
|
return attrs
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ from rest_framework import serializers
|
||||||
from collection import models
|
from collection import models
|
||||||
from location import models as location_models
|
from location import models as location_models
|
||||||
from utils.serializers import TranslatedField
|
from utils.serializers import TranslatedField
|
||||||
|
from main.models import SiteSettings
|
||||||
|
from main.serializers import SiteShortSerializer
|
||||||
|
|
||||||
|
|
||||||
class CollectionBaseSerializer(serializers.ModelSerializer):
|
class CollectionBaseSerializer(serializers.ModelSerializer):
|
||||||
|
|
@ -47,8 +49,33 @@ class CollectionSerializer(CollectionBaseSerializer):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class GuideSerializer(serializers.ModelSerializer):
|
class GuideTypeBaseSerializer(serializers.ModelSerializer):
|
||||||
|
"""GuideType serializer."""
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta class."""
|
||||||
|
model = models.GuideType
|
||||||
|
fields = [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class GuideBaseSerializer(serializers.ModelSerializer):
|
||||||
"""Guide serializer"""
|
"""Guide serializer"""
|
||||||
|
state_display = serializers.CharField(source='get_state_display',
|
||||||
|
read_only=True)
|
||||||
|
guide_type = GuideTypeBaseSerializer(allow_null=True,
|
||||||
|
read_only=True)
|
||||||
|
guide_type_id = serializers.PrimaryKeyRelatedField(
|
||||||
|
queryset=models.GuideType.objects.all(),
|
||||||
|
write_only=True)
|
||||||
|
site = SiteShortSerializer(read_only=True)
|
||||||
|
site_id = serializers.PrimaryKeyRelatedField(
|
||||||
|
queryset=SiteSettings.objects.all(),
|
||||||
|
write_only=True)
|
||||||
|
entities = serializers.DictField(read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Guide
|
model = models.Guide
|
||||||
fields = [
|
fields = [
|
||||||
|
|
@ -56,4 +83,16 @@ class GuideSerializer(serializers.ModelSerializer):
|
||||||
'name',
|
'name',
|
||||||
'start',
|
'start',
|
||||||
'end',
|
'end',
|
||||||
|
'vintage',
|
||||||
|
'slug',
|
||||||
|
'guide_type',
|
||||||
|
'guide_type_id',
|
||||||
|
'site',
|
||||||
|
'site_id',
|
||||||
|
'state',
|
||||||
|
'state_display',
|
||||||
|
'entities',
|
||||||
]
|
]
|
||||||
|
extra_kwargs = {
|
||||||
|
'state': {'write_only': True},
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ from rest_framework.routers import SimpleRouter
|
||||||
from collection.views import back as views
|
from collection.views import back as views
|
||||||
|
|
||||||
app_name = 'collection'
|
app_name = 'collection'
|
||||||
|
|
||||||
router = SimpleRouter()
|
router = SimpleRouter()
|
||||||
router.register(r'', views.CollectionBackOfficeViewSet)
|
router.register(r'collections', views.CollectionBackOfficeViewSet)
|
||||||
|
router.register(r'guides', views.GuideBackOfficeViewSet)
|
||||||
|
|
||||||
urlpatterns = router.urls
|
urlpatterns = router.urls
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,7 @@ app_name = 'collection'
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.CollectionHomePageView.as_view(), name='list'),
|
path('', views.CollectionHomePageView.as_view(), name='list'),
|
||||||
path('<slug:slug>/', views.CollectionDetailView.as_view(), name='detail'),
|
path('slug/<slug:slug>/', views.CollectionDetailView.as_view(), name='detail'),
|
||||||
path('<slug:slug>/establishments/', views.CollectionEstablishmentListView.as_view(),
|
path('slug/<slug:slug>/establishments/', views.CollectionEstablishmentListView.as_view(),
|
||||||
name='detail'),
|
name='detail'),
|
||||||
|
|
||||||
path('guides/', views.GuideListView.as_view(), name='guides-list'),
|
|
||||||
path('guides/<int:pk>/', views.GuideRetrieveView.as_view(), name='guides-detail'),
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
from collection.urls.common import urlpatterns as common_url_patterns
|
from collection.urls.common import urlpatterns as common_url_patterns
|
||||||
|
|
||||||
|
|
||||||
|
app_name = 'web'
|
||||||
|
|
||||||
urlpatterns_api = []
|
urlpatterns_api = []
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
from rest_framework import permissions
|
from rest_framework import permissions
|
||||||
from rest_framework import viewsets, mixins
|
from rest_framework import viewsets, mixins
|
||||||
|
|
||||||
from collection import models
|
from collection import models, serializers
|
||||||
from collection.serializers import back as serializers
|
|
||||||
from utils.views import BindObjectMixin
|
from utils.views import BindObjectMixin
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -15,6 +14,14 @@ class CollectionViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
|
||||||
serializer_class = serializers.CollectionBackOfficeSerializer
|
serializer_class = serializers.CollectionBackOfficeSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class GuideViewSet(viewsets.ModelViewSet):
|
||||||
|
"""ViewSet for Guide model."""
|
||||||
|
pagination_class = None
|
||||||
|
queryset = models.Guide.objects.all()
|
||||||
|
serializer_class = serializers.GuideBaseSerializer
|
||||||
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
|
|
||||||
|
|
||||||
class CollectionBackOfficeViewSet(mixins.CreateModelMixin,
|
class CollectionBackOfficeViewSet(mixins.CreateModelMixin,
|
||||||
mixins.UpdateModelMixin,
|
mixins.UpdateModelMixin,
|
||||||
mixins.DestroyModelMixin,
|
mixins.DestroyModelMixin,
|
||||||
|
|
@ -24,8 +31,6 @@ class CollectionBackOfficeViewSet(mixins.CreateModelMixin,
|
||||||
"""ViewSet for Collection model for BackOffice users."""
|
"""ViewSet for Collection model for BackOffice users."""
|
||||||
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
queryset = models.Collection.objects.all()
|
|
||||||
serializer_class = serializers.CollectionBackOfficeSerializer
|
|
||||||
bind_object_serializer_class = serializers.CollectionBindObjectSerializer
|
bind_object_serializer_class = serializers.CollectionBindObjectSerializer
|
||||||
|
|
||||||
def perform_binding(self, serializer):
|
def perform_binding(self, serializer):
|
||||||
|
|
@ -47,3 +52,7 @@ class CollectionBackOfficeViewSet(mixins.CreateModelMixin,
|
||||||
collection.establishments.remove(related_object)
|
collection.establishments.remove(related_object)
|
||||||
elif obj_type == self.bind_object_serializer_class.PRODUCT:
|
elif obj_type == self.bind_object_serializer_class.PRODUCT:
|
||||||
collection.products.remove(related_object)
|
collection.products.remove(related_object)
|
||||||
|
|
||||||
|
|
||||||
|
class GuideBackOfficeViewSet(GuideViewSet):
|
||||||
|
"""ViewSet for Guide model for BackOffice users."""
|
||||||
|
|
|
||||||
|
|
@ -72,10 +72,10 @@ class CollectionEstablishmentListView(CollectionListView):
|
||||||
class GuideListView(GuideViewMixin, generics.ListAPIView):
|
class GuideListView(GuideViewMixin, generics.ListAPIView):
|
||||||
"""List Guide view"""
|
"""List Guide view"""
|
||||||
permission_classes = (permissions.AllowAny,)
|
permission_classes = (permissions.AllowAny,)
|
||||||
serializer_class = serializers.GuideSerializer
|
serializer_class = serializers.GuideBaseSerializer
|
||||||
|
|
||||||
|
|
||||||
class GuideRetrieveView(GuideViewMixin, generics.RetrieveAPIView):
|
class GuideRetrieveView(GuideViewMixin, generics.RetrieveAPIView):
|
||||||
"""Retrieve Guide view"""
|
"""Retrieve Guide view"""
|
||||||
permission_classes = (permissions.AllowAny,)
|
permission_classes = (permissions.AllowAny,)
|
||||||
serializer_class = serializers.GuideSerializer
|
serializer_class = serializers.GuideBaseSerializer
|
||||||
|
|
|
||||||
|
|
@ -152,8 +152,6 @@ class SiteShortSerializer(serializers.ModelSerializer):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AwardBaseSerializer(serializers.ModelSerializer):
|
class AwardBaseSerializer(serializers.ModelSerializer):
|
||||||
"""Award base serializer."""
|
"""Award base serializer."""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ class GuideSerializer(TransferSerializerMixin):
|
||||||
class GuideFilterSerializer(TransferSerializerMixin):
|
class GuideFilterSerializer(TransferSerializerMixin):
|
||||||
id = serializers.IntegerField()
|
id = serializers.IntegerField()
|
||||||
year = serializers.CharField(allow_null=True)
|
year = serializers.CharField(allow_null=True)
|
||||||
type = serializers.CharField(allow_null=True, source='establishment_type')
|
establishment_type = serializers.CharField(allow_null=True)
|
||||||
countries = serializers.CharField(allow_null=True)
|
countries = serializers.CharField(allow_null=True)
|
||||||
regions = serializers.CharField(allow_null=True)
|
regions = serializers.CharField(allow_null=True)
|
||||||
subregions = serializers.CharField(allow_null=True)
|
subregions = serializers.CharField(allow_null=True)
|
||||||
|
|
@ -86,7 +86,7 @@ class GuideFilterSerializer(TransferSerializerMixin):
|
||||||
fields = (
|
fields = (
|
||||||
'id',
|
'id',
|
||||||
'year',
|
'year',
|
||||||
'type',
|
'establishment_type',
|
||||||
'countries',
|
'countries',
|
||||||
'regions',
|
'regions',
|
||||||
'subregions',
|
'subregions',
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ urlpatterns = [
|
||||||
path('timetables/', include('timetable.urls.mobile')),
|
path('timetables/', include('timetable.urls.mobile')),
|
||||||
# path('account/', include('account.urls.web')),
|
# path('account/', include('account.urls.web')),
|
||||||
path('re_blocks/', include('advertisement.urls.mobile')),
|
path('re_blocks/', include('advertisement.urls.mobile')),
|
||||||
# path('collection/', include('collection.urls.web')),
|
# path('collection/', include('collection.urls.mobile')),
|
||||||
# path('establishments/', include('establishment.urls.web')),
|
# path('establishments/', include('establishment.urls.web')),
|
||||||
path('news/', include('news.urls.mobile')),
|
path('news/', include('news.urls.mobile')),
|
||||||
# path('partner/', include('partner.urls.web')),
|
# path('partner/', include('partner.urls.web')),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user