def update_entry(entry_id): entry = Entry.query.get(entry_id) user = User.query.get(current_user.id) if request.method == 'DELETE': if entry.user_id == current_user.id: db.session.delete(entry) db.session.commit() return 'Deleted Entry' else: return 'Error: could not delete' elif request.method == 'PUT': if entry.user_id == current_user.id: form = EntryForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): if form.data['body_weight']: entry.body_weight = form.data['body_weight'] if form.data['bench_press']: entry.bench_press = form.data['bench_press'] if form.data['squat']: entry.squat = form.data['squat'] if form.data['deadlift']: entry.deadlift = form.data['deadlift'] if form.data['created_at']: entry.created_at = form.data['created_at'] db.session.add(entry) db.session.commit() return entry.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)} else: return 'Error: could not delete'
def update_community(community_id): if community_id == 1: return {"errors": ["The first community cannot be deleted."]} community = Community.query.get(community_id) if request.method == "PUT": form = CreateCommunity() if form.validate_on_submit(): community.name = form["name"].data community.description = form["description"].data db.session.commit() return community.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)} elif request.method == "DELETE": posts = Post.query.filter_by(community_id=community.id).all() for post in posts: post.community_id = 1 db.session.commit() db.session.delete(community) db.session.commit() communities = Community.query.paginate(page=1, per_page=20) return { community.id: community.to_dict() for community in communities.items } return {"errors": ["Invalid Route"]}
def post_by_id(post_id): post = Post.query.get(post_id) if request.method == "GET": return post.to_dict() elif request.method == "PUT": form = CreatePost() if form.validate_on_submit(): form.populate_obj(post) db.session.commit() if "images" in request.files: images = request.files.getlist("images") for image in images: if allowed_file(image.filename): image.filename = get_unique_filename(image.filename) image_url = upload_file_to_s3(image, Config.S3_BUCKET) image = PostsImage(post_id=post.id, image_url=image_url) db.session.add(image) db.session.commit() return post.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)} elif request.method == "DELETE": post.title = "[DELETED]" post.body = "[DELETED]" posts_images = PostsImage.query.filter( PostsImage.post_id == post_id).all() for image in posts_images: db.session.delete(image) db.session.commit() return post.to_dict() return post.to_dict()
def create_retailer(): form = CreateRetailer() if form.validate_on_submit(): retailer = Retailer() form.populate_obj(retailer) db.session.add(retailer) db.session.commit() return retailer.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def create_community(): form = CreateCommunity() if form.validate_on_submit(): community = Community() form.populate_obj(community) db.session.add(community) db.session.commit() return community.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def create_meetup(): form = CreateMeetup() if form.validate_on_submit(): meetup = Meetup() print(request.form.get("date")) form.populate_obj(meetup) db.session.add(meetup) db.session.commit() return meetup.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def create_workout(): fields = json.loads(request.form['exercises']) form = WorkoutForm() form['csrf_token'].data = request.cookies['csrf_token'] while len(form.exercises) > 0: form.exercises.pop_entry() for field in fields: w_e_form = WorkoutsExercisesForm() w_e_form.exercise_id = field['exercise_id'] w_e_form.sets = field['sets'] w_e_form.repetitions = field['repetitions'] form.exercises.append_entry(w_e_form) if form.validate_on_submit(): exercises = [] workout = Workout( workout_name=form.data['workout_name'], created_by=current_user.id ) db.session.add(workout) db.session.flush() obj = workout.to_dict() obj['exercises'] = exercises for idx, field in enumerate(form.exercises): workout_exercise = Workouts_Exercises( workout_id=workout.id, exercise_id=field.exercise_id.data, sets=field.sets.data, repetitions=field.repetitions.data, created_at=datetime.date.today(), updated_at=datetime.date.today(), ) db.session.add(workout_exercise) db.session.flush() exercise = workout_exercise.exercise exercise_dict = exercise.to_dict() workout_exercise_dict = workout_exercise.to_dict() exercise_dict['sets'] = workout_exercise_dict['sets'] exercise_dict['repetitions'] = workout_exercise_dict['repetitions'] exercises.append(exercise_dict) db.session.commit() return jsonify(obj) return {'errors': validation_errors_to_error_messages(form.errors)}
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(): print(form.data) user = User() form.populate_obj(user) db.session.add(user) db.session.commit() login_user(user) return user.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}
def login(): """ Logs a user in """ form = LoginForm() print(request.get_json()) # Get the csrf_token from the request cookie and put it into the # form manually to validate_on_submit can be used form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): # Add the user to the session, we are logged in! user = User.query.filter(User.email == form.data['email']).first() login_user(user) return user.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}, 401
def login(): """ Logs a user in """ form = LoginForm() if form.validate_on_submit(): credential = form.data["credential"] user = User.query.filter( or_(User.email == credential, User.username == credential)).first() login_user(user) return FullUserResponse.from_orm(user).dict() response = UnauthenticatedErrorsResponse( errors=validation_errors_to_error_messages(form.errors)).dict() return response, response["status"]
def create_comment(post_id): post = Post.query.get(post_id) form = CreateComment() if form.validate_on_submit(): comment = Comment() if not form["comment_id"].data: thread = Thread(post_id=post.id) db.session.add(thread) db.session.commit() form["thread_id"].data = thread.id form["path"].data = f"{post.id}" form["level"].data = 1 form.populate_obj(comment) db.session.add(comment) db.session.commit() return comment.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def update_comment(comment_id): comment = Comment.query.get(comment_id) if request.method == "PUT": form = CreateComment() if form.validate_on_submit(): form["thread_id"].data = comment.thread_id form["path"].data = comment.path form["level"].data = comment.level form.populate_obj(comment) db.session.commit() return comment.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)} elif request.method == "DELETE": comment.body = "[DELETED]" db.session.commit() return comment.to_dict() return "Bad route", 404
def sign_up(): """ Creates a new user and logs them in """ form = SignUpForm() if form.validate_on_submit(): user = User( username=form.data["username"], email=form.data["email"], password=form.data["password"], ) db.session.add(user) db.session.commit() login_user(user) return FullUserResponse.from_orm(user).dict() response = UnauthenticatedErrorsResponse( errors=validation_errors_to_error_messages(form.errors)).dict() return response, response["status"]
def create_entry(): user = User.query.get(current_user.id) form = EntryForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): entry = Entry( user_id=user.id, body_weight=form.data['body_weight'], bench_press=form.data['bench_press'], squat=form.data['squat'], deadlift=form.data['deadlift'], created_at=form.data['created_at'], ) db.session.add(entry) db.session.commit() return entry.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}
def create_post(): form = CreatePost() if form.validate_on_submit(): post = Post() form.populate_obj(post) db.session.add(post) db.session.commit() if "images" in request.files: images = request.files.getlist("images") for image in images: if allowed_file(image.filename): image.filename = get_unique_filename(image.filename) image_url = upload_file_to_s3(image, Config.S3_BUCKET) image = PostsImage(post_id=post.id, image_url=image_url) db.session.add(image) db.session.commit() return post.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def rate_post(post_id): post = Post.query.get(post_id) form = CreatePostRating() if form.validate_on_submit(): user_id = form["user_id"].data post_rating = PostRating.query.filter( PostRating.user_id == user_id, PostRating.post_id == post_id).first() if post_rating: post_rating.rating = form["rating"].data db.session.commit() return post.to_dict() post_rating = PostRating() form.populate_obj(post_rating) post_rating.post_id = post_id db.session.add(post_rating) db.session.commit() return post.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def update_or_delete_retailer(retailer_id): retailer = Retailer.query.get(retailer_id) if request.method == "PUT": form = CreateRetailer() if form.validate_on_submit(): retailer.name = form["name"].data retailer.description = form["description"].data retailer.city = form["city"].data retailer.state = form["state"].data db.session.commit() return retailer.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)} elif request.method == "DELETE": if retailer: db.session.delete(retailer) db.session.commit() return {"message": "Delete Successful."} return {"errors": "Invalid Retailer."} return {"errors": "Invalid route."}, 405
def rate_comment(comment_id): comment = Comment.query.get(comment_id) form = CreateCommentRating() if form.validate_on_submit(): user_id = form["user_id"].data comment_rating = CommentRating.query.filter( CommentRating.user_id == user_id, CommentRating.comment_id == comment_id).first() if comment_rating: comment_rating.rating = form["rating"].data db.session.commit() return comment.to_dict() comment_rating = CommentRating() form.populate_obj(comment_rating) comment_rating.comment_id = comment_id db.session.add(comment_rating) db.session.commit() return comment.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def update_meetup(meetup_id): meetup = Meetup.query.get(meetup_id) if request.method == "PUT": form = CreateMeetup() if form.validate_on_submit(): meetup.name = form["name"].data meetup.description = form["description"].data meetup.city = form["city"].data meetup.state = form["state"].data meetup.date = form["date"].data db.session.commit() return meetup.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)} elif request.method == "DELETE": if meetup: db.session.delete(meetup) db.session.commit() return {"message": "Delete Successful"} return {"errors": "Invalid Meetup."} return "Bad route", 404