dashboard.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from flask import request, current_app
  2. from flask_security import login_required
  3. from flask_security.core import current_user
  4. from sqlalchemy import select
  5. from flexmeasures.auth.policy import user_has_admin_access
  6. from flexmeasures.data.queries.generic_assets import get_asset_group_queries
  7. from flexmeasures.data import db
  8. from flexmeasures.ui.views import flexmeasures_ui
  9. from flexmeasures.ui.utils.view_utils import render_flexmeasures_template, clear_session
  10. from flexmeasures.data.models.generic_assets import (
  11. GenericAssetType,
  12. get_center_location_of_assets,
  13. )
  14. from flexmeasures.data.services.asset_grouping import (
  15. AssetGroup,
  16. )
  17. # Dashboard (default root view, see utils/app_utils.py)
  18. @flexmeasures_ui.route("/dashboard")
  19. @login_required
  20. def dashboard_view():
  21. """Dashboard view.
  22. This is the default landing page.
  23. It shows a map with the location of all of the assets in the user's account,
  24. or all assets if the user is an admin.
  25. Assets are grouped by asset type, which leads to map layers and a table with asset counts by type.
  26. Admins get to see all assets.
  27. TODO: Assets for which the platform has identified upcoming balancing opportunities are highlighted.
  28. """
  29. msg = ""
  30. if "clear-session" in request.values:
  31. clear_session()
  32. msg = "Your session was cleared."
  33. aggregate_type_groups = current_app.config.get("FLEXMEASURES_ASSET_TYPE_GROUPS", {})
  34. group_by_accounts = request.args.get("group_by_accounts", "0") != "0"
  35. if user_has_admin_access(current_user, "read") and group_by_accounts:
  36. asset_groups = get_asset_group_queries(
  37. group_by_type=False, group_by_account=True
  38. )
  39. else:
  40. asset_groups = get_asset_group_queries(
  41. group_by_type=True, custom_aggregate_type_groups=aggregate_type_groups
  42. )
  43. map_asset_groups = {}
  44. for asset_group_name, asset_group_query in asset_groups.items():
  45. asset_group = AssetGroup(asset_group_name, asset_query=asset_group_query)
  46. if any([a.location for a in asset_group.assets]):
  47. map_asset_groups[asset_group_name] = asset_group
  48. known_asset_types = [
  49. gat.name for gat in db.session.scalars(select(GenericAssetType)).all()
  50. ]
  51. return render_flexmeasures_template(
  52. "dashboard.html",
  53. message=msg,
  54. mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""),
  55. map_center=get_center_location_of_assets(user=current_user),
  56. known_asset_types=known_asset_types,
  57. asset_groups=map_asset_groups,
  58. aggregate_type_groups=aggregate_type_groups,
  59. group_by_accounts=group_by_accounts,
  60. )