diff --git a/oshipka/persistance/__init__.py b/oshipka/persistance/__init__.py index 0ae08d7..e6e57bd 100644 --- a/oshipka/persistance/__init__.py +++ b/oshipka/persistance/__init__.py @@ -14,7 +14,7 @@ from flask_security import RoleMixin, UserMixin from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE from sqlalchemy.orm.collections import InstrumentedList from sqlalchemy_utils import Choice -from tww.tww import solve_query +from tww.tww import solve_query, resolve_timezone, dt_tz_translation db = SQLAlchemy() @@ -172,7 +172,7 @@ def register_filters(app): return Markup(result) @app.template_filter('format_dt') - def format_datetime(dt, formatting="%A, %d %b %Y"): + def format_datetime(dt, formatting="%a, %d %b %Y"): """ Formats the datetime string provided in value into whatever format you want that is supported by python strftime http://strftime.org/ @@ -184,6 +184,17 @@ def register_filters(app): dt = solve_query(dt) return dt.strftime(formatting) + @app.template_filter('to_tz') + def to_tz(dt, human_tz="utc", formatting='%H:%M', include_offset=True): + if type(dt) is str: + dt = solve_query(dt) + tz = resolve_timezone(human_tz) + dt = dt_tz_translation(dt, to_tz_offset=tz.get('tz_offset')) + base = dt.strftime(formatting) + if not include_offset: + return base + return "{} ({})".format(base, tz.get('tz_offset')) + def init_db(app): app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index 338b203..893f588 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -14,13 +14,14 @@ def list_view(model_view, template): return inner -def get_view(model_view, template, template_ctx): +def get_view(model_view, template, template_ctx_func): def inner(uuid): model = model_view.model instance = model.query.filter_by(uuid=uuid).first() if not instance: flash("No {}:{}".format(model_view.model_name, uuid)) return redirect(request.referrer or url_for('home')) + template_ctx = template_ctx_func(instance) if template_ctx_func else {} return render_template(template, instance=instance, **template_ctx) return inner @@ -113,11 +114,12 @@ class ModelView(object): 'list_{}'.format(self.model_name), list_view(self, list_template)) - def register_get(self, retrieve_template, template_ctx=None): + def register_get(self, retrieve_template, template_ctx_func=None): url = '/{}/'.format(self.model_name_pl) + self.app.add_url_rule(url, 'get_{}'.format(self.model_name), - get_view(self, retrieve_template, template_ctx)) + get_view(self, retrieve_template, template_ctx_func)) def register_update(self, update_template): url = '/{}//edit'.format(self.model_name_pl)