def delete(cls, comment_id: int): user_id = get_jwt_identity() comment = CommentModel.find_by_comment_id(comment_id) if comment: if comment.user_id == user_id: comment.delete_comment() return {"Message": gettext("comment_delete_successful")}, 201 return {"Message": gettext("comment_not_authorized_to_change_others_comment")}, 401 return {"Message": gettext("comment_not_exist")}, 401
def delete(cls, post_id: int): user_id = get_jwt_identity() posts = PostsModel.find_by_id(post_id) if posts: if posts.user_id == user_id: posts.delete_post() return {"Message": gettext("post_deleted_successful")}, 201 return {"Message": gettext("post_cannot_deleted")}, 401 return {"Message": gettext("post_id_not_found").format(post_id)}, 401
def delete(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return { "Message": gettext("user_id_not_found_error").format(user_id) }, 401 user.delete_from_db() return { "Message": gettext("user_deleted_successful").format(user_id) }, 201
def get(cls, confirmation_id: str): confirmation = ConfirmationModel.find_by_id(confirmation_id) if not confirmation: return {"Message": gettext("confirmation_token_not_found")} if confirmation.expired: return {"Message": gettext("confirmation_token_expired")} if confirmation.is_confirmed: return {"Message": gettext("confirmation_already_confirmed")} confirmation.is_confirmed = True confirmation.insert_in_db() return {"Message": gettext("confirmation_successful")}
def get(cls, desc: str): posts = PostsModel.find_by_desc(desc) if not posts: return { "Message": gettext("post_desc_not_found").format(desc) }, 401 return posts_schemas.dump(posts), 201
def get(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return { "Message": gettext("user_id_not_found_error").format(user_id) }, 401 return user_schema.dump(user), 201
def put(cls, comment_id: int): user_id = get_jwt_identity() comment = CommentModel.find_by_comment_id(comment_id) comment_json = request.get_json() try: comment_data = comment_schema.load(comment_json, partial=("user_id", "post_id")) except ValidationError as err: return err.messages, 401 if comment: if comment.user_id == user_id: comment.comment = comment_data.comment comment.insert_comment() return {"Message": gettext("comment_update_successful")}, 201 return {"Message": gettext("comment_not_authorized_to_change_others_comment")}, 401 return {"Message": gettext("comment_not_exist")}, 401
def post(cls): post_json = request.get_json() try: post_data = post_schema.load(post_json, partial=("user_id", )) except ValidationError as err: return {"Message": err.messages}, 401 user_id = get_jwt_identity() post_data.user_id = user_id post_data.insert_post() return {"Message": gettext("post_created_successful")}
def post(cls): user_json = request.get_json() try: user_data = user_schema.load(user_json, partial=("email", )) except ValidationError as err: return {"Message": err.messages}, 401 user = UserModel.find_by_username(user_data.username) if user and check_password_hash(user.password, user_data.password): confirmation = user.most_recent_confirmation if confirmation and confirmation.is_confirmed: access_token = create_access_token(identity=user.id, fresh=True) refresh_token = create_refresh_token(identity=user.id) return { "access_token": access_token, "refresh_token": refresh_token }, 201 return {"Message": gettext("user_not_confirmed_error")}, 401 return {"Message": gettext("user_invalid_credential_error")}, 401
def put(cls, post_id: int): user_id = get_jwt_identity() post_json = request.get_json() try: post_data = post_schema.load(post_json, partial=( "user_id", "title", )) except ValidationError as err: return {"Message": err.messages}, 401 posts = PostsModel.find_by_id(post_id) if posts: if posts.user_id == user_id: posts.description = post_data.description posts.insert_post() return {"Message": gettext("post_updated_successful")}, 201 return {"Message": gettext("post_cannot_deleted")}, 401 return {"Message": gettext("post_id_not_found").format(post_id)}
def post(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"Message": gettext("confirmation_user_not_found").format(user_id)}, 401 try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.is_confirmed: return {"Message": gettext("confirmation_already_confirmed")}, 401 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.insert_in_db() user.send_confirmation_mail() return {"Message": gettext("confirmation_mail_resend_successful")}, 201 except MailGunException as me: return {"Message": str(me)}, 501 except: traceback.print_exc() return {"Message": gettext("confirmation_mail_resend_failed")}, 401
def get(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"Message": gettext("confirmation_user_not_found").format(user_id)} return ( { "current time": int(time()), "confirmation": [ confirmation_schema.dump(each) for each in user.confirmation.order_by(ConfirmationModel.expired_at) ], }, 200, )
def post(cls): user_json = request.get_json() try: user_data = user_schema.load(user_json) user_data.password = generate_password_hash(user_data.password) print(user_data.password) except ValidationError as err: return err.messages, 401 if UserModel.find_by_username(user_data.username): return { "Message": gettext("user_username_already_exist").format( user_data.username) }, 401 if UserModel.find_by_email(user_data.email): return { "Message": gettext("user_email_already_exist").format(user_data.email) }, 401 try: user_data.insert_in_db() confirmation = ConfirmationModel(user_data.id) confirmation.insert_in_db() user_data.send_confirmation_mail() return {"Message": gettext("user_registration_successful")}, 201 except MailGunException as me: user_data.delete_from_db() return {"Message": gettext("user_email_sending_failed")}, 501 except: traceback.print_exc() return {"Message": gettext("user_internal_server_error")}, 501
def get(cls, post_id: int): comments_data = CommentModel.find_by_post_id(post_id) if comments_data: return comments_schema.dump(comments_data), 201 return {"Message": gettext("comment_post_not_found").format(post_id)}, 401
def get(cls): user_id = get_jwt_identity() posts = PostsModel.find_by_user_id(user_id) if not posts: return {"Message": gettext("post_not_created_yet")}, 401 return posts_schemas.dump(posts), 201
def post(cls): jti = get_raw_jwt()['jti'] BLACKLIST.add(jti) return {"Message": gettext("user_logout_successful")}, 201
def get(cls): n = CommentModel.delete_all() print(n) return {"Message": gettext("comments_deleted_successful")}, 201
def revoked_token_callback(): return jsonify({'description': gettext("app_token_revoked")})
def get(cls, title: str): posts_data = PostsModel.find_by_tile(title) if not posts_data: return {"Message": gettext("post_not_found").format(title)}, 401 return posts_schemas.dump(posts_data), 201