home · contact · privacy
Fix single-card view CSS.
[guiltcards] / guiltcards.py
index aa77ce3f789add3e2cf05a8d65b35db27aaee6e8..00f02f8344bb0e0bfd4927d86ff1ed66132084c2 100755 (executable)
@@ -5,98 +5,140 @@ 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, card_type_hint):
+    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())
     else:
-        data = {'title': '?',
-                'prompt':'?',
-                'answers': ['?', '?']}
+        data = {'type': card_type_hint,
+                'title': 'title',
+                'paragraphs': ['first paragraph', 'second paragraph'],
+                'answers': ['first option', 'second option']}
     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 = {}
+    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>/printable')
+@view('cards_print')
+def print_view(deck_id):
+    cards_dir = decks_dir + deck_id
+    cards = []
+    if os.path.exists(cards_dir):
+        card_ids = os.listdir(cards_dir)
+        for card_id in card_ids:
+            cards += [get_card_data(deck_id, card_id, None)]
+    deck_name = base64.b64decode(deck_id).decode()
+    return dict(cards=cards)
+
+@get(web_path + '/decks/<deck_id>/cards/')
+def new_card(deck_id):
     card_name = request.query.get('card_name')
+    card_type = request.query.get('card_type')
     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?type=' + card_type)
 
-@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, None)
     return dict(web_path=web_path,
-                title=data['title'],
-                prompt=data['prompt'],
-                answers=data['answers'])
+                deck_id=deck_id,
+                card_id=card_id,
+                card=data)
 
-@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
-    json_dict = {
-        'title': request.forms.get('title'),
-        'prompt': request.forms.get('prompt'),
-        'answers': request.forms.getall('answer')
-    }
-    json_dict['answers'] = [answer for answer in json_dict['answers']
-                            if answer.strip() != '']
+    path_card = cards_dir + '/' + card_id
+    card_type = request.forms.get('type')
+    json_dict = {'type': request.forms.get('type')}
+    if card_type == 'action':
+        json_dict['title'] = request.forms.get('title')
+        json_dict['paragraphs'] = request.forms.getall('paragraph')
+        json_dict['paragraphs'] = [paragraph for paragraph in json_dict['paragraphs']
+                                if paragraph.strip() != '']
+    else:
+        json_dict['answers'] = request.forms.getall('answer')
+        json_dict['answers'] = [answer for answer in json_dict['answers']
+                                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):
+    card_type = request.query.get('type')
+    data = get_card_data(deck_id, card_id, card_type)
+    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,
-                title=data['title'],
-                prompt=data['prompt'],
-                answers=data['answers'],
+                deck_id=deck_id,
+                card=data,
                 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.