2 from bottle import debug, run, default_app, get, view, request, post, redirect
6 web_path = '/guiltcards'
9 def get_card_data(card_id):
10 if not os.path.exists(cards_dir):
11 os.makedirs(cards_dir)
12 path_card = cards_dir + card_id
13 if os.path.exists(path_card):
14 with open(path_card, 'r') as f:
15 data = json.loads(f.read())#f.read()
19 'answers': ['?', '?']}
27 @get(web_path + '/cards')
30 if not os.path.exists(cards_dir):
31 os.makedirs(cards_dir)
32 card_ids = os.listdir(cards_dir)
33 return dict(web_path=web_path,
36 @get(web_path + '/cards/')
38 card_id = request.query.get('card_id')
39 redirect(web_path + '/cards/' + card_id + '/form')
41 @get(web_path + '/cards/<card_id>/view')
43 def show_card(card_id):
44 data = get_card_data(card_id)
45 return dict(web_path=web_path,
47 prompt=data['prompt'],
48 answers=data['answers'])
50 @post(web_path + '/cards/<card_id>')
51 def update_card(card_id):
52 if not os.path.exists(cards_dir):
53 os.makedirs(cards_dir)
54 path_card = cards_dir + card_id
56 'title': request.forms.get('title'),
57 'prompt': request.forms.get('prompt'),
58 'answers': request.forms.getall('answer')
60 json_dict['answers'] = [answer for answer in json_dict['answers']
61 if answer.strip() != '']
62 with open(path_card, 'w') as f:
63 f.write(json.dumps(json_dict, indent=4))
64 redirect(web_path + '/cards/' + card_id + '/view')
66 @get(web_path + '/cards/<card_id>/form')
68 def card_form(card_id):
69 data = get_card_data(card_id)
70 card_path = cards_dir + '/' + card_id
72 if os.path.exists(card_path):
74 return dict(web_path=web_path,
77 prompt=data['prompt'],
78 answers=data['answers'],
81 @get(web_path + '/cards/<card_id>/delete')
83 def delete_card_ask(card_id):
84 return dict(web_path=web_path,
87 @post(web_path + '/cards/<card_id>/delete')
88 def delete_card_do(card_id):
89 card_path = cards_dir + '/' + card_id
90 if os.path.exists(card_path):
92 redirect(web_path + '/cards')
96 if __name__ == "__main__":
100 app = application = default_app()