From c0290f81d02b98927666ccb8a7e94147bae79ac0 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 11 Nov 2020 23:59:44 +0100
Subject: [PATCH] Add deck management.

---
 guiltcards.py         | 86 ++++++++++++++++++++++++++++---------------
 views/card.tpl        |  2 +-
 views/card_form.tpl   |  4 +-
 views/cards.tpl       | 10 +++--
 views/delete_card.tpl |  4 +-
 views/intro.tpl       |  5 ---
 6 files changed, 68 insertions(+), 43 deletions(-)
 delete mode 100644 views/intro.tpl

diff --git a/guiltcards.py b/guiltcards.py
index aa77ce3..cad1cb3 100755
--- a/guiltcards.py
+++ b/guiltcards.py
@@ -5,12 +5,13 @@ import json
 import base64
 
 web_path = '/guiltcards'
-cards_dir = 'cards/'
+decks_dir = 'decks/'
 
-def get_card_data(card_id):
+def get_card_data(deck_id, card_id):
+    cards_dir = decks_dir + deck_id
     if not os.path.exists(cards_dir):
         os.makedirs(cards_dir)
-    path_card = cards_dir + 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())
@@ -21,42 +22,64 @@ def get_card_data(card_id):
     return data
 
 @get(web_path + '/')
-@view('intro')
-def intro():
-    return dict()
+@get(web_path + '/decks')
+@view('decks')
+def list_decks():
+    if not os.path.exists(decks_dir):
+        os.makedirs(decks_dir)
+    deck_ids = os.listdir(decks_dir)
+    decks = {}
+    print('DEBUG', deck_ids)
+    for deck_id in deck_ids:
+        decks[deck_id] = base64.b64decode(deck_id).decode()
+    return dict(web_path=web_path, decks=decks)
 
-@get(web_path + '/cards')
+@get(web_path + '/decks/')
+def new_deck():
+    deck_name = request.query.get('deck_name')
+    deck_id = base64.b64encode(deck_name.encode()).decode()
+    redirect(web_path + '/decks/' + deck_id + '/cards')
+
+@get(web_path + '/decks/<deck_id>/cards')
+@get(web_path + '/decks/<deck_id>/')
+@get(web_path + '/decks/<deck_id>')
 @view('cards')
-def list_cards():
-    if not os.path.exists(cards_dir):
-        os.makedirs(cards_dir)
-    card_ids = os.listdir(cards_dir)
+def list_cards(deck_id):
+    cards_dir = decks_dir + deck_id
+    card_ids = []
+    if os.path.exists(cards_dir):
+        card_ids = os.listdir(cards_dir)
     cards = {}
     for card_id in card_ids:
         cards[card_id] = base64.b64decode(card_id).decode()
+    deck_name = base64.b64decode(deck_id).decode()
     return dict(web_path=web_path,
+                deck_id=deck_id,
+                deck_name=deck_name,
                 cards=cards)
 
-@get(web_path + '/cards/')
-def new_card():
+@get(web_path + '/decks/<deck_id>/cards/')
+def new_card(deck_id):
     card_name = request.query.get('card_name')
     card_id = base64.b64encode(card_name.encode()).decode()
-    redirect(web_path + '/cards/' + card_id + '/form')
+    redirect(web_path + '/decks/' + deck_id + '/cards/' + card_id + '/form')
 
-@get(web_path + '/cards/<card_id>/view')
+@get(web_path + '/decks/<deck_id>/cards/<card_id>/view')
 @view('card')
-def show_card(card_id):
-    data = get_card_data(card_id)
+def show_card(deck_id, card_id):
+    data = get_card_data(deck_id, card_id)
     return dict(web_path=web_path,
+                deck_id=deck_id,
                 title=data['title'],
                 prompt=data['prompt'],
                 answers=data['answers'])
 
-@post(web_path + '/cards/<card_id>')
-def update_card(card_id):
+@post(web_path + '/decks/<deck_id>/cards/<card_id>')
+def update_card(deck_id, card_id):
+    cards_dir = decks_dir + deck_id
     if not os.path.exists(cards_dir):
         os.makedirs(cards_dir)
-    path_card = cards_dir + card_id
+    path_card = cards_dir + '/' + card_id
     json_dict = {
         'title': request.forms.get('title'),
         'prompt': request.forms.get('prompt'),
@@ -66,37 +89,40 @@ def update_card(card_id):
                             if answer.strip() != '']
     with open(path_card, 'w') as f:
         f.write(json.dumps(json_dict, indent=4))
-    redirect(web_path + '/cards/' + card_id + '/view')
+    redirect(web_path + '/decks/' + deck_id + '/cards/' + card_id + '/view')
 
-@get(web_path + '/cards/<card_id>/form')
+@get(web_path + '/decks/<deck_id>/cards/<card_id>/form')
 @view('card_form')
-def card_form(card_id):
-    data = get_card_data(card_id)
-    card_path = cards_dir + '/' + card_id
+def card_form(deck_id, card_id):
+    data = get_card_data(deck_id, card_id)
+    card_path = decks_dir + deck_id + '/' + card_id
     deletable = False
     if os.path.exists(card_path):
         deletable = True
     return dict(web_path=web_path,
                 card_id=card_id,
+                deck_id=deck_id,
                 title=data['title'],
                 prompt=data['prompt'],
                 answers=data['answers'],
                 deletable=deletable)
 
-@get(web_path + '/cards/<card_id>/delete')
+@get(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
 @view('delete_card')
-def delete_card_ask(card_id):
+def delete_card_ask(deck_id, card_id):
     card_name = base64.b64decode(card_id.encode()).decode()
     return dict(web_path=web_path,
+                deck_id=deck_id,
                 card_name=card_name,
                 card_id=card_id)
 
-@post(web_path + '/cards/<card_id>/delete')
-def delete_card_do(card_id):
+@post(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
+def delete_card_do(deck_id, card_id):
+    cards_dir = decks_dir + deck_id
     card_path = cards_dir + '/' + card_id
     if os.path.exists(card_path):
         os.remove(card_path)
-    redirect(web_path + '/cards')
+    redirect(web_path + '/decks/' + deck_id + '/cards')
 
 # App running.
 
diff --git a/views/card.tpl b/views/card.tpl
index 8e89f50..cb121e6 100644
--- a/views/card.tpl
+++ b/views/card.tpl
@@ -17,6 +17,6 @@
     % end
     <ul/>
   </div>
-<a href="{{ web_path }}/cards">back to overview</a>
+<a href="{{ web_path }}/decks/{{deck_id}}/cards">back to overview</a>
 </body>
 </html>
diff --git a/views/card_form.tpl b/views/card_form.tpl
index a2aa551..c4c6488 100644
--- a/views/card_form.tpl
+++ b/views/card_form.tpl
@@ -1,7 +1,7 @@
 <!DOCTYPE HTML>
 <html>
 <body>
-<form action="{{ web_path }}/cards/{{ card_id }}" method="POST">
+<form action="{{ web_path }}/decks/{{deck_id}}/cards/{{ card_id }}" method="POST">
 title: <input type="text" name="title" value="{{ title }}" /><br />
 prompt: <input type="text" name="prompt" value="{{ prompt }}" /><br />
 % for answer in answers:
@@ -17,7 +17,7 @@ answer: <input type="text" name="answer" /><br />
 </p>
 % if deletable:
 <p>
-Or would you rather <a href="{{ web_path }}/cards/{{ card_id }}/delete">DELETE</a> this card?
+Or would you rather <a href="{{ web_path }}/decks/{{deck_id}}/cards/{{ card_id }}/delete">DELETE</a> this card?
 </p>
 % end
 </form>
diff --git a/views/cards.tpl b/views/cards.tpl
index 758fe22..2c20cb0 100644
--- a/views/cards.tpl
+++ b/views/cards.tpl
@@ -1,13 +1,17 @@
 <!DOCTYPE HTML>
 <html>
 <body>
+<h1>deck: {{deck_name}}</h1>
 <ul>
 % 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>
+<li><a href="{{ web_path }}/decks/{{deck_id}}/cards/{{card_id}}/view">{{cards[card_id]}}</a> (<a href="{{ web_path }}/decks/{{deck_id}}/cards/{{card_id}}/form">edit</a>)</li>
 % end
 </ul>
-<form action="{{ web_path }}/cards/" method="GET">
-add another? name: <input type="text" name="card_name" />
+<form action="{{ web_path }}/decks/{{deck_id}}/cards/" method="GET">
+add another card? name: <input type="text" name="card_name" />
 </form>
+<p>
+<a href="{{ web_path }}/decks">back to decks overview</a>
+</p>
 </body>
 </html>
diff --git a/views/delete_card.tpl b/views/delete_card.tpl
index cd1ec0f..c9c0677 100644
--- a/views/delete_card.tpl
+++ b/views/delete_card.tpl
@@ -1,9 +1,9 @@
 <!DOCTYPE HTML>
 <html>
 <body>
-<form action="{{ web_path }}/cards/{{ card_id }}/delete" method="POST">
+<form action="{{ web_path }}/decks/{{ deck_id }}/cards/{{ card_id }}/delete" method="POST">
 <input type="submit" value="delete {{card_name}}?" />
 </form>
-<a href="{{ web_path }}/cards">Nah, better not …</a>
+<a href="{{ web_path }}/decks/{{ deck_id }}/cards">Nah, better not …</a>
 </body>
 </html>
diff --git a/views/intro.tpl b/views/intro.tpl
deleted file mode 100644
index e24a7a0..0000000
--- a/views/intro.tpl
+++ /dev/null
@@ -1,5 +0,0 @@
-<html>
-<body>
-Hey there!
-</body>
-</html>
-- 
2.30.2