Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search sensors in API by asset #1219

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2d00ca5
feat: limit sensor api search to asset
joshuaunity Oct 22, 2024
e03759f
chore: added to changelog
joshuaunity Oct 23, 2024
7db6d45
chore: modify changelog
joshuaunity Oct 28, 2024
a69a924
refactor: removed extra permission checks and added extra filter layer
joshuaunity Oct 28, 2024
90e2c77
chore: added test for filtering by asset id
joshuaunity Oct 28, 2024
23ac6e2
refactor: all_accessible sesnor logic revamp
joshuaunity Oct 28, 2024
954962d
Merge branch 'main' into limit-sensors-search
joshuaunity Oct 29, 2024
37be8a8
refactor: updated query param name, logic and docstring
joshuaunity Oct 29, 2024
e9ce0b0
refactor: modified child asset query to be recursive
joshuaunity Oct 30, 2024
0d95d98
refactor: more concise query
joshuaunity Oct 30, 2024
c9dc708
refactor: extend test case
joshuaunity Oct 31, 2024
366568b
chore: recommiting
joshuaunity Oct 31, 2024
44a8aff
chore: update doc string
joshuaunity Oct 31, 2024
613e776
refactor: update test case for post sensor
joshuaunity Nov 1, 2024
9a391e9
refactor: update test case for post sensor v2
joshuaunity Nov 1, 2024
4bb8394
chore: pushing a suggestion
joshuaunity Nov 1, 2024
55bcf65
refactor: updated access logic for sensor index API
joshuaunity Nov 1, 2024
6dd2073
chore: updated test case due to change in sensor index API
joshuaunity Nov 1, 2024
49950b2
chore: update doc string and logic hierachy
joshuaunity Nov 1, 2024
e6cd1ea
refactor: updated logic and test case extentsion
joshuaunity Nov 4, 2024
5d0a626
chore: update docstring and added extra testcase
joshuaunity Nov 4, 2024
c794929
chore: added else statement
joshuaunity Nov 4, 2024
0c21942
chore: update testcase
joshuaunity Nov 4, 2024
6a71600
chore: test logic update
joshuaunity Nov 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions flexmeasures/api/v3_0/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask import current_app, url_for
from flask_classful import FlaskView, route
from flask_json import as_json
from flask_security import auth_required
from flask_security import auth_required, current_user
from marshmallow import fields, ValidationError
import marshmallow.validate as validate
from rq.job import Job, NoSuchJobError
Expand Down Expand Up @@ -155,7 +155,6 @@ def index(
:status 422: UNPROCESSABLE_ENTITY
"""
account_ids: list = [account.id]
accounts: list = [account]

if asset is not None:
child_assets = (
Expand All @@ -170,10 +169,14 @@ def index(
filter_statement = GenericAsset.account_id.in_(account_ids)

if all_accessible:
consultancy_account_ids: list = [
acc.consultancy_account_id for acc in accounts
]
account_ids.extend(consultancy_account_ids)
if current_user.has_role("consultant"):
consultancy_accounts = (
db.session.query(Account)
.filter(Account.consultancy_account_id == account.id)
.all()
)
consultancy_account_ids: list = [acc.id for acc in consultancy_accounts]
joshuaunity marked this conversation as resolved.
Show resolved Hide resolved
account_ids.extend(consultancy_account_ids)
joshuaunity marked this conversation as resolved.
Show resolved Hide resolved

if asset and asset.account_id not in account_ids:
return {"message": "Asset does not belong to the account"}, 422
Expand Down
13 changes: 12 additions & 1 deletion flexmeasures/api/v3_0/tests/test_sensors_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


@pytest.mark.parametrize(
"requesting_user, search_by, search_value, exp_sensor_name, exp_num_results, all_accessible, use_pagination",
"requesting_user, search_by, search_value, exp_sensor_name, exp_num_results, all_accessible, use_pagination, asset",
[
(
"test_supplier_user_4@seita.nl",
Expand All @@ -30,6 +30,7 @@
2,
True,
False,
5,
),
(
"test_supplier_user_4@seita.nl",
Expand All @@ -39,6 +40,7 @@
1,
True,
False,
None,
),
(
"test_supplier_user_4@seita.nl",
Expand All @@ -48,6 +50,7 @@
3,
True,
True,
None,
),
(
"test_supplier_user_4@seita.nl",
Expand All @@ -57,6 +60,7 @@
1,
False,
False,
None,
),
],
indirect=["requesting_user"],
Expand All @@ -71,6 +75,7 @@ def test_fetch_sensors(
exp_num_results,
all_accessible,
use_pagination,
asset,
):
"""
Retrieve all sensors.
Expand All @@ -91,6 +96,9 @@ def test_fetch_sensors(
if all_accessible:
query["all_accessible"] = True

if asset:
query["asset_id"] = asset

response = client.get(
url_for("SensorAPI:index"),
query_string=query,
Expand All @@ -110,6 +118,9 @@ def test_fetch_sensors(
assert response.json[0]["name"] == exp_sensor_name
assert len(response.json) == exp_num_results

if asset:
assert response.json[0]["generic_asset_id"] == asset

if search_by == "unit":
assert response.json[0]["unit"] == search_value

Expand Down
Loading