def signup(request): if request.method == "POST": email = request.POST.get("email") if email: validate_email(email) signup = models.UserSignup(email=email) signup.save() return json_response({"status": OK}) else: return HttpResponseNotAllowed('Method not allowed')
def scenefavorite(request, sceneid): """ NOTE: Needs to be in API """ # Not using login_required decorator because this is primarly used in an # API manner and I want a 403 returned instead of a redirect if not request.user.is_authenticated(): return HttpResponseForbidden() if request.method == "POST": scene = get_object_or_404(models.Scene, pk=sceneid) request.user.scenefavorites.add(scene) request.user.save() return json_response({"status": "OK"}) elif request.method == "DELETE": scene = get_object_or_404(models.Scene, pk=sceneid) request.user.scenefavorites.remove(scene) request.user.save() return json_response({"status": "OK"}) else: return HttpResponse("User scene favorites")
def _delete(): if art.user != request.user: return HttpResponseForbidden("User does not own art") else: try: fq_filename = os.path.join(ART_IMAGE_PATH, art.image.filename) os.remove(fq_filename) except Exception as e: # This is ok. There will be a batch job to remove files no longer # associated with anything print("Error deleting image file: %s" % e) art.delete() return json_response({"status": "OK"})
def artcommentedit(request, commentid): if not request.user.is_authenticated(): resp_data = { "status": "ERR", "message": "User not logged in", } return json_response(resp_data) elif request.method != "POST": return HttpResponseNotAllowed("Method not allowed") comment = get_object_or_404(models.Comment, pk=commentid) if comment.user != request.user: raise HttpResponseForbidden("User is not associated with comment") comment_text = request.POST.get("comment") if not comment_text: raise ValueError("Comment cannot be empty") comment.comment = comment_text comment.save() return json_response({"status": "OK"})
def comment(request, commentid): if not request.user.is_authenticated(): return HttpResponseForbidden() elif request.method != "DELETE": return HttpResponseNotAllowed("Method not allowed") comment = get_object_or_404(models.Comment, pk=commentid) if comment.user != request.user: raise HttpResponseForbidden("User is not associated with comment") comment.datedeleted = timezone.now() comment.save() return json_response({"status": "OK"})
def commentvote(request, commentid): if not request.user.is_authenticated(): return HttpResponseForbidden() elif request.method != "POST": return HttpResponseNotAllowed("Method not allowed") comment = get_object_or_404(models.Comment, pk=commentid) # See if user has commented on art already vote = models.CommentVote.objects.filter( user_id=request.user.id ).filter( comment_id=comment.id ).first() if vote is None: vote = models.CommentVote() vote.user = request.user vote.comment = comment vote.votetype_id = request.POST['upvotetype'] vote.save() return json_response({"status": "OK"})
def scene(request, booktitle): if request.method == "POST": book = models.Book.objects.filter(title_url=booktitle).first() title = request.POST['title'].strip() startpage = request.POST['startPage'].strip() endpage = request.POST['endPage'].strip() text = request.POST['text'].strip() nsfw = True if request.POST["nsfw"] == 'true' else False scene_type = get_object_or_404(models.SceneType, scenetypcd=request.POST['sceneType']) if bool(startpage) != bool(endpage): raise ValueError("If one page is passed so must the other") else: if startpage == "": startpage = None else: startpage = int(startpage) if endpage == "": endpage = None else: endpage = int(endpage) # Validate # Title required for all if not title: raise ValueError("Title cannot be null or empty") if scene_type.scenetypcd == "gnrc": if not text: raise ValueError("Text is required") elif not startpage: raise ValueError("StartPage is required") elif not endpage: raise ValueError("EndPage is required") # Since other scene types might have start/end pages check if startpage and endpage and (startpage > endpage): raise ValueError("Start page must come before end page") new_scene = models.Scene( user=request.user, book=book, scenetype=scene_type, title=title, title_url=string_to_url(title), startpage=startpage, endpage=endpage, text=text, nsfw=nsfw, ) new_scene.save() # Give user some points usertasks.add_point_to_user.delay(request.user.id, "adscn") new_scene_url = request.path + "/" + new_scene.title_url resp_data = { "status": OK, "newSceneUrl": new_scene_url, "sceneid": new_scene.id, } return json_response(resp_data) if request.method == "GET" and request.META.get("CONTENT_TYPE") == "application/json": book = models.Book.objects.filter(title_url=booktitle).first() if not book: resp_data = err_resp("Book not found") else: scenes = [] scenetypcd = request.GET.get("scenetypcd") book_scenes = book.scene.filter(scenetype_id=scenetypcd).all() for scene in book_scenes: scenes.append(scene.dict) resp_data = { "scenes": scenes, "status": OK, } return json_response(resp_data) # Default return HttpResponse("Scene Page")
def _delete(): if scene.user != request.user: return HttpResponseForbidden("User does not own scene") else: scene.delete() return json_response({"status": "OK"})
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")