diff --git a/royalnet/database/tables/bios.py b/royalnet/database/tables/bios.py index 4636b890..82176c7c 100644 --- a/royalnet/database/tables/bios.py +++ b/royalnet/database/tables/bios.py @@ -2,7 +2,7 @@ from sqlalchemy import Column, \ Integer, \ Text, \ ForeignKey -from sqlalchemy.orm import relationship +from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declared_attr from .royals import Royal @@ -16,11 +16,11 @@ class Bio: @declared_attr def royal(self): - return relationship("Royal") + return relationship("Royal", backref=backref("bio", uselist=False)) @declared_attr def contents(self): - return Column(Text, unique=True, nullable=False) + return Column(Text, nullable=False, default="") def __repr__(self): return f"" diff --git a/royalnet/database/tables/wikipages.py b/royalnet/database/tables/wikipages.py index 0ea21aea..f3b0bc1a 100644 --- a/royalnet/database/tables/wikipages.py +++ b/royalnet/database/tables/wikipages.py @@ -22,7 +22,7 @@ class WikiPage: return Column(String, nullable=False) @declared_attr - def content(self): + def contents(self): return Column(Text) @declared_attr diff --git a/royalnet/utils/wikirender.py b/royalnet/utils/wikirender.py new file mode 100644 index 00000000..32e385c9 --- /dev/null +++ b/royalnet/utils/wikirender.py @@ -0,0 +1,29 @@ +import re +import markdown2 + + +class RenderError(Exception): + """An error occurred while trying to render the page.""" + + +def prepare_page_markdown(markdown): + if list(markdown).count(">") > 99: + raise RenderError("Too many nested quotes") + converted_md = markdown2.markdown(markdown.replace("<", "<"), + extras=["spoiler", "tables", "smarty-pants", "fenced-code-blocks"]) + converted_md = re.sub(r"{https?://(?:www\.)?(?:youtube\.com/watch\?.*?&?v=|youtu.be/)([0-9A-Za-z-]+).*?}", + r'
' + r' ' + r'
', converted_md) + converted_md = re.sub(r"{https?://clyp.it/([a-z0-9]+)}", + r'
' + r' ' + r'
', converted_md) + return converted_md diff --git a/royalnet/web/royalprints/diarioview/templates/diarioview_page.html b/royalnet/web/royalprints/diarioview/templates/diarioview_page.html index b8c09ead..ef7ea435 100644 --- a/royalnet/web/royalprints/diarioview/templates/diarioview_page.html +++ b/royalnet/web/royalprints/diarioview/templates/diarioview_page.html @@ -23,7 +23,7 @@
— {% if entry.quoted_account %} - {{ entry.quoted }} + {{ entry.quoted }} {% else %} {{ entry.quoted }} {% endif %} @@ -33,7 +33,7 @@
{% endif %} {% if entry.creator %} -
Salvato da {{ entry.creator.username }}
+
Salvato da {{ entry.creator.username }}
{% endif %} #{{ entry.diario_id }} diff --git a/royalnet/web/royalprints/profile/__init__.py b/royalnet/web/royalprints/profile/__init__.py index 46d54dd0..252b2a5a 100644 --- a/royalnet/web/royalprints/profile/__init__.py +++ b/royalnet/web/royalprints/profile/__init__.py @@ -5,13 +5,14 @@ import os from ...royalprint import Royalprint from ...shortcuts import error from ....database.tables import * +from ....utils.wikirender import prepare_page_markdown, RenderError # Maybe some of these tables are optional... tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') rp = Royalprint("profile", __name__, url_prefix="/profile", template_folder=tmpl_dir, required_tables={Royal, ActiveKvGroup, Alias, Diario, Discord, Keygroup, Keyvalue, Telegram, WikiPage, - WikiRevision}) + WikiRevision, Bio}) @rp.route("/") @@ -22,9 +23,39 @@ def profile_index(): @rp.route("/") -def profile_by_username(username): +def profile_page(username): alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] royal = alchemy_session.query(alchemy.Royal).filter_by(username=username).one_or_none() if royal is None: return error(404, "Non esiste nessun utente con l'username richiesto.") - return f.render_template("profile_page.html", royal=royal) + if royal.bio is not None and royal.bio.contents != "": + try: + parsed_bio = f.Markup(prepare_page_markdown(royal.bio.contents)) + except RenderError as e: + return error(500, f"Il profilo non può essere visualizzato a causa di un errore nella bio: {str(e)}") + else: + parsed_bio = None + return f.render_template("profile_page.html", royal=royal, parsed_bio=parsed_bio) + + +@rp.route("//editbio", methods=["GET", "POST"]) +def profile_editbio(username): + if "royal" not in f.session: + return error(403, "Devi aver effettuato il login per modificare una bio.") + alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] + royal = alchemy_session.query(alchemy.Royal).filter_by(username=username).one_or_none() + if not (f.session["royal"]["uid"] == royal.uid or f.session["royal"]["role"] == "Admin"): + return error(403, "Non sei autorizzato a modificare questa pagina bio.") + + if f.request.method == "GET": + return f.render_template("profile_editbio.html", royal=royal) + + elif f.request.method == "POST": + fd = f.request.form + if royal.bio is None: + bio = alchemy.Bio(royal=royal, contents=fd.get("contents", "")) + alchemy_session.add(bio) + else: + royal.bio.contents = fd.get("contents", "") + alchemy_session.commit() + return f.redirect(f.url_for(".profile_page", username=username)) diff --git a/royalnet/web/royalprints/profile/templates/profile_editbio.html b/royalnet/web/royalprints/profile/templates/profile_editbio.html new file mode 100644 index 00000000..0d8c92d8 --- /dev/null +++ b/royalnet/web/royalprints/profile/templates/profile_editbio.html @@ -0,0 +1,126 @@ +{% extends "base.html" %} + +{% block title %} + {{ royal.username }} - RYGbioeditor +{% endblock %} + +{% block head %} + + +{% endblock %} + +{% block content %} +
+
+ + Editor Bio + +
+
+
+ + + +
+
+
+{% endblock %} + +{% block footscripts %} + +{% endblock %} \ No newline at end of file diff --git a/royalnet/web/royalprints/profile/templates/profile_index.html b/royalnet/web/royalprints/profile/templates/profile_index.html index 6a958856..c101e6ae 100644 --- a/royalnet/web/royalprints/profile/templates/profile_index.html +++ b/royalnet/web/royalprints/profile/templates/profile_index.html @@ -14,7 +14,7 @@
diff --git a/royalnet/web/royalprints/profile/templates/profile_page.html b/royalnet/web/royalprints/profile/templates/profile_page.html index 466d98fc..894746e1 100644 --- a/royalnet/web/royalprints/profile/templates/profile_page.html +++ b/royalnet/web/royalprints/profile/templates/profile_page.html @@ -9,6 +9,29 @@

{{ royal.username }}

+
+
+
+ + Bio + + + {% if session["royal"]["uid"] == royal.uid or session["royal"]["role"] == "Admin" %} + Modifica + {% else %} + Modifica + {% endif %} + +
+
+ {% if parsed_bio %} + {{ parsed_bio }} + {% else %} + Questo utente non ha nessuna bio. + {% endif %} +
+
+
-

- Medals -

-

- TODO -

{% endblock %} diff --git a/royalnet/web/royalprints/wikiedit/__init__.py b/royalnet/web/royalprints/wikiedit/__init__.py index 3f1660e9..13342e27 100644 --- a/royalnet/web/royalprints/wikiedit/__init__.py +++ b/royalnet/web/royalprints/wikiedit/__init__.py @@ -24,12 +24,12 @@ def wikiedit_newpage(): elif f.request.method == "POST": fd = f.request.form - if not ("title" in fd and "content" in fd and "css" in fd): + if not ("title" in fd and "contents" in fd and "css" in fd): return error(400, "Uno dei campi obbligatori non è stato compilato. Controlla e riprova!") alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] page = alchemy.WikiPage(page_id=uuid.uuid4(), title=fd["title"], - content=fd["content"], + contents=fd["contents"], format="markdown", css=fd["css"] if fd["css"] != "None" else None) revision = alchemy.WikiRevision(revision_id=uuid.uuid4(), @@ -37,7 +37,7 @@ def wikiedit_newpage(): author_id=f.session["royal"]["uid"], timestamp=datetime.datetime.now(), reason=fd.get("reason"), - diff="\n".join(difflib.unified_diff([], page.content.split("\n")))) + diff="\n".join(difflib.unified_diff([], page.contents.split("\n")))) alchemy_session.add(page) alchemy_session.add(revision) alchemy_session.commit() @@ -61,7 +61,7 @@ def wikiedit_by_id(page_id: str, title: str): elif f.request.method == "POST": fd = f.request.form - if not ("title" in fd and "content" in fd and "css" in fd): + if not ("title" in fd and "contents" in fd and "css" in fd): return error(400, "Uno dei campi obbligatori non è stato compilato. Controlla e riprova!") # Create new revision revision = alchemy.WikiRevision(revision_id=uuid.uuid4(), @@ -69,10 +69,10 @@ def wikiedit_by_id(page_id: str, title: str): author_id=f.session["royal"]["uid"], timestamp=datetime.datetime.now(), reason=fd.get("reason"), - diff="\n".join(difflib.unified_diff(page.content.split("\n"), fd["content"].split("\n")))) + diff="\n".join(difflib.unified_diff(page.contents.split("\n"), fd["contents"].split("\n")))) alchemy_session.add(revision) # Apply changes - page.content = fd["content"] + page.contents = fd["contents"] page.title = fd["title"] page.css = fd["css"] if fd["css"] != "None" else None alchemy_session.commit() diff --git a/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html b/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html index 3929b4e4..b9e6fff6 100644 --- a/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html +++ b/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html @@ -27,7 +27,7 @@ - +