2020-09-27 20:10:29 +00:00
|
|
|
from flask import Flask, render_template, request, redirect, url_for, make_response, Markup
|
|
|
|
from enum import Enum
|
2020-09-26 23:32:38 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
app = Flask('ui', static_url_path="/static")
|
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
|
2020-09-27 20:10:29 +00:00
|
|
|
class Status(Enum):
|
|
|
|
ERREUR_LIEN = "Le lien doit être en http ou https !",
|
|
|
|
BON = "Lien ajouté !"
|
|
|
|
|
|
|
|
def ecritureFichierHtml(nouvLien, cheminFichier):
|
|
|
|
with open(cheminFichier, 'r') as file:
|
|
|
|
soup = BeautifulSoup(file, 'html.parser')
|
|
|
|
soup.find("hr").insert_after("", nouvLien)
|
|
|
|
with open(cheminFichier, 'w') as file:
|
|
|
|
file.write(soup.prettify())
|
|
|
|
|
2020-09-26 23:32:38 +00:00
|
|
|
@app.route('/')
|
|
|
|
def slash():
|
2020-09-27 10:17:06 +00:00
|
|
|
response = make_response(render_template("index.html"))
|
|
|
|
response.headers["Content-Security-Policy"] = "default-src 'self'"
|
|
|
|
return response
|
2020-09-26 23:32:38 +00:00
|
|
|
|
|
|
|
@app.route("/ajout")
|
|
|
|
def ajout():
|
|
|
|
return render_template("ajout.html")
|
|
|
|
|
|
|
|
@app.route("/apropos")
|
|
|
|
def apropos():
|
|
|
|
return render_template("apropos.html")
|
|
|
|
|
|
|
|
@app.route("/bizutage", methods=["POST"])
|
|
|
|
def bizutage():
|
|
|
|
if request.method == "POST":
|
|
|
|
lien = request.values['lien']
|
2020-09-27 20:10:29 +00:00
|
|
|
if not (lien.startswith("http") or lien.startswith("https")):
|
|
|
|
return render_template("ajout.html", erreur=Status.ERREUR_LIEN.value)
|
|
|
|
|
|
|
|
titre = Markup.escape(request.values['titre'])
|
|
|
|
desc = Markup.escape(request.values['desc'])
|
2020-09-26 23:32:38 +00:00
|
|
|
nouvLien = "<div class=\"elem\"><h2>{}</h2><p><a href=\"{}\">Lien</a></p><hr><p>{}</p>".format(titre, lien, desc)
|
|
|
|
nouvLienHtml = BeautifulSoup(nouvLien, "html.parser")
|
2020-09-27 20:10:29 +00:00
|
|
|
nouvLienHtmlJinja = BeautifulSoup("{% raw %}" + nouvLien + "{% endraw %}", "html.parser")
|
2020-09-27 10:17:06 +00:00
|
|
|
|
2020-09-27 20:10:29 +00:00
|
|
|
ecritureFichierHtml(nouvLienHtmlJinja, "templates/index.html")
|
|
|
|
ecritureFichierHtml(nouvLienHtml, "lite/index.html")
|
2020-09-27 10:44:59 +00:00
|
|
|
|
2020-09-26 23:32:38 +00:00
|
|
|
else:
|
|
|
|
print("error")
|
2020-09-27 20:10:29 +00:00
|
|
|
return render_template("ajout.html", reussi=Status.BON.value)
|
2020-09-26 23:32:38 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run()
|