2022-03-14 16:10:00 +00:00
|
|
|
import os
|
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
from apiflask import APIBlueprint, abort
|
2022-03-14 16:10:00 +00:00
|
|
|
|
2022-03-14 16:36:38 +00:00
|
|
|
from db import db
|
2022-03-22 10:21:39 +00:00
|
|
|
from helper_functions import get_user_id_from_username, get_username_or_abort_401, make_response
|
2022-03-17 08:26:25 +00:00
|
|
|
from auth import auth
|
2022-03-22 10:21:39 +00:00
|
|
|
from schema import KeywordResponseSchema, KeywordSchema, DeleteSuccessfulSchema
|
2022-03-14 16:36:38 +00:00
|
|
|
from models import Keyword
|
2022-03-14 16:10:00 +00:00
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
keyword_blueprint = APIBlueprint('keyword', __name__, url_prefix='/api')
|
2022-03-14 16:10:00 +00:00
|
|
|
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
|
|
|
|
|
|
|
|
|
|
|
@keyword_blueprint.route('/keyword', methods=['POST'])
|
2022-03-17 08:26:25 +00:00
|
|
|
@keyword_blueprint.output(KeywordResponseSchema(many=True), 200)
|
|
|
|
@keyword_blueprint.input(schema=KeywordSchema)
|
|
|
|
@keyword_blueprint.auth_required(auth)
|
|
|
|
@keyword_blueprint.doc(summary="Add new keyword", description="Adds new keyword for current user")
|
|
|
|
def add_keyword(data):
|
|
|
|
username = get_username_or_abort_401()
|
2022-03-14 21:57:03 +00:00
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
check_if_keyword_data_exists(data)
|
|
|
|
|
|
|
|
key = data['keyword']
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
check_keyword = db.session.query(Keyword).filter_by(keyword=key, user_id=get_user_id_from_username(username)).first()
|
|
|
|
if check_keyword is None:
|
|
|
|
# Keyword doesn't exist yet for this user
|
|
|
|
new_keyword = Keyword(
|
|
|
|
user_id=get_user_id_from_username(username),
|
|
|
|
keyword=key
|
|
|
|
)
|
|
|
|
db.session.add(new_keyword)
|
|
|
|
db.session.commit()
|
|
|
|
|
2022-03-22 10:21:39 +00:00
|
|
|
return make_response(new_keyword.as_dict(), 200, "Successfully added keyword")
|
2022-03-14 16:10:00 +00:00
|
|
|
else:
|
2022-03-17 08:26:25 +00:00
|
|
|
abort(500, message="Keyword already exist for this user")
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword_blueprint.route('/keyword', methods=['DELETE'])
|
2022-03-17 08:26:25 +00:00
|
|
|
@keyword_blueprint.output(DeleteSuccessfulSchema, 200)
|
|
|
|
@keyword_blueprint.input(schema=KeywordSchema)
|
|
|
|
@keyword_blueprint.auth_required(auth)
|
|
|
|
@keyword_blueprint.doc(summary="Removes existing keyword", description="Removes existing keyword for current user")
|
|
|
|
def remove_keyword(data):
|
|
|
|
username = get_username_or_abort_401()
|
|
|
|
|
|
|
|
check_if_keyword_data_exists(data)
|
2022-03-14 21:57:03 +00:00
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
key = data['keyword']
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
db.session.query(Keyword).filter_by(keyword=key, user_id=get_user_id_from_username(username)).delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
2022-03-22 10:21:39 +00:00
|
|
|
return make_response({}, 200, "Successfully removed keyword")
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword_blueprint.route('/keywords', methods=['GET'])
|
2022-03-17 08:26:25 +00:00
|
|
|
@keyword_blueprint.output(KeywordResponseSchema(many=True), 200)
|
|
|
|
@keyword_blueprint.auth_required(auth)
|
|
|
|
@keyword_blueprint.doc(summary="Returns all keywords", description="Returns all keywords for current user")
|
2022-03-14 16:10:00 +00:00
|
|
|
def get_keywords():
|
2022-03-17 08:26:25 +00:00
|
|
|
username = get_username_or_abort_401()
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
return_keywords = []
|
|
|
|
keywords = db.session.query(Keyword).filter_by(user_id=get_user_id_from_username(username)).all()
|
|
|
|
|
|
|
|
if keywords is not None:
|
|
|
|
for row in keywords:
|
|
|
|
return_keywords.append(row.as_dict())
|
|
|
|
|
2022-03-22 10:21:39 +00:00
|
|
|
return make_response(return_keywords, 200, "Successfully loaded keywords")
|
2022-03-17 08:26:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_if_keyword_data_exists(data):
|
|
|
|
if "keyword" not in data:
|
|
|
|
abort(400, message="Keyword missing")
|
|
|
|
|
|
|
|
if data['keyword'] == "" or data['keyword'] is None:
|
|
|
|
abort(400, message="Keyword missing")
|