def post(self, *args, **kwargs):
        serializer = UploadMediumSerializer(data=self.request.data)
        serializer.is_valid(raise_exception=True)

        file = self.get_uploaded_file(serializer)
        product = self.get_object()

        max_record = product.media.aggregate(Max('order'))
        order = max_record['order__max'] + 1 if max_record['order__max'] else 1

        if file:
            medium = self.upload_medium(file,
                                        'product',
                                        '{}_{}'.format(
                                            product.pk,
                                            random.randint(10000000,
                                                           99999999)),
                                        content_object=product,
                                        order=order)
        else:
            medium = self.create_embedded_medium(
                serializer.validated_data['embed'],
                content_object=product,
                order=order)

        serializer = MediumSerializer(medium)
        return Response(serializer.data)
Exemple #2
0
    def post(self, *args, **kwargs):
        serializer = MediaReorderSerializer(data=self.request.data)
        serializer.is_valid(raise_exception=True)

        donor = self.get_object()
        medium_ids = serializer.validated_data['media_order']
        order = 1
        for medium_id in medium_ids:
            donor.media.filter(pk=medium_id).update(order=order)
            order += 1

        serializer = MediumSerializer(donor.media.order_by('order'), many=True)
        return Response(serializer.data)
 def get_media(self, obj):
     return MediumSerializer(obj.media.order_by('order'), many=True).data
 def get_media(self, obj):
     return MediumSerializer(obj.media.all(), many=True).data