-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_cohorts.py
158 lines (125 loc) · 3.97 KB
/
api_cohorts.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
from flask import Blueprint, jsonify, request
from sqlalchemy import exc
from database import db_session
from models import Cohort
from schemata import CohortSchema
api_cohorts = Blueprint('api_cohorts', __name__)
@api_cohorts.route('/cohorts')
def get_cohorts():
cohorts = Cohort.query.order_by(Cohort.id).all()
data = CohortSchema(many=True).dump(cohorts)
return jsonify(data), 200
@api_cohorts.route('/cohorts', methods=['POST'])
def create_cohort():
keys = ['name', 'nickname', 'duration', 'start_date']
if sorted([key for key in request.json]) == sorted(keys):
cohort_schema = CohortSchema()
try:
cohort = cohort_schema.load(request.json)
except Exception as _:
data = {
'title': 'Bad Request',
'status': 400,
'detail': 'Some values failed validation'
}
return data, 400
else:
db_session.add(cohort)
db_session.commit()
data = {
'title': 'Created',
'status': 201,
'detail': f'Cohort {cohort.id} created'
}
return data, 201
else:
data = {
'title': 'Bad Request',
'status': 400,
'detail': 'Missing some keys or contains extra keys'
}
return data, 400
@api_cohorts.route('/cohorts/<int:id>')
def get_cohort(id):
cohort = Cohort.query.filter_by(id=id).one_or_none()
if cohort is not None:
data = CohortSchema().dump(cohort)
return data, 200
else:
data = {
'title': 'Not Found',
'status': 404,
'detail': f'Cohort {id} not found'
}
return data, 404
@api_cohorts.route('/cohorts/<int:id>', methods=['PUT'])
def update_cohort(id):
keys = [
'name',
'nickname',
'duration',
'start_date',
'review_schema',
'feedback_schema'
]
if all(key in keys for key in request.json):
existing_cohort = Cohort.query.filter_by(id=id).one_or_none()
if existing_cohort is not None:
cohort_schema = CohortSchema()
try:
cohort = cohort_schema.load(request.json)
except Exception as _:
data = {
'title': 'Bad Request',
'status': 400,
'detail': 'Some values failed validation'
}
return data, 400
else:
cohort.id = existing_cohort.id
db_session.merge(cohort)
try:
db_session.commit()
except exc.IntegrityError as _:
data = {
'title': 'Bad Request',
'status': 400,
'detail': 'Some values failed validation'
}
return data, 400
else:
data = cohort_schema.dump(existing_cohort)
return data, 200
else:
data = {
'title': 'Not Found',
'status': 404,
'detail': f'Cohort {id} not found'
}
return data, 404
else:
data = {
'title': 'Bad Request',
'status': 400,
'detail': 'Missing some keys or contains extra keys'
}
return data, 400
@api_cohorts.route('/cohorts/<int:id>', methods=['DELETE'])
def delete_cohort(id):
cohort = Cohort.query.filter_by(id=id).one_or_none()
if cohort is not None:
db_session.delete(cohort)
db_session.commit()
data = {
'title': 'OK',
'status': 200,
'detail': f'Cohort {id} deleted'
}
return data, 200
else:
data = {
'title': 'Not Found',
'status': 404,
'detail': f'Cohort {id} not found'
}
return data, 404