Skip to content

Commit

Permalink
avoid using superdesk.app (#2377)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek authored Sep 13, 2022
1 parent f33ac4a commit 754c8e5
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 32 deletions.
6 changes: 3 additions & 3 deletions apps/publish/odbc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions apps/saved_searches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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 = {
Expand All @@ -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)


Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions superdesk/auth/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion superdesk/commands/clean_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 6 additions & 10 deletions superdesk/commands/flush_elastic_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion superdesk/commands/index_from_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions superdesk/io/commands/add_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion superdesk/io/commands/remove_expired_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion superdesk/io/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions superdesk/publish/transmitters/odbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", {})
Expand Down

0 comments on commit 754c8e5

Please sign in to comment.