Merge branch 'feature/back-office-collections' into 'develop'

Feature/back office collections

See merge request gm/gm-backend!178
This commit is contained in:
Олег Хаятов 2019-12-16 11:02:27 +00:00
commit 23522eb1f0
5 changed files with 78 additions and 29 deletions

View File

@ -0,0 +1,27 @@
# Generated by Django 2.2.7 on 2019-12-15 21:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0023_advertorial'),
]
operations = [
migrations.AddField(
model_name='collection',
name='rank',
field=models.IntegerField(default=None, null=True),
),
migrations.AlterField(
model_name='collection',
name='start',
field=models.DateTimeField(blank=True, default=None, null=True, verbose_name='start'),
),
migrations.RemoveField(
model_name='collection',
name='description',
)
]

View File

@ -6,9 +6,10 @@ from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
from utils.models import ProjectBaseMixin, URLImageMixin
from utils.models import TJSONField
from utils.models import TranslatedFieldsMixin
from utils.models import (
ProjectBaseMixin, TJSONField, TranslatedFieldsMixin,
URLImageMixin,
)
from utils.querysets import RelatedObjectsCountMixin
@ -24,7 +25,8 @@ class CollectionNameMixin(models.Model):
class CollectionDateMixin(models.Model):
"""CollectionDate mixin"""
start = models.DateTimeField(_('start'))
start = models.DateTimeField(blank=True, null=True, default=None,
verbose_name=_('start'))
end = models.DateTimeField(blank=True, null=True, default=None,
verbose_name=_('end'))
@ -73,13 +75,15 @@ class Collection(ProjectBaseMixin, CollectionDateMixin,
block_size = JSONField(
_('collection block properties'), null=True, blank=True,
default=None, help_text='{"width": "250px", "height":"250px"}')
description = TJSONField(
_('description'), null=True, blank=True,
default=None, help_text='{"en-GB":"some text"}')
# description = TJSONField(
# _('description'), null=True, blank=True,
# default=None, help_text='{"en-GB":"some text"}')
slug = models.SlugField(max_length=50, unique=True,
verbose_name=_('Collection slug'), editable=True, null=True)
old_id = models.IntegerField(null=True, blank=True)
rank = models.IntegerField(null=True, default=None)
objects = CollectionQuerySet.as_manager()
class Meta:
@ -108,20 +112,29 @@ class Collection(ProjectBaseMixin, CollectionDateMixin,
@property
def related_object_names(self) -> list:
"""Return related object names."""
raw_object_names = []
for related_object in [related_object.name for related_object in self._related_objects]:
instances = getattr(self, f'{related_object}')
raw_object_names = {}
for related_object in [(related_object.id, related_object.name) for related_object in self._related_objects]:
instances = getattr(self, f'{related_object[1]}')
if instances.exists():
for instance in instances.all():
raw_object_names.append(instance.slug if hasattr(instance, 'slug') else None)
raw_object_names[related_object[0]] = instance.slug if hasattr(instance, 'slug') else None
# parse slugs
object_names = []
related_objects = []
object_names = set()
re_pattern = r'[\w]+'
for raw_name in raw_object_names:
result = re.findall(re_pattern, raw_name)
if result: object_names.append(' '.join(result).capitalize())
return set(object_names)
for object_id in raw_object_names:
result = re.findall(re_pattern, raw_object_names[object_id])
if result:
name = ' '.join(result).capitalize()
if name not in object_names:
related_objects.append({
'id': object_id,
'name': name
})
object_names.add(name)
return related_objects
class GuideTypeQuerySet(models.QuerySet):

View File

@ -7,7 +7,8 @@ from location.models import Country
from location.serializers import CountrySimpleSerializer
from product.models import Product
from utils.exceptions import (
BindingObjectNotFound, RemovedBindingObjectNotFound, ObjectAlreadyAdded
BindingObjectNotFound, ObjectAlreadyAdded,
RemovedBindingObjectNotFound,
)
@ -33,13 +34,14 @@ class CollectionBackOfficeSerializer(CollectionBaseSerializer):
'on_top',
'country',
'country_id',
'block_size',
'description',
# 'block_size',
# 'description',
'slug',
'start',
'end',
# 'start',
# 'end',
'count_related_objects',
'related_object_names',
'rank',
]
@ -68,15 +70,15 @@ class CollectionBindObjectSerializer(serializers.Serializer):
attrs['collection'] = collection
if obj_type == self.ESTABLISHMENT:
establishment = Establishment.objects.filter(pk=obj_id).\
establishment = Establishment.objects.filter(pk=obj_id). \
first()
if not establishment:
raise BindingObjectNotFound()
if request.method == 'POST' and collection.establishments.\
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).\
if request.method == 'DELETE' and not collection. \
establishments.filter(pk=establishment.pk). \
exists():
raise RemovedBindingObjectNotFound()
attrs['related_object'] = establishment
@ -84,10 +86,10 @@ class CollectionBindObjectSerializer(serializers.Serializer):
product = Product.objects.filter(pk=obj_id).first()
if not product:
raise BindingObjectNotFound()
if request.method == 'POST' and collection.products.\
if request.method == 'POST' and collection.products. \
filter(pk=product.pk).exists():
raise ObjectAlreadyAdded()
if request.method == 'DELETE' and not collection.products.\
if request.method == 'DELETE' and not collection.products. \
filter(pk=product.pk).exists():
raise RemovedBindingObjectNotFound()
attrs['related_object'] = product

View File

@ -1,4 +1,5 @@
"""Collection common urlpaths."""
from django.urls import path
from rest_framework.routers import SimpleRouter
from collection.views import back as views
@ -8,3 +9,4 @@ router = SimpleRouter()
router.register(r'', views.CollectionBackOfficeViewSet)
urlpatterns = router.urls

View File

@ -1,5 +1,6 @@
from rest_framework import permissions
from rest_framework import viewsets, mixins
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import mixins, permissions, viewsets
from rest_framework.filters import OrderingFilter
from collection import models
from collection.serializers import back as serializers
@ -31,9 +32,13 @@ class CollectionBackOfficeViewSet(mixins.CreateModelMixin,
permission_classes = (permissions.IsAuthenticated,)
queryset = models.Collection.objects.all()
filter_backends = [DjangoFilterBackend, OrderingFilter]
serializer_class = serializers.CollectionBackOfficeSerializer
bind_object_serializer_class = serializers.CollectionBindObjectSerializer
ordering_fields = ('rank', 'start')
ordering = ('-start', )
def perform_binding(self, serializer):
data = serializer.validated_data
collection = data.pop('collection')