From 44053eef4eb97e6721293abcd3a88f7f1d6219d4 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 10 Jul 2019 18:36:43 +0300 Subject: [PATCH] some unfinished stuff --- requirements.txt | 1 + royalnet/royalgames.py | 2 + royalnet/royalgamesweb.py | 4 +- royalnet/web/flaskserver.py | 2 +- royalnet/web/royalprints/__init__.py | 30 ++++++++---- royalnet/web/royalprints/login/__init__.py | 42 ++++++++++++++++ .../login/templates/login_index.html | 46 ++++++++++++++++++ .../login/templates/login_success.html | 18 +++++++ .../profile/templates/profile_index.html | 4 +- royalnet/web/royalprints/tglogin/__init__.py | 2 +- .../tglogin/templates/tglogin_index.html | 2 +- .../tglogin/templates/tglogin_success.html | 4 +- .../wikiedit/templates/wikiedit_page.html | 2 +- royalnet/web/royalprints/wikiview/__init__.py | 11 +++-- royalnet/web/shortcuts.py | 11 +++++ royalnet/web/static/ryg.css | 48 +++++++++---------- royalnet/web/static/ryg.less | 2 +- 17 files changed, 183 insertions(+), 48 deletions(-) create mode 100644 royalnet/web/royalprints/login/__init__.py create mode 100644 royalnet/web/royalprints/login/templates/login_index.html create mode 100644 royalnet/web/royalprints/login/templates/login_success.html diff --git a/requirements.txt b/requirements.txt index f64e3932..b6493f03 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +bcrypt python-telegram-bot>=11.1.0 websockets>=7.0 pytest>=4.3.1 diff --git a/royalnet/royalgames.py b/royalnet/royalgames.py index df45a0dd..19632a27 100644 --- a/royalnet/royalgames.py +++ b/royalnet/royalgames.py @@ -1,3 +1,5 @@ +"""The production Royalnet, active at @royalgamesbot on Telegram and Royalbot on Discord.""" + import os import asyncio import logging diff --git a/royalnet/royalgamesweb.py b/royalnet/royalgamesweb.py index b5a37c11..4a606527 100644 --- a/royalnet/royalgamesweb.py +++ b/royalnet/royalgamesweb.py @@ -1,3 +1,5 @@ +"""The production Royalnet available at ryg.steffo.eu .""" + import os from royalnet.web import create_app from royalnet.web.royalprints import * @@ -9,7 +11,7 @@ class TestConfig: app = create_app(TestConfig, [rp_home, rp_wikiview, rp_tglogin, rp_docs, rp_wikiedit, rp_mcstatus, rp_diarioview, - rp_profile]) + rp_profile, rp_login]) if __name__ == "__main__": diff --git a/royalnet/web/flaskserver.py b/royalnet/web/flaskserver.py index 260af039..b85f4c5d 100644 --- a/royalnet/web/flaskserver.py +++ b/royalnet/web/flaskserver.py @@ -14,7 +14,7 @@ def create_app(config_obj: typing.Type, blueprints: typing.List[Royalprint]): Also requires a ``DB_PATH`` key in ``config_obj`` to initialize the database connection. Warning: - The code for this class was written at 1 AM, and I have no clue of how and why it works or even of if it really does work. + The code for this class was written at 1 AM, and I have no clue of how and why it works or even if it really does work. Use with caution? Args: diff --git a/royalnet/web/royalprints/__init__.py b/royalnet/web/royalprints/__init__.py index a902af18..f298a935 100644 --- a/royalnet/web/royalprints/__init__.py +++ b/royalnet/web/royalprints/__init__.py @@ -1,12 +1,24 @@ """Some Royalprints that can be used with the Royalnet Flask server.""" -from .home import rp as rp_home -from .wikiview import rp as rp_wikiview -from .tglogin import rp as rp_tglogin -from .docs import rp as rp_docs -from .wikiedit import rp as rp_wikiedit -from .mcstatus import rp as rp_mcstatus -from .diarioview import rp as rp_diarioview -from .profile import rp as rp_profile +from . import home +from . import wikiview +from . import tglogin +from . import docs +from . import wikiedit +from . import mcstatus +from . import diarioview +from . import profile +from . import login -__all__ = ["rp_home", "rp_wikiview", "rp_tglogin", "rp_docs", "rp_wikiedit", "rp_mcstatus", "rp_diarioview", "rp_profile"] +rp_home = home.rp +rp_wikiview = wikiview.rp +rp_tglogin = tglogin.rp +rp_docs = docs.rp +rp_wikiedit = wikiedit.rp +rp_mcstatus = mcstatus.rp +rp_diarioview = diarioview.rp +rp_profile = profile.rp +rp_login = login.rp + +__all__ = ["rp_home", "rp_wikiview", "rp_tglogin", "rp_docs", "rp_wikiedit", "rp_mcstatus", "rp_diarioview", + "rp_profile", "rp_login"] diff --git a/royalnet/web/royalprints/login/__init__.py b/royalnet/web/royalprints/login/__init__.py new file mode 100644 index 00000000..96c620da --- /dev/null +++ b/royalnet/web/royalprints/login/__init__.py @@ -0,0 +1,42 @@ +"""A Royalnet password-based login :py:class:`royalnet.web.Royalprint`.""" +import flask as f +import os +import datetime +import bcrypt +from ...royalprint import Royalprint +from ...shortcuts import error +from ....database.tables import Royal + + +tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') +rp = Royalprint("login", __name__, url_prefix="/login/password", required_tables={Royal}, + template_folder=tmpl_dir) + + +@rp.route("/") +def login_index(): + f.session.pop("royal", None) + return f.render_template("login_index.html") + + +@rp.route("/done", methods=["POST"]) +def login_done(): + alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] + data = f.request.form + if "username" not in data: + return error(400, "Nessun username inserito.") + royal_user = alchemy_session.query(alchemy.Royal).filter_by(username=data["username"]).one_or_none() + if royal_user is None: + return error(404, "L'username inserito non corrisponde a nessun account registrato.") + if "password" not in data: + return error(400, "Nessuna password inserita.") + if not bcrypt.checkpw(bytes(data["password"], encoding="utf8"), royal_user.password): + return error(400, "La password inserita non รจ valida.") + f.session["royal"] = { + "uid": royal_user.uid, + "username": royal_user.username, + "avatar": royal_user.avatar, + "role": royal_user.role + } + f.session["login_date"] = datetime.datetime.now() + return f.render_template("login_success.html") diff --git a/royalnet/web/royalprints/login/templates/login_index.html b/royalnet/web/royalprints/login/templates/login_index.html new file mode 100644 index 00000000..31ac598a --- /dev/null +++ b/royalnet/web/royalprints/login/templates/login_index.html @@ -0,0 +1,46 @@ +{% extends "base.html" %} + +{% block title %} + Password Login +{% endblock %} + +{% block content %} +
+
+
+ + Password Login + +
+
+

