2 from bottle import debug, run, default_app, get, view, request, post, redirect
7 web_path = '/guiltcards'
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())
19 data = {'type': card_type_hint,
21 'paragraphs': ['first paragraph', 'second paragraph'],
22 'answers': ['first option', 'second option']}
26 @get(web_path + '/decks')
29 if not os.path.exists(decks_dir):
30 os.makedirs(decks_dir)
31 deck_ids = os.listdir(decks_dir)
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)
37 @get(web_path + '/decks/')
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')
43 @get(web_path + '/decks/<deck_id>/cards')
44 @get(web_path + '/decks/<deck_id>/')
45 @get(web_path + '/decks/<deck_id>')
47 def list_cards(deck_id):
48 cards_dir = decks_dir + deck_id
50 if os.path.exists(cards_dir):
51 card_ids = os.listdir(cards_dir)
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,
61 @get(web_path + '/decks/<deck_id>/printable')
63 def print_view(deck_id):
64 cards_dir = decks_dir + deck_id
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)
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)
81 @get(web_path + '/decks/<deck_id>/cards/<card_id>/view')
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,
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() != '']
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')
111 @get(web_path + '/decks/<deck_id>/cards/<card_id>/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
118 if os.path.exists(card_path):
120 return dict(web_path=web_path,
126 @get(web_path + '/decks/<deck_id>/cards/<card_id>/delete')
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,
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):
141 redirect(web_path + '/decks/' + deck_id + '/cards')
145 if __name__ == "__main__":
149 app = application = default_app()