home · contact · privacy
Change default web path.
[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
6 web_path = '/guiltcards'
7 cards_dir = 'cards/'
8
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()
16     else:
17         data = {'title': '?',
18                 'prompt':'?',
19                 'answers': ['?', '?']}
20     return data
21
22 @get(web_path + '/')
23 @view('intro')
24 def intro():
25     return dict()
26
27 @get(web_path + '/cards')
28 @view('cards')
29 def list_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,
34                 card_ids=card_ids)
35
36 @get(web_path + '/cards/')
37 def new_card():
38     card_id = request.query.get('card_id')
39     redirect(web_path + '/cards/' + card_id + '/form')
40
41 @get(web_path + '/cards/<card_id>/view')
42 @view('card')
43 def show_card(card_id):
44     data = get_card_data(card_id)
45     return dict(web_path=web_path,
46                 title=data['title'],
47                 prompt=data['prompt'],
48                 answers=data['answers'])
49
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
55     json_dict = {
56         'title': request.forms.get('title'),
57         'prompt': request.forms.get('prompt'),
58         'answers': request.forms.getall('answer')
59     }
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')
65
66 @get(web_path + '/cards/<card_id>/form')
67 @view('card_form')
68 def card_form(card_id):
69     data = get_card_data(card_id)
70     card_path = cards_dir + '/' + card_id
71     deletable = False
72     if os.path.exists(card_path):
73         deletable = True
74     return dict(web_path=web_path,
75                 card_id=card_id,
76                 title=data['title'],
77                 prompt=data['prompt'],
78                 answers=data['answers'],
79                 deletable=deletable)
80
81 @get(web_path + '/cards/<card_id>/delete')
82 @view('delete_card')
83 def delete_card_ask(card_id):
84     return dict(web_path=web_path,
85                 card_id=card_id)
86
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):
91         os.remove(card_path)
92     redirect(web_path + '/cards')
93
94 # App running.
95
96 if __name__ == "__main__":
97     debug(True)
98     run(port=8000)
99 else:
100     app = application = default_app()