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