2020-09-28 22:25:17 +00:00
|
|
|
from flask import Flask, render_template, request, redirect, make_response, \
|
2020-09-30 22:55:37 +00:00
|
|
|
Markup
|
2020-09-27 20:10:29 +00:00
|
|
|
from enum import Enum
|
2020-09-26 23:32:38 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2020-09-28 22:25:17 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
__author__ = "rick@gnous.eu | Romain"
|
|
|
|
__licence__ = "GPL3 or later"
|
2020-09-26 23:32:38 +00:00
|
|
|
|
|
|
|
app = Flask('ui', static_url_path="/static")
|
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
|
2020-09-27 20:10:29 +00:00
|
|
|
class Status(Enum):
|
2020-09-28 22:25:17 +00:00
|
|
|
ERREUR_LIEN = "Le lien doit être en http ou https et valide !"
|
2020-09-29 09:28:56 +00:00
|
|
|
ERREUR_INCONNUE = "Une erreur inconnue a été rencontrée !"
|
2020-09-27 20:10:29 +00:00
|
|
|
BON = "Lien ajouté !"
|
|
|
|
|
2020-09-29 09:28:56 +00:00
|
|
|
class Manip():
|
|
|
|
def ecritureFichierHtml(nouvLien, cheminFichier):
|
2020-09-30 22:55:37 +00:00
|
|
|
nouvLienHtml = BeautifulSoup(nouvLien, "html.parser")
|
2020-09-29 09:28:56 +00:00
|
|
|
with open(cheminFichier, 'r+') as file:
|
|
|
|
soup = BeautifulSoup(file, 'html.parser')
|
2020-09-30 22:55:37 +00:00
|
|
|
soup.find("hr").insert_after("", nouvLienHtml)
|
2020-09-29 09:28:56 +00:00
|
|
|
file.seek(0)
|
|
|
|
file.write(soup.prettify())
|
|
|
|
|
|
|
|
def valideUrl(url: str) -> bool:
|
|
|
|
# thx django
|
|
|
|
regex = re.compile(
|
|
|
|
r'^(?:http|ftp)s?://' # http:// or https://
|
|
|
|
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
|
|
|
|
r'localhost|' # localhost...
|
|
|
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
|
|
|
|
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
|
|
|
|
r'(?::\d+)?' # optional port
|
|
|
|
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
|
|
|
|
|
|
|
return bool(re.search(regex, url))
|
2020-09-28 22:25:17 +00:00
|
|
|
|
2020-09-26 23:32:38 +00:00
|
|
|
@app.route('/')
|
|
|
|
def slash():
|
2020-09-28 17:49:54 +00:00
|
|
|
response = make_response(app.send_static_file("index.html"))
|
2020-09-27 10:17:06 +00:00
|
|
|
response.headers["Content-Security-Policy"] = "default-src 'self'"
|
|
|
|
return response
|
2020-09-26 23:32:38 +00:00
|
|
|
|
2020-09-30 22:55:37 +00:00
|
|
|
@app.route("/categories/<path:subpath>")
|
|
|
|
def categories(subpath):
|
|
|
|
return app.send_static_file(subpath + ".html")
|
|
|
|
|
2020-09-26 23:32:38 +00:00
|
|
|
@app.route("/ajout")
|
|
|
|
def ajout():
|
|
|
|
return render_template("ajout.html")
|
|
|
|
|
|
|
|
@app.route("/apropos")
|
|
|
|
def apropos():
|
2020-09-28 17:49:54 +00:00
|
|
|
return app.send_static_file("apropos.html")
|
2020-09-26 23:32:38 +00:00
|
|
|
|
2020-09-28 22:25:17 +00:00
|
|
|
@app.route("/bizutage", methods=["GET"])
|
|
|
|
def bizutage_redirect():
|
|
|
|
return redirect('/')
|
|
|
|
|
2020-09-26 23:32:38 +00:00
|
|
|
@app.route("/bizutage", methods=["POST"])
|
|
|
|
def bizutage():
|
2020-09-30 22:55:37 +00:00
|
|
|
lien = request.values["lien"]
|
|
|
|
if not Manip.valideUrl(lien):
|
2020-09-29 09:28:56 +00:00
|
|
|
return render_template(
|
|
|
|
"ajout.html",
|
|
|
|
erreur=Status.ERREUR_LIEN.value
|
|
|
|
)
|
2020-09-27 10:17:06 +00:00
|
|
|
|
2020-09-30 22:55:37 +00:00
|
|
|
titre = Markup.escape(request.values["titre"])
|
|
|
|
desc = Markup.escape(request.values["desc"])
|
|
|
|
categorie = Markup.escape(request.values["categories"])
|
|
|
|
nouvLien = f"""<div class="elem {categorie}">
|
2020-09-29 09:28:56 +00:00
|
|
|
<h2>{titre}</h2>
|
|
|
|
<p><a href=\"{lien}\">Lien</a></p>
|
|
|
|
<hr>
|
|
|
|
<p>{desc}</p>
|
|
|
|
</div>"""
|
2020-09-27 10:44:59 +00:00
|
|
|
|
2020-09-30 22:55:37 +00:00
|
|
|
Manip.ecritureFichierHtml(nouvLien, "static/index.html")
|
|
|
|
Manip.ecritureFichierHtml(nouvLien, "static/" + categorie + ".html")
|
|
|
|
Manip.ecritureFichierHtml(nouvLien, "lite/index.html")
|
|
|
|
Manip.ecritureFichierHtml(nouvLien, "lite/" + categorie + ".html")
|
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()
|