From 754c8e5053fa42a6fc5a65df4d7a0a5f05dc1a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Tue, 13 Sep 2022 08:53:44 +0200 Subject: [PATCH] avoid using superdesk.app (#2377) --- apps/publish/odbc_test.py | 6 +++--- apps/saved_searches/__init__.py | 9 ++++----- superdesk/auth/oauth.py | 8 ++++---- superdesk/commands/clean_images.py | 2 +- superdesk/commands/flush_elastic_index.py | 16 ++++++---------- superdesk/commands/index_from_mongo.py | 2 +- superdesk/io/commands/add_provider.py | 7 +++---- superdesk/io/commands/remove_expired_content.py | 3 ++- superdesk/io/ingest.py | 2 +- superdesk/publish/transmitters/odbc.py | 4 ++-- 10 files changed, 27 insertions(+), 32 deletions(-) diff --git a/apps/publish/odbc_test.py b/apps/publish/odbc_test.py index bb099fa7e2..2a283e240d 100644 --- a/apps/publish/odbc_test.py +++ b/apps/publish/odbc_test.py @@ -81,19 +81,19 @@ class ODBCTests(TestCase): ] def setUp(self): - self.subscribers[0]["destinations"][0]["config"]["connection_string"] = superdesk.app.config[ + self.subscribers[0]["destinations"][0]["config"]["connection_string"] = self.app.config[ "ODBC_TEST_CONNECTION_STRING" ] self.app.data.insert("subscribers", self.subscribers) - self.queue_items[0]["destination"]["config"]["connection_string"] = superdesk.app.config[ + self.queue_items[0]["destination"]["config"]["connection_string"] = self.app.config[ "ODBC_TEST_CONNECTION_STRING" ] self.app.data.insert("publish_queue", self.queue_items) init_app(self.app) def test_transmit(self): - if superdesk.app.config["ODBC_PUBLISH"]: + if self.app.config["ODBC_PUBLISH"]: subscriber = self.app.data.find("subscribers", None, None)[0] publish_service = ODBCPublishService() diff --git a/apps/saved_searches/__init__.py b/apps/saved_searches/__init__.py index 89be8fe169..a42edc5388 100644 --- a/apps/saved_searches/__init__.py +++ b/apps/saved_searches/__init__.py @@ -28,7 +28,7 @@ import pytz from croniter import croniter from datetime import datetime -from flask import render_template +from flask import render_template, current_app as app from superdesk import emails import json @@ -84,7 +84,7 @@ def get_next_date(scheduling, base=None): :return datetime: date of next schedule """ if base is None: - tz = pytz.timezone(superdesk.app.config["DEFAULT_TIMEZONE"]) + tz = pytz.timezone(app.config["DEFAULT_TIMEZONE"]) base = datetime.now(tz=tz) cron_iter = croniter(scheduling, base) return cron_iter.get_next(datetime) @@ -99,7 +99,6 @@ def send_report_email(user_id, search, docs): users_service = get_resource_service("users") user_data = next(users_service.find({"_id": user_id})) recipients = [user_data["email"]] - app = superdesk.app admins = app.config["ADMINS"] subject = "Saved searches report" context = { @@ -120,7 +119,7 @@ def publish_report(user_id, search_data): search_filter = json.loads(search_data["filter"]) query = es_utils.filter2query(search_filter, user_id=user_id) repos = es_utils.filter2repos(search_filter) or es_utils.REPOS.copy() - docs = list(superdesk.app.data.elastic.search(query, repos)) + docs = list(app.data.elastic.search(query, repos)) send_report_email(user_id, search_data, docs) @@ -155,7 +154,7 @@ def report(): try: saved_searches = get_resource_service("saved_searches") subscribed_searches = saved_searches.find({"subscribers": {"$exists": 1}}) - tz = pytz.timezone(superdesk.app.config["DEFAULT_TIMEZONE"]) + tz = pytz.timezone(app.config["DEFAULT_TIMEZONE"]) now = datetime.now(tz=tz) for search in subscribed_searches: do_update = False diff --git a/superdesk/auth/oauth.py b/superdesk/auth/oauth.py index 4b710ba72d..b33f0d201f 100644 --- a/superdesk/auth/oauth.py +++ b/superdesk/auth/oauth.py @@ -29,7 +29,7 @@ from typing import Optional, List, Tuple from bson import ObjectId import superdesk -from flask import url_for, render_template +from flask import url_for, render_template, current_app as app from flask_babel import lazy_gettext as l_ from eve.utils import config from authlib.integrations.flask_client import OAuth @@ -149,16 +149,16 @@ def google_login(url_id=None): if OAuth is used for Superdesk login, url_id is None. Otherwise, it is used to associate the token with the provider needing it """ - superdesk.app.redis.set(KEY_GOOGLE_PROVIDER_ID, url_id or "", ex=TTL_GOOGLE_PROVIDER_ID) + app.redis.set(KEY_GOOGLE_PROVIDER_ID, url_id or "", ex=TTL_GOOGLE_PROVIDER_ID) redirect_uri = url_for(".google_authorized", _external=True) return oauth.google.authorize_redirect(redirect_uri) @bp.route("/login/google_authorized") def google_authorized(): - token_id = superdesk.app.redis.get(KEY_GOOGLE_PROVIDER_ID) + token_id = app.redis.get(KEY_GOOGLE_PROVIDER_ID) if token_id is not None: token_id = token_id.decode() - superdesk.app.redis.delete(KEY_GOOGLE_PROVIDER_ID) + app.redis.delete(KEY_GOOGLE_PROVIDER_ID) token = oauth.google.authorize_access_token() if not token: diff --git a/superdesk/commands/clean_images.py b/superdesk/commands/clean_images.py index 62c72e3f4a..5dce5df137 100644 --- a/superdesk/commands/clean_images.py +++ b/superdesk/commands/clean_images.py @@ -58,7 +58,7 @@ def run(self): print("Number of used files: ", len(used_images)) - superdesk.app.media.remove_unreferenced_files(used_images) + app.media.remove_unreferenced_files(used_images) def __add_existing_files(self, used_images, items): for item in items: diff --git a/superdesk/commands/flush_elastic_index.py b/superdesk/commands/flush_elastic_index.py index 7b8e94bbab..de97e75c6c 100644 --- a/superdesk/commands/flush_elastic_index.py +++ b/superdesk/commands/flush_elastic_index.py @@ -45,12 +45,12 @@ def run(self, sd_index, capi_index): if not (sd_index or capi_index): raise SystemExit("You must specify at least one elastic index to flush. " "Options: `--sd`, `--capi`") - self._es = get_es(superdesk.app.config["ELASTICSEARCH_URL"]) + self._es = get_es(app.config["ELASTICSEARCH_URL"]) if sd_index: - self._delete_elastic(superdesk.app.config["ELASTICSEARCH_INDEX"]) + self._delete_elastic(app.config["ELASTICSEARCH_INDEX"]) if capi_index: - self._delete_elastic(superdesk.app.config["CONTENTAPI_ELASTICSEARCH_INDEX"]) + self._delete_elastic(app.config["CONTENTAPI_ELASTICSEARCH_INDEX"]) self._index_from_mongo(sd_index, capi_index) @@ -97,21 +97,17 @@ def _index_from_mongo(self, sd_index, capi_index): for resource in resources: # get es prefix per resource - es_backend = superdesk.app.data._search_backend(resource) + es_backend = app.data._search_backend(resource) resource_es_prefix = es_backend._resource_prefix(resource) if resource_es_prefix == SD_ELASTIC_PREFIX and sd_index: - print( - '- Indexing mongo collections into "{}" elastic index.'.format( - superdesk.app.config["ELASTICSEARCH_INDEX"] - ) - ) + print('- Indexing mongo collections into "{}" elastic index.'.format(app.config["ELASTICSEARCH_INDEX"])) IndexFromMongo.copy_resource(resource, IndexFromMongo.default_page_size) if resource_es_prefix == CAPI_ELASTIC_PREFIX and capi_index: print( '- Indexing mongo collections into "{}" elastic index.'.format( - superdesk.app.config["CONTENTAPI_ELASTICSEARCH_INDEX"] + app.config["CONTENTAPI_ELASTICSEARCH_INDEX"] ) ) IndexFromMongo.copy_resource(resource, IndexFromMongo.default_page_size) diff --git a/superdesk/commands/index_from_mongo.py b/superdesk/commands/index_from_mongo.py index 95f5c7f058..29297ba0a9 100644 --- a/superdesk/commands/index_from_mongo.py +++ b/superdesk/commands/index_from_mongo.py @@ -61,7 +61,7 @@ def copy_resource(cls, resource, page_size, last_id=None): for i in range(1, 4): try: - success, failed = superdesk.app.data._search_backend(resource).bulk_insert(resource, items) + success, failed = app.data._search_backend(resource).bulk_insert(resource, items) except Exception as ex: print("Exception thrown on insert to elastic {}", ex) time.sleep(10) diff --git a/superdesk/io/commands/add_provider.py b/superdesk/io/commands/add_provider.py index 7d3dc07c12..73f135aae4 100644 --- a/superdesk/io/commands/add_provider.py +++ b/superdesk/io/commands/add_provider.py @@ -9,6 +9,7 @@ # at https://www.sourcefabric.org/superdesk/license import superdesk +from flask import current_app as app from superdesk import get_resource_service from superdesk.errors import ProviderError @@ -35,11 +36,9 @@ def run(self, provider): try: data = {} data = superdesk.json.loads(provider) - data.setdefault("content_expiry", superdesk.app.config["INGEST_EXPIRY_MINUTES"]) + data.setdefault("content_expiry", app.config["INGEST_EXPIRY_MINUTES"]) - validator = superdesk.app.validator( - superdesk.app.config["DOMAIN"]["ingest_providers"]["schema"], "ingest_providers" - ) + validator = app.validator(app.config["DOMAIN"]["ingest_providers"]["schema"], "ingest_providers") validation = validator.validate(data) if validation: diff --git a/superdesk/io/commands/remove_expired_content.py b/superdesk/io/commands/remove_expired_content.py index 3aa87d0e07..783aca98f2 100644 --- a/superdesk/io/commands/remove_expired_content.py +++ b/superdesk/io/commands/remove_expired_content.py @@ -9,6 +9,7 @@ # at https://www.sourcefabric.org/superdesk/license import superdesk +from flask import current_app as app from superdesk.logging import logger from superdesk.utc import utcnow from superdesk.notification import push_notification @@ -88,7 +89,7 @@ def remove_expired_data(provider): for file_id in file_ids: logger.info("Deleting file: %s" % file_id) - superdesk.app.media.delete(file_id) + app.media.delete(file_id) logger.info( "Removed expired content for provider: {0} count: {1}".format(provider.get("_id", "Detached items"), len(ids)) diff --git a/superdesk/io/ingest.py b/superdesk/io/ingest.py index d4f7b73360..aa125fefbb 100644 --- a/superdesk/io/ingest.py +++ b/superdesk/io/ingest.py @@ -77,7 +77,7 @@ def on_deleted(self, docs): ] for file_id in file_ids: - superdesk.app.media.delete(file_id) + app.media.delete(file_id) ids = [ ref.get("residRef") diff --git a/superdesk/publish/transmitters/odbc.py b/superdesk/publish/transmitters/odbc.py index 464e74fc7f..efef9c34a6 100644 --- a/superdesk/publish/transmitters/odbc.py +++ b/superdesk/publish/transmitters/odbc.py @@ -9,8 +9,8 @@ # at https://www.sourcefabric.org/superdesk/license import json -import superdesk +from flask import current_app as app from superdesk.publish import register_transmitter from superdesk.publish.publish_service import PublishService from superdesk.errors import PublishODBCError @@ -43,7 +43,7 @@ def _transmit(self, queue_item, subscriber): Configuration must have connection string and the name of a stored procedure. """ - if not superdesk.app.config["ODBC_PUBLISH"] or not pyodbc_available: + if not app.config["ODBC_PUBLISH"] or not pyodbc_available: raise PublishODBCError() config = queue_item.get("destination", {}).get("config", {})