def create(self, validated_data): request = self.context.get("request", None) advertiser = request.user.publisher images_data = validated_data.pop('discount_images') features = validated_data.pop('discount_features') discount = Discount.objects.create(publisher=advertiser, **validated_data) room_images = [] for image in images_data: room_images.append( DiscountImage.objects.create(discount=discount, **image)) Image.compress_list_of_images(room_images) for feature in features: OfferAndDiscountFeature.objects.create(discount=discount, **feature) return discount
def create(self, validated_data): request = self.context.get("request", None) advertiser = request.user.publisher images_data = validated_data.pop('offer_images') plus_items = validated_data.pop('plus_offer') features = validated_data.pop('offer_features') offer = Offer.objects.create(publisher=advertiser, **validated_data) room_images = [] for image in images_data: room_images.append(OfferImage.objects.create(offer=offer, **image)) Image.compress_list_of_images(room_images) for item in plus_items: PlusItem.objects.create(offer=offer, **item) for feature in features: OfferAndDiscountFeature.objects.create(offer=offer, **feature) return offer
def create(self, request): context = dict() required_fields = ['discount', 'images'] error = False for key in required_fields: if key not in request.data.keys(): error = True context[key] = "This field is required." if error: return Response(context, 400) discount_id = request.data['discount'] discounts = Discount.objects.filter(publisher=request.user.publisher) get_object_or_404(discounts, pk=discount_id) for i in range(len(request.data['images'])): request.data['images'][i]['discount'] = discount_id serializer = DiscountImageSerializer( data=request.data['images'], many=True) if serializer.is_valid(): images = serializer.save() Image.compress_list_of_images(images) context['detail'] = "Images uploaded successfully." return Response(context) else: return Response(serializer.errors, 400)
def compress_discount_image(sender, instance, **kwargs): img = Image() for image in instance.discount_images.all(): image.small_image_path = img.compress_image_tinify(image=image.image) image.save()
def clean(self, *args, **kwargs): super().save(*args, **kwargs) img = Image() self.small_image_path = img.compress_image_tinify(image=self.image) super().save(*args, **kwargs)
def compress_plus_item_image(sender, instance, **kwargs): img = Image() instance.small_image_path = img.compress_image_tinify(image=instance.image) instance.save()
def compress_list_of_images(images): for image in images: # img = Image() image.small_image_path = Image.compress_image_tinify(image.image) image.save()