/
assets.py
436 lines (374 loc) · 16 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from typing import Union, Optional, List, Tuple
import copy
import json
from flask import url_for, current_app
from flask_classful import FlaskView
from flask_wtf import FlaskForm
from flask_security import login_required, current_user
from wtforms import StringField, DecimalField, SelectField
from wtforms.validators import DataRequired
from flexmeasures.auth.policy import user_has_admin_access
from flexmeasures.data import db
from flexmeasures.auth.error_handling import unauthorized_handler
from flexmeasures.auth.policy import check_access
from flexmeasures.data.models.generic_assets import (
GenericAssetType,
GenericAsset,
get_center_location_of_assets,
)
from flexmeasures.data.models.user import Account
from flexmeasures.data.models.time_series import Sensor
from flexmeasures.ui.utils.view_utils import render_flexmeasures_template
from flexmeasures.ui.crud.api_wrapper import InternalApi
"""
Asset crud view.
Note: This uses the internal dev API version
― if those endpoints get moved or updated to a higher version,
we probably should change the version used here, as well.
"""
class AssetForm(FlaskForm):
"""The default asset form only allows to edit the name and location."""
name = StringField("Name")
latitude = DecimalField(
"Latitude",
places=None,
render_kw={"placeholder": "--Click the map or enter a latitude--"},
)
longitude = DecimalField(
"Longitude",
places=None,
render_kw={"placeholder": "--Click the map or enter a longitude--"},
)
attributes = StringField("Other attributes (JSON)", default="{}")
def validate_on_submit(self):
if (
hasattr(self, "generic_asset_type_id")
and self.generic_asset_type_id.data == -1
):
self.generic_asset_type_id.data = (
"" # cannot be coerced to int so will be flagged as invalid input
)
if hasattr(self, "account_id") and self.account_id.data == -1:
del self.account_id # asset will be public
return super().validate_on_submit()
def to_json(self) -> dict:
"""turn form data into a JSON we can POST to our internal API"""
data = copy.copy(self.data)
data["longitude"] = float(data["longitude"])
data["latitude"] = float(data["latitude"])
if "csrf_token" in data:
del data["csrf_token"]
return data
def process_api_validation_errors(self, api_response: dict):
"""Process form errors from the API for the WTForm"""
if not isinstance(api_response, dict):
return
for error_header in ("json", "validation_errors"):
if error_header not in api_response:
continue
for field in list(self._fields.keys()):
if field in list(api_response[error_header].keys()):
field_errors = api_response[error_header][field]
if isinstance(field_errors, list):
self._fields[field].errors += api_response[error_header][field]
else:
self._fields[field].errors.append(
api_response[error_header][field]
)
class NewAssetForm(AssetForm):
"""Here, in addition, we allow to set asset type and account."""
generic_asset_type_id = SelectField(
"Asset type", coerce=int, validators=[DataRequired()]
)
account_id = SelectField("Account", coerce=int)
def with_options(
form: Union[AssetForm, NewAssetForm]
) -> Union[AssetForm, NewAssetForm]:
if "generic_asset_type_id" in form:
form.generic_asset_type_id.choices = [(-1, "--Select type--")] + [
(atype.id, atype.name) for atype in GenericAssetType.query.all()
]
if "account_id" in form:
form.account_id.choices = [(-1, "--Select account--")] + [
(account.id, account.name) for account in Account.query.all()
]
return form
def process_internal_api_response(
asset_data: dict, asset_id: Optional[int] = None, make_obj=False
) -> Union[GenericAsset, dict]:
"""
Turn data from the internal API into something we can use to further populate the UI.
Either as an asset object or a dict for form filling.
If we add other data by querying the database, we make sure the asset is not in the session afterwards.
"""
def expunge_asset():
# use if no insert is wanted from a previous query which flushes its results
if asset in db.session:
db.session.expunge(asset)
asset_data.pop("status", None) # might have come from requests.response
if asset_id:
asset_data["id"] = asset_id
if make_obj:
asset = GenericAsset(
**{
**asset_data,
**{"attributes": json.loads(asset_data.get("attributes", "{}"))},
}
) # TODO: use schema?
asset.generic_asset_type = GenericAssetType.query.get(
asset.generic_asset_type_id
)
expunge_asset()
asset.owner = Account.query.get(asset_data["account_id"])
expunge_asset()
if "id" in asset_data:
asset.sensors = Sensor.query.filter(
Sensor.generic_asset_id == asset_data["id"]
).all()
expunge_asset()
return asset
return asset_data
def user_can_create_assets() -> bool:
try:
check_access(current_user.account, "create-children")
except Exception:
return False
return True
def user_can_delete(asset) -> bool:
try:
check_access(asset, "delete")
except Exception:
return False
return True
class AssetCrudUI(FlaskView):
"""
These views help us offer a Jinja2-based UI.
The main focus on logic is the API, so these views simply call the API functions,
and deal with the response.
Some new functionality, like fetching accounts and asset types, is added here.
"""
route_base = "/assets"
@login_required
def index(self, msg=""):
"""GET from /assets
List the user's assets. For admins, list across all accounts.
"""
assets = []
def get_asset_by_account(account_id) -> List[GenericAsset]:
if account_id is not None:
get_assets_response = InternalApi().get(
url_for("AssetAPI:index"), query={"account_id": account_id}
)
else:
get_assets_response = InternalApi().get(url_for("AssetAPI:public"))
return [
process_internal_api_response(ad, make_obj=True)
for ad in get_assets_response.json()
]
if user_has_admin_access(current_user, "read"):
for account in Account.query.all():
assets += get_asset_by_account(account.id)
assets += get_asset_by_account(account_id=None)
else:
assets = get_asset_by_account(current_user.account_id)
return render_flexmeasures_template(
"crud/assets.html",
assets=assets,
message=msg,
user_can_create_assets=user_can_create_assets(),
)
@login_required
def owned_by(self, account_id: str):
"""/assets/owned_by/<account_id>"""
msg = ""
get_assets_response = InternalApi().get(
url_for("AssetAPI:index"),
query={"account_id": account_id},
do_not_raise_for=[404],
)
if get_assets_response.status_code == 404:
assets = []
msg = f"Account {account_id} unknown."
else:
assets = [
process_internal_api_response(ad, make_obj=True)
for ad in get_assets_response.json()
]
return render_flexmeasures_template(
"crud/assets.html",
account=Account.query.get(account_id),
assets=assets,
msg=msg,
user_can_create_assets=user_can_create_assets(),
)
@login_required
def get(self, id: str):
"""GET from /assets/<id> where id can be 'new' (and thus the form for asset creation is shown)"""
if id == "new":
if not user_can_create_assets():
return unauthorized_handler(None, [])
asset_form = with_options(NewAssetForm())
return render_flexmeasures_template(
"crud/asset_new.html",
asset_form=asset_form,
msg="",
map_center=get_center_location_of_assets(user=current_user),
mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""),
)
get_asset_response = InternalApi().get(url_for("AssetAPI:fetch_one", id=id))
asset_dict = get_asset_response.json()
asset_form = with_options(AssetForm())
asset = process_internal_api_response(asset_dict, int(id), make_obj=True)
asset_form.process(data=process_internal_api_response(asset_dict))
return render_flexmeasures_template(
"crud/asset.html",
asset=asset,
asset_form=asset_form,
msg="",
mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""),
user_can_create_assets=user_can_create_assets(),
user_can_delete_asset=user_can_delete(asset),
)
@login_required
def post(self, id: str):
"""POST to /assets/<id>, where id can be 'create' (and thus a new asset is made from POST data)
Most of the code deals with creating a user for the asset if no existing is chosen.
"""
asset: GenericAsset = None
error_msg = ""
if id == "create":
asset_form = with_options(NewAssetForm())
account, account_error = _set_account(asset_form)
asset_type, asset_type_error = _set_asset_type(asset_form)
form_valid = asset_form.validate_on_submit()
# Fill up the form with useful errors for the user
if account_error is not None:
form_valid = False
asset_form.account_id.errors.append(account_error)
if asset_type_error is not None:
form_valid = False
asset_form.generic_asset_type_id.errors.append(asset_type_error)
# Create new asset or return the form for new assets with a message
if form_valid and asset_type is not None:
post_asset_response = InternalApi().post(
url_for("AssetAPI:post"),
args=asset_form.to_json(),
do_not_raise_for=[400, 422],
)
if post_asset_response.status_code in (200, 201):
asset_dict = post_asset_response.json()
asset = process_internal_api_response(
asset_dict, int(asset_dict["id"]), make_obj=True
)
msg = "Creation was successful."
else:
current_app.logger.error(
f"Internal asset API call unsuccessful [{post_asset_response.status_code}]: {post_asset_response.text}"
)
asset_form.process_api_validation_errors(post_asset_response.json())
if "message" in post_asset_response.json():
asset_form.process_api_validation_errors(
post_asset_response.json()["message"]
)
if "json" in post_asset_response.json()["message"]:
error_msg = str(
post_asset_response.json()["message"]["json"]
)
if asset is None:
msg = "Cannot create asset. " + error_msg
return render_flexmeasures_template(
"crud/asset_new.html",
asset_form=asset_form,
msg=msg,
map_center=get_center_location_of_assets(user=current_user),
mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""),
)
else:
asset_form = with_options(AssetForm())
if not asset_form.validate_on_submit():
asset = GenericAsset.query.get(id)
# Display the form data, but set some extra data which the page wants to show.
asset_info = asset_form.to_json()
asset_info["id"] = id
asset_info["account_id"] = asset.account_id
asset = process_internal_api_response(
asset_info, int(id), make_obj=True
)
return render_flexmeasures_template(
"crud/asset.html",
asset_form=asset_form,
asset=asset,
msg="Cannot edit asset.",
mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""),
user_can_create_assets=user_can_create_assets(),
user_can_delete_asset=user_can_delete(asset),
)
patch_asset_response = InternalApi().patch(
url_for("AssetAPI:patch", id=id),
args=asset_form.to_json(),
do_not_raise_for=[400, 422],
)
asset_dict = patch_asset_response.json()
if patch_asset_response.status_code in (200, 201):
asset = process_internal_api_response(
asset_dict, int(id), make_obj=True
)
msg = "Editing was successful."
else:
current_app.logger.error(
f"Internal asset API call unsuccessful [{patch_asset_response.status_code}]: {patch_asset_response.text}"
)
msg = "Cannot edit asset."
asset_form.process_api_validation_errors(
patch_asset_response.json().get("message")
)
asset = GenericAsset.query.get(id)
return render_flexmeasures_template(
"crud/asset.html",
asset=asset,
asset_form=asset_form,
msg=msg,
mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""),
user_can_create_assets=user_can_create_assets(),
user_can_delete_asset=user_can_delete(asset),
)
@login_required
def delete_with_data(self, id: str):
"""Delete via /assets/delete_with_data/<id>"""
InternalApi().delete(url_for("AssetAPI:delete", id=id))
return self.index(
msg=f"Asset {id} and assorted meter readings / forecasts have been deleted."
)
def _set_account(asset_form: NewAssetForm) -> Tuple[Optional[Account], Optional[str]]:
"""Set an account for the to-be-created asset.
Return the account (if available) and an error message"""
account_error = None
if asset_form.account_id.data == -1:
if user_has_admin_access(current_user, "update"):
return None, None # Account can be None (public asset)
else:
account_error = "Please pick an existing account."
account = Account.query.filter_by(id=int(asset_form.account_id.data)).one_or_none()
if account:
asset_form.account_id.data = account.id
else:
current_app.logger.error(account_error)
return account, account_error
def _set_asset_type(
asset_form: NewAssetForm,
) -> Tuple[Optional[GenericAssetType], Optional[str]]:
"""Set an asset type for the to-be-created asset.
Return the asset type (if available) and an error message."""
asset_type = None
asset_type_error = None
if int(asset_form.generic_asset_type_id.data) == -1:
asset_type_error = "Pick an existing asset type."
else:
asset_type = GenericAssetType.query.filter_by(
id=int(asset_form.generic_asset_type_id.data)
).one_or_none()
if asset_type:
asset_form.generic_asset_type_id.data = asset_type.id
else:
current_app.logger.error(asset_type_error)
return asset_type, asset_type_error