2 from bottle import debug, run, default_app, get, view, request, post, redirect
7 web_path = '/guiltcards'
10 def get_card_data(card_id):
11 if not os.path.exists(cards_dir):
12 os.makedirs(cards_dir)
13 path_card = cards_dir + card_id
14 if os.path.exists(path_card):
15 with open(path_card, 'r') as f:
16 data = json.loads(f.read())
20 'answers': ['?', '?']}
28 @get(web_path + '/cards')
31 if not os.path.exists(cards_dir):
32 os.makedirs(cards_dir)
33 card_ids = os.listdir(cards_dir)
35 for card_id in card_ids:
36 cards[card_id] = base64.b64decode(card_id).decode()
37 return dict(web_path=web_path,
40 @get(web_path + '/cards/')
42 card_name = request.query.get('card_name')
43 card_id = base64.b64encode(card_name.encode()).decode()
44 redirect(web_path + '/cards/' + card_id + '/form')
46 @get(web_path + '/cards/<card_id>/view')
48 def show_card(card_id):
49 data = get_card_data(card_id)
50 return dict(web_path=web_path,
52 prompt=data['prompt'],
53 answers=data['answers'])
55 @post(web_path + '/cards/<card_id>')
56 def update_card(card_id):
57 if not os.path.exists(cards_dir):
58 os.makedirs(cards_dir)
59 path_card = cards_dir + card_id
61 'title': request.forms.get('title'),
62 'prompt': request.forms.get('prompt'),
63 'answers': request.forms.getall('answer')
65 json_dict['answers'] = [answer for answer in json_dict['answers']
66 if answer.strip() != '']
67 with open(path_card, 'w') as f:
68 f.write(json.dumps(json_dict, indent=4))
69 redirect(web_path + '/cards/' + card_id + '/view')
71 @get(web_path + '/cards/<card_id>/form')
73 def card_form(card_id):
74 data = get_card_data(card_id)
75 card_path = cards_dir + '/' + card_id
77 if os.path.exists(card_path):
79 return dict(web_path=web_path,
82 prompt=data['prompt'],
83 answers=data['answers'],
86 @get(web_path + '/cards/<card_id>/delete')
88 def delete_card_ask(card_id):
89 card_name = base64.b64decode(card_id.encode()).decode()
90 return dict(web_path=web_path,
94 @post(web_path + '/cards/<card_id>/delete')
95 def delete_card_do(card_id):
96 card_path = cards_dir + '/' + card_id
97 if os.path.exists(card_path):
99 redirect(web_path + '/cards')
103 if __name__ == "__main__":
107 app = application = default_app()