/
app.py
336 lines (280 loc) · 9.95 KB
/
app.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import typing as t
from flask import Flask, request, jsonify
from sqlalchemy.exc import IntegrityError
from sqlalchemy.engine import Engine
from sqlalchemy import event
app = Flask(__name__)
app.config.from_object('default_settings')
@event.listens_for(Engine, 'connect')
def set_sqlite_pragma(conn, record):
cursor = conn.cursor()
cursor.execute('PRAGMA foreign_keys=ON')
cursor.close()
@app.route('/')
def index():
return app.send_static_file('index.html')
# SQLAlchemy Models
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
class Client(db.Model):
__tablename__ = 'client'
client_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
# Relationships
featurerequests = db.relationship('FeatureRequest',
back_populates='client', lazy='dynamic')
def __repr__(self) -> str:
return f'<User {self.name}>'
@property
def id(self) -> int:
return self.client_id
class ProductArea(db.Model):
__tablename__ = 'productarea'
productarea_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
# Relationships
featurerequests = db.relationship('FeatureRequest',
back_populates='productarea',
lazy='dynamic')
def __repr__(self) -> str:
return f'<ProductArea {self.name}>'
@property
def id(self) -> int:
return self.productarea_id
class FeatureRequest(db.Model):
__tablename__ = 'featurerequest'
featurerequest_id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, unique=True, nullable=False)
description = db.Column(db.String, nullable=False)
priority = db.Column(db.Integer, nullable=False)
target_date = db.Column(db.Date)
# References
client_id = db.Column(db.Integer, db.ForeignKey('client.client_id'),
nullable=False)
productarea_id = db.Column(db.Integer,
db.ForeignKey('productarea.productarea_id'),
nullable=False)
# Relationships
client = db.relationship('Client', back_populates='featurerequests',
lazy='joined')
productarea = db.relationship('ProductArea',
back_populates='featurerequests',
lazy='joined')
# Constraints
__table_args__ = (
db.UniqueConstraint('client_id', 'priority'),
)
@property
def id(self) -> int:
return self.featurerequest_id
def handle_unique_constraints(self) -> t.Optional[str]:
'''
Each FeatureRequest must have a unique priority number for each client.
When creating or updating a request, use this method to manage the
conflict when a request with an existing priority is created. This
increments the priority of all other request for that client equal to
or above the new request's priority.
Just return an error if any other unique constraints (title) are
violated.
'''
cls = type(self)
db.session.add(self)
try:
db.session.flush()
except IntegrityError as err:
reason = err.args[0]
is_uniq = 'UNIQUE constraint failed: ' in reason
if not is_uniq:
raise
is_priority_client = (
'.client_id' in reason
and '.priority' in reason
)
if is_priority_client:
db.session.rollback()
# Two-step priority shift. Can't defer unique constraints in
# SQLite otherwise it could be 1-step.
(db.session.query(cls)
.filter(cls.client_id == self.client_id)
.filter(cls.priority >= self.priority)
.update({cls.priority: (cls.priority + 1) * -1}))
(db.session.query(cls)
.filter(cls.priority < 0)
.update({cls.priority: cls.priority * -1}))
db.session.add(self)
if '.title' in reason:
return 'Unique title required'
def save(self) -> t.Tuple[dict, int]:
err = self.handle_unique_constraints()
if err is not None:
db.session.rollback()
return {'errors': [err]}, 409
db.session.commit()
self = self.query.get(self.id)
result = FeatureRequestSchema().dump(self)
return {'featurerequest': result}, 200
# SQLAlchemy Commands
@app.cli.command('create-all')
def create_all():
db.create_all()
@app.cli.command('default-data')
def default_data():
session = db.session
for c in 'ABC':
session.add(Client(name=f'Client {c}'))
for area in ['Policies', 'Billing', 'Claims', 'Reports']:
session.add(ProductArea(name=area))
session.commit()
__all__ = ['Client', 'ProductArea', 'FeatureRequest']
@app.cli.command('drop-all')
def drop_all():
db.drop_all()
# Define Flask-Restful Resources
# These use the Marshmallow schemas
from marshmallow import ValidationError
from schema import FeatureRequestSchema, ProductAreaSchema, ClientSchema
from flask_restful import Api, Resource
api = Api(app)
class FeatureRequestListResource(Resource):
'''
API resource create and list FeatureRequests
'''
schema = FeatureRequestSchema
def get(self):
'''List all FeatureRequests
---
tags: [featureRequest]
operationId: listFeatureRequests
description: list all feature requests
responses:
200:
description: list all feature requests
schema:
featurerequests:
type: array
items:
$ref: '#/definitions/FeatureRequest'
'''
reqs = self.schema(many=True).dump(FeatureRequest.query.all())
return {'featurerequests': reqs}
def post(self):
'''Create new FeatureRequest
---
tags: [featureRequest]
operationId: createFeatureRequest
description: create new feature request
consumes:
- application/json
parameters:
- in: body
name: body
description: FeatureRequest object to add
required: true
schema: {$ref: '#/definitions/FeatureRequest'}
responses:
200:
description: successfully created featurerequest
schema:
featurerequest:
type: array
items:
$ref: '#/definitions/FeatureRequest'
message:
type: string
400:
description: unreadable input
schema:
message:
type: string
422:
description: invalid input
schema:
errors:
type: array
items: string
'''
json_data = request.get_json()
if not json_data:
return {'message': 'Empty input received'}, 400
json_data.pop('id', None)
try:
req = self.schema().load(json_data)
except ValidationError as err:
return {'errors': err.messages}, 422
response, status = req.save()
if status != 200:
return response, status
response['message'] = 'Created a new FeatureRequest'
return response, 201
api.add_resource(FeatureRequestListResource,
'/v1/featurerequest')
class ProductAreaResource(Resource):
schema = ProductAreaSchema
def get(self):
'''List all ProductAreas
---
tags: [productArea]
operationId: listProductAreas
description: list all product areas
responses:
200:
description: list all product areas
schema:
productareas:
type: array
items:
$ref: '#/definitions/ProductArea'
'''
areas = ProductArea.query.all()
return {'productareas': [{'id': a.id, 'name': a.name} for a in areas]}
api.add_resource(ProductAreaResource,
'/v1/productarea')
class ClientResource(Resource):
schema = ClientSchema
def get(self):
'''List all Clients
---
tags: [client]
operationId: listClients
description: list all clients
responses:
200:
description: list all clients
schema:
clients:
type: array
items:
$ref: '#/definitions/Client'
'''
clients = Client.query.all()
return {'clients': [{'id': c.id, 'name': c.name} for c in clients]}
api.add_resource(ClientResource,
'/v1/client')
# Swagger API
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
# Create an APISpec
spec = APISpec(
title='Feature Requests',
version='0.1.0b1',
openapi_version='2.0',
plugins=[
MarshmallowPlugin(),
'apispec_flask_restful',
],
)
spec.add_tag({'name': 'featureRequest'})
spec.add_tag({'name': 'productArea'})
spec.add_tag({'name': 'client'})
spec.definition('FeatureRequest', schema=FeatureRequestSchema)
spec.definition('Client', schema=ClientSchema)
spec.definition('ProductArea', schema=ProductAreaSchema)
spec.add_path(resource=FeatureRequestListResource, api=api)
spec.add_path(resource=ClientResource, api=api)
spec.add_path(resource=ProductAreaResource, api=api)
@app.route('/v1')
def swagger():
return jsonify(spec.to_dict())
@app.cli.command('swagger')
def show_swagger_yaml():
print(spec.to_yaml())