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