diff --git a/apps/collection/models.py b/apps/collection/models.py index 7acd9991..14bd99c0 100644 --- a/apps/collection/models.py +++ b/apps/collection/models.py @@ -80,6 +80,8 @@ class Collection(ProjectBaseMixin, CollectionDateMixin, 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: diff --git a/apps/collection/serializers/back.py b/apps/collection/serializers/back.py index 6cedb087..f41e3875 100644 --- a/apps/collection/serializers/back.py +++ b/apps/collection/serializers/back.py @@ -40,6 +40,7 @@ class CollectionBackOfficeSerializer(CollectionBaseSerializer): # 'end', 'count_related_objects', 'related_object_names', + 'rank', ] @@ -68,15 +69,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 +85,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 diff --git a/apps/collection/views/back.py b/apps/collection/views/back.py index 65bd6c8b..67548c83 100644 --- a/apps/collection/views/back.py +++ b/apps/collection/views/back.py @@ -1,6 +1,7 @@ from rest_framework import generics from rest_framework import permissions -from rest_framework import viewsets, mixins +from rest_framework import viewsets +from rest_framework.response import Response from collection import models from collection.serializers import back as serializers @@ -49,6 +50,11 @@ class CollectionBackOfficeViewSet(mixins.CreateModelMixin, elif obj_type == self.bind_object_serializer_class.PRODUCT: collection.products.remove(related_object) + def list(self, request, *args, **kwargs): + queryset = self.filter_queryset(self.get_queryset()).order_by('rank', '-start') + serializer = self.get_serializer(queryset, many=True) + return Response(serializer.data) + class CollectionBackOfficeView(generics.GenericAPIView): """ViewS for Collection model."""