2022-07-07 18:15:24 +02:00
|
|
|
from flask import flash, render_template, Blueprint, request, session
|
2023-08-27 18:52:59 +02:00
|
|
|
from create_db import User, get_session, get_engine
|
2022-06-23 15:59:55 +02:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
bp = Blueprint('settings', __name__, url_prefix='/settings')
|
|
|
|
|
|
|
|
def get_settings(user_id):
|
|
|
|
"""get settings from db, turns it into a dict and returns it"""
|
2023-08-27 18:52:59 +02:00
|
|
|
Session = get_session()
|
|
|
|
with Session() as dbsession:
|
2022-07-08 00:36:21 +02:00
|
|
|
user = dbsession.query(User).get(user_id)
|
2023-08-27 18:52:59 +02:00
|
|
|
#tu rešujejo none user bug, ki se pojavi, ko na novo reg user, ni zazznan v querryju
|
|
|
|
# zdaj imamo problem clasha med globalno in lokalno spremenljivko "dbsession"
|
|
|
|
if user == None:
|
|
|
|
user = dbsession.query(User).get(user_id)
|
|
|
|
|
|
|
|
settings_db = user.settings
|
|
|
|
if settings_db == "":
|
|
|
|
#userji se zaenkrat generirajo brez settingsov aka empty string, zato dodamo te defaulte
|
|
|
|
settings_dict = {
|
|
|
|
"max_new" : 5,
|
|
|
|
"max_due" : 5,
|
|
|
|
"max_shared" : 5,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
settings_dict = json.loads(settings_db)
|
|
|
|
|
|
|
|
dbsession.close()
|
2022-07-08 00:36:21 +02:00
|
|
|
|
2023-08-27 18:52:59 +02:00
|
|
|
get_engine().dispose()
|
2022-06-23 15:59:55 +02:00
|
|
|
return settings_dict
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/save_settings", methods=["GET", "POST"])
|
|
|
|
def save_settings():
|
|
|
|
"""takes a dict of settings turns it into json and updates the database with it"""
|
|
|
|
|
2023-08-27 18:52:59 +02:00
|
|
|
|
2022-06-23 15:59:55 +02:00
|
|
|
user_id = session['user_id']
|
|
|
|
username = session['username']
|
|
|
|
|
|
|
|
if request.method == "GET":
|
|
|
|
settings_old = get_settings(user_id)
|
|
|
|
return render_template("settings.html", username=username, settings=settings_old)
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
#request form stuff
|
|
|
|
settings_dict = {
|
|
|
|
"max_new" : request.form.get('max_new', False),
|
|
|
|
"max_due" : request.form.get('max_due', False),
|
|
|
|
"max_shared" : request.form.get('max_shared', False),
|
|
|
|
}
|
2023-08-27 18:52:59 +02:00
|
|
|
Session = get_session()
|
|
|
|
with Session() as dbsession:
|
|
|
|
settings_json = json.dumps(settings_dict)
|
|
|
|
user = dbsession.query(User).get(user_id)
|
|
|
|
|
|
|
|
user.settings = settings_json
|
|
|
|
dbsession.commit()
|
|
|
|
dbsession.close()
|
|
|
|
flash("settings updated")
|
|
|
|
get_engine().dispose()
|
2022-06-23 15:59:55 +02:00
|
|
|
|
|
|
|
return render_template("settings.html", username=username, settings=settings_dict)
|