Skip to content

Commit

Permalink
Add Content profile name in Production API and Content API [SDESK-744…
Browse files Browse the repository at this point in the history
…0] (#2811)

* add Content profile name in Production API [SDESK-7440]

* minor chnages

* add new field and refactore changes

* update content_types

* remove mapping

* minor changes

* refactore code
  • Loading branch information
devketanpro authored Jan 10, 2025
1 parent 86f3bad commit 83b2944
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
6 changes: 4 additions & 2 deletions apps/content_types/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
from apps.auth import get_user_id
from apps.desks import remove_profile_from_desks
from eve.utils import ParsedRequest
from superdesk.resource import build_custom_hateoas
from superdesk.resource import build_custom_hateoas, not_analyzed
from flask_babel import _
from superdesk.utc import utcnow
from superdesk.utils import format_content_type_name
from superdesk.services import CacheableService


Expand Down Expand Up @@ -108,6 +109,7 @@ class ContentTypesResource(superdesk.Resource):
"created_by": superdesk.Resource.rel("users", nullable=True),
"updated_by": superdesk.Resource.rel("users", nullable=True),
"init_version": {"type": "integer"},
"output_name": {"type": "string", "nullable": True},
}

item_url = r'regex("[\w,.:-]+")'
Expand Down Expand Up @@ -240,7 +242,7 @@ def get_output_name(self, profile):
try:
_id = bson.ObjectId(profile)
item = self.find_one(req=None, _id=_id) or {}
return re.compile("[^0-9a-zA-Z_]").sub("", item.get("label", str(_id)))
return format_content_type_name(item, str(_id))
except bson.errors.InvalidId:
return profile

Expand Down
1 change: 1 addition & 0 deletions prod_api/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"prod_api.planning",
"prod_api.contacts",
"prod_api.users",
"prod_api.content_types",
)

# NOTE: no trailing slash for the PRODAPI_URL setting!
Expand Down
13 changes: 13 additions & 0 deletions prod_api/content_types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import superdesk
from .service import ContentTypesService
from .resource import ContentTypesResource


def init_app(app) -> None:
"""Initialize the `content_api` API endpoint.
:param app: the API application object
:type app: `Eve`
"""
service = ContentTypesService(datasource="content_types", backend=superdesk.get_backend())
ContentTypesResource(endpoint_name="content_types", app=app, service=service)
24 changes: 24 additions & 0 deletions prod_api/content_types/resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2025 Sourcefabric z.u. and contributors.
#
# 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 superdesk.resource import Resource
from superdesk.metadata.utils import item_url


class ContentTypesResource(Resource):
url = "content_types"
item_url = item_url
item_methods = ["GET"]
resource_methods = ["GET"]
allow_unknown = True
datasource = {
"default_sort": [("priority", -1)],
}
internal_resource = True
24 changes: 24 additions & 0 deletions prod_api/content_types/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2025 Sourcefabric z.u. and contributors.
#
# 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

import bson
import re
import superdesk
from superdesk.utils import format_content_type_name


class ContentTypesService(superdesk.Service):
def get_output_name(self, profile):
try:
_id = bson.ObjectId(profile)
item = superdesk.get_resource_service("content_types").find_one(req=None, _id=_id) or {}
return format_content_type_name(item, str(_id))
except bson.errors.InvalidId:
return "None"
8 changes: 8 additions & 0 deletions prod_api/items/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# at https://www.sourcefabric.org/superdesk/license

from ..service import ProdApiService
from ..content_types.service import ContentTypesService


class ItemsService(ProdApiService):
Expand All @@ -22,3 +23,10 @@ class ItemsService(ProdApiService):
"lock_time",
"lock_user",
} | ProdApiService.excluded_fields

def _process_fetched_object(self, doc):
super()._process_fetched_object(doc)

profile_id = doc.get("profile")
if profile_id:
doc["profile"] = ContentTypesService().get_output_name(profile_id)
15 changes: 15 additions & 0 deletions superdesk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,18 @@ def get_list_chunks(items, chunk_size=100):

def get_dict_hash(data: Mapping) -> str:
return document_etag(data)


def format_content_type_name(item: Dict[str, str], default_name: str) -> str:
"""
Extracts and formats the name from the content type item.
Args:
item (Dict[str, str]): The content type item containing details.
default_name (str): The default name to use if no valid name is found.
Returns:
str: The formatted and sanitized name.
"""
name = item.get("output_name") or item.get("label", default_name)
return re.compile("[^0-9a-zA-Z_]").sub("", name)

0 comments on commit 83b2944

Please sign in to comment.