Repository: hydroshare/hydroshare

Issue:
Version message at top of resource incorrect at top of landing page
When there is a newer version of a resource the message at the top is supposed to state "A newer version of this resource is available that replaces this version" as below.
![image](https://user-images.githubusercontent.com/3456833/183246365-153484d1-6bdb-427e-9a30-b732436660ca.png)

However for this resource https://www.hydroshare.org/resource/e0cbe52ad4524c07bb5b7ff8c373a343/ the message is incorrect
![image](https://user-images.githubusercontent.com/3456833/183246402-145b339d-4442-41b5-819d-ae1541a0e382.png)

Versioning was worked on in #4633  and #4637.  Look first at those changes to see if they introduced this bug.



Discussion:
Resources that I've found impacted:
https://www.hydroshare.org/resource/0f02af18e5344ae7a65dfa7fe1444f34/
https://www.hydroshare.org/resource/e0cbe52ad4524c07bb5b7ff8c373a343/
https://www.hydroshare.org/resource/bfdfd782ffc74c42b0347690ae543961/

From my initial investigation, it seems like this occurs when someone creates a new version of a resource and publishes that new version _before_ the celery [create_new_version_resource_task](https://github.com/hydroshare/hydroshare/blob/develop/hs_core/tasks.py#L475) is actually fully completed.

working on a fix...

## More info
### 0f02af18e5344ae7a65dfa7fe1444f34
> curl -X GET "https://www.hydroshare.org/hsapi/resource/0f02af18e5344ae7a65dfa7fe1444f34/scimeta/elements/" -H  "accept: application/json"

> "relations": [
>     {
>       "type": "isReplacedBy",
>       "value": "Nölscher, M., M. Mutz, S. Broda (2022). EU-MOHP v013.1.1 Dataset, HydroShare, https://doi.org/10.4211/hs.0d6999591fb048cab5ab71fcb690eadb, DOI for this published resource is pending activation."
>     }
>   ]

> "dates": [
>     {
>       "type": "created",
>       "start_date": "2021-06-09T11:25:08.697369Z",
>       "end_date": null
>     },
>     {
>       "type": "modified",
>       "start_date": "2021-06-25T10:12:37.742585Z",
>       "end_date": null
>     },
>     {
>       "type": "published",
>       "start_date": "2021-06-25T10:12:28.919219Z",
>       "end_date": null
>     }
>   ],

`curl -X GET "https://www.hydroshare.org/hsapi/resource/0d6999591fb048cab5ab71fcb690eadb/scimeta/elements/" -H  "accept: application/json"`

> "dates": [
>     {
>       "type": "created",
>       "start_date": "2022-05-18T14:59:04.044654Z",
>       "end_date": null
>     },
>     {
>       "type": "modified",
>       "start_date": "2022-06-27T12:10:55.498493Z",
>       "end_date": null
>     },
>     {
>       "type": "published",
>       "start_date": "2022-06-27T12:10:46.838592Z",
>       "end_date": null
>     }
>   ],
scratch that. what causes this is [update_relation_meta](https://github.com/hydroshare/hydroshare/blob/develop/hs_core/models.py#L3749) which was added in https://github.com/hydroshare/hydroshare/pull/4442. This method updates all of the relation metadata on page load. Which is fine. Except that it has the same bug that caused https://github.com/hydroshare/hydroshare/issues/4633. So my fix will be the same as in PR https://github.com/hydroshare/hydroshare/pull/4637

Resolving patch:
diff --git a/hs_core/models.py b/hs_core/models.py
--- a/hs_core/models.py
+++ b/hs_core/models.py
@@ -2430,7 +2430,7 @@ def get_custom_citation(self):
             return ''
         return str(self.metadata.citation.first())
 
-    def get_citation(self, includePendingMessage=True):
+    def get_citation(self, forceHydroshareURI=True):
         """Get citation or citations from resource metadata."""
 
         citation_str_lst = []
@@ -2467,7 +2467,7 @@ def get_citation(self, includePendingMessage=True):
         citation_str_lst.append(self.metadata.title.value)
 
         isPendingActivation = False
-        if self.metadata.identifiers.all().filter(name="doi"):
+        if self.metadata.identifiers.all().filter(name="doi") and not forceHydroshareURI:
             hs_identifier = self.metadata.identifiers.all().filter(name="doi")[0]
             if self.doi.find('pending') >= 0 or self.doi.find('failure') >= 0:
                 isPendingActivation = True
@@ -2478,7 +2478,7 @@ def get_citation(self, includePendingMessage=True):
 
         citation_str_lst.append(", HydroShare, {url}".format(url=hs_identifier.url))
 
-        if isPendingActivation and includePendingMessage:
+        if isPendingActivation and not forceHydroshareURI:
             citation_str_lst.append(", DOI for this published resource is pending activation.")
 
         return ''.join(citation_str_lst)
@@ -3966,7 +3966,8 @@ def get_rdf_graph(self):
 
         # if custom citation does not exist, use the default citation
         if not self.citation.first():
-            graph.add((subject, DCTERMS.bibliographicCitation, Literal(self.resource.get_citation())))
+            graph.add((subject, DCTERMS.bibliographicCitation, Literal(
+                self.resource.get_citation(forceHydroshareURI=False))))
 
         from .hydroshare import current_site_url
         TYPE_SUBJECT = URIRef("{}/terms/{}".format(current_site_url(), self.resource.resource_type))
diff --git a/hs_core/page_processors.py b/hs_core/page_processors.py
--- a/hs_core/page_processors.py
+++ b/hs_core/page_processors.py
@@ -173,7 +173,7 @@ def get_page_context(page, user, resource_edit=False, extended_metadata_layout=N
                    'cm': content_model,
                    'resource_edit_mode': resource_edit,
                    'metadata_form': None,
-                   'citation': content_model.get_citation(),
+                   'citation': content_model.get_citation(forceHydroshareURI=False),
                    'custom_citation': content_model.get_custom_citation(),
                    'title': title,
                    'readme': readme,
@@ -283,7 +283,7 @@ def get_page_context(page, user, resource_edit=False, extended_metadata_layout=N
                'keywords': keywords,
                'metadata_status': metadata_status,
                'missing_metadata_elements': content_model.metadata.get_required_missing_elements(),
-               'citation': content_model.get_citation(),
+               'citation': content_model.get_citation(forceHydroshareURI=False),
                'custom_citation': content_model.get_custom_citation(),
                'citation_id': citation_id,
                'rights': content_model.metadata.rights,
diff --git a/hs_core/tasks.py b/hs_core/tasks.py
--- a/hs_core/tasks.py
+++ b/hs_core/tasks.py
@@ -504,8 +504,7 @@ def create_new_version_resource_task(ori_res_id, username, new_res_id=None):
             # element is copied over to this new version resource, needs to delete this element so
             # it can be created to link to its original resource correctly
             new_res.metadata.relations.all().filter(type=RelationTypes.isVersionOf).first().delete()
-        new_res.metadata.create_element('relation', type=RelationTypes.isVersionOf,
-                                        value=ori_res.get_citation(includePendingMessage=False))
+        new_res.metadata.create_element('relation', type=RelationTypes.isVersionOf, value=ori_res.get_citation())
 
         if ori_res.resource_type.lower() == "collectionresource":
             # clone contained_res list of original collection and add to new collection


Test patch: