Add better cardlist with sorting

This commit is contained in:
2017-10-15 11:44:12 +02:00
parent e45da6436f
commit 676e9dc051
3 changed files with 133 additions and 7 deletions
+24
View File
@@ -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;
}
+79 -6
View File
@@ -1,11 +1,84 @@
<!DOCTYPE html>
<link rel="stylesheet" href="cardlist.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<form action="show" method="get">
#foreach($card in $cards)
<input type="checkbox" name="card" value="$escape_html($card.filename)">$escape_html($card.title)<br>
<script>
function toggle() {
checkboxes = document.getElementsByName('card');
var target = ! checkboxes[0].checked;
for(var i=0; i < checkboxes.length; i++) {
checkboxes[i].checked = target;
}
}
</script>
#foreach($card in $cards)
<form id="$escape_html($card.filename)_inc" action="" method="post">
<input type="hidden" name="filename" value="$escape_html($card.filename)">
<input type="hidden" name="action" value="increment_stock">
#if($sorting_key)
<input type="hidden" name="sort" value="$sorting_key">
#end
<br>
<input type="submit" value="Submit">
</form>
<form id="$escape_html($card.filename)_dec" action="" method="post">
<input type="hidden" name="filename" value="$escape_html($card.filename)">
<input type="hidden" name="action" value="decrement_stock">
#if($sorting_key)
<input type="hidden" name="sort" value="$sorting_key">
#end
</form>
#end
<form action="show" method="get" id="cardlist">
<table>
<tr>
<th></th>
<th><a href="../cards/?sort=title">Title</a></th>
<th><a href="../cards/?sort=filename" >Filename</a></th>
<th style="text-align: center;"><a href="../cards/?sort=cp">CP</a></th>
<th><a href="../cards/?sort=copies">Copies owned</a></th>
<th><a href="../cards/?sort=description">Description</a></th>
</tr>
#foreach($card in $cards)
<tr>
<td>
<button type="button" onclick="location.href='creator?filename=$escape_html($card.filename)'">Edit</button>
<input type="checkbox" name="card" value="$escape_html($card.filename)" id="$escape_html($card.filename)_check">
</td>
<td><label for="$escape_html($card.filename)_check">$escape_html($card.title)</label></td>
<td>$escape_html($card.filename)</td>
#if($card.cp)
<td style="text-align: right;">$escape_html($card.cp) CP</td>
#else
<td style="text-align: right;">0 CP</td>
#end
<td style="text-align: center;">
<button type="button" class="small" onclick="document.getElementById('$escape_html($card.filename)_dec').submit()">-</button>
$escape_html($card.copies_owned)
<button type="button" class="small" onclick="document.getElementById('$escape_html($card.filename)_inc').submit()">+</button>
</td>
<td>$escape_html($card.description.split("\n").get(0))</td>
</tr>
#end
<tr>
<td><button type="button" onclick="location.href='creator'">New</button></td>
</tr>
<tr>
<td></td>
<td>Total:</td>
<td>N/A</td>
<td>$escape_html($sum_cp)</td>
<td>$escape_html($sum_copies)</td>
<td>N/A</td>
</tr>
<tr>
<td></td>
<td><br>
<button type="button" onclick="toggle();">Toggle all</button>
<input type="submit" value="View selected">
</td>
</tr>
</table>
</form>
+30 -1
View File
@@ -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')