Skip to content

Commit

Permalink
update settings module to async
Browse files Browse the repository at this point in the history
  • Loading branch information
devketanpro committed Jul 22, 2024
1 parent 2650493 commit f70269f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 25 deletions.
15 changes: 8 additions & 7 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ blinker==1.8.2
# raven
# sentry-sdk
# superdesk-core
boto3==1.34.144
boto3==1.34.145
# via superdesk-core
botocore==1.34.144
botocore==1.34.145
# via
# boto3
# s3transfer
Expand Down Expand Up @@ -107,7 +107,7 @@ coverage[toml]==7.6.0
# via pytest-cov
croniter==2.0.7
# via superdesk-core
cryptography==42.0.8
cryptography==43.0.0
# via
# authlib
# jwcrypto
Expand Down Expand Up @@ -277,6 +277,8 @@ multidict==6.0.5
# yarl
mypy-extensions==1.0.0
# via black
nest-asyncio==1.6.0
# via -r requirements.txt
oauth2client==4.1.3
# via flask-oidc-ex
oauthlib==3.2.2
Expand Down Expand Up @@ -351,13 +353,13 @@ pyparsing==3.1.2
# via
# httplib2
# pyrtf3
pypdf==4.3.0
pypdf==4.3.1
# via xhtml2pdf
pypng==0.20220715.0
# via qrcode
pyrtf3==0.47.5
# via -r requirements.txt
pytest==8.2.2
pytest==8.3.1
# via
# -r dev-requirements.in
# pytest-cov
Expand All @@ -366,7 +368,7 @@ pytest-cov==5.0.0
# via -r dev-requirements.in
pytest-mock==3.14.0
# via -r dev-requirements.in
python-bidi==0.4.2
python-bidi==0.5.0
# via xhtml2pdf
python-dateutil==2.9.0.post0
# via
Expand Down Expand Up @@ -446,7 +448,6 @@ six==1.16.0
# isodate
# oauth2client
# parse-type
# python-bidi
# python-dateutil
superdesk-core @ git+https://github.com/superdesk/superdesk-core.git@async
# via -r requirements.txt
Expand Down
3 changes: 1 addition & 2 deletions newsroom/media_releases/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ async def item(_id):
item = get_entity_or_404(_id, "items")
set_permissions(item, "media_releases")
ui_config_service = UiConfigResourceService()
display_char_count = await ui_config_service.get_section_config("media_releases").get("char_count", False)
config = await ui_config_service.get_section_config("factcheck")
config = await ui_config_service.get_section_config("media_releases")
display_char_count = config.get("char_count", False)
if is_json_request(flask.request):
return flask.jsonify(item)
Expand Down
50 changes: 36 additions & 14 deletions newsroom/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import re
import copy

import flask
import asyncio
import nest_asyncio

from flask_babel import gettext, lazy_gettext
from superdesk.utc import utcnow

from newsroom.utils import get_json_or_400, set_version_creator
from newsroom.template_filters import newsroom_config
from newsroom.decorator import admin_only, account_manager_only

# Apply nest_asyncio to allow nested event loops
nest_asyncio.apply()

blueprint = flask.Blueprint("settings", __name__)

Expand All @@ -23,7 +27,7 @@ def get_settings_collection():

@blueprint.route("/settings/<app_id>")
@account_manager_only
def app(app_id):
async def app(app_id):
for app in flask.current_app.settings_apps:
if app._id == app_id:
return flask.render_template("settings.html", setting_type=app_id, data=app.data())
Expand All @@ -32,10 +36,10 @@ def app(app_id):

@blueprint.route("/settings/general_settings", methods=["POST"])
@admin_only
def update_values():
async def update_values():
values = get_json_or_400()

error = validate_general_settings(values)
error = await validate_general_settings(values)
if error:
return "", error

Expand All @@ -48,12 +52,12 @@ def update_values():
return flask.jsonify(updates)


def get_initial_data(setting_key=None):
data = get_setting(setting_key=setting_key, include_audit=True)
async def get_initial_data(setting_key=None):
data = await get_setting(setting_key=setting_key, include_audit=True)
return data


