-
Notifications
You must be signed in to change notification settings - Fork 30
/
assets.py
314 lines (257 loc) · 10.2 KB
/
assets.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
import json
from flask import current_app
from flask_classful import FlaskView, route
from flask_security import auth_token_required
from flask_json import as_json
from marshmallow import fields
from webargs.flaskparser import use_kwargs, use_args
from flexmeasures.auth.decorators import permission_required_for_context
from flexmeasures.data import db
from flexmeasures.data.models.user import Account
from flexmeasures.data.models.generic_assets import GenericAsset
from flexmeasures.data.schemas import AwareDateTimeField
from flexmeasures.data.schemas.generic_assets import GenericAssetSchema as AssetSchema
from flexmeasures.api.common.schemas.generic_assets import AssetIdField
from flexmeasures.api.common.schemas.users import AccountIdField
from flexmeasures.utils.coding_utils import flatten_unique
from flexmeasures.ui.utils.view_utils import set_time_range_for_session
asset_schema = AssetSchema()
assets_schema = AssetSchema(many=True)
partial_asset_schema = AssetSchema(partial=True, exclude=["account_id"])
class AssetAPI(FlaskView):
"""
This API view exposes generic assets.
Under development until it replaces the original Asset API.
"""
route_base = "/assets"
trailing_slash = False
@route("", methods=["GET"])
@use_kwargs(
{
"account": AccountIdField(
data_key="account_id", load_default=AccountIdField.load_current
),
},
location="query",
)
@permission_required_for_context("read", ctx_arg_name="account")
@as_json
def index(self, account: Account):
"""List all assets owned by a certain account.
.. :quickref: Asset; Download asset list
This endpoint returns all accessible assets for the account of the user.
The `account_id` query parameter can be used to list assets from a different account.
**Example response**
An example of one asset being returned:
.. sourcecode:: json
[
{
"id": 1,
"name": "Test battery",
"latitude": 10,
"longitude": 100,
"account_id": 2,
"generic_asset_type_id": 1
}
]
:reqheader Authorization: The authentication token
:reqheader Content-Type: application/json
:resheader Content-Type: application/json
:status 200: PROCESSED
:status 400: INVALID_REQUEST
:status 401: UNAUTHORIZED
:status 403: INVALID_SENDER
:status 422: UNPROCESSABLE_ENTITY
"""
return assets_schema.dump(account.generic_assets), 200
@route("/public", methods=["GET"])
@auth_token_required
@as_json
def public(self):
"""Return all public assets.
.. :quickref: Asset; Return all public assets.
This endpoint returns all public assets.
:reqheader Authorization: The authentication token
:reqheader Content-Type: application/json
:resheader Content-Type: application/json
:status 200: PROCESSED
:status 400: INVALID_REQUEST
:status 401: UNAUTHORIZED
:status 422: UNPROCESSABLE_ENTITY
"""
assets = GenericAsset.query.filter(GenericAsset.account_id.is_(None)).all()
return assets_schema.dump(assets), 200
@route("", methods=["POST"])
@permission_required_for_context(
"create-children", ctx_loader=AccountIdField.load_current
)
@use_args(asset_schema)
def post(self, asset_data: dict):
"""Create new asset.
.. :quickref: Asset; Create a new asset
This endpoint creates a new asset.
**Example request**
.. sourcecode:: json
{
"name": "Test battery",
"generic_asset_type_id": 2,
"account_id": 2,
"latitude": 40,
"longitude": 170.3,
}
The newly posted asset is returned in the response.
:reqheader Authorization: The authentication token
:reqheader Content-Type: application/json
:resheader Content-Type: application/json
:status 201: CREATED
:status 400: INVALID_REQUEST
:status 401: UNAUTHORIZED
:status 403: INVALID_SENDER
:status 422: UNPROCESSABLE_ENTITY
"""
asset = GenericAsset(**asset_data)
db.session.add(asset)
db.session.commit()
return asset_schema.dump(asset), 201
@route("/<id>", methods=["GET"])
@use_kwargs({"asset": AssetIdField(data_key="id")}, location="path")
@permission_required_for_context("read", ctx_arg_name="asset")
@as_json
def fetch_one(self, id, asset):
"""Fetch a given asset.
.. :quickref: Asset; Get an asset
This endpoint gets an asset.
**Example response**
.. sourcecode:: json
{
"generic_asset_type_id": 2,
"name": "Test battery",
"id": 1,
"latitude": 10,
"longitude": 100,
"account_id": 1,
}
:reqheader Authorization: The authentication token
:reqheader Content-Type: application/json
:resheader Content-Type: application/json
:status 200: PROCESSED
:status 400: INVALID_REQUEST, REQUIRED_INFO_MISSING, UNEXPECTED_PARAMS
:status 401: UNAUTHORIZED
:status 403: INVALID_SENDER
:status 422: UNPROCESSABLE_ENTITY
"""
return asset_schema.dump(asset), 200
@route("/<id>", methods=["PATCH"])
@use_args(partial_asset_schema)
@use_kwargs({"db_asset": AssetIdField(data_key="id")}, location="path")
@permission_required_for_context("update", ctx_arg_name="db_asset")
@as_json
def patch(self, asset_data: dict, id: int, db_asset: GenericAsset):
"""Update an asset given its identifier.
.. :quickref: Asset; Update an asset
This endpoint sets data for an existing asset.
Any subset of asset fields can be sent.
The following fields are not allowed to be updated:
- id
- account_id
**Example request**
.. sourcecode:: json
{
"latitude": 11.1,
"longitude": 99.9,
}
**Example response**
The whole asset is returned in the response:
.. sourcecode:: json
{
"generic_asset_type_id": 2,
"id": 1,
"latitude": 11.1,
"longitude": 99.9,
"name": "Test battery",
"account_id": 2,
}
:reqheader Authorization: The authentication token
:reqheader Content-Type: application/json
:resheader Content-Type: application/json
:status 200: UPDATED
:status 400: INVALID_REQUEST, REQUIRED_INFO_MISSING, UNEXPECTED_PARAMS
:status 401: UNAUTHORIZED
:status 403: INVALID_SENDER
:status 422: UNPROCESSABLE_ENTITY
"""
for k, v in asset_data.items():
setattr(db_asset, k, v)
db.session.add(db_asset)
db.session.commit()
return asset_schema.dump(db_asset), 200
@route("/<id>", methods=["DELETE"])
@use_kwargs({"asset": AssetIdField(data_key="id")}, location="path")
@permission_required_for_context("delete", ctx_arg_name="asset")
@as_json
def delete(self, id: int, asset: GenericAsset):
"""Delete an asset given its identifier.
.. :quickref: Asset; Delete an asset
This endpoint deletes an existing asset, as well as all sensors and measurements recorded for it.
:reqheader Authorization: The authentication token
:reqheader Content-Type: application/json
:resheader Content-Type: application/json
:status 204: DELETED
:status 400: INVALID_REQUEST, REQUIRED_INFO_MISSING, UNEXPECTED_PARAMS
:status 401: UNAUTHORIZED
:status 403: INVALID_SENDER
:status 422: UNPROCESSABLE_ENTITY
"""
asset_name = asset.name
db.session.delete(asset)
db.session.commit()
current_app.logger.info("Deleted asset '%s'." % asset_name)
return {}, 204
@route("/<id>/chart/")
@use_kwargs(
{"asset": AssetIdField(data_key="id")},
location="path",
)
@use_kwargs(
{
"event_starts_after": AwareDateTimeField(format="iso", required=False),
"event_ends_before": AwareDateTimeField(format="iso", required=False),
"beliefs_after": AwareDateTimeField(format="iso", required=False),
"beliefs_before": AwareDateTimeField(format="iso", required=False),
"include_data": fields.Boolean(required=False),
"dataset_name": fields.Str(required=False),
"height": fields.Str(required=False),
"width": fields.Str(required=False),
},
location="query",
)
@permission_required_for_context("read", ctx_arg_name="asset")
def get_chart(self, id: int, asset: GenericAsset, **kwargs):
"""GET from /assets/<id>/chart
.. :quickref: Chart; Download a chart with time series
"""
set_time_range_for_session()
return json.dumps(asset.chart(**kwargs))
@route("/<id>/chart_data/")
@use_kwargs(
{"asset": AssetIdField(data_key="id")},
location="path",
)
@use_kwargs(
{
"event_starts_after": AwareDateTimeField(format="iso", required=False),
"event_ends_before": AwareDateTimeField(format="iso", required=False),
"beliefs_after": AwareDateTimeField(format="iso", required=False),
"beliefs_before": AwareDateTimeField(format="iso", required=False),
"most_recent_beliefs_only": fields.Boolean(required=False),
},
location="query",
)
@permission_required_for_context("read", ctx_arg_name="asset")
def get_chart_data(self, id: int, asset: GenericAsset, **kwargs):
"""GET from /assets/<id>/chart_data
.. :quickref: Chart; Download time series for use in charts
Data for use in charts (in case you have the chart specs already).
"""
sensors = flatten_unique(asset.sensors_to_show)
return asset.search_beliefs(sensors=sensors, as_json=True, **kwargs)