Skip to content

Commit

Permalink
Merge branch 'main' into 867-export-sensor-data-with-non-unique-senso…
Browse files Browse the repository at this point in the history
…r-names

Signed-off-by: Felix Claessen <30658763+Flix6x@users.noreply.github.com>
  • Loading branch information
Flix6x committed Jan 8, 2024
2 parents e0f9419 + 909f161 commit 3a2c44e
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 27 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ services:
environment:
SQLALCHEMY_DATABASE_URI: "postgresql://fm-dev-db-user:fm-dev-db-pass@dev-db:5432/fm-dev-db"
SECRET_KEY: notsecret
FLASK_ENV: development
FLEXMEASURES_ENV: development
LOGGING_LEVEL: INFO
volumes:
# a place for config and plugin code - the mount point is for running the FlexMeasures CLI, the 2nd for gunicorn
Expand All @@ -80,7 +80,7 @@ services:
FLEXMEASURES_REDIS_URL: queue-db
FLEXMEASURES_REDIS_PASSWORD: fm-redis-pass
SECRET_KEY: notsecret
FLASK_ENV: development
FLEXMEASURES_ENV: development
LOGGING_LEVEL: INFO
volumes:
# a place for config and plugin code
Expand Down
19 changes: 18 additions & 1 deletion documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ FlexMeasures Changelog
**********************


v0.19.0 | February xx, 2024
============================

.. warning:: This version replaces FLASK_ENV with FLEXMEASURES_ENV (FLASK_ENV will still be used as a fallback).

New features
-------------

Infrastructure / Support
----------------------

* Deprecate use of flask's ``FLASK_ENV`` variable and replace it with ``FLEXMEASURES_ENV`` [see `PR #907 <https://github.com/FlexMeasures/flexmeasures/pull/907>`_]

Bugfixes
-----------


v0.18.1 | January XX, 2024
============================

Expand All @@ -16,7 +33,7 @@ Bugfixes
v0.18.0 | December 23, 2023
============================

.. note:: Read more on these features on `the FlexMeasures blog <https://flexmeasures.io/018-better-use-of-future-knowledge/>`__.
.. note:: Read more on these features on `the FlexMeasures blog <https://flexmeasures.io/018-better-use-of-future-knowledge/>`__.

.. warning:: Upgrading to this version requires running ``flexmeasures db upgrade`` (you can create a backup first with ``flexmeasures db-ops dump``).

