2 from bottle import debug, run, default_app, get, view, request, post, redirect
7 web_path = '/guiltcards'
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())
21 'answers': ['?', '?']}
25 @get(web_path + '/decks')
28 if not os.path.exists(decks_dir):
29 os.makedirs(decks_dir)
30 deck_ids = os.listdir(decks_dir)
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)
37 @get(web_path + '/decks/')
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')
43 @get(web_path + '/decks/<deck_id>/cards')
44 @get(web_path + '/decks/<deck_id>/')
45 @get(web_path + '/decks/<deck_id>')
47 def list_cards(deck_id):
48 cards_dir = decks_dir + deck_id
50 if os.path.exists(cards_dir):
51 card_ids = os.listdir(cards_dir)
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,
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')
67 @get(web_path + '/decks/<deck_id>/cards/<card_id>/view')
69 def show_card(deck_id, card_id):
70 data = get_card_data(deck_id, card_id)
71 return dict(web_path=web_path,
74 prompt=data['prompt'],
75 answers=data['answers'])
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
84 'title': request.forms.get('title'),
85 'prompt': request.forms.get('prompt'),
86 'answers': request.forms.getall('answer')
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')
94 @get(web_path + '/decks/<deck_id>/cards/<card_id>/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
100 if os.path.exists(card_path):
102 return dict(web_path=web_path,
106 prompt=data['prompt'],
107 answers=data['answers'],
110 @get(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
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,
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):
125 redirect(web_path + '/decks/' + deck_id + '/cards')
129 if __name__ == "__main__":
133 app = application = default_app()