From 5c6bf7d2abee96e6baf6c2e851db503925fcc72a Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 6 Feb 2018 11:47:19 +0000 Subject: Template rendering --- pysite/base_route.py | 12 +++++++++--- pysite/route_manager.py | 5 +++-- pysite/views/healthcheck.py | 4 ++-- pysite/views/index.py | 6 +++--- pysite/views/invite.py | 4 ++-- templates/.gitkeep | 0 templates/index.html | 10 ++++++++++ 7 files changed, 29 insertions(+), 12 deletions(-) delete mode 100644 templates/.gitkeep create mode 100644 templates/index.html diff --git a/pysite/base_route.py b/pysite/base_route.py index 17e9aee3..16bf0984 100644 --- a/pysite/base_route.py +++ b/pysite/base_route.py @@ -1,23 +1,29 @@ # coding=utf-8 -from flask import Flask +from flask import Flask, render_template from flask.views import MethodView __author__ = "Gareth Coles" class BaseView(MethodView): + def render(self, *template_names, **context): + # thin wrapper here in case it needs to be modified later + return render_template(template_names, **context) + + +class RouteView(BaseView): path = None #: str name = None #: str @classmethod - def setup(cls: "BaseView", app: Flask): + def setup(cls: "RouteView", app: Flask): if not cls.path or not cls.name: raise RuntimeError("Route views must have both `path` and `name` defined") app.add_url_rule(cls.path, view_func=cls.as_view(cls.name)) -class ErrorView(MethodView): +class ErrorView(BaseView): name = None #: str error_code = None #: int diff --git a/pysite/route_manager.py b/pysite/route_manager.py index 501076b7..f922be6a 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -5,7 +5,7 @@ import os from flask import Flask -from pysite.base_route import BaseView, ErrorView +from pysite.base_route import BaseView, ErrorView, RouteView __author__ = "Gareth Coles" @@ -35,7 +35,8 @@ class RouteManager: inspect.isclass(cls) and cls is not BaseView and cls is not ErrorView and - (BaseView in cls.__mro__ or ErrorView in cls.__mro__) + cls is not RouteView and + BaseView in cls.__mro__ ): cls.setup(self.app) print(f"View loaded: {cls.name: <25} ({module.__name__}.{cls_name})") diff --git a/pysite/views/healthcheck.py b/pysite/views/healthcheck.py index 660c8a96..088bc7b3 100644 --- a/pysite/views/healthcheck.py +++ b/pysite/views/healthcheck.py @@ -1,13 +1,13 @@ # coding=utf-8 from flask import jsonify -from pysite.base_route import BaseView +from pysite.base_route import RouteView __author__ = "Gareth Coles" -class IndexView(BaseView): +class IndexView(RouteView): path = "/healthcheck" name = "healthcheck" diff --git a/pysite/views/index.py b/pysite/views/index.py index 2e779003..040332cc 100644 --- a/pysite/views/index.py +++ b/pysite/views/index.py @@ -1,12 +1,12 @@ # coding=utf-8 -from pysite.base_route import BaseView +from pysite.base_route import RouteView __author__ = "Gareth Coles" -class IndexView(BaseView): +class IndexView(RouteView): path = "/" name = "index" def get(self): - return "Coming soon:tm:" + return self.render("index.html") diff --git a/pysite/views/invite.py b/pysite/views/invite.py index d035fc99..07ddc830 100644 --- a/pysite/views/invite.py +++ b/pysite/views/invite.py @@ -1,13 +1,13 @@ # coding=utf-8 from flask import redirect -from pysite.base_route import BaseView +from pysite.base_route import RouteView __author__ = "Gareth Coles" -class InviteView(BaseView): +class InviteView(RouteView): path = "/invite" name = "invite" diff --git a/templates/.gitkeep b/templates/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 00000000..07bd4541 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,10 @@ + + + + + Python | Home + + +

Coming soon:tm:

