2024-10-28 17:19:47 +02:00
|
|
|
from flask import Flask, request
|
2024-09-24 20:25:06 +03:00
|
|
|
import time
|
2024-10-28 20:10:04 +02:00
|
|
|
import json
|
2024-10-28 17:19:47 +02:00
|
|
|
from app.local_db import LocalDB
|
2024-09-24 20:25:06 +03:00
|
|
|
|
2024-12-27 21:51:19 +02:00
|
|
|
from marshmallow import Schema, fields
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
2024-09-24 20:25:06 +03:00
|
|
|
app = Flask(__name__)
|
2024-12-27 21:51:19 +02:00
|
|
|
app.config.from_pyfile('config.py', silent=True)
|
|
|
|
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
|
|
class UserModel(db.Model):
|
|
|
|
__tablename__ = "user"
|
|
|
|
uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False)
|
|
|
|
name = db.Column(db.String(64), nullable=False)
|
|
|
|
|
|
|
|
records = db.relationship('RecordSchema', backref='user.uuid')
|
|
|
|
|
|
|
|
class CategoryModel(db.Model):
|
|
|
|
__tablename__ = "category"
|
|
|
|
uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False)
|
|
|
|
name = db.Column(db.String(64), nullable=False)
|
|
|
|
|
|
|
|
records = db.relationship('RecordSchema', backref='category.uuid')
|
|
|
|
|
|
|
|
class RecordSchema(db.Model):
|
|
|
|
__tablename__ = "record"
|
|
|
|
uuid = db.Column(db.String(32), primary_key=True, nullable=False)
|
|
|
|
user_uuid = db.Column(db.String(32), db.ForeignKey('user.uuid'))
|
|
|
|
cat_uuid = db.Column(db.String(32), db.ForeignKey('category.uuid'))
|
|
|
|
date = db.Column(db.Date)
|
|
|
|
amount = db.Column(db.Integer)
|
|
|
|
|
|
|
|
class UserSchema(Schema):
|
|
|
|
uuid = fields.Str()
|
|
|
|
name = fields.Str()
|
|
|
|
|
|
|
|
class CategorySchema(Schema):
|
|
|
|
uuid = fields.Str()
|
|
|
|
name = fields.Str()
|
|
|
|
|
|
|
|
class RecordSchema(Schema):
|
|
|
|
uuid = fields.Str()
|
|
|
|
user_uuid = fields.Str()
|
|
|
|
cat_uuid = fields.Str()
|
|
|
|
date = fields.Date()
|
|
|
|
amount = fields.Integer()
|
|
|
|
|
|
|
|
# "migration"
|
|
|
|
with app.app_context():
|
|
|
|
db.create_all()
|
2024-09-24 20:25:06 +03:00
|
|
|
|
|
|
|
@app.route("/healthcheck")
|
|
|
|
def ep_healthcheck():
|
|
|
|
return {
|
|
|
|
"date": time.strftime('%Y.%m.%d %H:%M:%S'),
|
|
|
|
"status": "OK"
|
|
|
|
}
|
2024-10-28 17:19:47 +02:00
|
|
|
|
2024-10-28 17:27:34 +02:00
|
|
|
@app.route("/reset_users_because_postman_is_dumb_like_that")
|
|
|
|
def ep_reset():
|
|
|
|
ldb.reset()
|
|
|
|
return {}
|
|
|
|
|
2024-10-28 17:19:47 +02:00
|
|
|
@app.route("/users", methods = ["GET"])
|
|
|
|
def ep_users_get():
|
|
|
|
return ldb.get_users()
|
|
|
|
|
|
|
|
@app.route("/user/<user_id>", methods = ["GET"])
|
|
|
|
def ep_user_get(user_id):
|
|
|
|
user = ldb.get_user(user_id)
|
|
|
|
|
|
|
|
if 'uuid' in user:
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
return user, 404
|
|
|
|
|
|
|
|
@app.route("/user", methods = ["POST"])
|
|
|
|
def ep_user_post():
|
|
|
|
body = request.json
|
|
|
|
|
|
|
|
if 'name' in body:
|
|
|
|
r = ldb.add_user(body['name'])
|
|
|
|
|
|
|
|
if 'uuid' in r:
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
return r, 403
|
|
|
|
else:
|
|
|
|
return {}, 403
|
|
|
|
|
|
|
|
@app.route("/user/<user_id>", methods = ["DELETE"])
|
|
|
|
def ep_user_delete(user_id):
|
|
|
|
r = ldb.del_user(user_id)
|
|
|
|
|
|
|
|
if 'uuid' in r:
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
return r, 403
|
2024-10-28 18:18:28 +02:00
|
|
|
|
|
|
|
@app.route("/category", methods = ["GET"])
|
|
|
|
def ep_category_get():
|
|
|
|
body = request.json
|
|
|
|
|
|
|
|
if 'uuid' in body:
|
|
|
|
category = ldb.get_category(body['uuid'])
|
|
|
|
|
|
|
|
if 'uuid' in category:
|
|
|
|
return category
|
|
|
|
else:
|
|
|
|
return category, 404
|
|
|
|
else:
|
|
|
|
return {}, 403
|
|
|
|
|
|
|
|
@app.route("/category", methods = ["POST"])
|
|
|
|
def ep_category_post():
|
|
|
|
body = request.json
|
|
|
|
|
|
|
|
if 'name' in body:
|
|
|
|
r = ldb.add_category(body['name'])
|
|
|
|
|
|
|
|
if 'uuid' in r:
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
return r, 403
|
|
|
|
else:
|
|
|
|
return {}, 403
|
|
|
|
|
|
|
|
@app.route("/category", methods = ["DELETE"])
|
|
|
|
def ep_category_delete():
|
|
|
|
body = request.json
|
|
|
|
|
|
|
|
if 'uuid' in body:
|
|
|
|
category = ldb.del_category(body['uuid'])
|
|
|
|
|
|
|
|
if 'uuid' in category:
|
|
|
|
return category
|
|
|
|
else:
|
|
|
|
return category, 404
|
|
|
|
else:
|
|
|
|
return {}, 403
|
2024-10-28 20:10:04 +02:00
|
|
|
|
|
|
|
@app.route("/record/<record_id>", methods = ["GET"])
|
|
|
|
def ep_record_get(record_id):
|
|
|
|
r = ldb.get_record(record_id)
|
|
|
|
|
|
|
|
if 'uuid' in r:
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
return r, 404
|
|
|
|
|
|
|
|
@app.route("/record", methods = ["GET"])
|
|
|
|
def ep_record_get_filtered():
|
|
|
|
options = {}
|
|
|
|
|
|
|
|
if 'user_id' in request.json:
|
|
|
|
options['user_id'] = request.json['user_id']
|
|
|
|
|
|
|
|
if 'cat_id' in request.json:
|
|
|
|
options['cat_id'] = request.json['cat_id']
|
|
|
|
|
|
|
|
if len(list(options.keys())) == 0:
|
|
|
|
return [], 400
|
|
|
|
|
|
|
|
r = ldb.filter_records(options)
|
|
|
|
|
|
|
|
return json.dumps(r)
|
|
|
|
|
|
|
|
@app.route("/record/<record_id>", methods = ["DELETE"])
|
|
|
|
def ep_record_del(record_id):
|
|
|
|
r = ldb.del_record(record_id)
|
|
|
|
|
|
|
|
if 'uuid' in r:
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
return r, 404
|
|
|
|
|
|
|
|
@app.route("/record", methods = ["POST"])
|
|
|
|
def ep_record_post():
|
|
|
|
body = request.json
|
|
|
|
|
|
|
|
if 'user_id' not in body:
|
|
|
|
return {}, 400
|
|
|
|
|
|
|
|
if 'cat_id' not in body:
|
|
|
|
return {}, 400
|
|
|
|
|
|
|
|
if 'amount' not in body:
|
|
|
|
return {}, 400
|
|
|
|
|
|
|
|
r = ldb.add_record(body['user_id'], body['cat_id'], body['amount'])
|
|
|
|
|
|
|
|
if 'uuid' in r:
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
return r, 403
|