flexmeasures.data.models.user

Functions

flexmeasures.data.models.user.is_user(o) bool

True if object is or proxies a User, False otherwise.

Takes into account that object can be of LocalProxy type, and uses get_current_object to get the underlying (User) object.

flexmeasures.data.models.user.remember_last_seen(user)

Update the last_seen field

flexmeasures.data.models.user.remember_login(the_app, user)

We do not use the tracking feature of flask_security, but this basic meta data are quite handy to know

Classes

class flexmeasures.data.models.user.Account(**kwargs)

Account of a tenant on the server. Bundles Users as well as GenericAssets.

__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

has_role(role: str | AccountRole) bool

Returns True if the account has the specified role.

Parameters:

role – An account role name or AccountRole instance

query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

search_annotations(annotation_starts_after: datetime | None = None, annotations_after: datetime | None = None, annotation_ends_before: datetime | None = None, annotations_before: datetime | None = None, source: DataSource | list[DataSource] | int | list[int] | str | list[str] | None = None, as_frame: bool = False) list[Annotation] | pd.DataFrame

Return annotations assigned to this account.

Parameters:
  • annotations_after – only return annotations that end after this datetime (exclusive)

  • annotations_before – only return annotations that start before this datetime (exclusive)

class flexmeasures.data.models.user.AccountRole(**kwargs)
__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

class flexmeasures.data.models.user.Role(**kwargs)
__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

class flexmeasures.data.models.user.RolesAccounts(**kwargs)
__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

class flexmeasures.data.models.user.RolesUsers(**kwargs)
__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

class flexmeasures.data.models.user.User(**kwargs)

We use the flask security UserMixin, which does include functionality, but not the fields (those are in flask_security/models::FsUserMixin). We went with a pick&choose approach. This gives us more freedom, e.g. to choose our own table name or add logic around the activation status. If we add new FS functionality (e.g. 2FA), the fields needed for that need to be added here.

__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

has_role(role: str | Role) bool
Returns True if the user identifies with the specified role.

Overwritten from flask_security.core.UserMixin.

Parameters:

role – A role name or Role instance

property is_authenticated: bool

We are overloading this, so it also considers being active. Inactive users can by definition not be authenticated.

query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

roles

The roles attribute is being used by Flask-Security in the @roles_required decorator (among others). With this little overload fix, it will only return the user’s roles if they are authenticated. We do this to prevent that if a user is logged in while the admin deactivates them, their session would still work. In effect, we strip unauthenticated users from their roles. To read roles of an unauthenticated user (e.g. being inactive), use the flexmeasures_roles attribute. If our auth model has moved to an improved way, e.g. requiring modern tokens, we should consider relaxing this. Note: This needed to become a hybrid property when moving to Flask-Security 3.4