Skip to content

Commit

Permalink
Fix broken relations info (#1673)
Browse files Browse the repository at this point in the history
* Update add.py

* Fix broken relations info

- Additional info for broken: paths.
- Catch and ignore invalid/broken relations.
- Fix @id for relations, broken, and relation stats
- fix items_total for broken

* Create 1673.bugfix

* black
  • Loading branch information
ksuess committed Aug 14, 2023
1 parent 754b5e5 commit 719cf2f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
1 change: 1 addition & 0 deletions news/1673.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix broken relations info. @ksuess
2 changes: 1 addition & 1 deletion src/plone/restapi/services/relations/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@implementer(IPublishTraverse)
class PostRelations(Service):
"""Create new relations."""
"""Create new relations or rebuild relations."""

def __init__(self, context, request):
super().__init__(context, request)
Expand Down
32 changes: 12 additions & 20 deletions src/plone/restapi/services/relations/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@
except ImportError:
get_relations_stats = None

try:
from Products.CMFPlone.relationhelper import rebuild_relations
except ImportError:
try:
from collective.relationhelpers.api import rebuild_relations
except ImportError:
rebuild_relations = None


def make_summary(obj, request):
"""Add UID to metadata_fields."""
Expand Down Expand Up @@ -100,7 +92,8 @@ def get_relations(
except TypeError as e:
raise ValueError(str(e))
count = 0
relations = relation_catalog.findRelations(query)
relations = list(relation_catalog.findRelations(query))

for relation in relations:
if relation.isBroken():
if not onlyBroken:
Expand All @@ -125,11 +118,15 @@ def get_relations(
if onlyBroken:
results[relation.from_attribute].append(
[
source_obj and source_obj.absolute_url() or "",
target_obj and target_obj.absolute_url() or "",
source_obj and source_obj.absolute_url() or relation.from_path,
target_obj and target_obj.absolute_url() or relation.to_path,
]
)
else:
# Exclude relations without source or target.
# Dispensable with https://github.com/zopefoundation/z3c.relationfield/pull/24
if not source_obj or not target_obj:
continue
results[relation.from_attribute].append(
{
"source": make_summary(source_obj, request),
Expand Down Expand Up @@ -174,8 +171,6 @@ class GetRelations(Service):
onlyBroken: boolean: dictionary with broken relations per relation type
query_source: Restrict relations by path or SearchableText
query_target: Restrict relations by path or SearchableText
rebuild: Rebuild relations
flush: If rebuild, then this also flushes the intIds
Returns:
stats if no parameter, else relations
Expand All @@ -188,7 +183,6 @@ def reply(self):
# Disable CSRF protection
if "IDisableCSRFProtection" in dir(plone.protect.interfaces):
alsoProvides(self.request, plone.protect.interfaces.IDisableCSRFProtection)

source = self.request.get("source", None)
target = self.request.get("target", None)
relationship = self.request.get("relation", None)
Expand All @@ -209,24 +203,22 @@ def reply(self):
if len(relationNames) == 0:
return self.reply_no_content(status=204)
result = {
"@id": f'{self.request["SERVER_URL"]}{self.request.environ["REQUEST_URI"]}',
"@id": f"{self.context.absolute_url()}/@relations?onlyBroken=true",
"relations": {},
}
for relationName in relationNames:
rels = get_relations(relationship=relationName, onlyBroken=True)
result["relations"][relationName] = {
"items": rels[relationName],
"items_total": len(rels),
"items_total": len(rels[relationName]),
}
return result

# Stats
if not source and not target and not relationship:
try:
stats = relation_stats()
stats[
"@id"
] = f'{self.request["SERVER_URL"]}{self.request.environ["REQUEST_URI"]}'
stats["@id"] = f"{self.context.absolute_url()}/@relations"
return stats
except ImportError:
self.request.response.setStatus(501)
Expand Down Expand Up @@ -287,7 +279,7 @@ def reply(self):
)

result = {
"@id": f'{self.request["SERVER_URL"]}{self.request.environ["REQUEST_URI"]}',
"@id": f"{self.context.absolute_url()}/@relations?{self.request['QUERY_STRING']}",
"relations": {},
}
if relationship and not data:
Expand Down

0 comments on commit 719cf2f

Please sign in to comment.