def attachment_url(request, size='medium'): # the media_file parameter is required, check also for empty string media_file = request.GET.get('media_file','').split("?")[0] # TODO: how to make sure we have the right media file, # this assumes duplicates are the same file result = Attachment.objects.filter(media_file=media_file)[0:1] if result.count() == 0: if "instance" in request.GET.keys(): instance = Instance.objects.filter(uuid=request.GET.get('instance')) result = Attachment.objects.filter(instance=instance, original_name=media_file.split("/")[-1])[0:1] if result.count() == 0: return HttpResponseNotFound(_(u'Attachment not found')) if result.count() == 0: return HttpResponseNotFound(_(u'Attachment not found')) attachment = result[0] if not attachment.mimetype.startswith('image'): return redirect(attachment.media_file.url) try: media_url = image_url(attachment, size) except: # TODO: log this somewhere # image not found, 404, S3ResponseError timeouts pass else: if media_url: return redirect(media_url) return HttpResponseNotFound(_(u'Error: Attachment not found'))
def attachment_url(request, size='medium'): media_file = request.GET.get('media_file') # TODO: how to make sure we have the right media file, # this assumes duplicates are the same file result = Attachment.objects.filter(media_file=media_file)[0:1] if result.count() == 0: return HttpResponseNotFound(_(u'Attachment not found')) attachment = result[0] if attachment.mimetype == '': # guess mimetype mimetype, encoding = mimetypes.guess_type(attachment.media_file.name) if mimetype: attachment.mimetype = mimetype attachment.save() if not attachment.mimetype.startswith('image'): redirect(attachment.media_file.url) try: media_url = image_url(attachment, size) except: # TODO: log this somewhere # image not found, 404, S3ResponseError timeouts pass else: if media_url: if size == 'original': return render_to_response("image.html", {'image': media_url}) return redirect(media_url) return HttpResponseNotFound(_(u'Attachment not found'))
def attachment_url(request, size='medium'): media_file = request.GET.get('media_file') # TODO: how to make sure we have the right media file, # this assumes duplicates are the same file result = Attachment.objects.filter(media_file=media_file)[0:1] if result.count() == 0: return HttpResponseNotFound(_(u'Attachment not found')) attachment = result[0] if not attachment.mimetype.startswith('image'): return redirect(attachment.media_file.url) try: media_url = image_url(attachment, size) except: # TODO: log this somewhere # image not found, 404, S3ResponseError timeouts pass else: if media_url: original_attachment = result[0] original_attachment = '/home/fhuser/formhub/%s' % original_attachment.media_file try: with open(original_attachment, 'rb') as f: return HttpResponse(f.read(), mimetype='image/jpeg') except: # TODO: log this somewhere # image not found, 404, S3ResponseError timeouts pass return HttpResponseNotFound(_(u'Error: Attachment not found'))
def test_thumbnails(self): for attachment in Attachment.objects.filter(instance=self.instance): url = image_url(attachment, 'small') filename = attachment.media_file.name.replace('.jpg', '') thumbnail = '%s-small.jpg' % filename self.assertNotEqual( url.find(thumbnail), -1) for size in ['small', 'medium', 'large']: thumbnail = '%s-%s.jpg' % (filename, size) self.assertTrue( default_storage.exists(thumbnail)) default_storage.delete(thumbnail)
def attachment_url(request, size='medium'): media_file = request.GET.get('media_file') # TODO: how to make sure we have the right media file, # this assumes duplicates are the same file result = Attachment.objects.filter(media_file=media_file)[0:1] if result.count() == 0: return HttpResponseNotFound(_(u'Attachment not found')) attachment = result[0] media_url = image_url(attachment, size) if size == 'original': return render_to_response("image.html", {'image': media_url}) if media_url is None: return HttpResponseNotFound(_(u'Attachment not found')) return redirect(media_url)
def attachment_url(request, size='medium'): media_file = request.GET.get('media_file') # TODO: how to make sure we have the right media file, # this assumes duplicates are the same file result = Attachment.objects.filter(media_file=media_file)[0:1] if result.count() == 0: return HttpResponseNotFound(_(u'Attachment not found')) attachment = result[0] if not attachment.mimetype.startswith('image'): return redirect(attachment.media_file.url) try: media_url = image_url(attachment, size) except: # TODO: log this somewhere # image not found, 404, S3ResponseError timeouts pass else: if media_url: return redirect(media_url) return HttpResponseNotFound(u'Error: Attachment not found')