+ Facendo il login su questo sito, acconsenti a ricevere due biscottini che memorizzino l'account con cui hai fatto il login.
+

+

+ Essi avranno il seguente formato: +

+
session["royal"] = {
+"uid": [il tuo id Royalnet]
+"username": [il tuo username Royalnet],
+"avatar": [il tuo avatar Royalnet],
+"role": [il tuo ruolo Royalnet]
+}
+
+session["login_date"] = [la data e l'ora di adesso]
+ +
+
+
+{% endblock %} diff --git a/royalnet/web/royalprints/login/templates/login_success.html b/royalnet/web/royalprints/login/templates/login_success.html new file mode 100644 index 00000000..2cf02b4a --- /dev/null +++ b/royalnet/web/royalprints/login/templates/login_success.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} + +{% block title %} + Successful Password Login +{% endblock %} + +{% block content %} +
+
+ + Password Login + +
+
+ Login riuscito! Sei connesso come {{ session["royal"]["username"] }}! +
+
+{% endblock %} diff --git a/royalnet/web/royalprints/profile/templates/profile_index.html b/royalnet/web/royalprints/profile/templates/profile_index.html index 83137d86..7f352243 100644 --- a/royalnet/web/royalprints/profile/templates/profile_index.html +++ b/royalnet/web/royalprints/profile/templates/profile_index.html @@ -1,14 +1,14 @@ {% extends "base.html" %} {% block title %} - Indice RYGwiki + Elenco Utenti {% endblock %} {% block content %}
- Elenco Membri + Elenco Utenti
diff --git a/royalnet/web/royalprints/tglogin/__init__.py b/royalnet/web/royalprints/tglogin/__init__.py index 64fab63a..780a8ddf 100644 --- a/royalnet/web/royalprints/tglogin/__init__.py +++ b/royalnet/web/royalprints/tglogin/__init__.py @@ -47,4 +47,4 @@ def tglogin_done(): "role": royal_user.role } f.session["login_date"] = datetime.datetime.now() - return f.render_template("tglogin_success.html") + return f.render_template("login_success.html") diff --git a/royalnet/web/royalprints/tglogin/templates/tglogin_index.html b/royalnet/web/royalprints/tglogin/templates/tglogin_index.html index 373d0ffc..b4a5ad24 100644 --- a/royalnet/web/royalprints/tglogin/templates/tglogin_index.html +++ b/royalnet/web/royalprints/tglogin/templates/tglogin_index.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block title %} - Login with Telegram + Telegram Login {% endblock %} {% block content %} diff --git a/royalnet/web/royalprints/tglogin/templates/tglogin_success.html b/royalnet/web/royalprints/tglogin/templates/tglogin_success.html index 2f9590f0..5eb5c050 100644 --- a/royalnet/web/royalprints/tglogin/templates/tglogin_success.html +++ b/royalnet/web/royalprints/tglogin/templates/tglogin_success.html @@ -1,14 +1,14 @@ {% extends "base.html" %} {% block title %} - Login with Telegram + Successful Telegram Login {% endblock %} {% block content %}
- Telegram login + Telegram Login
diff --git a/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html b/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html index b612d07f..d017d767 100644 --- a/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html +++ b/royalnet/web/royalprints/wikiedit/templates/wikiedit_page.html @@ -17,7 +17,7 @@
-
+