diff --git a/database/helpers.py b/database/helpers.py new file mode 100644 index 0000000..319285e --- /dev/null +++ b/database/helpers.py @@ -0,0 +1,32 @@ + +from database import db +from database.models import Configuration + +class ConfigurationHelper: + def getValue(self, key:str) : + conf = Configuration.query.filter_by(key=key).first() + if conf == None: + return None + if (key.endswith('_enable')) : + return conf.value in ['true', '1', 'yes', 'on'] + return conf.value + + def getIntValue(self, key:str) : + conf = Configuration.query.filter_by(key=key).first() + if conf == None: + return 0 + return int(conf.value) + + def createOrUpdate(self, key:str, value) : + conf = Configuration.query.filter_by(key=key).first() + if (key.endswith('_enable')) : + value = value in ['true', '1', 'yes', 'on'] + if conf : + conf.value = value + else : + conf = Configuration(key = key, value = value) + db.session.add(conf) + + + + diff --git a/database/schema.sql b/database/schema.sql index a0c2d59..7a88e61 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -22,4 +22,3 @@ CREATE TABLE IF NOT EXISTS `message` ( `text` VARCHAR(256) NULL, periodicity INTEGER NULL ); - diff --git a/run-web.py b/run-web.py index 009268e..218fe2b 100644 --- a/run-web.py +++ b/run-web.py @@ -9,7 +9,6 @@ def start_server(): logging.info("Start Web Serveur") from waitress import serve serve(webapp, host="0.0.0.0", port=5000) - # webapp.run() def start_discord_bot(): logging.info("Start Discord Bot") diff --git a/webapp/configurations.py b/webapp/configurations.py index 110f5cf..534e533 100644 --- a/webapp/configurations.py +++ b/webapp/configurations.py @@ -2,27 +2,24 @@ from flask import render_template, request, redirect, url_for from webapp import webapp from database import db from database.models import Configuration +from database.helpers import ConfigurationHelper from discordbot import bot +from discord import TextChannel @webapp.route("/configurations") def openConfigurations(): all = Configuration.query.all() - configurations = {conf.key: conf for conf in all} - return render_template("configurations.html", configurations = configurations, channels = bot.get_all_channels()) + channels = [] + for channel in bot.get_all_channels(): + if isinstance(channel, TextChannel): + channels.append(channel) + return render_template("configurations.html", configuration = ConfigurationHelper(), channels = channels) -@webapp.route("/updateConfiguration", methods=['POST']) +@webapp.route("/configurations/update", methods=['POST']) def updateConfiguration(): - - return redirect(url_for('openConfigurations')) - - -@webapp.route('/configurations/set/', methods=['POST']) -def setConfiguration(key): - conf = Configuration.query.filter_by(key=key).first() - if conf : - conf.value = request.form['value'] - else : - conf = Configuration(key = key, value = request.form['value']) - db.session.add(conf) + for key in request.form : + ConfigurationHelper().createOrUpdate(key, request.form.get(key)) + if (request.form.get("humble_bundle_channel") != None and request.form.get("humble_bundle_enable") == None) : + ConfigurationHelper().createOrUpdate('humble_bundle_enable', False) db.session.commit() return redirect(url_for('openConfigurations')) diff --git a/webapp/templates/configurations.html b/webapp/templates/configurations.html index 7d67adc..f2e6038 100644 --- a/webapp/templates/configurations.html +++ b/webapp/templates/configurations.html @@ -5,21 +5,25 @@

Humble Bundle

- - {{channels}} + + + +
-

Api

-
- - + + +

Nécéssite un redémarrage