From fce7e1c4ee545b8702c2fc925b263d5495d48069 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 14 Dec 2016 16:58:45 -0600 Subject: [PATCH] Flask basics, style. --- .gitignore | 3 ++ LICENSE | 2 +- README.md | 22 +++++++++++--- app.py | 24 +++++++++++++++ calefaction/__init__.py | 0 calefaction/util.py | 45 +++++++++++++++++++++++++++++ requirements.txt | 3 ++ static/style.css | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ templates/_base.mako | 29 +++++++++++++++++++ templates/_default.mako | 9 ++++++ templates/_layout.mako | 14 +++++++++ templates/error.mako | 15 ++++++++++ templates/landing.mako | 8 +++++ 13 files changed, 246 insertions(+), 5 deletions(-) create mode 100755 app.py create mode 100644 calefaction/__init__.py create mode 100644 calefaction/util.py create mode 100644 requirements.txt create mode 100644 static/style.css create mode 100644 templates/_base.mako create mode 100644 templates/_default.mako create mode 100644 templates/_layout.mako create mode 100644 templates/error.mako create mode 100644 templates/landing.mako diff --git a/.gitignore b/.gitignore index e69de29..300eb37 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +__pycache__/ +venv/ diff --git a/LICENSE b/LICENSE index 3ecce7d..b334a25 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (C) 2015-2016 Ben Kurtovic +Copyright (C) 2016 Ben Kurtovic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 0ea224d..3f18342 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,24 @@ calefaction =========== -__calefaction__ is a modular corporation manager for the video game +__calefaction__ is a corporation manager and dashboard for the video game [EVE Online](https://www.eveonline.com/). -Installing ----------- +Guide +----- -... +### Install + + git clone git@github.com:earwig/calefaction.git + cd calefaction + python3 -m venv venv + . venv/bin/activate + pip install -r requirements.txt + +### Setup + + ... + +### Run + + ... diff --git a/app.py b/app.py new file mode 100755 index 0000000..19318c6 --- /dev/null +++ b/app.py @@ -0,0 +1,24 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- + +from flask import Flask, g +from flask_mako import MakoTemplates, render_template + +from calefaction.util import catch_errors, set_up_hash_versioning + +app = Flask(__name__) + +MakoTemplates(app) +set_up_hash_versioning(app) + +@app.before_request +def prepare_request(): + g.something = None # ... + +@app.route("/") +@catch_errors(app) +def index(): + return render_template("landing.mako") + +if __name__ == "__main__": + app.run(debug=True, port=8080) diff --git a/calefaction/__init__.py b/calefaction/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/calefaction/util.py b/calefaction/util.py new file mode 100644 index 0000000..b288d46 --- /dev/null +++ b/calefaction/util.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +from functools import wraps +from hashlib import md5 +from os import path +from traceback import format_exc + +from flask import url_for +from flask_mako import render_template, TemplateError + +__all__ = ["catch_errors", "set_up_hash_versioning"] + +def catch_errors(app): + def callback(func): + @wraps(func) + def inner(*args, **kwargs): + try: + return func(*args, **kwargs) + except TemplateError as exc: + app.logger.error("Caught exception:\n{0}".format(exc.text)) + return render_template("error.mako", traceback=exc.text) + except Exception: + app.logger.exception("Caught exception:") + return render_template("error.mako", traceback=format_exc()) + return inner + return callback + +def set_up_hash_versioning(app): + def callback(app, error, endpoint, values): + if endpoint == "staticv": + filename = values["filename"] + fpath = path.join(app.static_folder, filename) + mtime = path.getmtime(fpath) + cache = app._hash_cache.get(fpath) + if cache and cache[0] == mtime: + hashstr = cache[1] + else: + with open(fpath, "rb") as f: + hashstr = md5(f.read()).hexdigest() + app._hash_cache[fpath] = (mtime, hashstr) + return url_for("static", filename=filename, v=hashstr) + raise error + + app._hash_cache = {} + app.url_build_error_handlers.append(lambda *args: callback(app, *args)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9923c3c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +Flask==0.11.1 +Flask-Mako==0.4 +requests==2.12.4 diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..dc13260 --- /dev/null +++ b/static/style.css @@ -0,0 +1,77 @@ +/* FONT... */ +/* MOBILE... */ + +body { + display: flex; + min-height: 100vh; + flex-direction: column; + margin: 0; + font-family: sans-serif; + background-color: #173350; + color: #E6EAEF; +} + +#container { + display: flex; + flex: 1; +} + +#container > div { + width: 100%; +} + +main, header, footer { + background-color: rgba(0, 0, 0, 0.85); +} + +main, header > div, footer > div { + max-width: 1000px; + width: 80%; +} + +main { + margin: 2em auto; + padding: 1em; +} + +header > div { + margin: 0 auto; + padding: 1.5em; +} + +header > div > div { + display: inline-block; +} + +header > div > .left { + text-align: left; + width: 70%; +} + +header > div > .right { + text-align: right; + width: 30%; +} + +footer { + font-size: 85%; +} + +footer > div { + margin: 0 auto; + padding: 1em; +} + +a { + color: #78CEFF; + text-decoration: none; +} + +a:hover { + color: #68BEDD; + text-decoration: underline; +} + +#error pre { + white-space: pre-wrap; +} diff --git a/templates/_base.mako b/templates/_base.mako new file mode 100644 index 0000000..f83cdb8 --- /dev/null +++ b/templates/_base.mako @@ -0,0 +1,29 @@ +<%! + from datetime import datetime +%>\ + + + + + + <%block name="title">CORP_NAME...</%block> + + + + + + ${next.body()} +
+
+ <% + copyright_year = datetime.now().year + %> + Copyright © ${copyright_year} COPYRIGHT_HOLDER... + • + Running Calefaction CALEFACTION_VERSION... + • + EVE Online and all related trademarks are property of CCP hf. +
+
+ + diff --git a/templates/_default.mako b/templates/_default.mako new file mode 100644 index 0000000..811e25e --- /dev/null +++ b/templates/_default.mako @@ -0,0 +1,9 @@ +<%inherit file="_layout.mako"/> +<%block name="lefthead"> + [CORP_LOGO] CORP_NAME... + + +<%block name="righthead-"> + PLAYER_NAME [logout] + +${next.body()} diff --git a/templates/_layout.mako b/templates/_layout.mako new file mode 100644 index 0000000..5080fec --- /dev/null +++ b/templates/_layout.mako @@ -0,0 +1,14 @@ +<%inherit file="_base.mako"/> +
+
+
<%block name="lefthead"/>
<%block name="righthead"/>
+
+
+
+
+
+ ${next.body()} +
+
+
diff --git a/templates/error.mako b/templates/error.mako new file mode 100644 index 0000000..b26c88a --- /dev/null +++ b/templates/error.mako @@ -0,0 +1,15 @@ +<%inherit file="_base.mako"/> +<%block name="title"> + Error – ...name... + +
+
+
+

Error!

+

You may report the following information to the developers:

+
+
${traceback | trim,h}
+
+
+
+
diff --git a/templates/landing.mako b/templates/landing.mako new file mode 100644 index 0000000..af63e49 --- /dev/null +++ b/templates/landing.mako @@ -0,0 +1,8 @@ +<%inherit file="_layout.mako"/> +<%block name="lefthead"> + [C_LOGO] C_NAME... + +<%block name="righthead"> + [login] + +

Hello, world!