Expand Down
2 changes: 1 addition & 1 deletion documentation/dev/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Also, we add some env settings in an `.env` file. Create that file in the `flexm

.. code-block:: bash
FLASK_ENV="development"
FLEXMEASURES_ENV="development"
LOGGING_LEVEL="INFO"
The development mode makes sure we don't need SSL to connect, among other things.
Expand Down
4 changes: 2 additions & 2 deletions documentation/host/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ Running the image (as a container) might work like this (remember to get the ima

.. code-block:: bash
docker run --env SQLALCHEMY_DATABASE_URI=postgresql://user:pass@localhost:5432/dbname --env SECRET_KEY=blabla --env FLASK_ENV=development -d --net=host lfenergy/flexmeasures
docker run --env SQLALCHEMY_DATABASE_URI=postgresql://user:pass@localhost:5432/dbname --env SECRET_KEY=blabla --env FLEXMEASURES_ENV=development -d --net=host lfenergy/flexmeasures
.. note:: Don't know what your image is called (its "tag")? We used ``lfenergy/flexmeasures`` here, as that should be the name when pulling it from Docker Hub. You can run ``docker images`` to see which images you have.

The two minimal environment variables to run the container successfully are the database URI and the secret key, see :ref:`configuration`. ``FLASK_ENV=development`` is needed if you do not have an SSL certificate set up (the default mode is ``production``, and in that mode FlexMeasures requires https for security reasons). If you see too much output, you can also set ``LOGGING_LEVEL=INFO``.
The two minimal environment variables to run the container successfully are the database URI and the secret key, see :ref:`configuration`. ``FLEXMEASURES_ENV=development`` is needed if you do not have an SSL certificate set up (the default mode is ``production``, and in that mode FlexMeasures requires https for security reasons). If you see too much output, you can also set ``LOGGING_LEVEL=INFO``.

In this example, we connect to a postgres database running on our local computer, so we use the host network. In the docker-compose section below, we use a Docker container for the database, as well.

Expand Down
4 changes: 2 additions & 2 deletions documentation/tut/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ Set an environment variable to indicate in which environment you are operating (

.. code-block:: bash
$ export FLASK_ENV=development
$ export FLEXMEASURES_ENV=development
(on Windows, use ``set`` instead of ``export``\ )

or:

.. code-block:: bash
$ echo "FLASK_ENV=development" >> .env
$ echo "FLEXMEASURES_ENV=development" >> .env
.. note:: The default is ``production``\ , which will not work well on localhost due to SSL issues.

Expand Down
4 changes: 2 additions & 2 deletions documentation/tut/toy-example-setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Install Flexmeasures and the database
.. code-block:: bash
$ docker run --rm --name flexmeasures-tutorial-db -e POSTGRES_PASSWORD=fm-db-passwd -e POSTGRES_DB=flexmeasures-db -d --network=flexmeasures_network postgres:latest
$ docker run --rm --name flexmeasures-tutorial-fm --env SQLALCHEMY_DATABASE_URI=postgresql://postgres:fm-db-passwd@flexmeasures-tutorial-db:5432/flexmeasures-db --env SECRET_KEY=notsecret --env FLASK_ENV=development --env LOGGING_LEVEL=INFO -d --network=flexmeasures_network -p 5000:5000 lfenergy/flexmeasures
$ docker run --rm --name flexmeasures-tutorial-fm --env SQLALCHEMY_DATABASE_URI=postgresql://postgres:fm-db-passwd@flexmeasures-tutorial-db:5432/flexmeasures-db --env SECRET_KEY=notsecret --env FLEXMEASURES_ENV=development --env LOGGING_LEVEL=INFO -d --network=flexmeasures_network -p 5000:5000 lfenergy/flexmeasures
To upgrade the FlexMeasures database, execute:

Expand Down Expand Up @@ -105,7 +105,7 @@ Install Flexmeasures and the database
$ pip install flexmeasures
$ export SQLALCHEMY_DATABASE_URI="postgresql://flexmeasures-user:fm-db-passwd@localhost:5432/flexmeasures-db" SECRET_KEY=notsecret LOGGING_LEVEL="INFO" DEBUG=0
$ export FLASK_ENV="development"
$ export FLEXMEASURES_ENV="development"
$ flexmeasures db upgrade
.. note:: When installing with ``pip``, on some platforms problems might come up (e.g. macOS, Windows). One reason is that FlexMeasures requires some libraries with lots of C code support (e.g. Numpy). One way out is to use Docker, which uses a prepared Linux image, so it'll definitely work.
Expand Down
17 changes: 10 additions & 7 deletions flexmeasures/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def create( # noqa C901
"""
Create a Flask app and configure it.
Set the environment by setting FLASK_ENV as environment variable (also possible in .env).
Or, overwrite any FLASK_ENV setting by passing an env in directly (useful for testing for instance).
Set the environment by setting FLEXMEASURES_ENV as environment variable (also possible in .env).
Or, overwrite any FLEXMEASURES_ENV setting by passing an env in directly (useful for testing for instance).
A path to a config file can be passed in (otherwise a config file will be searched in the home or instance directories).
Expand All @@ -51,10 +51,10 @@ def create( # noqa C901
app = Flask("flexmeasures")

if env is not None: # overwrite
app.env = env
if app.env == "testing":
app.config["FLEXMEASURES_ENV"] = env
if app.config.get("FLEXMEASURES_ENV") == "testing":
app.testing = True
if app.env == "development":
if app.config.get("FLEXMEASURES_ENV") == "development":
app.debug = config_defaults.DevelopmentConfig.DEBUG

# App configuration
Expand All @@ -63,7 +63,10 @@ def create( # noqa C901
if plugins:
app.config["FLEXMEASURES_PLUGINS"] += plugins
add_basic_error_handlers(app)
if app.env not in ("development", "documentation") and not app.testing:
if (
app.config.get("FLEXMEASURES_ENV") not in ("development", "documentation")
and not app.testing
):
init_sentry(app)

app.mail = Mail(app)
Expand Down Expand Up @@ -103,7 +106,7 @@ def create( # noqa C901
set_secret_key(app)
if app.config.get("SECURITY_PASSWORD_SALT", None) is None:
app.config["SECURITY_PASSWORD_SALT"] = app.config["SECRET_KEY"]
if app.env not in ("documentation", "development"):
if app.config.get("FLEXMEASURES_ENV") not in ("documentation", "development"):
SSLify(app)

# Prepare profiling, if needed
Expand Down
4 changes: 2 additions & 2 deletions flexmeasures/ui/static/js/flexmeasures.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ function clickableTable(element, urlColumn) {


function handleClick(event, url) {
// ignore clicks on <a href> or <button> elements
if (['a', 'button'].includes(event.target.tagName.toLowerCase())) {
// ignore clicks on <a href>, <button> or <input> elements
if (['a', 'button', 'input'].includes(event.target.tagName.toLowerCase())) {
return
} else if (event.ctrlKey) {
window.open(url, "_blank");
Expand Down
1 change: 0 additions & 1 deletion flexmeasures/ui/utils/view_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

def render_flexmeasures_template(html_filename: str, **variables):
"""Render template and add all expected template variables, plus the ones given as **variables."""
variables["flask_env"] = current_app.env
variables["FLEXMEASURES_ENFORCE_SECURE_CONTENT_POLICY"] = current_app.config.get(
"FLEXMEASURES_ENFORCE_SECURE_CONTENT_POLICY"
)
Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/utils/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def init_sentry(app: Flask):
debug=app.debug,
release=f"flexmeasures@{get_distribution('flexmeasures').version}",
send_default_pii=True, # user data (current user id, email address, username) is attached to the event.
environment=app.env,
environment=app.config.get("FLEXMEASURES_ENV"),
**app.config["FLEXMEASURES_SENTRY_CONFIG"],
)
sentry_sdk.set_tag("mode", app.config.get("FLEXMEASURES_MODE"))
Expand Down
2 changes: 2 additions & 0 deletions flexmeasures/utils/config_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Config(object):
LOGGING_LEVEL: int = logging.WARNING
SECRET_KEY: str | None = None

FLEXMEASURES_ENV_DEFAULT = "production"

SQLALCHEMY_DATABASE_URI: str | None = None
# https://stackoverflow.com/questions/33738467/how-do-i-know-if-i-can-disable-sqlalchemy-track-modifications
SQLALCHEMY_TRACK_MODIFICATIONS: bool = False
Expand Down
20 changes: 16 additions & 4 deletions flexmeasures/utils/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,31 @@ def check_app_env(env: str | None):
"production",
):
print(
f'Flask(flexmeasures) environment needs to be either "documentation", "development", "testing", "staging" or "production". It currently is "{env}".'
f'Flexmeasures environment needs to be either "documentation", "development", "testing", "staging" or "production". It currently is "{env}".'
)
sys.exit(2)


def read_config(app: Flask, custom_path_to_config: str | None):
"""Read configuration from various expected sources, complain if not setup correctly."""

check_app_env(app.env)
flexmeasures_env = DefaultConfig.FLEXMEASURES_ENV_DEFAULT
if app.testing:
flexmeasures_env = "testing"
elif os.getenv("FLEXMEASURES_ENV", None):
flexmeasures_env = os.getenv("FLEXMEASURES_ENV", None)
elif os.getenv("FLASK_ENV", None):
flexmeasures_env = os.getenv("FLASK_ENV", None)
app.logger.warning(
"'FLASK_ENV' is deprecated and replaced by FLEXMEASURES_ENV"
" Change FLASK_ENV to FLEXMEASURES_ENV in the environment variables",
)

check_app_env(flexmeasures_env)

# First, load default config settings
app.config.from_object(
"flexmeasures.utils.config_defaults.%sConfig" % camelize(app.env)
"flexmeasures.utils.config_defaults.%sConfig" % camelize(flexmeasures_env)
)

# Now, potentially overwrite those from config file or environment variables
Expand All @@ -98,7 +110,7 @@ def read_config(app: Flask, custom_path_to_config: str | None):

# Check for missing values.
# Documentation runs fine without them.
if not app.testing and app.env != "documentation":
if not app.testing and flexmeasures_env != "documentation":
if not are_required_settings_complete(app):
if not os.path.exists(used_path_to_config):
print(
Expand Down
4 changes: 2 additions & 2 deletions requirements/app.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ uniplot>=0.7.0
# >=2.5.2: https://github.com/marshmallow-code/flask-marshmallow/issues/262
Flask-SQLAlchemy>=2.5.2
# flask should be after all the flask plugins, because setup might find they ARE flask
# <2.3: https://github.com/FlexMeasures/flexmeasures/issues/754
flask>=1.0, <2.3
# <3.0 https://github.com/FlexMeasures/flexmeasures/issues/936
flask>=1.0, <3.0
werkzeug<=2.2.3
vl-convert-python
Pillow>=10.0.1 # https://github.com/FlexMeasures/flexmeasures/security/dependabot/91

0 comments on commit 3a2c44e

Please sign in to comment.