From d4956dd8a9031b23be3bfd9fe791d413c64e919d Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 9 Nov 2020 21:59:48 +0100
Subject: [PATCH] Use b64 for card IDs.

---
 guiltcards.py         | 13 ++++++++++---
 views/cards.tpl       |  6 +++---
 views/delete_card.tpl |  2 +-
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/guiltcards.py b/guiltcards.py
index 92cd49b..aa77ce3 100755
--- a/guiltcards.py
+++ b/guiltcards.py
@@ -2,6 +2,7 @@
 from bottle import debug, run, default_app, get, view, request, post, redirect
 import os
 import json
+import base64
 
 web_path = '/guiltcards'
 cards_dir = 'cards/'
@@ -12,7 +13,7 @@ def get_card_data(card_id):
     path_card = cards_dir + card_id
     if os.path.exists(path_card):
         with open(path_card, 'r') as f:
-            data = json.loads(f.read())#f.read()
+            data = json.loads(f.read())
     else:
         data = {'title': '?',
                 'prompt':'?',
@@ -30,12 +31,16 @@ def list_cards():
     if not os.path.exists(cards_dir):
         os.makedirs(cards_dir)
     card_ids = os.listdir(cards_dir)
+    cards = {}
+    for card_id in card_ids:
+        cards[card_id] = base64.b64decode(card_id).decode()
     return dict(web_path=web_path,
-                card_ids=card_ids)
+                cards=cards)
 
 @get(web_path + '/cards/')
 def new_card():
-    card_id = request.query.get('card_id')
+    card_name = request.query.get('card_name')
+    card_id = base64.b64encode(card_name.encode()).decode()
     redirect(web_path + '/cards/' + card_id + '/form')
 
 @get(web_path + '/cards/<card_id>/view')
@@ -81,7 +86,9 @@ def card_form(card_id):
 @get(web_path + '/cards/<card_id>/delete')
 @view('delete_card')
 def delete_card_ask(card_id):
+    card_name = base64.b64decode(card_id.encode()).decode()
     return dict(web_path=web_path,
+                card_name=card_name,
                 card_id=card_id)
 
 @post(web_path + '/cards/<card_id>/delete')
diff --git a/views/cards.tpl b/views/cards.tpl
index dbf5ea6..758fe22 100644
--- a/views/cards.tpl
+++ b/views/cards.tpl
@@ -2,12 +2,12 @@
 <html>
 <body>
 <ul>
-% for card_id in card_ids:
-<li><a href="{{ web_path }}/cards/{{card_id}}/view">{{card_id}}</a> (<a href="{{ web_path }}/cards/{{card_id}}/form">edit</a>)</li>
+% for card_id in cards:
+<li><a href="{{ web_path }}/cards/{{card_id}}/view">{{cards[card_id]}}</a> (<a href="{{ web_path }}/cards/{{card_id}}/form">edit</a>)</li>
 % end
 </ul>
 <form action="{{ web_path }}/cards/" method="GET">
-add another? name: <input type="text" name="card_id" />
+add another? name: <input type="text" name="card_name" />
 </form>
 </body>
 </html>
diff --git a/views/delete_card.tpl b/views/delete_card.tpl
index 45b6449..cd1ec0f 100644
--- a/views/delete_card.tpl
+++ b/views/delete_card.tpl
@@ -2,7 +2,7 @@
 <html>
 <body>
 <form action="{{ web_path }}/cards/{{ card_id }}/delete" method="POST">
-<input type="submit" value="delete {{card_id}}?" />
+<input type="submit" value="delete {{card_name}}?" />
 </form>
 <a href="{{ web_path }}/cards">Nah, better not …</a>
 </body>
-- 
2.30.2