Added guides binding objects
This commit is contained in:
parent
22ee06403f
commit
982a52ab96
|
|
@ -1,6 +1,7 @@
|
|||
from rest_framework import serializers
|
||||
|
||||
from collection import models
|
||||
from collection.models import Guide
|
||||
from collection.serializers.common import CollectionBaseSerializer
|
||||
from establishment.models import Establishment
|
||||
from location.models import Country
|
||||
|
|
@ -97,3 +98,54 @@ class CollectionBindObjectSerializer(serializers.Serializer):
|
|||
raise RemovedBindingObjectNotFound()
|
||||
attrs['related_object'] = product
|
||||
return attrs
|
||||
|
||||
|
||||
class GuideBindObjectSerializer(serializers.Serializer):
|
||||
"""Serializer for binding guide and objects"""
|
||||
|
||||
GUIDE = 'guide'
|
||||
PRODUCT = 'product'
|
||||
|
||||
TYPE_CHOICES = (
|
||||
(GUIDE, 'Guide'),
|
||||
(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')
|
||||
|
||||
guide = view.get_object()
|
||||
attrs['guide'] = guide
|
||||
|
||||
if obj_type == self.GUIDE:
|
||||
guide = Guide.objects.filter(pk=obj_id).first()
|
||||
if not guide:
|
||||
raise BindingObjectNotFound()
|
||||
if request.method == 'POST' and guide.establishments.filter(pk=guide.pk).exists():
|
||||
raise ObjectAlreadyAdded()
|
||||
if request.method == 'DELETE' and not guide. \
|
||||
establishments.filter(pk=guide.pk). \
|
||||
exists():
|
||||
raise RemovedBindingObjectNotFound()
|
||||
attrs['related_object'] = guide
|
||||
|
||||
elif obj_type == self.PRODUCT:
|
||||
product = Product.objects.filter(pk=obj_id).first()
|
||||
if not product:
|
||||
raise BindingObjectNotFound()
|
||||
if request.method == 'POST' and guide.products. \
|
||||
filter(pk=product.pk).exists():
|
||||
raise ObjectAlreadyAdded()
|
||||
if request.method == 'DELETE' and not guide.products. \
|
||||
filter(pk=product.pk).exists():
|
||||
raise RemovedBindingObjectNotFound()
|
||||
attrs['related_object'] = product
|
||||
|
||||
return attrs
|
||||
|
|
|
|||
|
|
@ -30,4 +30,6 @@ urlpatterns = [
|
|||
name='guide-export-xml'),
|
||||
path('guides/<int:pk>/export-doc/', views.GuideElementExportDOCView.as_view(),
|
||||
name='guide-export-doc'),
|
||||
path('guides/<int:pk>/bind-object/', views.GuideBackOfficeList.as_view(),
|
||||
name='guide-bind-object')
|
||||
] + router.urls
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import generics
|
||||
from rest_framework import mixins, permissions, viewsets
|
||||
from rest_framework import status
|
||||
from rest_framework import generics, mixins, permissions, status, viewsets
|
||||
from rest_framework.response import Response
|
||||
|
||||
from collection import models, serializers, filters, tasks
|
||||
from collection import filters, models, serializers, tasks
|
||||
from utils.methods import get_permission_classes
|
||||
from utils.views import BindObjectMixin
|
||||
|
||||
|
|
@ -105,9 +103,41 @@ class GuideListCreateView(GuideBaseView,
|
|||
filter_class = filters.GuideFilterSet
|
||||
|
||||
|
||||
class GuideBackOfficeList(BindObjectMixin, GuideBaseView):
|
||||
"""ViewSet for Guides list for BackOffice users"""
|
||||
pagination_class = None
|
||||
filter_class = filters.GuideFilterSet
|
||||
bind_object_serializer_class = serializers.GuideBindObjectSerializer
|
||||
|
||||
def perform_binding(self, serializer):
|
||||
data = serializer.validated_data
|
||||
guide = data.pop('guide')
|
||||
obj_type = data.get('type')
|
||||
related_object = data.get('related_object')
|
||||
|
||||
if obj_type == self.bind_object_serializer_class.GUIDE:
|
||||
guide.establishments.add(related_object)
|
||||
|
||||
elif obj_type == self.bind_object_serializer_class.PRODUCT:
|
||||
guide.products.add(related_object)
|
||||
|
||||
def perform_unbinding(self, serializer):
|
||||
data = serializer.validated_data
|
||||
guide = data.pop('guide')
|
||||
obj_type = data.get('type')
|
||||
related_object = data.get('related_object')
|
||||
|
||||
if obj_type == self.bind_object_serializer_class.GUIDE:
|
||||
guide.establishments.remove(related_object)
|
||||
|
||||
elif obj_type == self.bind_object_serializer_class.PRODUCT:
|
||||
guide.products.remove(related_object)
|
||||
|
||||
|
||||
class GuideFilterCreateView(GuideFilterBaseView,
|
||||
generics.CreateAPIView):
|
||||
"""View for GuideFilter model for BackOffice users."""
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
super().create(request, *args, **kwargs)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user