| from flask import Flask, render_template, request, redirect, url_for, flash |
| from flask_sqlalchemy import SQLAlchemy |
| from sqlalchemy import func |
| from bleach import clean |
| from bs4 import BeautifulSoup |
| import os |
| cle = os.environ.get("TOKEN") |
|
|
|
|
|
|
|
|
| app = Flask(__name__) |
| app.config['SQLALCHEMY_DATABASE_URI'] = cle |
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| app.config['SECRET_KEY'] = 'une_cle_secrete_tres_longue' |
|
|
| db = SQLAlchemy(app) |
|
|
| |
|
|
| class Matiere(db.Model): |
| """Modèle représentant une matière.""" |
| __tablename__ = 'matiere' |
| id = db.Column(db.Integer, primary_key=True) |
| nom = db.Column(db.String(64), unique=True, nullable=False) |
| sous_categories = db.relationship('SousCategorie', backref='matiere', lazy='dynamic', cascade="all, delete-orphan") |
|
|
| def __repr__(self): |
| return f"<Matiere {self.nom}>" |
|
|
| class SousCategorie(db.Model): |
| """Modèle représentant une sous-catégorie.""" |
| __tablename__ = 'sous_categorie' |
| id = db.Column(db.Integer, primary_key=True) |
| nom = db.Column(db.String(64), nullable=False) |
| matiere_id = db.Column(db.Integer, db.ForeignKey('matiere.id'), nullable=False) |
| textes = db.relationship('Texte', backref='sous_categorie', lazy='dynamic', cascade="all, delete-orphan") |
| __table_args__ = (db.UniqueConstraint('nom', 'matiere_id', name='_nom_matiere_uc'), {}) |
|
|
| def __repr__(self): |
| return f"<SousCategorie {self.nom}>" |
|
|
| class Texte(db.Model): |
| """Modèle représentant un texte.""" |
| __tablename__ = 'texte' |
| id = db.Column(db.Integer, primary_key=True) |
| titre = db.Column(db.String(128), nullable=False) |
| contenu = db.Column(db.Text, nullable=False) |
| sous_categorie_id = db.Column(db.Integer, db.ForeignKey('sous_categorie.id'), nullable=False) |
|
|
| def __repr__(self): |
| return f"<Texte {self.titre}>" |
|
|
|
|
| |
|
|
| def sanitize_html(html_content): |
| """Assainit le contenu HTML (comme dans ton admin.py).""" |
| allowed_tags = [ |
| 'a', 'abbr', 'acronym', 'b', 'blockquote', 'br', 'code', 'div', 'em', |
| 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'li', 'ol', 'p', |
| 'pre', 'span', 'strong', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'ul' |
| ] |
| allowed_attributes = { |
| '*': ['class', 'id', 'style'], |
| 'a': ['href', 'rel', 'target', 'title'], |
| 'img': ['alt', 'src', 'width', 'height'], |
| 'table': ['border', 'cellpadding', 'cellspacing'] |
| } |
| cleaned_html = clean(html_content, tags=allowed_tags, attributes=allowed_attributes, strip=True) |
| soup = BeautifulSoup(cleaned_html, 'html.parser') |
| for tag in soup.find_all(): |
| if not tag.contents and tag.name not in ['br', 'hr', 'img']: |
| tag.decompose() |
| return str(soup) |
|
|
| |
|
|
| @app.route('/', methods=['GET', 'POST']) |
| def gere(): |
| if request.method == 'POST': |
| |
| action = request.form.get('action') |
|
|
| if action == 'add_matiere': |
| nom_matiere = request.form.get('nom_matiere') |
| if nom_matiere: |
| nouvelle_matiere = Matiere(nom=nom_matiere) |
| db.session.add(nouvelle_matiere) |
| try: |
| db.session.commit() |
| flash(f"Matière '{nom_matiere}' ajoutée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur lors de l'ajout de la matière : {e}", 'danger') |
|
|
| elif action == 'add_sous_categorie': |
| nom_sous_categorie = request.form.get('nom_sous_categorie') |
| matiere_id = request.form.get('matiere_id') |
| if nom_sous_categorie and matiere_id: |
| nouvelle_sous_categorie = SousCategorie(nom=nom_sous_categorie, matiere_id=matiere_id) |
| db.session.add(nouvelle_sous_categorie) |
| try: |
| db.session.commit() |
| flash(f"Sous-catégorie '{nom_sous_categorie}' ajoutée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
| elif action == 'add_texte': |
| titre_texte = request.form.get('titre_texte') |
| contenu_texte = request.form.get('contenu_texte') |
| sous_categorie_id = request.form.get('sous_categorie_id') |
|
|
| if titre_texte and contenu_texte and sous_categorie_id: |
| |
| contenu_texte_sanitized = sanitize_html(contenu_texte) |
| nouveau_texte = Texte(titre=titre_texte, contenu=contenu_texte_sanitized, sous_categorie_id=sous_categorie_id) |
| db.session.add(nouveau_texte) |
| try: |
| db.session.commit() |
| flash(f"Texte '{titre_texte}' ajouté avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur lors de l'ajout : {e}", 'danger') |
|
|
|
|
| elif action.startswith('edit_matiere_'): |
| matiere_id = int(action.split('_')[-1]) |
| matiere = Matiere.query.get_or_404(matiere_id) |
| matiere.nom = request.form.get(f'edit_nom_matiere_{matiere_id}') |
| try: |
| db.session.commit() |
| flash(f"Matière '{matiere.nom}' modifiée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur lors de la modification : {e}", 'danger') |
|
|
| elif action.startswith('edit_sous_categorie_'): |
| sous_categorie_id = int(action.split('_')[-1]) |
| sous_categorie = SousCategorie.query.get_or_404(sous_categorie_id) |
| sous_categorie.nom = request.form.get(f'edit_nom_sous_categorie_{sous_categorie_id}') |
| sous_categorie.matiere_id = request.form.get(f'edit_matiere_id_{sous_categorie_id}') |
| try: |
| db.session.commit() |
| flash(f"Sous-catégorie '{sous_categorie.nom}' modifiée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
|
|
| elif action.startswith('edit_texte_'): |
| texte_id = int(action.split('_')[-1]) |
| texte = Texte.query.get_or_404(texte_id) |
| texte.titre = request.form.get(f'edit_titre_texte_{texte_id}') |
| contenu = request.form.get(f'edit_contenu_texte_{texte_id}') |
| if contenu: |
| texte.contenu = sanitize_html(contenu) |
| texte.sous_categorie_id = request.form.get(f'edit_sous_categorie_id_{texte_id}') |
|
|
| try: |
| db.session.commit() |
| flash(f"Texte '{texte.titre}' modifié avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
|
|
| elif action.startswith('delete_matiere_'): |
| matiere_id = int(action.split('_')[-1]) |
| matiere = Matiere.query.get_or_404(matiere_id) |
| db.session.delete(matiere) |
| try: |
| db.session.commit() |
| flash(f"Matière '{matiere.nom}' supprimée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
| elif action.startswith('delete_sous_categorie_'): |
| sous_categorie_id = int(action.split('_')[-1]) |
| sous_categorie = SousCategorie.query.get_or_404(sous_categorie_id) |
| db.session.delete(sous_categorie) |
| try: |
| db.session.commit() |
| flash(f"Sous-catégorie '{sous_categorie.nom}' supprimée.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
|
|
| elif action.startswith('delete_texte_'): |
| texte_id = int(action.split('_')[-1]) |
| texte = Texte.query.get_or_404(texte_id) |
| db.session.delete(texte) |
| try: |
| db.session.commit() |
| flash(f"Texte '{texte.titre}' supprimé avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}",'danger') |
|
|
| return redirect(url_for('gere')) |
|
|
| |
| matieres = Matiere.query.order_by(func.lower(Matiere.nom)).all() |
| sous_categories = SousCategorie.query.order_by(func.lower(SousCategorie.nom)).all() |
| textes = Texte.query.order_by(Texte.titre).all() |
| return render_template('gere.html', matieres=matieres, sous_categories=sous_categories, textes=textes) |
|
|
| if __name__ == '__main__': |
| with app.app_context(): |
| db.create_all() |
| app.run(debug=True) |
|
|