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())#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)
34 return dict(web_path=web_path,
37 @get(web_path + '/cards/')
39 card_id = request.query.get('card_id')
40 redirect(web_path + '/cards/' + card_id + '/form')
42 @get(web_path + '/cards/<card_id>/view')
44 def show_card(card_id):
45 data = get_card_data(card_id)
46 return dict(web_path=web_path,
48 prompt=data['prompt'],
49 answers=data['answers'])
51 @post(web_path + '/cards/<card_id>')
52 def update_card(card_id):
53 if not os.path.exists(cards_dir):
54 os.makedirs(cards_dir)
55 path_card = cards_dir + card_id
57 'title': request.forms.get('title'),
58 'prompt': request.forms.get('prompt'),
59 'answers': request.forms.getall('answer')
61 json_dict['answers'] = [answer for answer in json_dict['answers']
62 if answer.strip() != '']
63 with open(path_card, 'w') as f:
64 f.write(json.dumps(json_dict, indent=4))
65 redirect(web_path + '/cards/' + card_id + '/view')
67 @get(web_path + '/cards/<card_id>/form')
69 def card_form(card_id):
70 data = get_card_data(card_id)
71 card_path = cards_dir + '/' + card_id
73 if os.path.exists(card_path):
75 return dict(web_path=web_path,
78 prompt=data['prompt'],
79 answers=data['answers'],
82 @get(web_path + '/cards/<card_id>/delete')
84 def delete_card_ask(card_id):
85 return dict(web_path=web_path,
88 @post(web_path + '/cards/<card_id>/delete')
89 def delete_card_do(card_id):
90 card_path = cards_dir + '/' + card_id
91 if os.path.exists(card_path):
93 redirect(web_path + '/cards')
97 if __name__ == "__main__":
101 app = application = default_app()