def test_upload_size_restriction_works(self, *all_mock_requests):
     self.successful_get_image(*all_mock_requests)
     with self.assertRaises(ImageDownloadException) as context:
         download_image_from_url('http://foo.com/bar.jpg', max_size_bytes=512)
     self.assertEqual(
         text_type(context.exception),
         'The image exceeded the maximum allowed size')
def upload_photo_url(request, person_id):
    person = get_object_or_404(Person, id=person_id)
    image_form = UploadPersonPhotoImageForm(initial={"person": person})
    url_form = UploadPersonPhotoURLForm(request.POST)

    if url_form.is_valid():
        image_url = url_form.cleaned_data["image_url"]
        try:
            img_temp_filename = download_image_from_url(image_url)
        except ImageDownloadException as ide:
            return HttpResponseBadRequest(unicode(ide).encode("utf-8"))
        try:
            queued_image = QueuedImage(
                why_allowed=url_form.cleaned_data["why_allowed_url"],
                justification_for_use=url_form.
                cleaned_data["justification_for_use_url"],
                person=person,
                user=request.user,
            )
            queued_image.save()
            with open(img_temp_filename, "rb") as f:
                queued_image.image.save(image_url, File(f))
            queued_image.save()
            LoggedAction.objects.create(
                user=request.user,
                action_type="photo-upload",
                ip_address=get_client_ip(request),
                popit_person_new_version="",
                person=person,
                source=url_form.cleaned_data["justification_for_use_url"],
            )
            return HttpResponseRedirect(
                reverse("photo-upload-success",
                        kwargs={"person_id": person.id}))
        finally:
            os.remove(img_temp_filename)
    else:
        return render(
            request,
            "moderation_queue/photo-upload-new.html",
            {
                "image_form":
                image_form,
                "url_form":
                url_form,
                "queued_images":
                QueuedImage.objects.filter(
                    person=person, decision="undecided").order_by("created"),
                "person":
                person,
            },
        )
Beispiel #3
0
def upload_photo_url(request, person_id):
    person = get_object_or_404(Person, id=person_id)
    image_form = UploadPersonPhotoImageForm(initial={'person': person})
    url_form = UploadPersonPhotoURLForm(request.POST)

    if url_form.is_valid():
        image_url = url_form.cleaned_data['image_url']
        try:
            img_temp_filename = download_image_from_url(image_url)
        except ImageDownloadException as ide:
            return HttpResponseBadRequest(unicode(ide).encode('utf-8'))
        try:
            queued_image = QueuedImage(
                why_allowed=url_form.cleaned_data['why_allowed_url'],
                justification_for_use=url_form.
                cleaned_data['justification_for_use_url'],
                person=person,
                user=request.user)
            queued_image.save()
            with open(img_temp_filename, 'rb') as f:
                queued_image.image.save(image_url, File(f))
            queued_image.save()
            LoggedAction.objects.create(
                user=request.user,
                action_type='photo-upload',
                ip_address=get_client_ip(request),
                popit_person_new_version='',
                person=person,
                source=url_form.cleaned_data['justification_for_use_url'],
            )
            return HttpResponseRedirect(
                reverse('photo-upload-success',
                        kwargs={'person_id': person.id}))
        finally:
            os.remove(img_temp_filename)
    else:
        return render(
            request, 'moderation_queue/photo-upload-new.html', {
                'image_form':
                image_form,
                'url_form':
                url_form,
                'queued_images':
                QueuedImage.objects.filter(
                    person=person,
                    decision='undecided',
                ).order_by('created'),
                'person':
                person
            })