Skip to content

Commit

Permalink
feat: load Flask configuration from pydantic_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Dec 12, 2023
1 parent 9554ddd commit e0a4971
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 324 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ extend-exclude = '''
'''

[tool.pytest.ini_options]
env_files = ["web.env"]
testpaths = "web"

[tool.ruff]
Expand Down
1 change: 0 additions & 1 deletion web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ COPY misc/wsgi.py misc/gunicorn.py misc/run_webserver.sh /opt/bbb-visio/
COPY misc/delete_uploaded_files.cron /etc/cron.d/delete_uploaded_files
RUN chmod u+x /opt/bbb-visio/run_webserver.sh
COPY migrations /opt/bbb-visio/migrations
COPY instance/config.py /opt/bbb-visio/instance/config.py
COPY flaskr /opt/bbb-visio/flaskr

WORKDIR /opt/bbb-visio/
Expand Down
2 changes: 0 additions & 2 deletions web/Dockerfile-tests
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ RUN pip install -r /tmp/requirements.txt
COPY requirements.dev.txt /tmp/requirements.dev.txt
RUN pip install -r /tmp/requirements.dev.txt

COPY instance/config.py /opt/bbb-visio/instance/config.py

WORKDIR /opt/bbb-visio
ENTRYPOINT ["pytest"]
6 changes: 3 additions & 3 deletions web/flaskr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@


def setup_configuration(app, config=None):
app.config.from_pyfile("config.py")
if config:
app.config.from_mapping(config)

MainSettings.model_validate(config)
config_obj = MainSettings.model_validate(config or {})
app.config.from_object(config_obj)


def setup_cache(app):
Expand Down Expand Up @@ -114,7 +114,7 @@ def internal_error(error):


def create_app(test_config=None, gunicorn_logging=False):
app = Flask(__name__, instance_relative_config=True)
app = Flask(__name__)
setup_configuration(app, test_config)
setup_cache(app)
setup_logging(app, gunicorn_logging)
Expand Down
10 changes: 6 additions & 4 deletions web/flaskr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def get_params_with_checksum(self, action, params):
bigbluebutton_secret = current_app.config["BIGBLUEBUTTON_SECRET"]
s = "{}{}".format(
pr.url.replace("?", "").replace(
current_app.config["BIGBLUEBUTTON_ENDPOINT"] + "/", ""
f'{current_app.config["BIGBLUEBUTTON_ENDPOINT"]}/', ""
),
bigbluebutton_secret,
)
Expand Down Expand Up @@ -316,7 +316,7 @@ def create(self):
if param := self.meeting.maxParticipants:
params["maxParticipants"] = str(param)
if param := self.meeting.logoutUrl:
params["logoutURL"] = param
params["logoutURL"] = str(param)
if param := self.meeting.duration:
params["duration"] = str(param)
bigbluebutton_analytics_callback_url = current_app.config[
Expand All @@ -327,7 +327,9 @@ def create(self):
params.update(
{
"meetingKeepEvents": "true",
"meta_analytics-callback-url": bigbluebutton_analytics_callback_url,
"meta_analytics-callback-url": str(
bigbluebutton_analytics_callback_url
),
}
)
if self.meeting.attendeePW is None:
Expand Down Expand Up @@ -413,7 +415,7 @@ def create(self):
pr = request.prepare()
bigbluebutton_secret = BIGBLUEBUTTON_SECRET
s = "{}{}".format(
pr.url.replace("?", "").replace(BIGBLUEBUTTON_ENDPOINT + "/", ""),
pr.url.replace("?", "").replace(f"{BIGBLUEBUTTON_ENDPOINT}/", ""),
bigbluebutton_secret,
)
params["checksum"] = hashlib.sha1(s.encode("utf-8")).hexdigest()
Expand Down
7 changes: 4 additions & 3 deletions web/flaskr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,13 @@ def get_attendee_attendee_scopes(
Semble inutilisé.
"""

@computed_field()
@computed_field
def DOCUMENTATION_LINK(self) -> Dict[str, Any]:
return {
"url": self.DOCUMENTATION_LINK_URL,
"label": self.DOCUMENTATION_LINK_LABEL,
"is_external": self.DOCUMENTATION_LINK_URL.lower().startswith(
"is_external": self.DOCUMENTATION_LINK_URL
and self.DOCUMENTATION_LINK_URL.lower().startswith(
("/", self.SERVER_FQDN.lower())
),
}
Expand Down Expand Up @@ -811,7 +812,7 @@ def get_meeting_mail_subject(
Sous-titre de la page de documentation.
"""

@computed_field()
@computed_field
def WORDINGS(self) -> Dict[str, Any]:
return {
"a_meeting": self.WORDING_A_MEETING,
Expand Down
6 changes: 3 additions & 3 deletions web/flaskr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def is_rie():
if not request.remote_addr:
return False

return any(
IPAddress(request.remote_addr) in IPNetwork(network_ip)
for network_ip in current_app.config.get("RIE_NETWORK_IPS", [])
return current_app.config["RIE_NETWORK_IPS"] and any(
IPAddress(request.remote_addr) in IPNetwork(str(network_ip))
for network_ip in current_app.config["RIE_NETWORK_IPS"]
if network_ip
)
Loading

0 comments on commit e0a4971

Please sign in to comment.