merged from develop, added merge migration and refactored a little bit

This commit is contained in:
Anatoly 2019-12-17 15:20:55 +03:00
parent 471239983e
commit 387e164510
3 changed files with 15 additions and 53 deletions

View File

@ -0,0 +1,14 @@
# Generated by Django 2.2.7 on 2019-12-17 11:51
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('collection', '0024_auto_20191213_0859'),
('collection', '0025_collection_description'),
]
operations = [
]

View File

@ -94,54 +94,3 @@ class CollectionBindObjectSerializer(serializers.Serializer):
raise RemovedBindingObjectNotFound()
attrs['related_object'] = product
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

View File

@ -2,9 +2,8 @@ from rest_framework import serializers
from collection import models
from location import models as location_models
from utils.serializers import TranslatedField
from main.models import SiteSettings
from main.serializers import SiteShortSerializer
from utils.serializers import TranslatedField
class CollectionBaseSerializer(serializers.ModelSerializer):