def update_profile_photo(): if "image" not in request.files: return {"errors": "image required"}, 400 image = request.files["image"] if not allowed_file(image.filename): return {"errors": "file type not permitted"}, 400 image.filename = get_unique_filename(image.filename) upload = upload_file_to_s3(image) if "url" not in upload: # if the dictionary doesn't have a url key # it means that there was an error when we tried to upload # so we send back that error message return upload, 400 url = upload["url"] # flask_login allows us to get the current user from the request currUser = User.query.get(current_user.id) currUser.profile_photo = url db.session.commit() return currUser.to_dict()
def sign_up(): """ Creates a new user and logs them in """ form = SignUpForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): if "image" not in request.files: url = 'https://myplanits.s3-us-west-1.amazonaws.com/Screen+Shot+2021-03-08+at+4.58.09+PM.png' else: image = request.files["image"] if not allowed_file(image.filename): return {"errors": "file type not permitted"}, 400 image.filename = get_unique_filename(image.filename) upload = upload_file_to_s3(image) if "url" not in upload: upload['url'] = '' url = upload['url'] # url = {'url': ''} # if request.files: # url = upload_file_to_s3(request.files['image']) user = User(first_name=form.data['first_name'], last_name=form.data['last_name'], image_url=url, email=form.data['email'], password=form.data['password']) db.session.add(user) db.session.commit() login_user(user) return user.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}
def add_product_photos(): if "image" not in request.files: return {"errors": "image required"}, 400 image = request.files["image"] if not allowed_file(image.filename): return {"errors": "file type not permitted"}, 400 image.filename = get_unique_filename(image.filename) upload = upload_file_to_s3(image) if "url" not in upload: # if the dictionary doesn't have a url key # it means that there was an error when we tried to upload # so we send back that error message return upload, 400 url = upload["url"] form = PhotoForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): photo = Photo(post_id=form.data["post_id"], media_url=url) db.session.add(photo) db.session.commit() return photo.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}, 401
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 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 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 pic(): if "image" not in request.files: url = '' else: image = request.files["image"] if not allowed_file(image.filename): return {"errors": "file type not permitted"}, 400 image.filename = get_unique_filename(image.filename) upload = upload_file_to_s3(image) if "url" not in upload: upload['url'] = '' url = upload['url'] print('hit.........', url) return {'url': url}
def pic(id): user = User.query.get(id) if "image" not in request.files: url = '' else: image = request.files["image"] if not allowed_file(image.filename): return {"errors": "file type not permitted"}, 400 image.filename = get_unique_filename(image.filename) upload = upload_file_to_s3(image) if "url" not in upload: upload['url'] = '' url = upload['url'] user.image_url = url db.session.commit() return user.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 }