+ + \ No newline at end of file -- cgit v1.2.3 From e3d01f857375b6a226dd09480f326dbb20764783 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 6 Feb 2018 14:36:49 +0000 Subject: Rethinkdb setup method; not actually called yet --- pysite/route_manager.py | 43 ++++++++++++++++++++++++++++++++++++++++++- requirements.txt | 3 ++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pysite/route_manager.py b/pysite/route_manager.py index f922be6a..813816b7 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -2,13 +2,19 @@ import importlib import inspect import os +import rethinkdb -from flask import Flask +from flask import Flask, g, abort from pysite.base_route import BaseView, ErrorView, RouteView __author__ = "Gareth Coles" +DB_HOST = os.environ["RETHINKDB_HOST"] +DB_PORT = os.environ["RETHINKDB_PORT"] +DB_DATABASE = os.environ["RETHINKDB_DATABASE"] +DB_TABLE = os.environ["RETHINKDB_TABLE"] + class RouteManager: def __init__(self): @@ -40,3 +46,38 @@ class RouteManager: ): cls.setup(self.app) print(f"View loaded: {cls.name: <25} ({module.__name__}.{cls_name})") + + def setup_db(self): + connection = self.get_db_connection(connect_database=False) + + try: + rethinkdb.db_create(DB_DATABASE).run(connection) + rethinkdb.db(DB_DATABASE).table_create(DB_TABLE).run(connection) + print("Database created") + except rethinkdb.RqlRuntimeError: + print("Database found") + finally: + connection.close() + + self.app.before_request(self.db_before_request) + self.app.teardown_request(self.db_teardown_request) + + def get_db_connection(self, connect_database=True): + if connect_database: + return rethinkdb.connect(host=DB_HOST, port=DB_PORT, db=DB_DATABASE) + else: + return rethinkdb.connect(host=DB_HOST, port=DB_PORT) + + def db_before_request(self): + try: + # g is the Flask global context object + g.rdb_conn = self.get_db_connection() + except rethinkdb.RqlDriverError: + abort(503, "Database connection could be established.") + + def db_teardown_request(self, _): + try: + # g is the Flask global context object + g.rdb_conn.close() + except AttributeError: + pass diff --git a/requirements.txt b/requirements.txt index 69ca547f..e6a3877f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -flask==0.12.2 \ No newline at end of file +flask==0.12.2 +rethinkdb -- cgit v1.2.3 From 520ff6c0072591d3b9a034ebdf85f158f5a0ddda Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 6 Feb 2018 14:37:25 +0000 Subject: snekchek --- pysite/route_manager.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pysite/route_manager.py b/pysite/route_manager.py index 813816b7..142882e6 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -2,9 +2,10 @@ import importlib import inspect import os -import rethinkdb -from flask import Flask, g, abort +from flask import Flask, abort, g + +import rethinkdb from pysite.base_route import BaseView, ErrorView, RouteView -- cgit v1.2.3 From bb86a6e537e8f556323f505ee3f5d62f84e416ec Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 6 Feb 2018 14:40:19 +0000 Subject: Use .get() for environs --- pysite/route_manager.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pysite/route_manager.py b/pysite/route_manager.py index 142882e6..b71dea2f 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -11,10 +11,10 @@ from pysite.base_route import BaseView, ErrorView, RouteView __author__ = "Gareth Coles" -DB_HOST = os.environ["RETHINKDB_HOST"] -DB_PORT = os.environ["RETHINKDB_PORT"] -DB_DATABASE = os.environ["RETHINKDB_DATABASE"] -DB_TABLE = os.environ["RETHINKDB_TABLE"] +DB_HOST = os.environ.get("RETHINKDB_HOST") +DB_PORT = os.environ.get("RETHINKDB_PORT") +DB_DATABASE = os.environ.get("RETHINKDB_DATABASE") +DB_TABLE = os.environ.get("RETHINKDB_TABLE") class RouteManager: -- cgit v1.2.3 From dea4ca1d9c601b4add6612d4850439dc1c703a98 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 6 Feb 2018 14:57:11 +0000 Subject: Fix template loading --- pysite/route_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pysite/route_manager.py b/pysite/route_manager.py index b71dea2f..e9e701e0 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -19,7 +19,7 @@ DB_TABLE = os.environ.get("RETHINKDB_TABLE") class RouteManager: def __init__(self): - self.app = Flask(__name__) + self.app = Flask(__name__, template_folder="../templates") self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY") self.load_views() -- cgit v1.2.3 From 8436882783fb2fed76d9a2bde86e66fbe30ec30c Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Tue, 6 Feb 2018 22:32:22 +0100 Subject: adds gunicorn server_ready hook to set up database --- gunicorn_config.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 gunicorn_config.py diff --git a/gunicorn_config.py b/gunicorn_config.py new file mode 100644 index 00000000..c4869dd3 --- /dev/null +++ b/gunicorn_config.py @@ -0,0 +1,44 @@ +def when_ready(server): + """ server hook that only runs when the gunicorn master process loads """ + + import traceback + import rethinkdb as r + + try: + server.log.info("rethinkdb initialising") + + construct = [ + {'DB':'test', 'TABLE':'test', 'INDEXES': ['test']}, + ] + for struct in construct: + DB = struct['DB'] + TABLE = struct['TABLE'] + INDEXES = struct['INDEXES'] + + conn = r.connect(host='pdrdb', port=28016, db=DB) + + # Check if database exists, if not create it + db_exists = r.db_list().contains(DB).run(conn) + if not db_exists: + log.info('adding database {0}'.format(DB)) + r.db_create(DB).run(conn) + + # Check if table exist, if not create it + table_exists = r.db(DB).table_list().contains(TABLE).run(conn) + if not table_exists: + log.info('adding table {0}'.format(TABLE)) + result = r.db(DB).table_create(TABLE).run(conn) + + # Check if index exists if not add it + rtable = r.db(DB).table(TABLE) + current_indexes = rtable.index_list().run(conn) + for index in INDEXES: + if index not in current_indexes: + log.info('adding index {0}'.format(index)) + rtable.index_create(index).run(conn) + + server.log.info("rethinkdb ready") + + except: + server.log.error(traceback.format_exc()) + server.log.error("rethinkdb failed to initialise") -- cgit v1.2.3 From a38d5503f52fdef6b3f910e85f70a5b999fd04b8 Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Tue, 6 Feb 2018 23:32:19 +0100 Subject: minor fixes --- gunicorn_config.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gunicorn_config.py b/gunicorn_config.py index c4869dd3..a2e6ac23 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -8,25 +8,25 @@ def when_ready(server): server.log.info("rethinkdb initialising") construct = [ - {'DB':'test', 'TABLE':'test', 'INDEXES': ['test']}, + {'DB': 'test', 'TABLE': 'test', 'INDEXES': ['test']}, ] for struct in construct: DB = struct['DB'] TABLE = struct['TABLE'] INDEXES = struct['INDEXES'] - conn = r.connect(host='pdrdb', port=28016, db=DB) + conn = r.connect(host='pdrdb', port=28016, db=DB) # Check if database exists, if not create it db_exists = r.db_list().contains(DB).run(conn) if not db_exists: - log.info('adding database {0}'.format(DB)) + server.log.info('adding database {0}'.format(DB)) r.db_create(DB).run(conn) # Check if table exist, if not create it table_exists = r.db(DB).table_list().contains(TABLE).run(conn) if not table_exists: - log.info('adding table {0}'.format(TABLE)) + server.log.info('adding table {0}'.format(TABLE)) result = r.db(DB).table_create(TABLE).run(conn) # Check if index exists if not add it @@ -34,11 +34,11 @@ def when_ready(server): current_indexes = rtable.index_list().run(conn) for index in INDEXES: if index not in current_indexes: - log.info('adding index {0}'.format(index)) + server.log.info('adding index {0}'.format(index)) rtable.index_create(index).run(conn) server.log.info("rethinkdb ready") - except: + except Exception as e: server.log.error(traceback.format_exc()) server.log.error("rethinkdb failed to initialise") -- cgit v1.2.3 From 95542464d4bf7b4405179d14adc0a61f5cc26bae Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Tue, 6 Feb 2018 23:34:59 +0100 Subject: last fix --- gunicorn_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gunicorn_config.py b/gunicorn_config.py index a2e6ac23..2b5c52ea 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -27,7 +27,7 @@ def when_ready(server): table_exists = r.db(DB).table_list().contains(TABLE).run(conn) if not table_exists: server.log.info('adding table {0}'.format(TABLE)) - result = r.db(DB).table_create(TABLE).run(conn) + r.db(DB).table_create(TABLE).run(conn) # Check if index exists if not add it rtable = r.db(DB).table(TABLE) -- cgit v1.2.3 From 7bc5e8bee74bb0a8b879a38f69748d40558a5e0b Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Wed, 7 Feb 2018 10:13:36 +0000 Subject: Static files; basic templates --- pysite/base_route.py | 12 +++++++----- pysite/route_manager.py | 7 ++++++- static/favicon.ico | Bin 0 -> 28957 bytes static/logos/logo_discord.png | Bin 0 -> 63590 bytes templates/base.html | 17 +++++++++++++++++ templates/index.html | 19 +++++++++---------- templates/navigation.html | 18 ++++++++++++++++++ 7 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 static/favicon.ico create mode 100644 static/logos/logo_discord.png create mode 100644 templates/base.html create mode 100644 templates/navigation.html diff --git a/pysite/base_route.py b/pysite/base_route.py index 16bf0984..f983c1a2 100644 --- a/pysite/base_route.py +++ b/pysite/base_route.py @@ -6,14 +6,17 @@ __author__ = "Gareth Coles" class BaseView(MethodView): + name = None # type: str + def render(self, *template_names, **context): - # thin wrapper here in case it needs to be modified later + context["current_page"] = self.name + context["view"] = self + return render_template(template_names, **context) class RouteView(BaseView): - path = None #: str - name = None #: str + path = None # type: str @classmethod def setup(cls: "RouteView", app: Flask): @@ -24,8 +27,7 @@ class RouteView(BaseView): class ErrorView(BaseView): - name = None #: str - error_code = None #: int + error_code = None # type: int @classmethod def setup(cls: "ErrorView", app: Flask): diff --git a/pysite/route_manager.py b/pysite/route_manager.py index e9e701e0..f61c4f65 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -16,10 +16,15 @@ DB_PORT = os.environ.get("RETHINKDB_PORT") DB_DATABASE = os.environ.get("RETHINKDB_DATABASE") DB_TABLE = os.environ.get("RETHINKDB_TABLE") +TEMPLATES_PATH = "../templates" +STATIC_PATH = "../static" + class RouteManager: def __init__(self): - self.app = Flask(__name__, template_folder="../templates") + self.app = Flask( + __name__, template_folder=TEMPLATES_PATH, static_folder=STATIC_PATH, static_url_path="/static" + ) self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY") self.load_views() diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 00000000..046480bf Binary files /dev/null and b/static/favicon.ico differ diff --git a/static/logos/logo_discord.png b/static/logos/logo_discord.png new file mode 100644 index 00000000..2bf74ffd Binary files /dev/null and b/static/logos/logo_discord.png differ diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 00000000..9e00af20 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,17 @@ + + + + {% block head %} + Python | {% block title %}{% endblock %} + + + + + + {% endblock %} + + +{% include "navigation.html" %} +{% block content %}{% endblock %} + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 07bd4541..418347d2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,10 +1,9 @@ - - - - - Python | Home - - -

