From bb2b1c44d032c99d289a4ac7c50f29236b8b4229 Mon Sep 17 00:00:00 2001 From: Kostanjevec Date: Thu, 23 Jun 2022 15:59:55 +0200 Subject: [PATCH] added some settings functionality that's stored in db as json --- settings.py | 59 +++++++++++++++++++++++++++++++++++++++++ templates/settings.html | 17 ++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 settings.py create mode 100644 templates/settings.html diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..98c6d3f --- /dev/null +++ b/settings.py @@ -0,0 +1,59 @@ +from flask import flash, redirect, render_template, url_for, Blueprint, request, session +from create_db import User, get_session +import json + + +bp = Blueprint('settings', __name__, url_prefix='/settings') +dbsession = get_session() + +def get_settings(user_id): + """get settings from db, turns it into a dict and returns it""" + + 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) + + print("this is it: ", settings_dict) + 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""" + + 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": + print('this is the form ', request.form) + + #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), + } + + print('this is the dict: ', settings_dict) + settings_json = json.dumps(settings_dict) + print('this is the json: ', settings_json) + user = dbsession.query(User).get(user_id) + + user.settings = settings_json + dbsession.commit() + dbsession.close() + flash("settings updated") + + return render_template("settings.html", username=username, settings=settings_dict) \ No newline at end of file diff --git a/templates/settings.html b/templates/settings.html new file mode 100644 index 0000000..2e4c4ff --- /dev/null +++ b/templates/settings.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} +quantity +{% block header %} + Settings +{% endblock %} + +{% block content %} +
+ + + + + + + +
+{% endblock %} \ No newline at end of file