|
@@ -1,18 +1,18 @@
|
|
|
import os
|
|
|
from pathlib import Path
|
|
|
-from flask import request, Flask
|
|
|
+from flask import Blueprint, Response, request
|
|
|
from flask_cors import CORS
|
|
|
from datetime import datetime
|
|
|
import json
|
|
|
import hashlib
|
|
|
import time
|
|
|
-from auth import Auth
|
|
|
-from planner_load import PlannerLoad
|
|
|
-from config_load import ConfigLoad
|
|
|
+from webservice.auth import Auth
|
|
|
+from webservice.planner_load import PlannerLoad
|
|
|
+from webservice.config_load import ConfigLoad
|
|
|
|
|
|
-app = Flask(__name__)
|
|
|
+bp = Blueprint('file_io', __name__)
|
|
|
# cors = CORS(app, resources={r"/*": {"origins": "http://localhost:4200/"}})
|
|
|
-CORS(app)
|
|
|
+CORS(bp)
|
|
|
|
|
|
script_dir = Path(__file__).parent
|
|
|
save_dir = script_dir.parent.joinpath('save')
|
|
@@ -22,19 +22,19 @@ config_dir = script_dir.parent.joinpath('config')
|
|
|
user_info = None
|
|
|
|
|
|
|
|
|
-@app.route('/login/', methods=['POST'])
|
|
|
+@bp.route('/login/', methods=['POST'])
|
|
|
def login():
|
|
|
user = request.get_json()['data']['user']
|
|
|
password = request.get_json()['data']['password']
|
|
|
user_info = Auth().get_user(user, password)
|
|
|
|
|
|
- return app.response_class(
|
|
|
+ return Response(
|
|
|
response=json.dumps(user_info),
|
|
|
mimetype='application/json'
|
|
|
)
|
|
|
|
|
|
|
|
|
-@app.route('/list/', methods=['GET'])
|
|
|
+@bp.route('/list', methods=['GET'])
|
|
|
def list_json():
|
|
|
return json.dumps(list_dict(), indent=2)
|
|
|
|
|
@@ -54,12 +54,12 @@ def list_dict():
|
|
|
return result
|
|
|
|
|
|
|
|
|
-@app.route('/load/<year>/<version>/<timestamp>', methods=['GET'])
|
|
|
+@bp.route('/load/<year>/<version>/<timestamp>', methods=['GET'])
|
|
|
def load(year, version, timestamp):
|
|
|
file = full_filename(year, version, timestamp)
|
|
|
|
|
|
if timestamp == 'new':
|
|
|
- return app.response_class(
|
|
|
+ return Response(
|
|
|
response=new_file(year),
|
|
|
mimetype='application/json'
|
|
|
)
|
|
@@ -77,7 +77,7 @@ def load(year, version, timestamp):
|
|
|
p_load = PlannerLoad(planner_dir)
|
|
|
structure = p_load.convert_file(structure)
|
|
|
|
|
|
- return app.response_class(
|
|
|
+ return Response(
|
|
|
response=json.dumps(structure),
|
|
|
mimetype='application/json'
|
|
|
)
|
|
@@ -87,14 +87,14 @@ def full_filename(year, version, timestamp):
|
|
|
return f'{str(save_dir)}/{year}_{version}_{timestamp}.json'
|
|
|
|
|
|
|
|
|
-@app.route('/new/<year>', methods=['GET'])
|
|
|
+@bp.route('/new/<year>', methods=['GET'])
|
|
|
def new_file(year):
|
|
|
p_load = PlannerLoad(planner_dir)
|
|
|
structure = p_load.new_file(year)
|
|
|
return json.dumps(structure)
|
|
|
|
|
|
|
|
|
-@app.route('/load/<filename>', methods=['GET'])
|
|
|
+@bp.route('/load/<filename>', methods=['GET'])
|
|
|
def load_file(filename):
|
|
|
full_filename = f'{str(save_dir)}/{filename}.json'
|
|
|
if not Path(full_filename).exists():
|
|
@@ -103,22 +103,24 @@ def load_file(filename):
|
|
|
full_filename = f'{full_filename[:-5]}_{timestamp}.json'
|
|
|
|
|
|
print(full_filename)
|
|
|
- return open(full_filename, 'r').read()
|
|
|
+ with open(full_filename, 'r') as frh:
|
|
|
+ return frh.read()
|
|
|
|
|
|
|
|
|
-@app.route('/save/<year>/<version>', methods=['POST'])
|
|
|
+@bp.route('/save/<year>/<version>', methods=['POST'])
|
|
|
def save_version(year, version):
|
|
|
return save(year + '_' + version)
|
|
|
|
|
|
|
|
|
-@app.route('/save/<filename>', methods=['POST'])
|
|
|
+@bp.route('/save/<filename>', methods=['POST'])
|
|
|
def save(filename):
|
|
|
if request.method != 'POST':
|
|
|
return 'File is missing!'
|
|
|
|
|
|
new_filename = str(save_dir) + '/' + filename + '_' + datetime.now().strftime('%Y%m%d%H%M%S') + '.json'
|
|
|
data = request.get_json()['data']
|
|
|
- json.dump(data, open(new_filename, 'w'), indent=2)
|
|
|
+ with open(new_filename, 'w') as fwh:
|
|
|
+ json.dump(data, fwh, indent=2)
|
|
|
print(new_filename)
|
|
|
|
|
|
try:
|
|
@@ -134,18 +136,19 @@ def save(filename):
|
|
|
return 'File saved with new data!'
|
|
|
|
|
|
|
|
|
-@app.route('/config', methods=['GET'])
|
|
|
+@bp.route('/config', methods=['GET'])
|
|
|
def config():
|
|
|
cfg = ConfigLoad(str(config_dir))
|
|
|
- return app.response_class(
|
|
|
+ return Response(
|
|
|
response=json.dumps(cfg.load_file('reisacher', '2023')),
|
|
|
mimetype='application/json'
|
|
|
)
|
|
|
|
|
|
|
|
|
-@app.route('/accounts/<period>', methods=['GET'])
|
|
|
+@bp.route('/accounts/<period>', methods=['GET'])
|
|
|
def accounts(period):
|
|
|
- return open(planner_dir.joinpath(f'accounts_{period}.json'), 'r').read()
|
|
|
+ with open(planner_dir.joinpath(f'accounts_{period}.json'), 'r') as frh:
|
|
|
+ return frh.read()
|
|
|
|
|
|
|
|
|
def hash(filename):
|
|
@@ -158,9 +161,3 @@ def hash(filename):
|
|
|
break
|
|
|
sha1.update(hashdata)
|
|
|
return sha1.hexdigest()
|
|
|
-
|
|
|
-
|
|
|
-if __name__ == '__main__':
|
|
|
- app.secret_key = os.urandom(24)
|
|
|
- app.run(host='0.0.0.0', port='8084', debug=True)
|
|
|
- # new_file('2022')
|