From 289b36b918594ca8a9819f818ac94e8e060d3175 Mon Sep 17 00:00:00 2001 From: Midgard Date: Thu, 5 Mar 2020 00:37:16 +0100 Subject: [PATCH] Correct timer behaviour, reload when time up --- app/app.py | 30 ++++++++----- app/static/js/timer.js | 78 +++++++++++++++++++++------------- app/templates/order.html | 6 ++- app/templates/order_items.html | 7 ++- app/templates/utils.html | 2 +- 5 files changed, 78 insertions(+), 45 deletions(-) diff --git a/app/app.py b/app/app.py index 6595a52..2d7b63c 100755 --- a/app/app.py +++ b/app/app.py @@ -14,6 +14,7 @@ from flask_login import LoginManager from flask_migrate import Migrate, MigrateCommand from flask_oauthlib.client import OAuth, OAuthException from flask_script import Manager, Server +from markupsafe import Markup from login import init_login from models import db @@ -146,17 +147,24 @@ def add_template_filters(app: Flask) -> None: # pylint: disable=W0612 @app.template_filter("countdown") def countdown(value, only_positive: bool = True, - show_text: bool = True) -> str: - "A function which returns time until the order is done" - delta = value - datetime.now() - if delta.total_seconds() < 0 and only_positive: - return "closed" - hours, remainder = divmod(delta.seconds, 3600) - minutes, seconds = divmod(remainder, 60) - time = "%02d:%02d:%02d" % (hours, minutes, seconds) - if show_text: - return f"{time} left" - return time + show_text: bool = True, reload: bool = True) -> str: + delta = int(value.timestamp() - datetime.now().timestamp()) + if delta < 0 and only_positive: + text = "closed" + else: + carry, seconds = divmod(delta, 60) + carry, minutes = divmod(carry, 60) + days, hours = divmod(carry, 24) + + days_text = f"{days} days, " if days else "" + + appendix = " left" if show_text else "" + text = f"{days_text}{hours:02d}:{minutes:02d}:{seconds:02d}{appendix}" + + reload_str = "yes" if reload else "no" + + return Markup(f"" + + text + "") @app.template_filter("year") def current_year(_value: typing.Any) -> str: diff --git a/app/static/js/timer.js b/app/static/js/timer.js index 127230c..baa4e35 100644 --- a/app/static/js/timer.js +++ b/app/static/js/timer.js @@ -1,36 +1,56 @@ -/** -* Created by feliciaan on 30/03/15. -*/ +var haldisCountdownStart = new Date(); $.ready(function(){ - $('.time').each(function() { - var timeEl = $( this ); - var time = timeEl.text().split(' ')[0].split(':'); + $(".time").each(function() { + var timeEl = $(this); - if (timeEl.text().indexOf('closed') < 0) { - window.setInterval(function () { - time = my_tick(time); - if (time !== "closed") { - timeS = ("0" + time[0]).slice(-2) + ":" + ("0" + time[1]).slice(-2) + ":" + ("0" + time[2]).slice(-2) + " left"; + var delta = parseInt(timeEl.data("seconds"), 10); + var end = new Date(haldisCountdownStart.getTime() + delta * 1000); + + var now = new Date(); + var delta = Math.floor((end - now) / 1000); + if (delta <= 0) { + timeEl.html("closed"); + return; + } + + function zeroPad(value) { + return ("0" + value).slice(-2) + } + + var intervalId; + + function update() { + var now = new Date(); + var delta = Math.floor((end - now) / 1000); + if (delta <= 0) { + window.clearInterval(intervalId); + if (timeEl.data("reload") === "yes") { + $("#form").slideUp(); + timeEl.html("closed, refreshing page..."); + window.setTimeout(function () { + window.location.reload(); + }, 2000); } else { - timeS = "closed" + timeEl.html("closed"); } - timeEl.html(timeS); - }, 1000); - } - }); + return; + } - function my_tick(time) { - if (time[2] > 0) { - time[2] = time[2] - 1; - } else if(time[1] > 0) { - time[2] = 59; - time[1] = time[1] - 1; - } else if(time[0] > 0) { - time[1] = 59; - time[0] = time[0] - 1; - } else { - return "closed"; + var seconds = delta % 60; + var carry = Math.floor(delta / 60); + var minutes = carry % 60; + carry = Math.floor(carry / 60); + var hours = carry % 24; + var days = Math.floor(carry / 24); + + var text = ""; + if (days) text = days + " days, "; + text += zeroPad(hours) + ":" + zeroPad(minutes) + ":" + zeroPad(seconds); + text += " left"; + + timeEl.html(text); } - return time; - } + intervalId = window.setInterval(update, 1000); + update(); + }); }()); diff --git a/app/templates/order.html b/app/templates/order.html index e0bf1d8..4dba8fe 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -35,7 +35,7 @@ {% endif %} start: {{ order.starttime.strftime("%d/%m/%Y %H:%M") }}
{% if order.stoptime %} - closing time: {{ order.stoptime.strftime("%H:%M") }} ({{ order.stoptime|countdown }}) + closing time: {{ order.stoptime.strftime("%H:%M") }} ({{ order.stoptime|countdown }}) {% else %}open{% endif %}
total price: {{ total_price|euro }} {% if courier_or_admin %}- remaining debts: {{ debts|euro }}{% endif %} @@ -193,6 +193,7 @@ {{ super() }} + {% endblock %} {% block content -%} @@ -25,8 +28,8 @@ Haldis - Order {{ order.id }} {% if not order.is_closed() %}
- {{ order.stoptime|countdown }}
- Refresh page when closed! + {{ order.stoptime|countdown }} +
Refresh page when closed!
{% endif %} diff --git a/app/templates/utils.html b/app/templates/utils.html index d835c04..6a34e0f 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -5,7 +5,7 @@ {{ order.items.count() }} orders

{% if order.stoptime %} - Closes {{ order.stoptime.strftime("%H:%M") }}{{ order.stoptime|countdown }} + Closes {{ order.stoptime.strftime("%H:%M") }}{{ order.stoptime|countdown }} {% else %}open{% endif %}