Skip to content

Commit

Permalink
Make primary field target adapter return early.
Browse files Browse the repository at this point in the history
When there is no primary field, do not go through all fields.
First compare the field name, then the permission: comparing two strings is cheaper.
If the field name matches, stop iterating over other fields: you have already found the field.
  • Loading branch information
mauritsvanrees committed Dec 12, 2024
1 parent d012d3f commit 4e928a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions news/1851.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make primary field target adapter return early.
[maurits]
15 changes: 8 additions & 7 deletions src/plone/restapi/serializer/dxcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,24 @@ def __init__(self, context, request):

def __call__(self):
primary_field_name = self.get_primary_field_name()
if not primary_field_name:
return
for schema in iterSchemata(self.context):
read_permissions = mergedTaggedValueDict(schema, READ_PERMISSIONS_KEY)

for name, field in getFields(schema).items():
if not self.check_permission(read_permissions.get(name), self.context):
continue

if name != primary_field_name:
continue

if not self.check_permission(read_permissions.get(name), self.context):
return

target_adapter = queryMultiAdapter(
(field, self.context, self.request), IPrimaryFieldTarget
)
if target_adapter:
target = target_adapter()
if target:
return target
if not target_adapter:
return
return target_adapter()

def get_primary_field_name(self):
fieldname = None
Expand Down

0 comments on commit 4e928a9

Please sign in to comment.