-
Notifications
You must be signed in to change notification settings - Fork 3
/
routes.py
173 lines (135 loc) · 4.64 KB
/
routes.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
from flask import jsonify, request, abort
from app import db
from app.models import Project, Contact
from . import project
# create a new project
@project.route("", methods=["POST"])
def create_project():
data = request.get_json(force=True)
address = data.get("address")
city = data.get("city")
province = data.get("province")
postal_code = data.get("postal_code")
neighbourhood = data.get("neighbourhood")
year = data.get("year")
name = data.get("name")
type = data.get("type")
is_completed = data.get("is_completed")
# check if all fields are empty, if so it's a garbage post
if (
address == ""
and city == ""
and province == ""
and postal_code == ""
and neighbourhood == ""
and year is None
and name == ""
and type == ""
and (is_completed == False or is_completed == True)
):
abort(400, "Cannot have all empty fields for a new project")
new_project = Project(
address=address,
city=city,
province=province,
postal_code=postal_code,
neighbourhood=neighbourhood,
year=year,
name=name,
type=type,
is_completed=is_completed,
)
db.session.add(new_project)
db.session.commit()
return jsonify(new_project.serialize)
# helper returns all project types;
@project.route("/types", methods=["GET"])
def get_all_project_types():
types = []
for project in Project.query.distinct(Project.type):
types.append(project.type)
return jsonify(types)
# helper returns all project boroughs
@project.route("/boroughs", methods=["GET"])
def get_all_project_boroughs():
boroughs = []
for project in Project.query.distinct(Project.neighbourhood):
boroughs.append(project.neighbourhood)
return jsonify(boroughs)
# get projects according to specified arguments (if any)
@project.route("", methods=["GET"])
def get_projects():
projects = Project.query.all()
type = request.args.get("type")
if type is not None:
if type == "":
abort(404, "Project type invalid")
projects = list(filter(lambda project: (project.type == type), projects))
is_completed = request.args.get("is-completed")
if is_completed is not None:
if is_completed == "":
abort(404, "completion status invalid")
# query param is of string type, therefore must convert to boolean first
if is_completed.lower() == "true":
is_completed = True
else:
is_completed = False
projects = list(
filter(lambda project: (project.is_completed == is_completed), projects)
)
# add additional request arguments below
return jsonify(Project.serialize_list(projects))
# get a project by id
@project.route("/<uuid:id>", methods=["GET"])
def get_project_by_id(id):
project = Project.query.filter_by(id=id).first()
if project is None:
abort(404, "No project found with specified ID.")
return jsonify(project.serialize)
# update a project by id
@project.route("/<uuid:id>", methods=["PUT"])
def update_project(id):
data = request.get_json(force=True)
address = data.get("address")
city = data.get("city")
province = data.get("province")
postal_code = data.get("postal_code")
neighbourhood = data.get("neighbourhood")
year = data.get("year")
name = data.get("name")
type = data.get("type")
is_completed = data.get("is_completed")
project = Project.query.filter_by(id=id).first()
if project is None:
abort(404, "No project with specified ID")
if address is not None:
project.address = address
if city is not None:
project.city = city
if province is not None:
project.province = province
if postal_code is not None:
project.postal_code = postal_code
if neighbourhood is not None:
project.neighbourhood = neighbourhood
if year is not None:
project.year = year
if name is not None:
project.name = name
if type is not None:
project.type = type
if is_completed is not None:
project.is_completed = is_completed
if not data:
abort(400, "No fields to update")
db.session.add(project)
db.session.commit()
return jsonify(project.serialize)
# get all contacts by project id
@project.route("/<uuid:id>/contacts", methods=["GET"])
def get_all_contacts_by_project(id):
project = Project.query.filter_by(id=id).first()
if project is None:
abort(404, "No project found with specified ID.")
contacts = project.contacts
return jsonify(Contact.serialize_list(contacts))