Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SensorAPI to API package under dev version
- Loading branch information
Showing
5 changed files
with
86 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from flask import Flask | ||
|
||
|
||
def register_at(app: Flask): | ||
"""This can be used to register FlaskViews.""" | ||
|
||
from flexmeasures.api.dev.sensors import SensorAPI | ||
|
||
SensorAPI.register(app, route_prefix="/api/dev") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from flask_classful import FlaskView, route | ||
from flask_login import login_required | ||
from flask_security import roles_required | ||
from marshmallow import fields | ||
from webargs.flaskparser import use_kwargs | ||
from werkzeug.exceptions import abort | ||
|
||
from flexmeasures.api.common.schemas.times import AwareDateTimeField | ||
from flexmeasures.data.models.time_series import Sensor | ||
|
||
|
||
class SensorAPI(FlaskView): | ||
""" | ||
This view exposes sensor attributes through API endpoints under development. | ||
These endpoints are not yet part of our official API, but support the FlexMeasures UI. | ||
""" | ||
|
||
route_base = "/sensor" | ||
|
||
@login_required | ||
@roles_required("admin") # todo: remove after we check for sensor ownership | ||
@route("/<id>/chart/") | ||
@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), | ||
"as_html": fields.Boolean(required=False), | ||
"dataset_name": fields.Str(required=False), | ||
}, | ||
location="query", | ||
) | ||
def get_chart(self, id, **kwargs): | ||
"""GET from /sensor/<id>/chart""" | ||
sensor = get_sensor_or_abort(id) | ||
return sensor.chart(**kwargs) | ||
|
||
@login_required | ||
@roles_required("admin") # todo: remove after we check for sensor ownership | ||
@route("/<id>/chart_data/") | ||
@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), | ||
}, | ||
location="query", | ||
) | ||
def get_chart_data(self, id, **kwargs): | ||
"""GET from /sensor/<id>/chart_data | ||
Data for use in charts (in case you have the chart specs already). | ||
""" | ||
sensor = get_sensor_or_abort(id) | ||
return sensor.search_beliefs(as_json=True, **kwargs) | ||
|
||
@login_required | ||
@roles_required("admin") # todo: remove after we check for sensor ownership | ||
def get(self, id: int): | ||
"""GET from /sensor/<id>""" | ||
sensor = get_sensor_or_abort(id) | ||
attributes = ["name", "timezone", "timerange"] | ||
return {attr: getattr(sensor, attr) for attr in attributes} | ||
|
||
|
||
def get_sensor_or_abort(id: int) -> Sensor: | ||
sensor = Sensor.query.filter(Sensor.id == id).one_or_none() | ||
if sensor is None: | ||
raise abort(404, f"Sensor {id} not found") | ||
return sensor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters