def post(self, request, pk, **kwargs): review = get_object_or_404(ParkReview, pk=pk, user=request.user) if review.photos.count == 10: return HttpResponseBadRequest('Max photos have been reached') im = Image.open(request.FILES.get('file')) width, height = im.size if width < 720 or height < 540: return HttpResponseBadRequest('Image is too small') else: photo = Photo() photo.park_review = review photo.image = request.FILES.get('file') photo.user = request.user photo.draft = True photo.save() return HttpResponse(photo.id)
def post(self, request, **kwargs): im = Image.open(request.FILES.get('file')) width, height = im.size if width < 720 or height < 540: return HttpResponseBadRequest('Image is too small') else: itinerary = False if 'itinerary_slug' in kwargs: itinerary_slug = self.kwargs.get('itinerary_slug') itineraries = Itinerary.objects.filter(slug=itinerary_slug) itineraries = itineraries.filter(tour_operator=self.request.user.profile.tour_operator) itinerary = itineraries.first() photo = Photo(draft=True) to = self.request.GET.get('to', -1) if not to: to = 0 to = int(to) if to == 1: photo.tour_operator = self.request.user.profile.tour_operator else: photo.user = request.user if itinerary: photo.itinerary = itinerary photo.image = request.FILES.get('file') if photo.image.size > settings.MAX_PHOTO_SIZE: resize_factor = math.sqrt(photo.image.size / settings.MAX_PHOTO_SIZE) new_width = int(photo.image.width / resize_factor) new_height = int(photo.image.height / resize_factor) output_size = (new_width, new_height) img = Image.open(photo.image.path) new_size = img.resize(output_size) new_size.save(photo.image.path) photo.uuid = uuid4().hex photo.save() return Response(photo.id)