Merge branch 'feature/back-office-collections' into 'develop'
Feature/back office collections See merge request gm/gm-backend!178
This commit is contained in:
commit
23522eb1f0
27
apps/collection/migrations/0024_auto_20191215_2156.py
Normal file
27
apps/collection/migrations/0024_auto_20191215_2156.py
Normal 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',
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
@ -6,9 +6,10 @@ from django.core.validators import MaxValueValidator, MinValueValidator
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from utils.models import ProjectBaseMixin, URLImageMixin
|
from utils.models import (
|
||||||
from utils.models import TJSONField
|
ProjectBaseMixin, TJSONField, TranslatedFieldsMixin,
|
||||||
from utils.models import TranslatedFieldsMixin
|
URLImageMixin,
|
||||||
|
)
|
||||||
from utils.querysets import RelatedObjectsCountMixin
|
from utils.querysets import RelatedObjectsCountMixin
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -24,7 +25,8 @@ class CollectionNameMixin(models.Model):
|
||||||
|
|
||||||
class CollectionDateMixin(models.Model):
|
class CollectionDateMixin(models.Model):
|
||||||
"""CollectionDate mixin"""
|
"""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,
|
end = models.DateTimeField(blank=True, null=True, default=None,
|
||||||
verbose_name=_('end'))
|
verbose_name=_('end'))
|
||||||
|
|
||||||
|
|
@ -73,13 +75,15 @@ class Collection(ProjectBaseMixin, CollectionDateMixin,
|
||||||
block_size = JSONField(
|
block_size = JSONField(
|
||||||
_('collection block properties'), null=True, blank=True,
|
_('collection block properties'), null=True, blank=True,
|
||||||
default=None, help_text='{"width": "250px", "height":"250px"}')
|
default=None, help_text='{"width": "250px", "height":"250px"}')
|
||||||
description = TJSONField(
|
# description = TJSONField(
|
||||||
_('description'), null=True, blank=True,
|
# _('description'), null=True, blank=True,
|
||||||
default=None, help_text='{"en-GB":"some text"}')
|
# default=None, help_text='{"en-GB":"some text"}')
|
||||||
slug = models.SlugField(max_length=50, unique=True,
|
slug = models.SlugField(max_length=50, unique=True,
|
||||||
verbose_name=_('Collection slug'), editable=True, null=True)
|
verbose_name=_('Collection slug'), editable=True, null=True)
|
||||||
old_id = models.IntegerField(null=True, blank=True)
|
old_id = models.IntegerField(null=True, blank=True)
|
||||||
|
|
||||||
|
rank = models.IntegerField(null=True, default=None)
|
||||||
|
|
||||||
objects = CollectionQuerySet.as_manager()
|
objects = CollectionQuerySet.as_manager()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
@ -108,20 +112,29 @@ class Collection(ProjectBaseMixin, CollectionDateMixin,
|
||||||
@property
|
@property
|
||||||
def related_object_names(self) -> list:
|
def related_object_names(self) -> list:
|
||||||
"""Return related object names."""
|
"""Return related object names."""
|
||||||
raw_object_names = []
|
raw_object_names = {}
|
||||||
for related_object in [related_object.name for related_object in self._related_objects]:
|
for related_object in [(related_object.id, related_object.name) for related_object in self._related_objects]:
|
||||||
instances = getattr(self, f'{related_object}')
|
instances = getattr(self, f'{related_object[1]}')
|
||||||
if instances.exists():
|
if instances.exists():
|
||||||
for instance in instances.all():
|
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
|
# parse slugs
|
||||||
object_names = []
|
related_objects = []
|
||||||
|
object_names = set()
|
||||||
re_pattern = r'[\w]+'
|
re_pattern = r'[\w]+'
|
||||||
for raw_name in raw_object_names:
|
for object_id in raw_object_names:
|
||||||
result = re.findall(re_pattern, raw_name)
|
result = re.findall(re_pattern, raw_object_names[object_id])
|
||||||
if result: object_names.append(' '.join(result).capitalize())
|
if result:
|
||||||
return set(object_names)
|
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):
|
class GuideTypeQuerySet(models.QuerySet):
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,8 @@ from location.models import Country
|
||||||
from location.serializers import CountrySimpleSerializer
|
from location.serializers import CountrySimpleSerializer
|
||||||
from product.models import Product
|
from product.models import Product
|
||||||
from utils.exceptions import (
|
from utils.exceptions import (
|
||||||
BindingObjectNotFound, RemovedBindingObjectNotFound, ObjectAlreadyAdded
|
BindingObjectNotFound, ObjectAlreadyAdded,
|
||||||
|
RemovedBindingObjectNotFound,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -33,13 +34,14 @@ class CollectionBackOfficeSerializer(CollectionBaseSerializer):
|
||||||
'on_top',
|
'on_top',
|
||||||
'country',
|
'country',
|
||||||
'country_id',
|
'country_id',
|
||||||
'block_size',
|
# 'block_size',
|
||||||
'description',
|
# 'description',
|
||||||
'slug',
|
'slug',
|
||||||
'start',
|
# 'start',
|
||||||
'end',
|
# 'end',
|
||||||
'count_related_objects',
|
'count_related_objects',
|
||||||
'related_object_names',
|
'related_object_names',
|
||||||
|
'rank',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -68,15 +70,15 @@ class CollectionBindObjectSerializer(serializers.Serializer):
|
||||||
attrs['collection'] = collection
|
attrs['collection'] = collection
|
||||||
|
|
||||||
if obj_type == self.ESTABLISHMENT:
|
if obj_type == self.ESTABLISHMENT:
|
||||||
establishment = Establishment.objects.filter(pk=obj_id).\
|
establishment = Establishment.objects.filter(pk=obj_id). \
|
||||||
first()
|
first()
|
||||||
if not establishment:
|
if not establishment:
|
||||||
raise BindingObjectNotFound()
|
raise BindingObjectNotFound()
|
||||||
if request.method == 'POST' and collection.establishments.\
|
if request.method == 'POST' and collection.establishments. \
|
||||||
filter(pk=establishment.pk).exists():
|
filter(pk=establishment.pk).exists():
|
||||||
raise ObjectAlreadyAdded()
|
raise ObjectAlreadyAdded()
|
||||||
if request.method == 'DELETE' and not collection.\
|
if request.method == 'DELETE' and not collection. \
|
||||||
establishments.filter(pk=establishment.pk).\
|
establishments.filter(pk=establishment.pk). \
|
||||||
exists():
|
exists():
|
||||||
raise RemovedBindingObjectNotFound()
|
raise RemovedBindingObjectNotFound()
|
||||||
attrs['related_object'] = establishment
|
attrs['related_object'] = establishment
|
||||||
|
|
@ -84,10 +86,10 @@ class CollectionBindObjectSerializer(serializers.Serializer):
|
||||||
product = Product.objects.filter(pk=obj_id).first()
|
product = Product.objects.filter(pk=obj_id).first()
|
||||||
if not product:
|
if not product:
|
||||||
raise BindingObjectNotFound()
|
raise BindingObjectNotFound()
|
||||||
if request.method == 'POST' and collection.products.\
|
if request.method == 'POST' and collection.products. \
|
||||||
filter(pk=product.pk).exists():
|
filter(pk=product.pk).exists():
|
||||||
raise ObjectAlreadyAdded()
|
raise ObjectAlreadyAdded()
|
||||||
if request.method == 'DELETE' and not collection.products.\
|
if request.method == 'DELETE' and not collection.products. \
|
||||||
filter(pk=product.pk).exists():
|
filter(pk=product.pk).exists():
|
||||||
raise RemovedBindingObjectNotFound()
|
raise RemovedBindingObjectNotFound()
|
||||||
attrs['related_object'] = product
|
attrs['related_object'] = product
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
"""Collection common urlpaths."""
|
"""Collection common urlpaths."""
|
||||||
|
from django.urls import path
|
||||||
from rest_framework.routers import SimpleRouter
|
from rest_framework.routers import SimpleRouter
|
||||||
|
|
||||||
from collection.views import back as views
|
from collection.views import back as views
|
||||||
|
|
@ -8,3 +9,4 @@ router = SimpleRouter()
|
||||||
router.register(r'', views.CollectionBackOfficeViewSet)
|
router.register(r'', views.CollectionBackOfficeViewSet)
|
||||||
|
|
||||||
urlpatterns = router.urls
|
urlpatterns = router.urls
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from rest_framework import permissions
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
from rest_framework import viewsets, mixins
|
from rest_framework import mixins, permissions, viewsets
|
||||||
|
from rest_framework.filters import OrderingFilter
|
||||||
|
|
||||||
from collection import models
|
from collection import models
|
||||||
from collection.serializers import back as serializers
|
from collection.serializers import back as serializers
|
||||||
|
|
@ -31,9 +32,13 @@ class CollectionBackOfficeViewSet(mixins.CreateModelMixin,
|
||||||
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
queryset = models.Collection.objects.all()
|
queryset = models.Collection.objects.all()
|
||||||
|
filter_backends = [DjangoFilterBackend, OrderingFilter]
|
||||||
serializer_class = serializers.CollectionBackOfficeSerializer
|
serializer_class = serializers.CollectionBackOfficeSerializer
|
||||||
bind_object_serializer_class = serializers.CollectionBindObjectSerializer
|
bind_object_serializer_class = serializers.CollectionBindObjectSerializer
|
||||||
|
|
||||||
|
ordering_fields = ('rank', 'start')
|
||||||
|
ordering = ('-start', )
|
||||||
|
|
||||||
def perform_binding(self, serializer):
|
def perform_binding(self, serializer):
|
||||||
data = serializer.validated_data
|
data = serializer.validated_data
|
||||||
collection = data.pop('collection')
|
collection = data.pop('collection')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user