Coming soon:tm:

- - \ No newline at end of file +{% extends "base.html" %} +{% block title %}Home{% endblock %} +{% block content %} +
+

+ Coming soon :tm: +

+
+{% endblock %} \ No newline at end of file diff --git a/templates/navigation.html b/templates/navigation.html new file mode 100644 index 00000000..d1f1f363 --- /dev/null +++ b/templates/navigation.html @@ -0,0 +1,18 @@ +
+ +
\ No newline at end of file -- cgit v1.2.3 From 89eb38b61bf243ef9ca01dcb62ca00ef684715a6 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Wed, 7 Feb 2018 10:28:28 +0000 Subject: Icons in nav --- templates/navigation.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/navigation.html b/templates/navigation.html index d1f1f363..7ad681ff 100644 --- a/templates/navigation.html +++ b/templates/navigation.html @@ -8,10 +8,11 @@
-- cgit v1.2.3 From 9b2bf10ab776b7ef8cde81a34cd805e509ea421e Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Wed, 7 Feb 2018 10:36:40 +0000 Subject: Switch to banner logo --- static/logos/logo_banner.png | Bin 0 -> 61803 bytes static/logos/logo_discord.png | Bin 63590 -> 0 bytes templates/navigation.html | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 static/logos/logo_banner.png delete mode 100644 static/logos/logo_discord.png diff --git a/static/logos/logo_banner.png b/static/logos/logo_banner.png new file mode 100644 index 00000000..9c475864 Binary files /dev/null and b/static/logos/logo_banner.png differ diff --git a/static/logos/logo_discord.png b/static/logos/logo_discord.png deleted file mode 100644 index 2bf74ffd..00000000 Binary files a/static/logos/logo_discord.png and /dev/null differ diff --git a/templates/navigation.html b/templates/navigation.html index 7ad681ff..cbd6a36a 100644 --- a/templates/navigation.html +++ b/templates/navigation.html @@ -2,7 +2,7 @@