major: refactoring and creating api structure
Moved the token-based functions in the new API folder and started commenting them. Changed the database_utils function to deal with the new format commented in api/routes.py. Minor adjustments in boot.sh function.
This commit is contained in:
parent
22e45857ff
commit
6cf5a91c8c
@ -45,6 +45,8 @@ def create_app(config_class=Config):
|
||||
app.register_blueprint(errors_bp)
|
||||
from app.main import bp as main_bp
|
||||
app.register_blueprint(main_bp)
|
||||
from app.api import bp as api_bp
|
||||
app.register_blueprint(api_bp, url_prefix="/api")
|
||||
|
||||
return app
|
||||
|
||||
|
||||
5
app/api/__init__.py
Normal file
5
app/api/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
from flask import Blueprint
|
||||
|
||||
bp = Blueprint('api', __name__)
|
||||
|
||||
from app.api import routes
|
||||
39
app/api/routes.py
Normal file
39
app/api/routes.py
Normal file
@ -0,0 +1,39 @@
|
||||
from app import LOGGER
|
||||
from app.main import bp
|
||||
from app.models import LoginToken
|
||||
from app.utils import database_utils
|
||||
from flask import abort, request
|
||||
from flask.json import jsonify
|
||||
|
||||
@bp.route('/auth')
|
||||
def token_authorization():
|
||||
LOGGER.debug("Token Login")
|
||||
if not request.json or 'login' not in request.json:
|
||||
abort(400)
|
||||
if not LoginToken.query.filter_by(token=request.json['login']).first():
|
||||
abort(403)
|
||||
return jsonify({}), 200
|
||||
|
||||
@bp.route('/insert_multiple_items', methods=['POST'])
|
||||
def insert():
|
||||
"""Accepts dictionaries in the following format:
|
||||
{ 'user': <user_token>,
|
||||
'dates': [
|
||||
{ 'date': <date_of_insertion>,
|
||||
'items': [
|
||||
{ 'item_id': <item_id>,
|
||||
'amount': <amount>
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
"""
|
||||
match request.json:
|
||||
case {'user': user, 'dates': dates}:
|
||||
failed = database_utils.insert_bought_items(user, dates)
|
||||
case _:
|
||||
abort(400)
|
||||
if failed:
|
||||
return jsonify(failed), 400
|
||||
return jsonify({'inserted': True}), 201
|
||||
@ -10,12 +10,12 @@ from sqlalchemy import and_, text
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from string import ascii_letters, digits
|
||||
|
||||
def insert_bought_items(token: str, items: dict, date: str = None):
|
||||
if not date:
|
||||
date = dtdate.today()
|
||||
for item, amount in deepcopy(items).items():
|
||||
query_insert = insert(Bought).values(token=token, item=int(item), date=date, amount=int(amount))
|
||||
query_insert = query_insert.on_conflict_do_update("bought_pkey", set_=dict(amount=text(f'bought.amount + {amount}')))
|
||||
def insert_bought_items(token: str, dates: dict):
|
||||
for date in deepcopy(dates):
|
||||
date_index = dates.index(date)
|
||||
for item in deepcopy(date['items']):
|
||||
query_insert = insert(Bought).values(token=token, item=int(item['item_id']), date=date['date'], amount=int(item["amount"]))
|
||||
query_insert = query_insert.on_conflict_do_update("bought_pkey", set_=dict(amount=text(f'bought.amount + {item["amount"]}')))
|
||||
try:
|
||||
db.session.execute(query_insert)
|
||||
db.session.commit()
|
||||
@ -25,8 +25,11 @@ def insert_bought_items(token: str, items: dict, date: str = None):
|
||||
db.session.rollback()
|
||||
LOGGER.exception("")
|
||||
else:
|
||||
del(items[item])
|
||||
return {'user':token, 'date': date, 'items': items} if items else {}
|
||||
item_index = dates[date_index]['items'].index(item)
|
||||
del(dates[date_index]['items'][item_index])
|
||||
if len(dates[date_index]['items']) == 0:
|
||||
del(dates[date_index])
|
||||
return {'user':token, 'dates': date} if date else {}
|
||||
|
||||
def get_report(**kwargs):
|
||||
query_select = db.session.query(bwp.c.token, User.email, bwp.c.date, bwp.c.item, Item.name, bwp.c.amount, bwp.c.price)
|
||||
|
||||
3
boot.sh
3
boot.sh
@ -1,13 +1,12 @@
|
||||
#!/bin/bash
|
||||
source venv/bin/activate
|
||||
while true; do
|
||||
flask db upgrade 05fce74b56cb
|
||||
flask db upgrade 2be4d1ae5493-1
|
||||
if [[ "$?" == "0" ]]; then
|
||||
break
|
||||
fi
|
||||
echo Deploy command failed, retrying in 5 secs...
|
||||
sleep 5
|
||||
done
|
||||
flask db upgrade 2be4d1ae5493
|
||||
flask db upgrade
|
||||
exec gunicorn -b :5000 --access-logfile - --error-logfile - run:app
|
||||
Loading…
x
Reference in New Issue
Block a user