-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.py
167 lines (135 loc) · 4.37 KB
/
contacts.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
from flask import make_response, abort
from datetime import datetime
from database import Contact, Email, Phone
from peewee import SQL, DoesNotExist, prefetch
# CONTACTS = []
# with open('contacts.csv') as contactsFile:
# reader = csv.reader(contactsFile, delimiter=',')
# fields = next(reader)
# for row in reader:
# CONTACTS.append(dict(zip(fields, row)))
def get_timestamp():
return datetime.now().strftime(('%Y-%m-%d %H:%M:%S'))
# Create GET all handler
def index(page=1, pagesize=10, sortby='id', sortdir='asc', search=None):
"""
/api/contacts
:return: list of contacts with paging information
"""
if search is not None:
return searchContacts(search)
contacts = Contact.select().order_by(
SQL(f"{sortby} COLLATE NOCASE {sortdir}")
).paginate(page, pagesize)
emails = Email.select()
phones = Phone.select()
contacts_with_emails_iter = prefetch(contacts, emails, phones)
data = []
for contact in contacts_with_emails_iter:
data.append(
contact.serialize()
# {
# 'id': contact.id,
# 'fname': contact.fname,
# 'lname': contact.lname,
# 'dob': contact.dob,
# 'emails': [ {'id': em.id, 'email': em.email } for em in contact.emails ]
# }
)
return {
'data': data,
'numberRecords': Contact.select().count(),
'page': page,
'pagesize': pagesize
}
def searchContacts(searchString):
print(searchString)
if len(searchString.split()) > 1:
first, last = searchString.split()
contacts = Contact.select().where((Contact.fname.contains(first)) &
(Contact.lname.contains(last)))
else:
contacts = Contact.select().where(Contact.fname.contains(searchString))
returnList = []
for c in contacts[:25]:
returnList.append(c.serialize())
return {
'data': returnList,
'numberRecords': len(returnList),
'page': 1,
'pagesize': 25
}
def create(contact):
"""
Create new contact
:param contact: contact information
:return: matching contact
"""
print(contact)
fname = contact.get("fname", None)
lname = contact.get("lname", None)
dob = contact.get("dob", None)
# emails = contact.get("emails", [])
if fname is None or lname is None or dob is None:
abort(406, f"Entries must not be empty")
contact = Contact.create(**{
'fname': fname,
'lname': lname,
'dob': dob[:10]
})
# if len(emails) > 0:
# for email in emails:
# Email.create(**{
# 'contact_id': contact.id,
# 'email': email
# })
# return make_response(f"{fname} {lname} successfully created", 201)
return contact.serialize()
# create GET one handler
def read(cid):
"""
Read one contact identified by cid
:param cid: id of the contact to retrieve
:return: matching contact
"""
try:
contact = Contact.get(Contact.id == cid)
except StopIteration:
abort(404, f"Contact with id {cid} not found")
return contact.serialize()
# create PUT handler
def update(cid, contact):
"""
Update contact identified by cid
:param cid: id of the contact to retrieve
:param contact: new contact information
:return: matching contact
"""
try:
theContact = Contact.get(Contact.id == cid)
except DoesNotExist:
abort(404, f"Contact with id {cid} not found")
# update with new information
print(contact)
if contact.get('fname', None):
theContact.fname = contact.get('fname')
if contact.get('lname', None):
theContact.lname = contact.get('lname')
if contact.get('dob', None):
theContact.dob = contact.get('dob')
theContact.save()
return theContact.serialize()
# create DELETE handler
def delete(cid):
"""
Delete contact identified by cid
:param cid: id of the contact to retrieve
:param contact: new contact information
:return: matching contact
"""
try:
contact = Contact.get(Contact.id == cid)
except DoesNotExist:
abort(404, f"Contact with id {cid} not found")
contact.delete_instance(recursive=True)
return contact.serialize()