Skip to content

Commit

Permalink
[SDESK-7325] Move flask imports to superdesk module (superdesk#2650)
Browse files Browse the repository at this point in the history
* Changes to apps

* Changes to content_api

* Changes to prod_api

* Changes to superdesk

* Changes to docs

* Changes to tests

* run black
  • Loading branch information
MarkLark86 authored Jul 30, 2024
1 parent 72b877c commit 88d2543
Show file tree
Hide file tree
Showing 312 changed files with 2,202 additions and 1,779 deletions.
97 changes: 49 additions & 48 deletions apps/archive/archive.py

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions apps/archive/archive_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

import logging
from apps.auth import get_user
from flask import request, current_app as app
from superdesk import get_resource_service, Service, config

from superdesk.core import get_current_app, get_app_config
from superdesk.resource_fields import ID_FIELD
from superdesk.flask import request
from superdesk import get_resource_service, Service
from superdesk.metadata.item import ITEM_STATE, CONTENT_STATE, metadata_schema
from superdesk.resource import Resource
from apps.archive.common import ARCHIVE, ITEM_CANCEL_CORRECTION, ITEM_CORRECTION
Expand Down Expand Up @@ -46,7 +49,7 @@ def on_update(self, updates, original):
self._validate_correction(original)
archive_service = get_resource_service(ARCHIVE)
published_service = get_resource_service("published")
archive_item = archive_service.find_one(req=None, _id=original.get(config.ID_FIELD))
archive_item = archive_service.find_one(req=None, _id=original.get(ID_FIELD))

if remove_correction:
published_article = published_service.find_one(
Expand Down Expand Up @@ -96,7 +99,7 @@ def on_update(self, updates, original):
try:
# modify item in published.
_published_item = published_service.system_update(
published_article.get(config.ID_FIELD), published_item_updates, published_article
published_article.get(ID_FIELD), published_item_updates, published_article
)
assert (
remove_correction
Expand All @@ -108,7 +111,8 @@ def on_update(self, updates, original):
), "Being corrected is not generated"

# modify item in archive.
archive_service.system_update(archive_item.get(config.ID_FIELD), archive_item_updates, archive_item)
archive_service.system_update(archive_item.get(ID_FIELD), archive_item_updates, archive_item)
app = get_current_app().as_any()
app.on_archive_item_updated(archive_item_updates, archive_item, ITEM_CORRECTION)

except Exception as e:
Expand All @@ -118,7 +122,7 @@ def on_update(self, updates, original):
)

user = get_user(required=True)
push_notification("item:correction", item=original.get(config.ID_FIELD), user=str(user.get(config.ID_FIELD)))
push_notification("item:correction", item=original.get(ID_FIELD), user=str(user.get(ID_FIELD)))

def _validate_correction(self, original):
"""Validates the article to be corrected.
Expand All @@ -129,8 +133,7 @@ def _validate_correction(self, original):
if not original:
raise SuperdeskApiError.notFoundError(message=_("Cannot find the article"))

if (
not is_workflow_state_transition_valid("correction", original[ITEM_STATE])
and not config.ALLOW_UPDATING_SCHEDULED_ITEMS
if not is_workflow_state_transition_valid("correction", original[ITEM_STATE]) and not get_app_config(
"ALLOW_UPDATING_SCHEDULED_ITEMS"
):
raise InvalidStateTransitionError()
8 changes: 5 additions & 3 deletions apps/archive/archive_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license
from eve.utils import config
from flask import request, current_app as app

from superdesk.core import get_current_app
from superdesk.resource_fields import ID_FIELD
from superdesk.flask import request
from superdesk import get_resource_service, Service
from superdesk.metadata.item import GUID_TAG
from superdesk.resource import Resource
Expand Down Expand Up @@ -72,5 +73,6 @@ def delete(self, lookup):

archive_service.system_update(target_id, updates, target)
user = get_user(required=True)
push_notification("item:unlink", item=target_id, user=str(user.get(config.ID_FIELD)))
push_notification("item:unlink", item=target_id, user=str(user.get(ID_FIELD)))
app = get_current_app().as_any()
app.on_archive_item_updated(updates, target, ITEM_UNLINK)
2 changes: 1 addition & 1 deletion apps/archive/archive_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from flask import request
from superdesk.flask import request
from superdesk.resource import Resource, build_custom_hateoas
from superdesk.metadata.item import get_schema
from superdesk.metadata.utils import item_url
Expand Down
17 changes: 10 additions & 7 deletions apps/archive/archive_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

import logging

from flask import abort, current_app as app
from eve.utils import config
from superdesk.core import get_current_app, get_app_config
from superdesk.resource_fields import ID_FIELD, VERSION
from superdesk.flask import abort
from apps.archive.common import copy_metadata_from_user_preferences
from superdesk.media.media_operations import process_file_from_stream, decode_metadata
from superdesk.media.renditions import generate_renditions, delete_file_on_error, get_renditions_spec
Expand Down Expand Up @@ -41,14 +42,15 @@ class ArchiveMediaService:
def on_create(self, docs):
"""Create corresponding item on file upload."""

app = get_current_app()
for doc in docs:
if "media" not in doc or doc["media"] is None:
abort(400, description="No media found")
# check content type of video by python-magic
content_type = app.media._get_mimetype(doc["media"])
doc["media"].seek(0)
file_type = content_type.split("/")[0]
if file_type == "video" and app.config.get("VIDEO_SERVER_ENABLED"):
if file_type == "video" and get_app_config("VIDEO_SERVER_ENABLED"):
# upload media to video server
res, renditions, metadata = self.upload_file_to_video_server(doc)
# get thumbnails for timeline bar
Expand All @@ -58,7 +60,7 @@ def on_create(self, docs):
inserted = [doc["media"]]
# if no_custom_crops is set to False the custom crops are generated automatically on media upload
# see (SDESK-4742)
rendition_spec = get_renditions_spec(no_custom_crops=app.config.get("NO_CUSTOM_CROPS"))
rendition_spec = get_renditions_spec(no_custom_crops=get_app_config("NO_CUSTOM_CROPS"))
with timer("archive:renditions"):
renditions = generate_renditions(
file, doc["media"], inserted, file_type, content_type, rendition_spec, url_for_media
Expand Down Expand Up @@ -94,8 +96,8 @@ def _set_metadata(self, doc):
update_dates_for(doc)
generate_unique_id_and_name(doc)
doc.setdefault("guid", generate_guid(type=GUID_TAG))
doc.setdefault(config.ID_FIELD, doc["guid"])
doc[config.VERSION] = 1
doc.setdefault(ID_FIELD, doc["guid"])
doc[VERSION] = 1
set_item_expiry({}, doc)

if not doc.get("_import", None):
Expand All @@ -104,7 +106,7 @@ def _set_metadata(self, doc):
doc.setdefault(ITEM_STATE, CONTENT_STATE.DRAFT)

if not doc.get("ingest_provider"):
doc["source"] = app.config.get("DEFAULT_SOURCE_VALUE_FOR_MANUAL_ARTICLES")
doc["source"] = get_app_config("DEFAULT_SOURCE_VALUE_FOR_MANUAL_ARTICLES")

copy_metadata_from_user_preferences(doc)

Expand All @@ -118,6 +120,7 @@ def get_file_from_document(self, doc):
file_name, content_type, metadata = res
logger.debug("Going to save media file with %s " % file_name)
content.seek(0)
app = get_current_app()
with timer("media:put.original"):
doc["media"] = app.media.put(content, filename=file_name, content_type=content_type, metadata=metadata)
return content, content_type, decode_metadata(metadata)
Expand Down
45 changes: 23 additions & 22 deletions apps/archive/archive_rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import logging
from apps.auth import get_user, get_user_id
from eve.versioning import resolve_document_version
from flask import request, current_app as app

from superdesk.core import get_current_app, get_app_config
from superdesk.resource_fields import ID_FIELD
from superdesk.flask import request
from apps.archive import ArchiveSpikeService
from superdesk import get_resource_service, Service, config
from superdesk import get_resource_service, Service
from superdesk.metadata.item import (
ITEM_STATE,
EMBARGO,
Expand Down Expand Up @@ -105,13 +108,14 @@ def create(self, docs, **kwargs):

# signal
item_rewrite.send(self, item=rewrite, original=original)
app = get_current_app().as_any()

if update_document:
# process the existing story
archive_service.patch(update_document[config.ID_FIELD], rewrite)
archive_service.patch(update_document[ID_FIELD], rewrite)
app.on_archive_item_updated(rewrite, update_document, ITEM_LINK)
rewrite[config.ID_FIELD] = update_document[config.ID_FIELD]
ids = [update_document[config.ID_FIELD]]
rewrite[ID_FIELD] = update_document[ID_FIELD]
ids = [update_document[ID_FIELD]]
else:
# Set the version.
resolve_document_version(rewrite, ARCHIVE, "POST")
Expand Down Expand Up @@ -150,16 +154,15 @@ def _validate_rewrite(self, original, update):
if original.get("rewritten_by"):
raise SuperdeskApiError.badRequestError(message=_("Article has been rewritten before !"))

if (
not is_workflow_state_transition_valid("rewrite", original[ITEM_STATE])
and not config.ALLOW_UPDATING_SCHEDULED_ITEMS
if not is_workflow_state_transition_valid("rewrite", original[ITEM_STATE]) and not get_app_config(
"ALLOW_UPDATING_SCHEDULED_ITEMS"
):
raise InvalidStateTransitionError()

if (
original.get("rewrite_of")
and not (original.get(ITEM_STATE) in PUBLISH_STATES)
and not app.config["WORKFLOW_ALLOW_MULTIPLE_UPDATES"]
and not get_app_config("WORKFLOW_ALLOW_MULTIPLE_UPDATES")
):
raise SuperdeskApiError.badRequestError(
message=_("Rewrite is not published. Cannot rewrite the story again.")
Expand Down Expand Up @@ -216,8 +219,8 @@ def _create_rewrite_article(self, original, existing_item=None, desk_id=None):
]
existing_item_preserve_fields = (ASSOCIATIONS, "flags", "extra")

if app.config.get("COPY_ON_REWRITE_FIELDS"):
fields.extend(app.config["COPY_ON_REWRITE_FIELDS"])
if get_app_config("COPY_ON_REWRITE_FIELDS"):
fields.extend(get_app_config("COPY_ON_REWRITE_FIELDS"))

if existing_item:
# for associate an existing file as update merge subjects
Expand Down Expand Up @@ -276,11 +279,11 @@ def _create_rewrite_article(self, original, existing_item=None, desk_id=None):
rewrite["flags"]["marked_for_sms"] = False

# SD-4595 - Default value for the update article to be set based on the system config.
if config.RESET_PRIORITY_VALUE_FOR_UPDATE_ARTICLES:
if get_app_config("RESET_PRIORITY_VALUE_FOR_UPDATE_ARTICLES"):
# if True then reset to the default priority value.
rewrite["priority"] = int(config.DEFAULT_PRIORITY_VALUE_FOR_MANUAL_ARTICLES)
rewrite["priority"] = int(get_app_config("DEFAULT_PRIORITY_VALUE_FOR_MANUAL_ARTICLES"))

rewrite["rewrite_of"] = original[config.ID_FIELD]
rewrite["rewrite_of"] = original[ID_FIELD]
rewrite["rewrite_sequence"] = (original.get("rewrite_sequence") or 0) + 1
rewrite.pop(PROCESSED_FROM, None)

Expand Down Expand Up @@ -309,15 +312,12 @@ def _add_rewritten_flag(self, original, rewrite):
:param dict original: item on which rewrite is triggered
:param dict rewrite: rewritten document
"""
get_resource_service("published").update_published_items(
original[config.ID_FIELD], "rewritten_by", rewrite[config.ID_FIELD]
)
get_resource_service("published").update_published_items(original[ID_FIELD], "rewritten_by", rewrite[ID_FIELD])

# modify the original item as well.
get_resource_service(ARCHIVE).system_update(
original[config.ID_FIELD], {"rewritten_by": rewrite[config.ID_FIELD]}, original
)
app.on_archive_item_updated({"rewritten_by": rewrite[config.ID_FIELD]}, original, ITEM_REWRITE)
get_resource_service(ARCHIVE).system_update(original[ID_FIELD], {"rewritten_by": rewrite[ID_FIELD]}, original)
app = get_current_app().as_any()
app.on_archive_item_updated({"rewritten_by": rewrite[ID_FIELD]}, original, ITEM_REWRITE)

def _set_take_key(self, rewrite):
"""Sets the anpa take key of the rewrite with ordinal.
Expand Down Expand Up @@ -368,5 +368,6 @@ def delete(self, lookup):

archive_service.system_update(target_id, updates, target)
user = get_user(required=True)
push_notification("item:unlink", item=target_id, user=str(user.get(config.ID_FIELD)))
push_notification("item:unlink", item=target_id, user=str(user.get(ID_FIELD)))
app = get_current_app().as_any()
app.on_archive_item_updated(updates, target, ITEM_UNLINK)
Loading

0 comments on commit 88d2543

Please sign in to comment.