home · contact · privacy
Fix single-card view CSS.
[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 decks_dir = 'decks/'
9
10 def get_card_data(deck_id, card_id, card_type_hint):
11     cards_dir = decks_dir + deck_id
12     if not os.path.exists(cards_dir):
13         os.makedirs(cards_dir)
14     path_card = cards_dir + '/' + card_id
15     if os.path.exists(path_card):
16         with open(path_card, 'r') as f:
17             data = json.loads(f.read())
18     else:
19         data = {'type': card_type_hint,
20                 'title': 'title',
21                 'paragraphs': ['first paragraph', 'second paragraph'],
22                 'answers': ['first option', 'second option']}
23     return data
24
25 @get(web_path + '/')
26 @get(web_path + '/decks')
27 @view('decks')
28 def list_decks():
29     if not os.path.exists(decks_dir):
30         os.makedirs(decks_dir)
31     deck_ids = os.listdir(decks_dir)
32     decks = {}
33     for deck_id in deck_ids:
34         decks[deck_id] = base64.b64decode(deck_id).decode()
35     return dict(web_path=web_path, decks=decks)
36
37 @get(web_path + '/decks/')
38 def new_deck():
39     deck_name = request.query.get('deck_name')
40     deck_id = base64.b64encode(deck_name.encode()).decode()
41     redirect(web_path + '/decks/' + deck_id + '/cards')
42
43 @get(web_path + '/decks/<deck_id>/cards')
44 @get(web_path + '/decks/<deck_id>/')
45 @get(web_path + '/decks/<deck_id>')
46 @view('cards')
47 def list_cards(deck_id):
48     cards_dir = decks_dir + deck_id
49     card_ids = []
50     if os.path.exists(cards_dir):
51         card_ids = os.listdir(cards_dir)
52     cards = {}
53     for card_id in card_ids:
54         cards[card_id] = base64.b64decode(card_id).decode()
55     deck_name = base64.b64decode(deck_id).decode()
56     return dict(web_path=web_path,
57                 deck_id=deck_id,
58                 deck_name=deck_name,
59                 cards=cards)
60
61 @get(web_path + '/decks/<deck_id>/printable')
62 @view('cards_print')
63 def print_view(deck_id):
64     cards_dir = decks_dir + deck_id
65     cards = []
66     if os.path.exists(cards_dir):
67         card_ids = os.listdir(cards_dir)
68         for card_id in card_ids:
69             cards += [get_card_data(deck_id, card_id, None)]
70     deck_name = base64.b64decode(deck_id).decode()
71     return dict(cards=cards)
72
73 @get(web_path + '/decks/<deck_id>/cards/')
74 def new_card(deck_id):
75     card_name = request.query.get('card_name')
76     card_type = request.query.get('card_type')
77     card_id = base64.b64encode(card_name.encode()).decode()
78     redirect(web_path + '/decks/' + deck_id + '/cards/' + card_id
79              + '/form?type=' + card_type)
80
81 @get(web_path + '/decks/<deck_id>/cards/<card_id>/view')
82 @view('card')
83 def show_card(deck_id, card_id):
84     data = get_card_data(deck_id, card_id, None)
85     return dict(web_path=web_path,
86                 deck_id=deck_id,
87                 card_id=card_id,
88                 card=data)
89
90 @post(web_path + '/decks/<deck_id>/cards/<card_id>')
91 def update_card(deck_id, card_id):
92     cards_dir = decks_dir + deck_id
93     if not os.path.exists(cards_dir):
94         os.makedirs(cards_dir)
95     path_card = cards_dir + '/' + card_id
96     card_type = request.forms.get('type')
97     json_dict = {'type': request.forms.get('type')}
98     if card_type == 'action':
99         json_dict['title'] = request.forms.get('title')
100         json_dict['paragraphs'] = request.forms.getall('paragraph')
101         json_dict['paragraphs'] = [paragraph for paragraph in json_dict['paragraphs']
102                                 if paragraph.strip() != '']
103     else:
104         json_dict['answers'] = request.forms.getall('answer')
105         json_dict['answers'] = [answer for answer in json_dict['answers']
106                                 if answer.strip() != '']
107     with open(path_card, 'w') as f:
108         f.write(json.dumps(json_dict, indent=4))
109     redirect(web_path + '/decks/' + deck_id + '/cards/' + card_id + '/view')
110
111 @get(web_path + '/decks/<deck_id>/cards/<card_id>/form')
112 @view('card_form')
113 def card_form(deck_id, card_id):
114     card_type = request.query.get('type')
115     data = get_card_data(deck_id, card_id, card_type)
116     card_path = decks_dir + deck_id + '/' + card_id
117     deletable = False
118     if os.path.exists(card_path):
119         deletable = True
120     return dict(web_path=web_path,
121                 card_id=card_id,
122                 deck_id=deck_id,
123                 card=data,
124                 deletable=deletable)
125
126 @get(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
127 @view('delete_card')
128 def delete_card_ask(deck_id, card_id):
129     card_name = base64.b64decode(card_id.encode()).decode()
130     return dict(web_path=web_path,
131                 deck_id=deck_id,
132                 card_name=card_name,
133                 card_id=card_id)
134
135 @post(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
136 def delete_card_do(deck_id, card_id):
137     cards_dir = decks_dir + deck_id
138     card_path = cards_dir + '/' + card_id
139     if os.path.exists(card_path):
140         os.remove(card_path)
141     redirect(web_path + '/decks/' + deck_id + '/cards')
142
143 # App running.
144
145 if __name__ == "__main__":
146     debug(True)
147     run(port=8000)
148 else:
149     app = application = default_app()