configuration humble bundle

This commit is contained in:
Kepka Ludovic
2025-08-11 10:39:58 +02:00
parent 38b2ecc216
commit 84b92f636f
5 changed files with 55 additions and 24 deletions

32
database/helpers.py Normal file
View File

@@ -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)

View File

@@ -22,4 +22,3 @@ CREATE TABLE IF NOT EXISTS `message` (
`text` VARCHAR(256) NULL, `text` VARCHAR(256) NULL,
periodicity INTEGER NULL periodicity INTEGER NULL
); );

View File

@@ -9,7 +9,6 @@ def start_server():
logging.info("Start Web Serveur") logging.info("Start Web Serveur")
from waitress import serve from waitress import serve
serve(webapp, host="0.0.0.0", port=5000) serve(webapp, host="0.0.0.0", port=5000)
# webapp.run()
def start_discord_bot(): def start_discord_bot():
logging.info("Start Discord Bot") logging.info("Start Discord Bot")

View File

@@ -2,27 +2,24 @@ from flask import render_template, request, redirect, url_for
from webapp import webapp from webapp import webapp
from database import db from database import db
from database.models import Configuration from database.models import Configuration
from database.helpers import ConfigurationHelper
from discordbot import bot from discordbot import bot
from discord import TextChannel
@webapp.route("/configurations") @webapp.route("/configurations")
def openConfigurations(): def openConfigurations():
all = Configuration.query.all() all = Configuration.query.all()
configurations = {conf.key: conf for conf in all} channels = []
return render_template("configurations.html", configurations = configurations, channels = bot.get_all_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(): def updateConfiguration():
for key in request.form :
return redirect(url_for('openConfigurations')) 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)
@webapp.route('/configurations/set/<key>', 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)
db.session.commit() db.session.commit()
return redirect(url_for('openConfigurations')) return redirect(url_for('openConfigurations'))

View File

@@ -5,21 +5,25 @@
<h2>Humble Bundle</h2> <h2>Humble Bundle</h2>
<form action="{{ url_for('updateConfiguration') }}" method="POST"> <form action="{{ url_for('updateConfiguration') }}" method="POST">
<label for="humble_bundle_channel">Api Discord (cachée)</label> <label for="humble_bundle_enable">Activer</label>
{{channels}} <input type="checkbox" name="humble_bundle_enable" {% if configuration.getValue('humble_bundle_enable') %}
checked="checked" {% endif %}>
<label>Activer les notifications Humble bundle</label>
<label for="humble_bundle_channel">Canal de notification des pack Humble Bundle</label>
<select name="humble_bundle_channel"> <select name="humble_bundle_channel">
{% for channel in channels %} {% for channel in channels %}
<option value="{{channel.id}}">{{channel.name}}</option> <option value="{{channel.id}}" {% if configuration.getIntValue('humble_bundle_channel')==channel.id %}
selected="selected" {% endif %}>
{{channel.name}}</option>
{% endfor %} {% endfor %}
</select> </select>
<input type="Submit" value="Définir"> <input type="Submit" value="Définir">
</form> </form>
<h2>Api</h2> <h2>Api</h2>
<form action="{{ url_for('setConfiguration', key = 'discord_token') }}" method="POST"> <form action="{{ url_for('updateConfiguration') }}" method="POST">
<label for="value">Api Discord (cachée)</label> <label for="discord_token">Api Discord (cachée)</label>
<input name="value" type="password" /> <input name="discord_token" type="password" />
<input type="Submit" value="Définir"> <input type="Submit" value="Définir">
<p>Nécéssite un redémarrage</p> <p>Nécéssite un redémarrage</p>
</form> </form>