def post_delete(id): #Delete a Study history jwt_username = get_jwt_identity() post_object = Post.query.filter_by(id=id).first() if post_object is None: return abort(401, description=f"There does not exist a post with id {id}") if (jwt_username != post_object.username): return abort( 401, description= f"You are logged in as username: {jwt_username} but the Post id matches to user {post_object.username} " ) # Check the user that wants to delete the Studyhistory post_object = Post.query.filter_by(id=id, username=jwt_username).first() if not post_object: return abort( 401, description=f"Post Id of {id} does not exist for this user.") db.session.delete(post_object) json_object_to_return = jsonify(post_schema.dump(post_object)) db.session.commit() return json_object_to_return
def post_delete(user, id): # Delete a post post = Post.query.filter_by(id=id, user_id=user.id).first() if not post: return abort(400, description="Unauthorized to update this post") db.session.delete(post) db.session.commit() return jsonify(post_schema.dump(post))
def post_update(post_name, user=None): #Update a user post = Post.query.filter_by(post_name=post_name) post_fields = post_schema.load(request.json) if post.count() != 1: return abort(401, description="Unauthorised to update this user") post.update(post_fields) db.session.commit() return jsonify(post_schema.dump(post[0]))
def post_update(user, id): # Update a post post_fields = post_schema.load(request.json) post = Post.query.filter_by(id=id, user_id=user.id) if post.count() != 1: return abort(401, description="Unauthorized to update this post") post_fields['updated_at'] = datetime.now() post.update(post_fields) db.session.commit() return jsonify(post_schema.dump(post[0]))
def post_delete(user, postid): #Delete a User #post = Post.query.filter_by(post_name=post_name).first() post = Post.query.options(joinedload("profile")).filter_by( postid=postid, profile_id=user.id).first() #post = db.session.query(Post).filter(Post.post_name == post_name).first() if not post: return abort(400, description="Unauthorised to delete user") db.session.delete(post) db.session.commit() return jsonify(post_schema.dump(post))
def post_create(user): #Create a new post post_fields = post_schema.load(request.json) new_post = Post() new_post.caption = post_fields["caption"] new_post.created_at = datetime.now() new_post.updated_at = datetime.now() new_post.total_comments = 0 new_post.total_likes = 0 new_post.total_dislikes = 0 user.posts.append(new_post) db.session.commit() return jsonify(post_schema.dump(new_post))
def post_create(): username_of_jwt = get_jwt_identity() user_of_jwt = User.query.get(username_of_jwt) if not user_of_jwt: return abort(404, description="User does not exist") # user_id = get_jwt_identity() post_object_from_fields = Post() post_object_from_fields.username = username_of_jwt post_object_from_fields.content = request.json["content"] db.session.add(post_object_from_fields) db.session.commit() return jsonify(post_schema.dump(post_object_from_fields))
def post_create(user=None): user_id = get_jwt_identity() post_fields = post_schema.load(request.json) profile = Profiles.query.get(user_id) new_post = Post() new_post.post_name = post_fields["post_name"] new_post.post_description = post_fields["post_description"] new_post.account_active = post_fields["account_active"] new_post.front_end = post_fields["front_end"] new_post.back_end = post_fields["back_end"] new_post.full_stack = post_fields["full_stack"] new_post.completed = post_fields["completed"] new_post.post_github = post_fields["post_github"] profile.post.append(new_post) db.session.add(new_post) db.session.commit() return jsonify(post_schema.dump(new_post))
def post_id_like(inputted_id): # Like a particular Post username_of_jwt = get_jwt_identity() user_of_jwt = User.query.get(username_of_jwt) if not user_of_jwt: return abort(404, description="User does not exist") post_retrieved = Post.query.filter_by(id=inputted_id).first() if not post_retrieved: return abort(404, description=f"No post with {inputted_id} exists") like_object = Likes_Table.query.filter( ((Likes_Table.post_id == inputted_id) & (Likes_Table.username_of_liker == username_of_jwt))).first() if like_object: return abort( 404, description= f"Post {inputted_id} has already been liked by user {username_of_jwt}" ) likes_object = Likes_Table() likes_object.post_id = inputted_id likes_object.username_of_liker = username_of_jwt post_retrieved.likes += 1 db.session.add(likes_object) db.session.add(post_retrieved) db.session.commit() return jsonify(post_schema.dump(post_retrieved))
def post_update(id): jwt_username = get_jwt_identity() post_fields = post_schema.load(request.json, partial=True) jwt_user = User.query.get(jwt_username) if not jwt_user: return abort(401, description="Invalid user") post_object = Post.query.filter_by(id=id, username=jwt_username) if post_object.count() != 1: return abort( 401, description=f"Either post id of {id} or the username is wrong") post_object.update(post_fields) db.session.commit() return jsonify(post_schema.dump(post_object[0]))
def post_show(id): # Return a single post post = Post.query.get(id) return jsonify(post_schema.dump(post))
def post_get(id): #Return a single Study history post_object = Post.query.get(id) return jsonify(post_schema.dump(post_object))
def post_show(post_name): #Return a single user post = Post.query.filter_by(post_name=post_name).first() return jsonify(post_schema.dump(post))