def get_setting(setting_key=None, include_audit=False):
async def get_setting(setting_key=None, include_audit=False):
if not getattr(flask.g, "settings", None):
values = get_settings_collection().find_one(GENERAL_SETTINGS_LOOKUP)
settings = copy.deepcopy(flask.current_app._general_settings)
Expand All @@ -72,9 +76,9 @@ def get_setting(setting_key=None, include_audit=False):
return flask.g.settings


def get_client_config():
config = newsroom_config()
for key, setting in (get_setting() or {}).items():
async def get_client_config():
config = await newsroom_config()
for key, setting in (await get_setting() or {}).items():
if key not in ["_updated", "version_creator"]:
value = setting.get("value", setting.get("default"))
config["client_config"][key] = value
Expand All @@ -88,7 +92,7 @@ def get_client_config():
return config


def validate_general_settings(values):
async def validate_general_settings(values):
# validate email formats for company_expiry_alert_recipients
email_regex = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
fields = [
Expand All @@ -109,15 +113,33 @@ def validate_general_settings(values):
return gettext("{}: Email IDs not in proper format".format(field_txt))


def sync_get_setting(setting_key=None):
loop = asyncio.get_event_loop()
result = loop.run_until_complete(get_setting(setting_key))
return result


def sync_get_client_config(*args):
loop = asyncio.get_event_loop()
result = loop.run_until_complete(get_client_config(*args))
return result


def sync_get_initial_data(setting_key=None):
loop = asyncio.get_event_loop()
result = loop.run_until_complete(get_initial_data(setting_key))
return result


def init_app(app):
app.settings_app(
"general-settings",
lazy_gettext("General Settings"),
weight=800,
data=get_initial_data,
data=sync_get_initial_data,
)
app.add_template_global(get_setting)
app.add_template_global(get_client_config)
app.add_template_global(sync_get_setting, "get_setting")
app.add_template_global(sync_get_client_config, "get_client_config")

# basic settings
app.general_setting(
Expand Down
2 changes: 1 addition & 1 deletion newsroom/template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def is_admin_or_internal(user=None):
return flask.session.get("user_type") in allowed_user_types


def newsroom_config():
async def newsroom_config():
port = int(os.environ.get("PORT", "5000"))
return {
"websocket": os.environ.get("NEWSROOM_WEBSOCKET_URL", "ws://localhost:%d" % (port + 100,)),
Expand Down
7 changes: 6 additions & 1 deletion newsroom/web/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from newsroom.settings import SettingsApp
from newsroom.webpack import NewsroomWebpack
from newsroom.utils import short_highlighted_text, get_location_string, get_agenda_dates
import asyncio


class NewsroomWebApp(BaseNewsroomApp):
Expand Down Expand Up @@ -112,6 +113,10 @@ def load_app_instance_config(self):
# config from env var
self.config.from_envvar("NEWSROOM_SETTINGS", silent=True)

def sync_newsroom_config(self):
loop = asyncio.get_event_loop()
return loop.run_until_complete(newsroom_config())

def _setup_jinja(self):
self.add_template_filter(to_json, name="tojson")
self.add_template_filter(datetime_short)
Expand All @@ -133,7 +138,7 @@ def _setup_jinja(self):
self.add_template_global(is_company_admin)
self.add_template_global(is_admin_manager_or_company_admin)
self.add_template_global(authorized_settings_apps)
self.add_template_global(newsroom_config)
self.add_template_global(self.sync_newsroom_config, "newsroom_config")
self.add_template_global(is_admin)
self.add_template_global(get_initial_notifications)
self.add_template_global(hash_string, "hash")
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ xhtml2pdf>=0.2.4
sentry-sdk[flask]>=1.5.7,<2.7
python3-saml>=1.15,<1.17
google-auth>=2.6,<2.30
nest-asyncio>=1.6.0

superdesk-core @ git+https://github.com/superdesk/superdesk-core.git@async
superdesk-planning @ git+https://github.com/superdesk/superdesk-planning.git@async

0 comments on commit f70269f

Please sign in to comment.