-
Notifications
You must be signed in to change notification settings - Fork 4
/
profile.py
243 lines (217 loc) · 7.4 KB
/
profile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# -*- coding: utf-8 -*-
import os.path
import time
from copy import deepcopy
import json
from tumulus.tags import *
# Local project imports:
from config import JSON_PATH
class Profile(object):
'''
Handles loading and saving of user profile data.
'''
@classmethod
def exists(identifier):
'Returns whether or not a profile for the given user exists.'
path = os.path.join(JSON_PATH, self.identifier + '.json')
return os.path.isfile(path)
def __init__(self, identifier):
self.identifier = identifier
self.load()
def load(self):
path = os.path.join(JSON_PATH, self.identifier + '.json')
if os.path.isfile(path):
self.data = json.load(open(path))
else:
self.data = new_profile(self.identifier)
def update(self, new):
for key in new:
value = new[key].strip()
self.update_field(key, value)
def update_field(self, field, value):
queue = field.split('.')
cursor = self.data
while len(queue) > 1:
key = queue.pop(0)
if key.isdigit():
key = int(key)
cursor = cursor[key]
key = queue.pop(0)
cursor[key] = value
def save(self):
# Saving copy on disk:
path = os.path.join(JSON_PATH, self.identifier + '.json')
if not os.path.isdir(JSON_PATH):
os.makedirs(JSON_PATH)
json.dump(self.data, open(path, 'w'))
# TODO: Running copy in DB:
#rethinkdb...
def page(user, message=None):
'''
Generates the profile editor page using Tumulus.
'''
def field(desc, path, large=False):
'Creates the appropriate input li fields from description and dotted path.'
# Extracting user values from the path
# (some.key -> user['some']['key'])
queue = path.split('.')
value = user.data
while queue:
key = queue.pop(0)
if key.isdigit():
value = value[int(key)]
else:
value = value[key]
if large:
return li(desc, br(), textarea(value, name=path))
else:
return li(desc, input_(name=path, value=value))
return html(
head(
meta(charset='utf-8'),
link(rel='stylesheet', href='/static/brick-1.0beta6.byob.min.css'),
link(rel='stylesheet', href='/static/16plus.css'),
title('Profil JSB 16+'),
style('x-datepicker {height: 1.5em;}', type='text/css'),
),
body(
header(
img(src='/static/16+logo.png', alt='logo', class_='logo'),
a(span('Retour'), href='/', class_='menu_link'),
h1('Profil 16+'),
div(message) if message else '',
),
form(
section(
input_(type='submit', value='Enregistrer'),
),
section(
h2('Compte'),
ul(
li('Identifiant : ', user.data['account']['id']),
li('Date de creation : ', time.ctime(user.data['account']['stats']['creation'])),
),
),
section(
h2('Personnel'),
ul(
field('Prenom', 'personal.firstname'),
field('Nom de famille', 'personal.familyname'),
#field('Date de naissance', 'personal.birthdate'),
li('Date de naissance',
'<x-datepicker name="personal.birthdate" value="' + \
user.data['personal']['birthdate'] + \
'"></x-datepicker>'),
),
),
section(
h2('Contact'),
ul(
field('Courriel (email)', 'contact.email'),
field('Telephone', 'contact.phone'),
field('Domicile', 'contact.domicile', large=True),
field('Adresse postale (si differente)', 'contact.post', large=True),
),
),
section(
h2('En cas de probleme'),
h3('Personne 0'),
ul(
field('Relation', 'emergency.0.relationship'),
field('Prenom', 'emergency.0.firstname'),
field('Nom de famille', 'emergency.0.familyname'),
field('Telephone portable', 'emergency.0.gsm'),
field('Telephone alternatif', 'emergency.0.phone'),
),
h3('Personne 1'),
ul(
field('Relation', 'emergency.1.relationship'),
field('Prenom', 'emergency.1.firstname'),
field('Nom de famille', 'emergency.1.familyname'),
field('Telephone portable', 'emergency.1.gsm'),
field('Telephone alternatif', 'emergency.1.phone'),
),
),
section(
input_(type='submit', value='Enregistrer'),
),
action='/profil', method='POST',
),
script(src='/static/brick-1.0beta6.byob.min.js'),
),
)
empty_profile = {
'account': {
'id': None,
'stats': {
'creation': None,
},
},
'personal': {
'firstname': '',
'familyname': '',
'birthdate': '',
},
'contact': {
'email': '',
'phone': '',
'domicile': '',
'post': '',
},
'emergency': [
{'firstname': '',
'familyname': '',
'relationship': '',
'gsm': '',
'phone': '',
},
{'firstname': '',
'familyname': '',
'relationship': '',
'gsm': '',
'phone': '',
},
],
}
def new_profile(identifier):
"Generates a new profile using the 'empty_profile' template."
profile = deepcopy(empty_profile)
profile['account']['id'] = identifier
profile['account']['stats']['creation'] = time.time()
return profile
if __name__ == '__main__':
# Test function outputs a sample user:
user = {
'account': {
'id': '[email protected]',
'stats': {
'creation': '1984-01-02',
},
},
'personal': {
'firstname': 'Georges',
'familyname': 'Orwell',
'birthdate': '1984-01-01',
},
'contact': {
'email': '',
'phone': '0486.000.002',
'domicile': 'rue du Cinema 32',
'post': '',
},
'emergency': [
{'firstname': 'Dark',
'familyname': 'Vador',
'relationship': 'pere',
'gsm': '0486.000.002',
'phone': '020000001',
},
{'firstname': 'Lily',
'familyname': 'Pad',
'relationship': 'mere',
'gsm': '0486.000.003',
'phone': '020000002',
},
],
}
print(page(user))