164 lines
4.8 KiB
Python
164 lines
4.8 KiB
Python
from flask import Flask, jsonify, request, render_template, session, redirect, url_for
|
|
from functools import wraps
|
|
import requests
|
|
|
|
import markdown
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = "53016ca1157efe86174936227721c701048926d4c31c4531201bcfcff4725277"
|
|
|
|
backend_config = {
|
|
'host': '127.0.0.1',
|
|
'port': '5001'
|
|
}
|
|
|
|
def login_required(func):
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
if 'user' not in session or session['user'] == None:
|
|
return redirect(url_for('login'))
|
|
return func(*args, **kwargs)
|
|
return wrapper
|
|
|
|
|
|
@app.route('/')
|
|
@login_required
|
|
def home():
|
|
return 'Hello, World!'
|
|
|
|
|
|
@app.route('/p/<uuid>', methods=['GET'])
|
|
@login_required
|
|
def package_main(uuid):
|
|
|
|
defined_types = [
|
|
{'value': '5e_class', 'string': '5e Class'},
|
|
{'value': '5e_race', 'string': '5e Race'},
|
|
{'value': '5e_background', 'string': '5e Background'},
|
|
{'value': '5e_subclass', 'string': '5e Subclass'},
|
|
]
|
|
|
|
if uuid == "new":
|
|
response = requests.post(url = f'http://{backend_config["host"]}:{backend_config["port"]}/packages/add')
|
|
if response.status_code == 200 and not response.json()['error']:
|
|
return redirect(f'/p/{response.json()['package']['package_uuid']}')
|
|
|
|
response = requests.get(url = f'http://{backend_config["host"]}:{backend_config["port"]}/packages/get/{uuid}')
|
|
|
|
if response.status_code == 200 and not response.json()['error']:
|
|
package = response.json()['package']
|
|
return render_template('package.html', package_object=package, defined_types=defined_types)
|
|
|
|
return redirect('/')
|
|
|
|
@app.route('/p/<uuid>/save', methods=['POST'])
|
|
def package_save(uuid):
|
|
if request.method != 'POST':
|
|
return jsonify(
|
|
error = True,
|
|
message = f"Incorrect Method attempted; {request.method}."
|
|
)
|
|
|
|
response = requests.post(
|
|
url = f'http://{backend_config["host"]}:{backend_config["port"]}/packages/update',
|
|
json = request.get_json()
|
|
)
|
|
|
|
if response.status_code == 200 and not response.json()['error']:
|
|
return jsonify(
|
|
error=False,
|
|
message=f"{uuid} updated successfully!"
|
|
)
|
|
|
|
return jsonify(
|
|
error=True,
|
|
message=f"Error while trying to update package {uuid}"
|
|
)
|
|
|
|
@app.route('/f/<uuid>', methods=['GET'])
|
|
@login_required
|
|
def feature_main(uuid):
|
|
if uuid == "new":
|
|
response = requests.post(url = f'http://{backend_config["host"]}:{backend_config["port"]}/features/add')
|
|
if response.status_code == 200 and not response.json()['error']:
|
|
return redirect(f'/f/{response.json()['feature']['feature_uuid']}')
|
|
|
|
response = requests.get(url = f'http://{backend_config["host"]}:{backend_config["port"]}/features/get/{uuid}')
|
|
|
|
if response.status_code == 200 and not response.json()['error']:
|
|
feature = response.json()['feature']
|
|
return render_template('feature.html', feature=feature)
|
|
|
|
return redirect('/')
|
|
|
|
@app.route('/f/<uuid>/save', methods=['POST'])
|
|
def feature_save(uuid):
|
|
if request.method != 'POST':
|
|
return jsonify(
|
|
error = True,
|
|
message = f"Incorrect Method attempted; {request.method}."
|
|
)
|
|
|
|
response = requests.post(
|
|
url = f'http://{backend_config["host"]}:{backend_config["port"]}/features/update',
|
|
json = request.get_json()
|
|
)
|
|
|
|
if response.status_code == 200 and not response.json()['error']:
|
|
return jsonify(
|
|
error=False,
|
|
message=f"{uuid} updated successfully!"
|
|
)
|
|
|
|
return jsonify(
|
|
error=True,
|
|
message=f"Error while trying to update feature {uuid}"
|
|
)
|
|
|
|
@app.route('/login', methods=['GET'])
|
|
def login():
|
|
return render_template('login.html')
|
|
|
|
@app.route('/logout', methods=['GET'])
|
|
def logout():
|
|
if request.method != "GET":
|
|
return jsonify(
|
|
error = True,
|
|
message = f"Incorrect Method attempted; {request.method}."
|
|
)
|
|
|
|
if session['user']:
|
|
del session['user']
|
|
|
|
return redirect('/login')
|
|
|
|
|
|
@app.route('/login/authenticate', methods=['POST'])
|
|
def basic_authenticate():
|
|
if request.method != 'POST':
|
|
return jsonify(
|
|
error = True,
|
|
message = f"Incorrect Method attempted; {request.method}."
|
|
)
|
|
|
|
response = requests.post(
|
|
url = f'http://{backend_config["host"]}:{backend_config["port"]}/users/authenticate',
|
|
json = request.get_json()
|
|
)
|
|
|
|
if response.status_code == 200 and not response.json()['error']:
|
|
session['user'] = response.json()['user']
|
|
return jsonify(
|
|
error = False,
|
|
message = f"Successfully Logged in."
|
|
)
|
|
|
|
return jsonify(
|
|
error = True,
|
|
message = f"Username or Password Incorrect!"
|
|
)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |