[PATCH] Add counting drink Club-Mate

Correctly informing user about drinking Club-Mate and coffee per day --- app.py | 1 + coffee_db.py | 26 ++++++++++++++++++++++++++ templates/user.html | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 62f79b8..2e94a29 100644 --- a/app.py +++ b/app.py @@ -54,6 +54,7 @@ def user(): return render_template('user.html', name=db.get_name(uid), flavors=[_name for (_name, _ord) in db.flavors()], + count_mate=db.mate_count(uid, 0), count=db.coffee_count(uid, 0), stamp=time.time() ) diff --git a/coffee_db.py b/coffee_db.py index 348113a..3d2013c 100644 --- a/coffee_db.py +++ b/coffee_db.py @@ -138,6 +138,8 @@ def coffee_count(uid=None, start=None, stop=None): if stop is not None: clauses.append("date(time, 'localtime') <= date('now', 'localtime', '-%d days')" % int(stop)) + clauses.append("flavor not like 'Club-Mate%'") + for count, in c.execute( "select count(*) from coffees where " + " and ".join(clauses) @@ -146,5 +148,29 @@ def coffee_count(uid=None, start=None, stop=None): if not res: res = "0" + return res + +def mate_count(uid=None, start=None): + conn, c = open_db() + + args = [] + clauses = [] + + if uid is not None: + clauses.append("id = ?") + args.append(uid) + if start is not None: + clauses.append("date(time, 'localtime') >= date('now', 'localtime', '-%d days')" % int(start)) + + clauses.append("flavor like 'Club-Mate%'") + + for count, in c.execute( + "select count(*) from coffees where " + + " and ".join(clauses) + , args): + res = count + + if not res: + res = "0" return res diff --git a/templates/user.html b/templates/user.html index 7e549b9..e7874a9 100644 --- a/templates/user.html +++ b/templates/user.html @@ -8,7 +8,7 @@ <p id="nextStep"></p> {% if count %} - <p>You've had {{ count }} coffee{% if count != 1 %}s{% endif %} today.</p> + <p>You've had {{ count }} coffee{% if count != 1 %}s{% endif %} and {{ count_mate }} Club-Mate today.</p> {% endif %} <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}"> -- 2.11.0

On Sun, Apr 07 2019, Tomas Prochazka wrote:
Correctly informing user about drinking Club-Mate and coffee per day
Jaký problém tento patch řeší? Počítá to špatně nebo to jen říká všemu kafe i když je to Club-Mate. Odpověď by měla být napsána tady v commit message.
--- app.py | 1 + coffee_db.py | 26 ++++++++++++++++++++++++++ templates/user.html | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/app.py b/app.py index 62f79b8..2e94a29 100644 --- a/app.py +++ b/app.py @@ -54,6 +54,7 @@ def user(): return render_template('user.html', name=db.get_name(uid), flavors=[_name for (_name, _ord) in db.flavors()], + count_mate=db.mate_count(uid, 0), count=db.coffee_count(uid, 0), stamp=time.time() ) diff --git a/coffee_db.py b/coffee_db.py index 348113a..3d2013c 100644 --- a/coffee_db.py +++ b/coffee_db.py @@ -138,6 +138,8 @@ def coffee_count(uid=None, start=None, stop=None): if stop is not None: clauses.append("date(time, 'localtime') <= date('now', 'localtime', '-%d days')" % int(stop))
+ clauses.append("flavor not like 'Club-Mate%'") + for count, in c.execute( "select count(*) from coffees where " + " and ".join(clauses) @@ -146,5 +148,29 @@ def coffee_count(uid=None, start=None, stop=None):
if not res: res = "0" + return res + +def mate_count(uid=None, start=None): + conn, c = open_db() + + args = [] + clauses = [] + + if uid is not None: + clauses.append("id = ?") + args.append(uid)
+ if start is not None: + clauses.append("date(time, 'localtime') >= date('now', 'localtime', '-%d days')" % int(start)) + + clauses.append("flavor like 'Club-Mate%'") + + for count, in c.execute( + "select count(*) from coffees where " + + " and ".join(clauses) + , args): + res = count + + if not res: + res = "0"
Myslím, že není potřeba přidávat funkci mate_count, která je skoro celá zkopírovaná z coffee_count, ale stačilo by funkci coffee_count přidat nepovinné parametry flavor_like a flavor_not_like a rozšířit kód následovně: if flavor_like is not None: clauses.append("flavor like ?") args.append(flavor_like) Podobně pro flavor_not_like. Případně by šlo funkci přejmenovat na drink_count, ale to bych udělal v samostatném commitu. -Michal
participants (2)
-
Michal Sojka
-
Tomas Prochazka