Skip to content

Commit

Permalink
Merge pull request #528 from samriddhi99/Issue-479-optimizing-api.con…
Browse files Browse the repository at this point in the history
…tent.get_view

Issue 479: Edited api.content.get_view for increased efficiency to get views
  • Loading branch information
davisagli committed Feb 8, 2024
2 parents 4a4c1c2 + 2e80155 commit 3565a0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions news/479.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved efficiency of view retrieval by deferring availability checks to error handling. @samriddhi99
44 changes: 24 additions & 20 deletions src/plone/api/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
from Products.CMFCore.WorkflowCore import WorkflowException
from zope.component import getMultiAdapter
from zope.component import getSiteManager
from zope.component import ComponentLookupError
from zope.container.interfaces import INameChooser
from zope.globalrequest import getRequest
from zope.interface import Interface
from zope.interface import providedBy


import random
import transaction

Expand Down Expand Up @@ -523,27 +525,29 @@ def get_view(name=None, context=None, request=None):
# available, because the __init__ of said view will contain
# errors in client code.

# Get all available views...
sm = getSiteManager()
available_views = sm.adapters.lookupAll(
required=(providedBy(context), providedBy(request)),
provided=Interface,
)
# and get their names.
available_view_names = [view[0] for view in available_views]

# Raise an error if the requested view is not available.
if name not in available_view_names:
raise InvalidParameterError(
"Cannot find a view with name '{name}'.\n"
"Available views are:\n"
"{views}".format(
name=name,
views="\n".join(sorted(available_view_names)),
),
try:
return getMultiAdapter((context, request), name=name)
except ComponentLookupError:
#Getting all available views
sm = getSiteManager()
available_views = sm.adapters.lookupAll(
required=(providedBy(context), providedBy(request)),
provided=Interface,
)
return getMultiAdapter((context, request), name=name)


# Check if the requested view is available
# by getting the names of all available views
available_view_names = [view[0] for view in available_views]
if name not in available_view_names:
# Raise an error if the requested view is not available.
raise InvalidParameterError(
"Cannot find a view with name '{name}'.\n"
"Available views are:\n"
"{views}".format(
name=name,
views="\n".join(sorted(available_view_names)),
),
)

@required_parameters("obj")
def get_uuid(obj=None):
Expand Down

0 comments on commit 3565a0b

Please sign in to comment.