def new_pub_char(): errors = ["Error creating a public character."] if "image" not in request.files: return {"errors": errors} image = request.files["image"] charactername = request.form['charactername'] characterlabel = request.form['characterlabel'] if check_lengths(charactername, characterlabel): return {"errors": errors} if not allowed_file(image.filename): return {"errors": errors} image.filename = get_unique_filename(image.filename) upload = upload_file(image) if "url" not in upload: return {"errors": errors} url = upload["url"] new_char = PublicCharacter(avatar=url, character_name=charactername, character_label=characterlabel, user_id=current_user.get_id()) db.session.add(new_char) db.session.commit() return {new_char.get_id(): new_char.to_dict()}
def update_pub_char(characterId): errors = ["An error occurred while updating your character."] if "image" not in request.files: return {"errors": errors} image = request.files["image"] charactername = request.form['charactername'] characterlabel = request.form['characterlabel'] if check_lengths(charactername, characterlabel): return {"errors": errors} if not allowed_file(image.filename): return {"errors": errors} image.filename = get_unique_filename(image.filename) upload = upload_file(image) if "url" not in upload: return {"errors": errors} url = upload["url"] old_char = PublicCharacter.query.get(characterId) key = old_char.get_url() if (key.startswith(get_s3_location())): key = key[39:] purge_aws_resource(key) old_char.update_character_data(url, charactername, characterlabel) db.session.add(old_char) db.session.commit() return {old_char.get_id(): old_char.to_dict()}
def create_pri_char(bookId): errors = ["Error creating a private character."] if "image" not in request.files: return {"errors": errors} image = request.files["image"] charactername = request.form['charactername'] characterlabel = request.form['characterlabel'] if len(charactername) == 0 or len(characterlabel) == 0: return {"errors": errors} if not allowed_file(image.filename): return {"errors": errors} image.filename = get_unique_filename(image.filename) upload = upload_file(image) if "url" not in upload: return {"errors": errors} url = upload["url"] new_char = PrivateCharacter(avatar=url, character_name=charactername, character_label=characterlabel, book_id=bookId) db.session.add(new_char) db.session.commit() return {new_char.get_id(): new_char.to_dict()}
def update_user_info(user_id): errors = ["An error occurred while updating your profile."] if "new_avatar" not in request.files: return {"errors": errors} form = UpdateUserForm() name = form.data['new_name'] email = form.data['new_email'] password = form.data['new_password'] bio = form.data['new_bio'] location = form.data['new_location'] birthdate = form.data['new_birthdate'] avatar = request.files["new_avatar"] if not allowed_file(avatar.filename): return {"errors": errors} avatar.filename = get_unique_filename(avatar.filename) form['csrf_token'].data = request.cookies['csrf_token'] if int(user_id) == int(current_user.get_id()): if check_lengths(name, email, password, bio, location, birthdate): return {"errors": errors} if form.validate_on_submit(): upload = upload_file(avatar) if "url" not in upload: return {"errors": errors} url = upload["url"] key = current_user.get_url() if (key.startswith(get_s3_location())): key = key[39:] purge_aws_resource(key) current_user.update_user(name, email, password, bio, location, url, birthdate) db.session.add(current_user) db.session.commit() return {"user": current_user.to_dict()} return {"errors": errors}
def sign_up(): errors = [ "Invalid sign-up, please try again." ] form = SignUpForm() form['csrf_token'].data = request.cookies['csrf_token'] if "image" not in request.files: return { "errors": errors } image = request.files["image"] username = request.form['username'] email = request.form['email'] password = request.form['password'] if not allowed_file(image.filename): return { "errors": errors } image.filename = get_unique_filename(image.filename) if form.validate_on_submit(): upload = upload_file(image) if "url" not in upload: return { "errors": errors } url = upload["url"] user = User(the_search_id=f'{randint(1, 100)}{randint(1, 10000000000)}', user_name=username, email=email, password=password, avatar=url ) db.session.add(user) db.session.commit() login_user(user) return user.to_dict() return { "errors": errors }