def save_js(request): response = {} response['errormsg'] = {} status = 403 if request.is_ajax() and request.method == 'POST': the_id = int(request.POST['id']) if 'owner_id' in request.POST: owner_id = int(request.POST['owner_id']) if owner_id != request.user.id: if not check_access_rights(owner_id, request.user): return False else: owner_id = request.user.id if 'image' in request.FILES and \ request.FILES['image'].content_type not in ALLOWED_FILETYPES: status = 200 # Not implemented response['errormsg']['error'] = _('Filetype not supported') else: # We only allow owners to change their images. image = Image.objects.filter(pk=the_id, owner=request.user) if image.exists(): image = image[0] status = 200 else: image = Image() image.uploader = request.user image.owner_id = owner_id status = 201 if 'checksum' in request.POST: image.checksum = request.POST['checksum'] image.title = request.POST['title'] if 'imageCat' in request.POST: image.image_cat = request.POST['imageCat'] if 'image' in request.FILES: image.image = request.FILES['image'] if status == 201 and 'image' not in request.FILES: status = 200 response['errormsg']['error'] = _('No file uploaded') else: image.save() response['values'] = { 'pk': image.pk, 'title': image.title, 'image': image.image.url, 'file_type': image.file_type, 'added': mktime(image.added.timetuple()) * 1000, 'checksum': image.checksum, 'cats': image.image_cat.split(',') } if image.thumbnail: response['values']['thumbnail'] = image.thumbnail.url response['values']['height'] = image.height response['values']['width'] = image.width return JsonResponse( response, status=status )
def save_js(request): response = {} response['errormsg'] = {} status = 403 if request.is_ajax() and request.method == 'POST': the_id = int(request.POST['id']) if 'owner_id' in request.POST: owner_id = int(request.POST['owner_id']) if owner_id != request.user.id: if not check_write_access_rights(owner_id, request.user): return False else: owner_id = request.user.id if 'image' in request.FILES and \ request.FILES['image'].content_type not in ALLOWED_FILETYPES: status = 200 # Not implemented response['errormsg']['error'] = _('Filetype not supported') else: # We only allow owners to change their images. image = Image.objects.filter(pk=the_id, owner=request.user) if image.exists(): image = image[0] status = 200 else: image = Image() image.uploader = request.user image.owner_id = owner_id status = 201 if 'checksum' in request.POST: image.checksum = request.POST['checksum'] image.title = request.POST['title'] if 'imageCat' in request.POST: image.image_cat = request.POST['imageCat'] if 'image' in request.FILES: image.image = request.FILES['image'] if status == 201 and 'image' not in request.FILES: status = 200 response['errormsg']['error'] = _('No file uploaded') else: image.save() response['values'] = { 'pk': image.pk, 'title': image.title, 'image': image.image.url, 'file_type': image.file_type, 'added': mktime(image.added.timetuple()) * 1000, 'checksum': image.checksum, 'cats': image.image_cat.split(',') } if image.thumbnail: response['values']['thumbnail'] = image.thumbnail.url response['values']['height'] = image.height response['values']['width'] = image.width return JsonResponse(response, status=status)