Skip to content

Commit

Permalink
Re-enable oneswipe
Browse files Browse the repository at this point in the history
  • Loading branch information
hackenbergstefan committed Jan 4, 2025
1 parent 582b6b6 commit a85f48f
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 173 deletions.
33 changes: 18 additions & 15 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ jobs:
matrix:
python-version: ["3.8", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install --user pdm
pdm sync -G dev
- name: Format
run: |
pdm run ruff format --check .
- name: Lint
run: |
pdm run ruff check .
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install --user pdm
pdm sync -G dev
- name: Format
run: |
pdm run ruff format --check .
- name: Lint
run: |
pdm run ruff check .
- name: Djlint
run: |
pdm run djlint coffeebuddy
5 changes: 3 additions & 2 deletions coffeebuddy/extensions/coffeemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def brew(self, data):
def init():
logging.getLogger(__name__).info("Init")
app = flask.current_app
config = app.config.get("COFFEEMAKER", None) or {}

if (brew_time := app.config.get("COFFEEMAKER_MOCK_BREW_TIME", False)) is not False:
CoffeeMakerMock(brew_time=brew_time)
if "mock" in config:
CoffeeMakerMock(brew_time=config["mock"])
6 changes: 5 additions & 1 deletion coffeebuddy/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def url(site: str, **kwargs):
for key, value in kwargs.items():
if isinstance(value, bytes):
kwargs[key] = value.hex()
return site + "?" + "&".join(f"{key}={value}" for key, value in kwargs.items())
return (
site
+ "?"
+ "&".join(f"{key}={value}" if value else key for key, value in kwargs.items())
)


@flask.current_app.context_processor
Expand Down
4 changes: 2 additions & 2 deletions coffeebuddy/ui/base/templates/selectuser.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h3>Most frequent users</h3>
<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'">
onclick="window.location.href = '../coffee.html?tag={{ user.tag.hex() }}&manually&can-oneswipe'">
{{- user.name }} {{ user.prename -}}
</div>
{% endfor %}
Expand All @@ -58,7 +58,7 @@ <h3 class="mt-5">All users</h3>
<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() }}&manually'">
onclick="window.location.href = '../coffee.html?tag={{ user.tag.hex() }}&manually&can-oneswipe'">
{{- user.name }} {{ user.prename -}}
</div>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion coffeebuddy/ui/base/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$(() => {
var socket = io.connect(window.location.host);
socket.on('card_connected', (msg) => {
window.location.href = `../coffee.html?tag=${msg['tag']}`;
window.location.href = `../coffee.html?tag=${msg['tag']}&can-oneswipe`;
});
});

Expand Down
35 changes: 29 additions & 6 deletions coffeebuddy/ui/coffee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,22 @@ def post():
if request.method == "POST":
return post()

has_coffeemaker = flask.current_app.config.get("COFFEEMAKER", None) is not None

if (
not has_coffeemaker
and "can-oneswipe" in flask.request.args
and user.option_oneswipe
):
return flask.redirect(url("oneswipe.html", **flask.request.args))

variants_favorites, variants = CoffeeVariant.all_for_user(user)
return flask.render_template(
"coffee.html",
user=user,
variants_favorites=variants_favorites,
variants=variants,
coffeemaker=flask.current_app.config.get("COFFEEMAKER", False)
or flask.current_app.config.get("COFFEEMAKER_MOCK_BREW_TIME", False)
is not False,
coffeemaker=has_coffeemaker,
price=flask.current_app.config["PRICE"],
)

Expand Down Expand Up @@ -160,12 +167,28 @@ def post():
)


@blueprint.route("/oneswipe.html", methods=["POST"])
@blueprint.route("/oneswipe.html", methods=["GET", "POST"])
@require_tag
def oneswipe(user: User):
db = flask.current_app.db

selected_manually = "manually" in flask.request.args

if "coffee" in flask.request.form:
db.session.add(Drink(user=user, price=flask.current_app.config["PRICE"]))
db.session.add(
Drink(
user=user,
price=flask.current_app.config["PRICE"],
selected_manually=selected_manually,
)
)
db.session.commit()
return ""
elif "undo" in flask.request.form:
return flask.redirect(
url(
"coffee.html",
tag=user.tag,
**{"manually" if selected_manually else None: None},
)
)
return flask.render_template("oneswipe.html", user=user)
78 changes: 31 additions & 47 deletions coffeebuddy/ui/coffee/templates/oneswipe.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
<!doctype html>
<html lang="en">
{% extends "_base.html" %}

<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="{{ url_for('static', filename='coffeebuddy.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='matrix.css') }}">
{% block header %}
<style>
button[name='undo'] {
background: transparent;
color: var(--color-fg);
}

#btn-coffee-icon {
font-size: 4rem;
}

#btn-coffee-text {
font-size: 2rem;
}

<script src="{{ url_for('static', filename='matrix-0.0.1.js') }}" defer></script>
#timeout-bar {
height: 5px;
border-radius: 10px;
background-color: var(--color-fg);
}
</style>
<script>
$(() => {
const timeout = 5;
setTimeout(() => {
$.post(window.location, data = { "coffee": "" });
$.post(window.location, data = {
"coffee": ""
});
window.location.href = '../';
}, timeout * 1000);
setInterval(() => {
Expand All @@ -29,37 +36,16 @@
}, 10);
});
</script>
<style>
#timeout-bar-container {
width: 400px;
align-self: center;
}
{% endblock header %}

#timeout-bar {
width: 100%;
height: 10px;
border-radius: 3px;
background: var(--color-engineering-dark);
}

</style>
</head>
{% block main_nav_items %}
{% endblock main_nav_items %}

<body>
<div class="text-left m-5">
<h1 id="user-name">{{ user.prename }} {{ user.name }}</h1>
<code class="h5">{{ hexstr(user.tag) }}</code>
<div class="mt-4">Your bill:
<div class="h2"> {{ "%.2f €" | format(user.unpayed) }}</div>
</div>
</div>
<canvas id="matrix"></canvas>

<div class="position-absolute w-100 h-100 mt-4 d-flex flex-row align-items-center justify-content-center"
style="top: 0; left: 0;">
{% block main_content %}
<div class="w-100 h-100 d-flex flex-row align-items-center justify-content-center self-align-center">
<div class="d-flex flex-column text-center">
<form method="post">
<button class="p-3" type="submit" name="">
<button class="p-3" type="submit" name="undo">
<div id="btn-coffee-icon" class="display-1 fas fa-undo-alt"></div>
<div id="btn-coffee-text" class="h3">Undo</div>
</button>
Expand All @@ -69,6 +55,4 @@ <h1 id="user-name">{{ user.prename }} {{ user.name }}</h1>
</div>
</div>
</div>
</body>

</html>
{% endblock main_content %}
8 changes: 4 additions & 4 deletions coffeebuddy/ui/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def post():
try:
user.tag = escapefromhex(request.form["tag"])
user.tag2 = escapefromhex(request.form["tag2"])
user.name = request.form["last_name"]
user.prename = request.form["first_name"]
user.name = request.form["name"]
user.prename = request.form["prename"]
user.email = request.form["email"]
user.option_oneswipe = "oneswipe" in request.form
user.option_oneswipe = request.form["option_oneswipe"] == "true"

if flask_login.current_user.is_authenticated:
user.enabled = "enabled" in request.form
user.enabled = request.form["enabled"] == "true"
balance = float(request.form["balance"])
if not math.isclose(balance, user.balance):
user.update_balance(balance)
Expand Down
Loading

0 comments on commit a85f48f

Please sign in to comment.