home · contact · privacy
Refactor.
[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=data)
88
89 @post(web_path + '/decks/<deck_id>/cards/<card_id>')
90 def update_card(deck_id, card_id):
91     cards_dir = decks_dir + deck_id
92     if not os.path.exists(cards_dir):
93         os.makedirs(cards_dir)
94     path_card = cards_dir + '/' + card_id
95     card_type = request.forms.get('type')
96     json_dict = {'type': request.forms.get('type')}
97     if card_type == 'action':
98         json_dict['title'] = request.forms.get('title')
99         json_dict['paragraphs'] = request.forms.getall('paragraph')
100         json_dict['paragraphs'] = [paragraph for paragraph in json_dict['paragraphs']
101                                 if paragraph.strip() != '']
102     else:
103         json_dict['answers'] = request.forms.getall('answer')
104         json_dict['answers'] = [answer for answer in json_dict['answers']
105                                 if answer.strip() != '']
106     with open(path_card, 'w') as f:
107         f.write(json.dumps(json_dict, indent=4))
108     redirect(web_path + '/decks/' + deck_id + '/cards/' + card_id + '/view')
109
110 @get(web_path + '/decks/<deck_id>/cards/<card_id>/form')
111 @view('card_form')
112 def card_form(deck_id, card_id):
113     card_type = request.query.get('type')
114     data = get_card_data(deck_id, card_id, card_type)
115     card_path = decks_dir + deck_id + '/' + card_id
116     deletable = False
117     if os.path.exists(card_path):
118         deletable = True
119     return dict(web_path=web_path,
120                 card_id=card_id,
121                 deck_id=deck_id,
122                 card=data,
123                 deletable=deletable)
124
125 @get(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
126 @view('delete_card')
127 def delete_card_ask(deck_id, card_id):
128     card_name = base64.b64decode(card_id.encode()).decode()
129     return dict(web_path=web_path,
130                 deck_id=deck_id,
131                 card_name=card_name,
132                 card_id=card_id)
133
134 @post(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
135 def delete_card_do(deck_id, card_id):
136     cards_dir = decks_dir + deck_id
137     card_path = cards_dir + '/' + card_id
138     if os.path.exists(card_path):
139         os.remove(card_path)
140     redirect(web_path + '/decks/' + deck_id + '/cards')
141
142 # App running.
143
144 if __name__ == "__main__":
145     debug(True)
146     run(port=8000)
147 else:
148     app = application = default_app()