home · contact · privacy
cad1cb36da57bb3f49a3857b9b0c250649293f11
[guiltcards] / guiltcards.py
1 #!/usr/bin/env python3
2 from bottle import debug, run, default_app, get, view, request, post, redirect
3 import os
4 import json
5 import base64
6
7 web_path = '/guiltcards'
8 decks_dir = 'decks/'
9
10 def get_card_data(deck_id, card_id):
11     cards_dir = decks_dir + deck_id
12     if not os.path.exists(cards_dir):
13         os.makedirs(cards_dir)
14     path_card = cards_dir + '/' + card_id
15     if os.path.exists(path_card):
16         with open(path_card, 'r') as f:
17             data = json.loads(f.read())
18     else:
19         data = {'title': '?',
20                 'prompt':'?',
21                 'answers': ['?', '?']}
22     return data
23
24 @get(web_path + '/')
25 @get(web_path + '/decks')
26 @view('decks')
27 def list_decks():
28     if not os.path.exists(decks_dir):
29         os.makedirs(decks_dir)
30     deck_ids = os.listdir(decks_dir)
31     decks = {}
32     print('DEBUG', deck_ids)
33     for deck_id in deck_ids:
34         decks[deck_id] = base64.b64decode(deck_id).decode()
35     return dict(web_path=web_path, decks=decks)
36
37 @get(web_path + '/decks/')
38 def new_deck():
39     deck_name = request.query.get('deck_name')
40     deck_id = base64.b64encode(deck_name.encode()).decode()
41     redirect(web_path + '/decks/' + deck_id + '/cards')
42
43 @get(web_path + '/decks/<deck_id>/cards')
44 @get(web_path + '/decks/<deck_id>/')
45 @get(web_path + '/decks/<deck_id>')
46 @view('cards')
47 def list_cards(deck_id):
48     cards_dir = decks_dir + deck_id
49     card_ids = []
50     if os.path.exists(cards_dir):
51         card_ids = os.listdir(cards_dir)
52     cards = {}
53     for card_id in card_ids:
54         cards[card_id] = base64.b64decode(card_id).decode()
55     deck_name = base64.b64decode(deck_id).decode()
56     return dict(web_path=web_path,
57                 deck_id=deck_id,
58                 deck_name=deck_name,
59                 cards=cards)
60
61 @get(web_path + '/decks/<deck_id>/cards/')
62 def new_card(deck_id):
63     card_name = request.query.get('card_name')
64     card_id = base64.b64encode(card_name.encode()).decode()
65     redirect(web_path + '/decks/' + deck_id + '/cards/' + card_id + '/form')
66
67 @get(web_path + '/decks/<deck_id>/cards/<card_id>/view')
68 @view('card')
69 def show_card(deck_id, card_id):
70     data = get_card_data(deck_id, card_id)
71     return dict(web_path=web_path,
72                 deck_id=deck_id,
73                 title=data['title'],
74                 prompt=data['prompt'],
75                 answers=data['answers'])
76
77 @post(web_path + '/decks/<deck_id>/cards/<card_id>')
78 def update_card(deck_id, card_id):
79     cards_dir = decks_dir + deck_id
80     if not os.path.exists(cards_dir):
81         os.makedirs(cards_dir)
82     path_card = cards_dir + '/' + card_id
83     json_dict = {
84         'title': request.forms.get('title'),
85         'prompt': request.forms.get('prompt'),
86         'answers': request.forms.getall('answer')
87     }
88     json_dict['answers'] = [answer for answer in json_dict['answers']
89                             if answer.strip() != '']
90     with open(path_card, 'w') as f:
91         f.write(json.dumps(json_dict, indent=4))
92     redirect(web_path + '/decks/' + deck_id + '/cards/' + card_id + '/view')
93
94 @get(web_path + '/decks/<deck_id>/cards/<card_id>/form')
95 @view('card_form')
96 def card_form(deck_id, card_id):
97     data = get_card_data(deck_id, card_id)
98     card_path = decks_dir + deck_id + '/' + card_id
99     deletable = False
100     if os.path.exists(card_path):
101         deletable = True
102     return dict(web_path=web_path,
103                 card_id=card_id,
104                 deck_id=deck_id,
105                 title=data['title'],
106                 prompt=data['prompt'],
107                 answers=data['answers'],
108                 deletable=deletable)
109
110 @get(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
111 @view('delete_card')
112 def delete_card_ask(deck_id, card_id):
113     card_name = base64.b64decode(card_id.encode()).decode()
114     return dict(web_path=web_path,
115                 deck_id=deck_id,
116                 card_name=card_name,
117                 card_id=card_id)
118
119 @post(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
120 def delete_card_do(deck_id, card_id):
121     cards_dir = decks_dir + deck_id
122     card_path = cards_dir + '/' + card_id
123     if os.path.exists(card_path):
124         os.remove(card_path)
125     redirect(web_path + '/decks/' + deck_id + '/cards')
126
127 # App running.
128
129 if __name__ == "__main__":
130     debug(True)
131     run(port=8000)
132 else:
133     app = application = default_app()