simplify logic for finding all new versions of a resource
@@ -3837,21 +3837,23 @@ class BaseResource(Page, AbstractResource):
def replaced_by(self):
""" return a list or resources that replaced this one """
- from hs_core.hydroshare import get_resource_by_shortkey, current_site_url # prevent import loop
- replacedby = self.metadata.relations.all().filter(type=RelationTypes.isReplacedBy)
- rlist = []
- for r in replacedby:
- citation = r.value
- res_id = citation[-32:]
- # TODO: This is a mistake. This hardcodes the server on which the URI is created as its URI
- res_path = "{}/resource/{}".format(current_site_url(), res_id)
- if citation.endswith(res_path):
+ from hs_core.hydroshare import get_resource_by_shortkey
+
+ replaced_by_resources = []
+ def get_replaced_by(resource):
+ replace_relation_meta = resource.metadata.relations.all().filter(type=RelationTypes.isReplacedBy).first()
+ if replace_relation_meta is not None:
+ version_citation = replace_relation_meta.value
+ version_res_id = version_citation.split('/resource/')[-1]
try:
- rv = get_resource_by_shortkey(res_id, or_404=False)
- rlist.append(rv)
+ new_version_res = get_resource_by_shortkey(version_res_id, or_404=False)
+ replaced_by_resources.append(new_version_res)
+ get_replaced_by(new_version_res)
except BaseResource.DoesNotExist:
pass
- return rlist
+
+ get_replaced_by(self)
+ return replaced_by_resources
def get_relation_version_res_url(self, rel_type):
"""Extracts the resource url from resource citation stored in relation metadata for resource
@@ -3872,26 +3874,15 @@ class BaseResource(Page, AbstractResource):
return True if a resource should be exhibited
A resource should be exhibited if it is at least discoverable
and not replaced by anything that exists and is at least discoverable.
-
- A resource is hidden if there is any descendant (according to isReplacedBy)
- that is discoverable. The descendent tree is searched via breadth-first search
- with cycle elimination. Thus the search always terminates regardless of the
- complexity of descendents.
"""
if not self.raccess.discoverable:
return False # not exhibitable
- replacedby = self.replaced_by()
- visited = {}
- visited[self.short_id] = True
- # breadth-first replacement search, first discoverable replacement wins
- for r in replacedby:
- if r.raccess.discoverable:
+ replaced_by_resources = self.replaced_by()
+ if any([res.raccess.discoverable for res in replaced_by_resources]):
+ # there is a newer discoverable resource - so this resource should not be shown id discover
return False
- if r.short_id not in visited:
- replacedby.extend(r.replaced_by())
- visited[r.short_id] = True
- return True # no reason not to show it
+ return True
def update_relation_meta(self):
"""Updates the citation stored in relation metadata for relation type
