Ajout regex pour lien
Merci à Romain pour ces modifications.
This commit is contained in:
parent
8cb5148ac5
commit
4153596e07
1 changed files with 37 additions and 7 deletions
44
app.py
44
app.py
|
@ -1,21 +1,39 @@
|
||||||
from flask import Flask, render_template, request, redirect, url_for, make_response, Markup
|
from flask import Flask, render_template, request, redirect, make_response, \
|
||||||
|
Markup
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
import re
|
||||||
|
|
||||||
|
__author__ = "rick@gnous.eu | Romain"
|
||||||
|
__licence__ = "GPL3 or later"
|
||||||
|
|
||||||
app = Flask('ui', static_url_path="/static")
|
app = Flask('ui', static_url_path="/static")
|
||||||
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ERREUR_LIEN = "Le lien doit être en http ou https !",
|
ERREUR_LIEN = "Le lien doit être en http ou https et valide !"
|
||||||
BON = "Lien ajouté !"
|
BON = "Lien ajouté !"
|
||||||
|
|
||||||
def ecritureFichierHtml(nouvLien, cheminFichier):
|
def ecritureFichierHtml(nouvLien, cheminFichier):
|
||||||
with open(cheminFichier, 'r') as file:
|
with open(cheminFichier, 'r+') as file:
|
||||||
soup = BeautifulSoup(file, 'html.parser')
|
soup = BeautifulSoup(file, 'html.parser')
|
||||||
soup.find("hr").insert_after("", nouvLien)
|
soup.find("hr").insert_after("", nouvLien)
|
||||||
with open(cheminFichier, 'w') as file:
|
file.seek(0)
|
||||||
file.write(soup.prettify())
|
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))
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def slash():
|
def slash():
|
||||||
response = make_response(app.send_static_file("index.html"))
|
response = make_response(app.send_static_file("index.html"))
|
||||||
|
@ -30,16 +48,28 @@ def ajout():
|
||||||
def apropos():
|
def apropos():
|
||||||
return app.send_static_file("apropos.html")
|
return app.send_static_file("apropos.html")
|
||||||
|
|
||||||
|
@app.route("/bizutage", methods=["GET"])
|
||||||
|
def bizutage_redirect():
|
||||||
|
return redirect('/')
|
||||||
|
|
||||||
@app.route("/bizutage", methods=["POST"])
|
@app.route("/bizutage", methods=["POST"])
|
||||||
def bizutage():
|
def bizutage():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
lien = request.values['lien']
|
lien = request.values['lien']
|
||||||
if not (lien.startswith("http") or lien.startswith("https")):
|
if not valideUrl(lien):
|
||||||
return render_template("ajout.html", erreur=Status.ERREUR_LIEN.value)
|
return render_template(
|
||||||
|
"ajout.html",
|
||||||
|
erreur=Status.ERREUR_LIEN.value
|
||||||
|
)
|
||||||
|
|
||||||
titre = Markup.escape(request.values['titre'])
|
titre = Markup.escape(request.values['titre'])
|
||||||
desc = Markup.escape(request.values['desc'])
|
desc = Markup.escape(request.values['desc'])
|
||||||
nouvLien = "<div class=\"elem\"><h2>{}</h2><p><a href=\"{}\">Lien</a></p><hr><p>{}</p>".format(titre, lien, desc)
|
nouvLien = f"""<div class="elem">
|
||||||
|
<h2>{titre}</h2>
|
||||||
|
<p><a href=\"{lien}\">Lien</a></p>
|
||||||
|
<hr>
|
||||||
|
<p>{desc}</p>
|
||||||
|
</div>"""
|
||||||
nouvLienHtml = BeautifulSoup(nouvLien, "html.parser")
|
nouvLienHtml = BeautifulSoup(nouvLien, "html.parser")
|
||||||
|
|
||||||
ecritureFichierHtml(nouvLienHtml, "static/index.html")
|
ecritureFichierHtml(nouvLienHtml, "static/index.html")
|
||||||
|
|
Loading…
Reference in a new issue