def get(self, id: str): """GET from /users/<id>""" get_user_response = InternalApi().get( url_for("flexmeasures_api_v2_0.get_user", id=id)) user = process_internal_api_response(get_user_response.json(), make_obj=True) return render_user(user)
def owned_by(self, owner_id: str): """/assets/owned_by/<user_id>""" get_assets_response = InternalApi().get( url_for("flexmeasures_api_v2_0.get_assets"), query={"owner_id": owner_id}) assets = [ process_internal_api_response(ad, make_obj=True) for ad in get_assets_response.json() ] return render_flexmeasures_template("crud/assets.html", assets=assets)
def index(self, msg=""): """/assets""" get_assets_response = InternalApi().get( url_for("flexmeasures_api_v2_0.get_assets")) assets = [ process_internal_api_response(ad, make_obj=True) for ad in get_assets_response.json() ] return render_flexmeasures_template("crud/assets.html", assets=assets, message=msg)
def index(self, msg=""): """/assets""" get_assets_response = InternalApi().get( url_for("AssetAPI:index"), query={"account_id": current_user.account_id}) assets = [ process_internal_api_response(ad, make_obj=True) for ad in get_assets_response.json() ] return render_flexmeasures_template("crud/assets.html", account=current_user.account, assets=assets, message=msg)
def index(self): """/users""" include_inactive = request.args.get("include_inactive", "0") != "0" get_users_response = InternalApi().get( url_for("flexmeasures_api_v2_0.get_users", include_inactive=include_inactive)) users = [ process_internal_api_response(user, make_obj=True) for user in get_users_response.json() ] return render_flexmeasures_template("crud/users.html", users=users, include_inactive=include_inactive)
def toggle_active(self, id: str): """Toggle activation status via /users/toggle_active/<id>""" user: User = get_user(id) user_response = InternalApi().patch( url_for("UserAPI:patch", id=id), args={"active": not user.active}, ) patched_user: User = process_internal_api_response( user_response.json(), make_obj=True) return render_user( user, msg="User %s's new activation status is now %s." % (patched_user.username, patched_user.active), )
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 delete_with_data(self, id: str): """Delete via /assets/delete_with_data/<id>""" InternalApi().delete( url_for("flexmeasures_api_v2_0.delete_asset", id=id), ) return self.index( msg= f"Asset {id} and assorted meter readings / forecasts have been deleted." )
def reset_password_for(self, id: str): """/users/reset_password_for/<id> Set the password to something random (in case of worries the password might be compromised) and send instructions on how to reset.""" user: User = get_user(id) InternalApi().patch(url_for("UserAPI:reset_user_password", id=id), ) return render_user( user, msg="The user's password has been changed to a random password" " and password reset instructions have been sent to the user." " Cookies and the API access token have also been invalidated.", )
def reset_password_for(self, id: str): """/users/reset_password_for/<id> Set the password to something random (in case of worries the password might be compromised) and send instructions on how to reset.""" user: User = get_user(id) InternalApi().patch( url_for("flexmeasures_api_v2_0.reset_user_password", id=id), ) return render_user( user, msg="The user's password has been changed to a random password" " and password reset instructions have been sent to the user.", )
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 current_user.has_role("admin"): 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)) latest_measurement_time_str, asset_plot_html = _get_latest_power_plot( asset) return render_flexmeasures_template( "crud/asset.html", asset=asset, asset_form=asset_form, msg="", latest_measurement_time_str=latest_measurement_time_str, asset_plot_html=asset_plot_html, mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""), )
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, )
def index(self): """/users""" include_inactive = request.args.get("include_inactive", "0") != "0" users = [] if current_user.has_role(ADMIN_ROLE) or current_user.has_role( ADMIN_READER_ROLE): accounts = Account.query.all() else: accounts = [current_user.account] for account in accounts: get_users_response = InternalApi().get( url_for( "UserAPI:index", account_id=account.id, include_inactive=include_inactive, )) users += [ process_internal_api_response(user, make_obj=True) for user in get_users_response.json() ] return render_flexmeasures_template("crud/users.html", users=users, include_inactive=include_inactive)
def get(self, id: str): """GET from /users/<id>""" get_user_response = InternalApi().get(url_for("UserAPI:get", id=id)) user: User = process_internal_api_response(get_user_response.json(), make_obj=True) asset_count = 0 if user: get_users_assets_response = InternalApi().get( url_for("AssetAPI:index", account_id=user.account_id)) asset_count = len(get_users_assets_response.json()) return render_user(user, asset_count=asset_count)
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() and "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) latest_measurement_time_str, asset_plot_html = _get_latest_power_plot( asset) # 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.", latest_measurement_time_str=latest_measurement_time_str, asset_plot_html=asset_plot_html, mapboxAccessToken=current_app.config.get( "MAPBOX_ACCESS_TOKEN", ""), ) 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()) asset = GenericAsset.query.get(id) latest_measurement_time_str, asset_plot_html = _get_latest_power_plot( asset) return render_flexmeasures_template( "crud/asset.html", asset=asset, asset_form=asset_form, msg=msg, latest_measurement_time_str=latest_measurement_time_str, asset_plot_html=asset_plot_html, mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""), )
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: Asset = None error_msg = "" if id == "create": asset_form = with_options(NewAssetForm()) owner, owner_error = set_owner(asset_form) market, market_error = set_market(asset_form) if asset_form.asset_type_name.data == "none chosen": asset_form.asset_type_name.data = "" form_valid = asset_form.validate_on_submit() # Fill up the form with useful errors for the user if owner_error is not None: form_valid = False asset_form.owner_id.errors.append(owner_error) if market_error is not None: form_valid = False asset_form.market_id.errors.append(market_error) # Create new asset or return the form for new assets with a message if form_valid and owner is not None and market is not None: post_asset_response = InternalApi().post( url_for("flexmeasures_api_v2_0.post_assets"), 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(): error_msg = post_asset_response.json()["message"] 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(db, user=current_user), mapboxAccessToken=current_app.config.get( "MAPBOX_ACCESS_TOKEN", ""), ) else: asset_form = with_options(AssetForm()) if not asset_form.validate_on_submit(): return render_flexmeasures_template( "crud/asset_new.html", asset_form=asset_form, msg="Cannot edit asset.", map_center=get_center_location(db, user=current_user), mapboxAccessToken=current_app.config.get( "MAPBOX_ACCESS_TOKEN", ""), ) patch_asset_response = InternalApi().patch( url_for("flexmeasures_api_v2_0.patch_asset", 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}" ) asset_form.process_api_validation_errors( patch_asset_response.json()) asset = Asset.query.get(id) latest_measurement_time_str, asset_plot_html = get_latest_power_as_plot( asset) return render_flexmeasures_template( "crud/asset.html", asset=asset, asset_form=asset_form, msg=msg, latest_measurement_time_str=latest_measurement_time_str, asset_plot_html=asset_plot_html, mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""), )