def _edit(): if art.user != request.user: return HttpResponseForbidden("User does not own art") # Update Fields art.arttype_id = request.POST['artType'] # Check if title changed art_title = request.POST.get("artTitle", "").strip() if art_title != art.title: if len(art_title) == 0: context = { "userart": art, "error": "Art title cannot be empty", } return render(request, 'cobalt/editart.html', context) art.title = art_title art.title_url = string_to_url(art.title) # Description art.description = request.POST['artText'] # NSFW if request.POST.get("nsfw"): art.nsfw = True else: art.nsfw = False # Image request_file = request.FILES.get('art-image') if request_file: if not image_type_valid(request_file.content_type): context = { "userart": art, "error": "Unsupported file type", } return render(request, 'cobalt/editart.html', context) # Save file to disk filename = art.image.filename fq_filename = os.path.join(ART_IMAGE_PATH, filename) handle_uploaded_image(request_file, fq_filename) art.save() return redirect('/art/%s' % art.id)
def art(request, booktitle): if request.method == "POST": book = models.Book.objects.filter(title_url=booktitle).first() if not book: raise ValueError("Unable to find book with title: %s" % booktitle) request_file = request.FILES['art-image'] art_title = request.POST.get("artTitle", "").strip() # Validate data if not image_type_valid(request_file.content_type): raise ValueError("Unsupported content type") elif len(art_title) == 0: raise ValueError("Title cannot be empty") elif request_file.size > MAX_FILE_SIZE: raise ValueError("File cannot be greater than 25MB") # Create art title_url = string_to_url(request.POST['artTitle']) filename, ext = os.path.splitext(request_file.name) for _ in range(50): # Try 50 times to find a filename filename = "art_image__" + random_string(length=80) + ext fq_filename = os.path.join(ART_IMAGE_PATH, filename) if os.path.exists(fq_filename): if _ == 49: raise ValueError("Error saving image") else: continue else: break # Save file to disk handle_uploaded_image(request_file, fq_filename) # If successful then create an Image and assign to user art_image = models.Image() art_image.imagetype = ART_IMAGE_TYPE art_image.relativepath = fq_filename art_image.filename = filename art_image.save() # Create Art model art = models.Art() art.user = request.user art.arttype_id = request.POST['artType'] art.scene_id = request.POST.get("sceneid") art.book = book art.image = art_image art.title = art_title art.title_url = title_url art.description = request.POST['artText'] art.nsfw = request.POST.get('nsfw', "f")[0] art.save() # # TODO: Need to resize and standardize the iamge with a job # usertasks.add_point_to_user.delay(request.user.id, "adart") art_url = "/book/" + book.title_url + "/art/" + art.title_url; data = { "status": OK, "artURL": art_url, } return json_response(data) else: return HttpResponse("Art Page")