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

Mask locations of inaccessible assets on map #409

Merged
merged 6 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions flexmeasures/cli/__init__.py
@@ -1,3 +1,5 @@
import sys

from flask import Flask


Expand All @@ -13,3 +15,15 @@ def register_at(app: Flask):
import flexmeasures.cli.data_delete
import flexmeasures.cli.db_ops
import flexmeasures.cli.testing # noqa: F401


def is_running() -> bool:
"""
True if we are running one of the custom FlexMeasures CLI commands.
"""
cli_sets = ("add", "delete", "show", "monitor", "jobs", "db-ops")
command_line = " ".join(sys.argv)
for cli_set in cli_sets:
if f"flexmeasures {cli_set}" in command_line:
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
return True
return False
14 changes: 7 additions & 7 deletions flexmeasures/data/queries/utils.py
@@ -1,18 +1,18 @@
from typing import List, Optional, Type, Tuple, Union
from datetime import datetime, timedelta

from flask import current_app
from flask_security import current_user
from werkzeug.exceptions import Forbidden
import pandas as pd
import timely_beliefs as tb
from sqlalchemy.orm import Query, Session
from sqlalchemy.sql.elements import BinaryExpression
from sqlalchemy.sql.elements import BinaryExpression, or_

from flexmeasures.data.config import db
from flexmeasures.data.models.generic_assets import GenericAsset
from flexmeasures.data.models.data_sources import DataSource
from flexmeasures.utils import flexmeasures_inflection
from flexmeasures.cli import is_running as running_as_cli
import flexmeasures.data.models.time_series as ts # noqa: F401
from flexmeasures.auth.policy import ADMIN_ROLE, ADMIN_READER_ROLE

Expand Down Expand Up @@ -49,10 +49,10 @@ def potentially_limit_query_to_account_assets(

:param account_id: if set, all assets that are not in the given account will be filtered out (only works for admins and CLI users). For querying public assets in particular, don't use this function.
"""
if not current_app.cli and not current_user.is_authenticated:
if not running_as_cli() and not current_user.is_authenticated:
raise Forbidden("Unauthenticated user cannot list assets.")
user_is_admin = (
current_app.cli
running_as_cli()
or current_user.has_role(ADMIN_ROLE)
or current_user.has_role(ADMIN_READER_ROLE)
)
Expand All @@ -68,9 +68,9 @@ def potentially_limit_query_to_account_assets(
account_id if account_id is not None else current_user.account_id
)
return query.filter(
(
GenericAsset.account_id == account_id_to_filter
or GenericAsset.account_id is None
or_(
GenericAsset.account_id == account_id_to_filter,
GenericAsset.account_id is None,
)
)

Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/data/services/asset_grouping.py
Expand Up @@ -28,7 +28,7 @@ def get_asset_group_queries(
group_by_location: bool = False,
) -> Dict[str, Query]:
"""
An asset group is defined by Asset queries. Each query has a name, and we prefer pluralised names.
An asset group is defined by Asset queries. Each query has a name, and we prefer pluralised names.
They still need an executive call, like all(), count() or first().

This function limits the assets to be queried to the current user's account,
Expand Down