home · contact · privacy
Use b64 for card IDs.
[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 import base64
6
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())
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     cards = {}
35     for card_id in card_ids:
36         cards[card_id] = base64.b64decode(card_id).decode()
37     return dict(web_path=web_path,
38                 cards=cards)
39
40 @get(web_path + '/cards/')
41 def new_card():
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')
45
46 @get(web_path + '/cards/<card_id>/view')
47 @view('card')
48 def show_card(card_id):
49     data = get_card_data(card_id)
50     return dict(web_path=web_path,
51                 title=data['title'],
52                 prompt=data['prompt'],
53                 answers=data['answers'])
54
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
60     json_dict = {
61         'title': request.forms.get('title'),
62         'prompt': request.forms.get('prompt'),
63         'answers': request.forms.getall('answer')
64     }
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')
70
71 @get(web_path + '/cards/<card_id>/form')
72 @view('card_form')
73 def card_form(card_id):
74     data = get_card_data(card_id)
75     card_path = cards_dir + '/' + card_id
76     deletable = False
77     if os.path.exists(card_path):
78         deletable = True
79     return dict(web_path=web_path,
80                 card_id=card_id,
81                 title=data['title'],
82                 prompt=data['prompt'],
83                 answers=data['answers'],
84                 deletable=deletable)
85
86 @get(web_path + '/cards/<card_id>/delete')
87 @view('delete_card')
88 def delete_card_ask(card_id):
89     card_name = base64.b64decode(card_id.encode()).decode()
90     return dict(web_path=web_path,
91                 card_name=card_name,
92                 card_id=card_id)
93
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):
98         os.remove(card_path)
99     redirect(web_path + '/cards')
100
101 # App running.
102
103 if __name__ == "__main__":
104     debug(True)
105     run(port=8000)
106 else:
107     app = application = default_app()