-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
77 lines (54 loc) · 2.09 KB
/
models.py
File metadata and controls
77 lines (54 loc) · 2.09 KB
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
import zipfile
import json
def populate_db(db, PersonasModel):
'''Unzips fake profiles and inserts every record into Personas database'''
#Unzip fake profiles
zip = zipfile.ZipFile('fake_profiles.zip')
zip.extractall('')
with open('fake_profiles.json') as fake_profiles:
profiles = json.load(fake_profiles)
store_profiles = []
for i in profiles:
user_entry = PersonasModel(
i['job'],
i['company'],
i['ssn'],
i['residence'],
" ".join(str(x) for x in i['current_location']),
i['blood_group'],
" ".join(str(x) for x in i['website']),
i['username'],
i['name'],
i['sex'],
i['address'],
i['mail'],
i['birthdate'])
store_profiles.append(user_entry)
db.session.add_all(store_profiles)
db.session.commit()
def retrieve_user(username, PersonasModel):
''' Retruns data for a single profile based on the username'''
username_data = PersonasModel.query.filter_by(username=username).first()
if username_data is None:
return None
username_dict = username_data.__dict__
del username_dict['_sa_instance_state']
return username_dict
def delete_person(username, PersonasModel, db):
'''Deltes profile for a single person based on the username'''
x = PersonasModel.query.filter_by(username = username).delete()
db.session.commit()
if x == 0:
return 404
return None
def get_all(page, PersonasModel, db):
'''Returns a specified page with 10 records per page'''
total_records = PersonasModel.query.paginate(page, per_page = 10).items
if total_records is None:
return None
record_list = []
for r in total_records:
record = r.__dict__
del record['_sa_instance_state']
record_list.append(record)
return record_list