Skip to content

Commit

Permalink
Implement "Favorites in select manually".
Browse files Browse the repository at this point in the history
Fixes #56
  • Loading branch information
hackenbergstefan committed Jul 24, 2024
1 parent bdf866b commit aedd30c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_backends.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
run: |
sudo apt-get install libpcsclite-dev
pip install --user pdm
pdm sync
pdm sync --prod
- name: Test backend sqlite
run: |
sed -i 's/^CARD.*/CARD = ""/' config.py
Expand Down
9 changes: 9 additions & 0 deletions coffeebuddy/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import socket
from typing import Optional

import flask
import sqlalchemy
Expand Down Expand Up @@ -125,6 +126,11 @@ def serialize(self):
def update_bill(self, newbill):
flask.current_app.db.session.add(Pay(user=self, amount=self.unpayed - newbill))

def count_selected_manually(self, host: Optional[str] = None) -> int:
"""Return how often this user logged in using 'Select manually'."""
host = host or socket.gethostname()
return sum(d.selected_manually for d in self.drinks if d.host == host)


class Drink(flask.current_app.db.Model):
id = flask.current_app.db.Column(flask.current_app.db.Integer, primary_key=True)
Expand All @@ -136,6 +142,9 @@ class Drink(flask.current_app.db.Model):
nullable=False,
)
host = flask.current_app.db.Column(flask.current_app.db.String(50))
selected_manually = flask.current_app.db.Column(
flask.current_app.db.Boolean, default=False
)

def __init__(self, *args, **kwargs):
if "timestamp" not in kwargs:
Expand Down
8 changes: 6 additions & 2 deletions coffeebuddy/route_coffee.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def init():
@flask.current_app.route("/coffee.html", methods=["GET", "POST"])
def coffee():
flask.current_app.events.fire("route_coffee")
user = User.by_tag(escapefromhex(flask.request.args["tag"]))
user: User = User.by_tag(escapefromhex(flask.request.args["tag"]))
if user is None:
return flask.render_template(
"cardnotfound.html", uuid=flask.request.args["tag"]
Expand All @@ -17,7 +17,11 @@ def coffee():
if flask.request.method == "POST":
if "coffee" in flask.request.form:
flask.current_app.db.session.add(
Drink(user=user, price=flask.current_app.config["PRICE"])
Drink(
user=user,
price=flask.current_app.config["PRICE"],
selected_manually="manually" in flask.request.args,
)
)
flask.current_app.db.session.commit()
elif "pay" in flask.request.form:
Expand Down
13 changes: 9 additions & 4 deletions coffeebuddy/route_selectuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
def init():
@flask.current_app.route("/selectuser.html")
def selectuser():
all_enabled_users = User.query.filter(User.enabled).order_by(User.name).all()
return flask.render_template(
"selectuser.html",
users=itertools.groupby(
User.query.filter(User.enabled).order_by(User.name).all(),
key=lambda u: u.name[0].upper(),
),
users=itertools.groupby(all_enabled_users, key=lambda u: u.name[0].upper()),
top_manual_users=list(
sorted(
(u for u in all_enabled_users if u.count_selected_manually() > 0),
key=lambda u: u.count_selected_manually(),
reverse=True,
)
)[:5],
)
19 changes: 17 additions & 2 deletions coffeebuddy/templates/selectuser.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,29 @@
<div class="display-1 fas fa-user mr-5"></div>
<h1>Users</h1>
</div>
<div class="d-flex flex-column scroll">

{% if top_manual_users|length > 0 %}
<h3>Most frequent users</h3>
<div class="d-flex flex-column scroll-y">
<div class="d-flex flex-row flex-wrap">
{% for user in top_manual_users %}
<div class="item" onclick="window.location.href = '../coffee.html?tag={{ user.tag.hex() }}&manually'">
{{ user.name }} {{ user.prename }}
</div>
{% endfor %}
</div>
</div>
{% endif %}

<h3 class="mt-5">All users</h3>
<div class="d-flex flex-column scroll-y">
{% for char, userlist in users %}
<div class="d-flex justify-content-start my-2">
<div class="h3 py-2 pr-2 mr-2 color-berry" style="border-right: 2px solid; min-width: 2.5ch;">{{char}}
</div>
<div class="d-flex flex-row flex-wrap">
{% for user in userlist %}
<div class="item" onclick="window.location.href = '../coffee.html?tag={{ user.tag.hex() }}'">
<div class="item" onclick="window.location.href = '../coffee.html?tag={{ user.tag.hex() }}&manually'">
{{ user.name }} {{ user.prename }}
</div>
{% endfor %}
Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@

# Password for admin access
ADMIN_PASSWORD = "coffeebuddy"

# Whether to prefill the database
PREFILLED = True

0 comments on commit aedd30c

Please sign in to comment.