-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.py
217 lines (181 loc) · 7 KB
/
resources.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
'''
Resources for the most part map to a combination of viewer and controller
in a mvc framework as it is now normally talked about
but they are closer to a model in the traditional GUI model of mvc
'''
from flask.ext.restful import Resource
from flask.ext.restful import reqparse
from flask.ext.restful import fields
from flask.ext.restful import marshal_with
from sqlalchemy import desc
from sqlalchemy import text
from models import Asset
from models import Credit
from models import insert_credit_asset_association
credit_resource_fields = {'job_type': fields.String,
'name': fields.String, }
asset_resource_fields = {'title': fields.String,
'description': fields.String,
'thumbnail_url': fields.String,
'creation_date': fields.DateTime(dt_format='rfc822'), }
class AssetResource(Resource):
def __init__(self):
self.put_reqparse = reqparse.RequestParser()
self.put_reqparse.add_argument('title')
self.put_reqparse.add_argument('description')
# TODO validate
self.put_reqparse.add_argument('thumbnail_url')
@marshal_with(asset_resource_fields)
def put(self, asset_id):
args = self.put_reqparse.parse_args()
asset = Asset.query.filter_by(identifier=asset_id).first()
if asset is None:
asset = Asset(args['title'],
args['description'],
args['thumbnail_url'])
header = {'location': '/assets/' + str(asset.identifier)}
return asset, 201, header
asset.update(args['title'], args['description'], args['thumbnail_url'])
return asset, 200
def delete(self, asset_id):
asset = Asset.query.filter_by(identifier=asset_id).first()
if asset is None:
return '', 404
asset.delete()
return '', 204
@marshal_with(asset_resource_fields)
def get(self, asset_id):
asset = Asset.query.filter_by(identifier=asset_id).first()
if asset is None:
return '', 404
return asset, 200
class AssetCollectionResource(Resource):
'''
View for looking at lists of assets
'''
def __init__(self):
self.post_reqparse = reqparse.RequestParser()
self.get_reqparse = reqparse.RequestParser()
# TODO force to int
self.get_reqparse.add_argument('number')
# TODO force one of the two types
self.get_reqparse.add_argument('sort')
self.post_reqparse.add_argument('title', required=True)
self.post_reqparse.add_argument('description', required=True)
# TODO validate
self.post_reqparse.add_argument('thumbnail_url', required=True)
@marshal_with(asset_resource_fields)
def post(self):
args = self.post_reqparse.parse_args()
asset = Asset(args['title'],
args['description'],
args['thumbnail_url'])
header = {'location': '/assets/' + str(asset.identifier)}
return asset, 201, header
@marshal_with(asset_resource_fields)
def get(self):
args = self.get_reqparse.parse_args()
asset_query = Asset.query
if args['sort'] is not None:
sort_type = args['sort']
if sort_type == 'reverse':
asset_query = asset_query.order_by(desc(text("title")))
else:
asset_query = asset_query.order_by(text("title"))
if args['number'] is not None:
number = args['number']
assets = asset_query.limit(int(number)).all()
else:
assets = asset_query.all()
return assets
class CreditResource(Resource):
def __init__(self):
self.put_reqparse = reqparse.RequestParser()
self.put_reqparse.add_argument('name')
self.put_reqparse.add_argument('job_type')
@marshal_with(credit_resource_fields)
def put(self, credit_id):
args = self.put_reqparse.parse_args()
credit = Credit.query.filter_by(identifier=credit_id).first()
if credit is None:
credit = Credit(args['name'], args['job_type'])
# todo not putting location in header
return credit, 201
credit.update(args['name'], args['job_type'])
return credit, 200
def delete(self, credit_id):
credit = Credit.query.filter_by(identifier=credit_id).first()
if credit is None:
return '', 404
credit.delete()
return '', 204
@marshal_with(credit_resource_fields)
def get(self, credit_id):
credit = Credit.query.filter_by(identifier=credit_id).first()
return credit, 200
class CreditCollectionResource(Resource):
'''
View for
looking at lists of credits
and for creating a credit
'''
def __init__(self):
self.get_reqparse = reqparse.RequestParser()
# TODO force to int
self.get_reqparse.add_argument('number')
# TODO force one of the two types
self.get_reqparse.add_argument('sort')
self.post_reqparse = reqparse.RequestParser()
self.post_reqparse.add_argument('name', required=True)
self.post_reqparse.add_argument('job_type', required=True)
@marshal_with(credit_resource_fields)
def post(self):
args = self.post_reqparse.parse_args()
credit = Credit(args['job_type'], args['name'])
# todo not putting location in header
return credit, 201, {'location': '/credits/' + str(credit.identifier)}
@marshal_with(credit_resource_fields)
def get(self):
args = self.get_reqparse.parse_args()
credit_query = Credit.query
if args['sort'] is not None:
sort_type = args['sort']
if sort_type == 'reverse':
credit_query = credit_query.order_by(desc(text("name")))
else:
credit_query = credit_query.order_by(text("name"))
if args['number'] is not None:
number = args['number']
credits = credit_query.limit(int(number)).all()
else:
credits = credit_query.all()
return credits
class AssetCreditAssociationResource(Resource):
def __init__(self):
pass
@marshal_with(credit_resource_fields)
def get(self, asset_id):
'''
for asset_id list credits
'''
asset = Asset.query.filter_by(identifier=asset_id).first()
if asset is None:
return '', 404
return asset.credits
def post(self, asset_id, credit_id):
error = insert_credit_asset_association(int(asset_id), int(credit_id))
if error is not None:
return error, 400
return '', 201
class CreditAssetAssociationResource(Resource):
def __init__(self):
pass
@marshal_with(asset_resource_fields)
def get(self, credit_id):
'''
for asset_id list credits
'''
credit = Credit.query.filter_by(identifier=credit_id).first()
if credit is None:
return '', 404
return credit.assets