This commit is contained in:
commit
d6f3e5cb1a
|
@ -0,0 +1,122 @@
|
|||
from flask import Flask, render_template, request, jsonify
|
||||
import sqlite3
|
||||
import random
|
||||
import string
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
app = Flask(__name__)
|
||||
app.debug = True
|
||||
app.config['SECRET_KEY'] = 'your-secret-key'
|
||||
|
||||
# Fonction pour générer un code aléatoire
|
||||
def generate_code():
|
||||
code = ''.join(random.choices(string.digits, k=6))
|
||||
return code
|
||||
|
||||
# Fonction pour créer une connexion à la base de données
|
||||
def get_db_connection():
|
||||
conn = sqlite3.connect('database.db')
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
||||
# Créer la table "codes" si elle n'existe pas
|
||||
conn.execute('''
|
||||
CREATE TABLE IF NOT EXISTS codes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
code TEXT NOT NULL,
|
||||
comment TEXT,
|
||||
active INTEGER,
|
||||
expiration TIMESTAMP
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
|
||||
return conn
|
||||
|
||||
# Route de la page d'accueil
|
||||
@app.route('/')
|
||||
def home():
|
||||
conn = get_db_connection()
|
||||
codes = conn.execute('SELECT * FROM codes').fetchall()
|
||||
conn.close()
|
||||
return render_template('index.html', codes=codes)
|
||||
|
||||
# Route pour générer un nouveau code
|
||||
@app.route('/generate_code', methods=['POST'])
|
||||
def generate_code_route():
|
||||
comment = request.form['comment']
|
||||
eternal = request.form.get('eternal', False)
|
||||
|
||||
conn = get_db_connection()
|
||||
code = generate_code()
|
||||
expiration = None if eternal else (datetime.now() + timedelta(days=365))
|
||||
conn.execute('INSERT INTO codes (code, comment, active, expiration) VALUES (?, ?, 1, ?)', (code, comment, expiration))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return 'Code généré : ' + code
|
||||
|
||||
# Route pour désactiver ou supprimer un code
|
||||
@app.route('/update_code', methods=['POST'])
|
||||
def update_code():
|
||||
code_id = request.form['code_id']
|
||||
action = request.form['action']
|
||||
|
||||
conn = get_db_connection()
|
||||
if action == 'disable':
|
||||
conn.execute('UPDATE codes SET active = 0 WHERE id = ?', (code_id,))
|
||||
elif action == 'delete':
|
||||
conn.execute('DELETE FROM codes WHERE id = ?', (code_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return 'OK'
|
||||
|
||||
@app.route('/process_action', methods=['POST'])
|
||||
def process_action():
|
||||
group_action = request.form.get('group_action')
|
||||
selected_codes = request.form.getlist('selected_codes')
|
||||
conn = get_db_connection()
|
||||
if group_action == 'disable_selected':
|
||||
# Traiter l'action de désactivation pour les codes sélectionnés
|
||||
for code_id in selected_codes:
|
||||
conn.execute('UPDATE codes SET active = 0 WHERE id = ?', (code_id,))
|
||||
|
||||
elif group_action == 'delete_selected':
|
||||
# Traiter l'action de suppression pour les codes sélectionnés
|
||||
for code_id in selected_codes:
|
||||
conn.execute('DELETE FROM codes WHERE id = ?', (code_id,))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
|
||||
# Route pour vérifier un code
|
||||
@app.route('/key/<code>')
|
||||
def verify_code(code):
|
||||
conn = get_db_connection()
|
||||
result = conn.execute('SELECT * FROM codes WHERE code = ? AND active = 1', (code,)).fetchone()
|
||||
|
||||
if result is None or not result['active']:
|
||||
unlock = False
|
||||
elif result["expiration"] is None:
|
||||
unlock = True
|
||||
else:
|
||||
unlock = True
|
||||
expiration = datetime.strptime(result["expiration"], '%Y-%m-%d %H:%M:%S.%f')
|
||||
if expiration > datetime.now() + timedelta(minutes=5):
|
||||
expiration = datetime.now() + timedelta(minutes=5)
|
||||
conn.execute('UPDATE codes SET expiration = ? WHERE code = ?', (expiration, code))
|
||||
conn.commit()
|
||||
elif expiration < datetime.now():
|
||||
conn.execute('UPDATE codes SET active = 0 WHERE code = ?', (code,))
|
||||
conn.commit()
|
||||
unlock = False
|
||||
conn.close()
|
||||
|
||||
return jsonify({'unlock': unlock})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
flask
|
|
@ -0,0 +1,101 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Gestion codes déverouillage "Boite aux colis"</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1 class="text-center">Gestion codes déverouillage "Boite aux colis"</h1>
|
||||
|
||||
<form method="POST" action="/generate_code">
|
||||
<div class="mb-3">
|
||||
<label for="comment" class="form-label">Commentaire:</label>
|
||||
<input type="text" id="comment" name="comment" class="form-control">
|
||||
</div>
|
||||
<div class="mb-3 form-check">
|
||||
<input type="checkbox" id="eternal" name="eternal" class="form-check-input">
|
||||
<label for="eternal" class="form-check-label">Eternel</label>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Générer un code</button>
|
||||
</form>
|
||||
|
||||
<h2>Codes actifs</h2>
|
||||
<form method="POST" action="/process_action">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Code</th>
|
||||
<th>Commentaire</th>
|
||||
<th>Date d'expiration</th>
|
||||
<th>Actions</th>
|
||||
<th>Sélectionner</th> <!-- Nouvelle colonne pour les cases à cocher -->
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for code in codes %}
|
||||
{% if code['active'] %}
|
||||
<tr>
|
||||
<td>{{ code['code'] }}</td>
|
||||
<td>{{ code['comment'] }}</td>
|
||||
<td>{{ code['expiration'] }}</td>
|
||||
<td>
|
||||
<form method="POST" action="/update_code" class="d-inline">
|
||||
<input type="hidden" name="code_id" value="{{ code['id'] }}">
|
||||
<button type="submit" name="action" value="disable" class="btn btn-warning">Désactiver</button>
|
||||
<button type="submit" name="action" value="delete" class="btn btn-danger">Supprimer</button>
|
||||
</form>
|
||||
</td>
|
||||
<td>
|
||||
<input type="checkbox" name="selected_codes" value="{{ code['id'] }}">
|
||||
</td> <!-- Nouvelle colonne avec la case à cocher -->
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="submit" name="group_action" value="disable_selected" class="btn btn-warning">Désactiver sélectionnés</button>
|
||||
<button type="submit" name="group_action" value="delete_selected" class="btn btn-danger">Supprimer sélectionnés</button>
|
||||
</form>
|
||||
|
||||
|
||||
<h2>Codes inactifs</h2>
|
||||
<form method="POST" action="/process_action">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Code</th>
|
||||
<th>Commentaire</th>
|
||||
<th>Date d'expiration</th>
|
||||
<th>Actions</th>
|
||||
<th>Sélectionner</th> <!-- Nouvelle colonne pour les cases à cocher -->
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for code in codes %}
|
||||
{% if not code['active'] %}
|
||||
<tr>
|
||||
<td>{{ code['code'] }}</td>
|
||||
<td>{{ code['comment'] }}</td>
|
||||
<td>{{ code['expiration'] }}</td>
|
||||
<td>
|
||||
<form method="POST" action="/update_code" class="d-inline">
|
||||
<input type="hidden" name="code_id" value="{{ code['id'] }}">
|
||||
<button type="submit" name="action" value="disable" class="btn btn-warning">Désactiver</button>
|
||||
<button type="submit" name="action" value="delete" class="btn btn-danger">Supprimer</button>
|
||||
</form>
|
||||
</td>
|
||||
<td>
|
||||
<input type="checkbox" name="selected_codes" value="{{ code['id'] }}">
|
||||
</td> <!-- Nouvelle colonne avec la case à cocher -->
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="submit" name="group_action" value="disable_selected" class="btn btn-warning">Désactiver sélectionnés</button>
|
||||
<button type="submit" name="group_action" value="delete_selected" class="btn btn-danger">Supprimer sélectionnés</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue