def upload_image(image_file, uploader_uri, media_root=None, delete_original=False): """ Upload image media API. """ image_db = Image(image_file=image_file, uploader_uri=uploader_uri) image_db.save() original_image_file = '%s/%s' % (media_root, image_db.image_file.name, ) image_file = process_image(original_image_file) image_file = image_file.replace(media_root, '')[1:] image_db.image_file.name = image_file image_db.save() if delete_original: os.remove(original_image_file) image_uri = _get_image_uri(image_db.id) return get_image(image_uri)
def upload_image(request): if request.method == "POST": from forms import UploadImage form = UploadImage(request.POST, request.FILES) if form.is_valid(): user_id = request.POST.get('user') user = User.objects.get(pk=int(user_id)) new_image = Image( path=request.FILES['file'], title=request.FILES['file'].name, user=user ) new_image.save() from django.conf import settings url = settings.MEDIA_URL + new_image.path.name response = { 'status': 'true', 'url': url } return HttpResponse(json.dumps( response), 200, content_type="application/json") else: print form.errors return HttpResponse(json.dumps( {'status': 'false', 'error': form.errors} ), 401, content_type="application/json") else: return HttpResponse(json.dumps( {'status': 'false'}), 401, content_type="application/json")
# Write the image to disk. # TODO(Varun): This variable gets a name. image_path = settings.MEDIA_ROOT + 'resource_thumbnail/tmp/' + str(key) + "-resource.jpg" localImage = open(image_path, 'w') localImage.write(img_web) localImage.close() final_image = ImageFile(open(image_path)) # Upload new image as media item. new_image = Image( path=final_image, title=final_image.name, user=user ) new_image.save() url = settings.MEDIA_URL + new_image.path.name image['src'] = url # Delete the image from disk. os.remove(image_path) contents = task_body.find('div', class_="taskContents") contents['class'] = 'task-contents' task.append(remove_class(contents)) commentary = task_body.find('div', class_="taskCommentary") commentary['class'] = 'task-commentary' task.append(remove_class(commentary))