-
Notifications
You must be signed in to change notification settings - Fork 0
/
addresses.py
76 lines (56 loc) · 1.75 KB
/
addresses.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
from flask import make_response, abort
from database import Contact, Address
from peewee import SQL, DoesNotExist
def index(cid):
"""
/api/contacts/{cid}/addresses
:return: list of a contact's addresses
"""
contact = Contact.get(Contact.id == cid)
addresses = []
for address in contact.addresses:
addresses.append(address.serialize())
return addresses
def create(cid, address):
"""
/api/contacts/{cid}/addresses
Create new contact address
:param address: address information
:return: matching address
"""
theAddress = address.get("address", None)
if theAddress is None:
abort(406, "Address field must not be empty")
model = Address.create(**{
'full_address': theAddress,
'contact_id': cid
})
return make_response(f"{theAddress} successfully created for contact {cid}", 201)
def update(aid, address):
"""
/api/addresses/{aid}
Update address identified by aid
:param aid: id of the address to retrieve
:param address: new address information
:return: the new address object
"""
try:
theAddress = Address.get(Address.id == aid)
except DoesNotExist:
abort(404, f"Address with id {aid} for contact {cid} not found")
theAddress.full_address = address.get("address")
theAddress.save()
return theAddress.serialize()
def delete(aid):
"""
/api/addresses/{aid}
Delete addresses identified by aid
:param aid: new address information
:return: the deleted address
"""
try:
address = Address.get(Address.id == aid)
except:
abort(404, f"Email with id {aid} not found")
address.delete_instance()
return address.serialize()