From 676e9dc051d94cf0c02e956c14d67786659243fe Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sun, 15 Oct 2017 11:44:12 +0200 Subject: [PATCH] Add better cardlist with sorting --- resources/cards/cardlist.css | 24 ++++++++++ resources/cards/cardlist.vm | 85 +++++++++++++++++++++++++++++++++--- server.py | 31 ++++++++++++- 3 files changed, 133 insertions(+), 7 deletions(-) diff --git a/resources/cards/cardlist.css b/resources/cards/cardlist.css index e69de29..6ceeb4b 100644 --- a/resources/cards/cardlist.css +++ b/resources/cards/cardlist.css @@ -0,0 +1,24 @@ +button { + min-width: 3.5em; +} +button.small { + min-width: 0; +} + +button, +input[type=checkbox], +label { +cursor: pointer; +} + +th, +td { + text-align: left; +} +th +th+th, +td +td+td { + padding-left: 1em; +} +td +td+td { + opacity: 0.6; +} diff --git a/resources/cards/cardlist.vm b/resources/cards/cardlist.vm index 64c6747..f693823 100644 --- a/resources/cards/cardlist.vm +++ b/resources/cards/cardlist.vm @@ -1,11 +1,84 @@ - -
- #foreach($card in $cards) - $escape_html($card.title)
+ + +#foreach($card in $cards) + + + + #if($sorting_key) + #end -
- +
+
+ + + #if($sorting_key) + + #end +
+#end + +
+ + + + + + + + + + + #foreach($card in $cards) + + + + + #if($card.cp) + + #else + + #end + + + + #end + + + + + + + + + + + + + + + + + +
TitleFilenameCPCopies ownedDescription
+ + + $escape_html($card.filename)$escape_html($card.cp) CP0 CP + + $escape_html($card.copies_owned) + + $escape_html($card.description.split("\n").get(0))
Total:N/A$escape_html($sum_cp)$escape_html($sum_copies)N/A

+ + +
diff --git a/server.py b/server.py index 753bebb..0d83bec 100755 --- a/server.py +++ b/server.py @@ -13,10 +13,39 @@ async def root(request): return response.redirect('/cards/') @app.get("/cards/") +@app.post("/cards/") @withTemplate("cards/cardlist.vm") async def show_cardlist(request, template={}): + if "action" in request.form and "filename" in request.form: + with card.open_file(request.form.get("filename")) as c: + if request.form.get("action") == "increment_stock": + c.copies_owned += 1 + elif request.form.get("action") == "decrement_stock": + c.copies_owned -= 1 + cards = card.from_dir(config.carddir) - + + sorting_key = None + if "sort" in request.args: + sorting_key = request.args["sort"][0] + if "sort" in request.form: + sorting_key = request.form["sort"][0] + + if sorting_key: + if sorting_key == "filename": + cards = sorted(cards, key=lambda x: x.filename.lower()) + elif sorting_key == "cp": + cards = sorted(cards, key=lambda x: -int(x.cp or 0)) + elif sorting_key == "description": + cards = sorted(cards, key=lambda x: x.description or "") + elif sorting_key == "copies": + cards = sorted(cards, key=lambda x: -int(x.copies_owned)) + elif sorting_key == "title": + cards = sorted(cards, key=lambda x: x.title.lower()) + + sum_cp = sum(int(i.copies_owned) * int(i.cp or 0) for i in cards) + sum_copies = sum(int(i.copies_owned) for i in cards) + return response.html(template["cardlist.vm"].merge(locals())) @app.get('/cards/creator')