Repository: hydroshare/hydroshare

Issue:
Error while approving resource for publication
**Description of the bug**
while attempting to approve a resource for publication I got the following error:  "This resource does not have a pending metadata review for you to approve."
![Screen Shot 2023-01-20 at 8 51 00 AM](https://user-images.githubusercontent.com/90924476/213713439-63954a04-1acc-4666-818c-fc75a7a836ea.png)


Steps to reproduce the bug:
1. Go to https://www.hydroshare.org/resource/0be7f3c7b2044fba8f19bfb8e806f1c2/

**Expected behavior**
should just approve the resource when I hit accept

**Additional information**
Add any other context about the problem here such as screenshots, software versions, and browser information.



Discussion:
Making some notes here...
Help received the email for review until 1/20/23 (notably, after 2.2 deploy, email was sent ~midnight)
Checked the logs on both workers, didn't find anything of consequence, except:

> grep 0be7f3c7b2044fba8f19bfb8e806f1c2/submit-for-review/ access.log
152.54.2.89 - - [20/Jan/2023:05:32:40 +0000] "GET /hsapi/_internal/0be7f3c7b2044fba8f19bfb8e806f1c2/submit-for-review/ HTTP/1.0" 302 0 "https://www.hydroshare.org/resource/0be7f3c7b2044fba8f19bfb8e806f1c2/"
^^^the UTC time corresponds to the time that the metadata request was emailed to Help, as we would expect.

I see in the logs that there are a lot of changes to the resource AFTER it was submitted for review. I'm going to check to ensure that we are handling immutability correctly. Related to https://github.com/hydroshare/hydroshare/issues/4946
So this is one theory: that the res was edited in some case unaccounted for, after submitting metadata review. But this wouldn't explain how the resource got the "pending publication" status...

The res is not in a published state, request was never sent to Crossref (or at least there is no record in crossref).
But landing page shows:
![image](https://user-images.githubusercontent.com/17934193/213753218-fab4665f-9d10-434b-9612-7ca9b30506e6.png)
There is only one place I see that we set resource dois to "pending"
https://github.com/hydroshare/hydroshare/blob/2.2/hs_core/hydroshare/resource.py#L1102
has me wondering if somehow this resource could've been in pending state already, before review was requested? I'm going to check that...

I noted review started date needs an optimization:
https://github.com/hydroshare/hydroshare/blob/2.2/hs_core/views/__init__.py#L2115
the dates should be deleted on rejection AND approval, I will fix that. But this is not what caused the problem.

We should also probably set review_pending=false here:
https://github.com/hydroshare/hydroshare/blob/2.2/hs_core/views/__init__.py#L1192
as we do here:
https://github.com/hydroshare/hydroshare/blob/2.2/hs_core/views/__init__.py#L2100
but that isn't what caused this issue either.

```
>>> from hs_core.models import BaseResource
>>> res = BaseResource.objects.get(short_id='0be7f3c7b2044fba8f19bfb8e806f1c2')
>>> res
<BaseResource: Data for clogging enhancement in rock fractures>
>>> res.doi
'https://doi.org/10.4211/hs.0be7f3c7b2044fba8f19bfb8e806f1c2pending'
>>> res.raccess.review_pending
False
>>> res.metadata.dates.all().filter(type='review_started')
<QuerySet [<Date: review_started 2023-01-20 05:32:39.683591+00:00>]>
>>> res.raccess.published
False
>>> res.metadata.dates.all().filter(type='published')
<QuerySet []>
```

The resource has a pending DOI from Hydroshare's perspective (db shows it pending), but our Celery task will fail to resubmit the request to Crossref because this resource has no published date
https://github.com/hydroshare/hydroshare/blob/2.2/hs_core/tasks.py#L157
so we should actually be receiving emails warning us about this res, I'm not sure if we are:
https://github.com/hydroshare/hydroshare/blob/2.2/hs_core/tasks.py#L151

This feels a bit reminiscent of https://github.com/hydroshare/hydroshare/issues/4795

Seems there are a few other resources that are in this "state" that I will look into:
```
>>> for res in BaseResource.objects.filter(raccess__published=False, doi__contains='pending').all():
     print(f"{res.short_id}: {res.title}, {res.raccess.published_or_review_pending}")

d2d2aa2b02d94f10bde859aa2584a423: Blue Nile SWAT models setup using QSWAT Workflow v1.5.8 and QSWAT GUI, False
6923878ec11c4138a12cee1365d3fb8a: Conditioning digital elevation models for use in the evaluation of river networks and height above the nearest drainage for flood inundation mapping, False
4cda7a99dff04474867e336a9ccb7425: api-sd2, False
0be7f3c7b2044fba8f19bfb8e806f1c2: Data for clogging enhancement in rock fractures, False
```
https://www.hydroshare.org/resource/d2d2aa2b02d94f10bde859aa2584a423/
https://www.hydroshare.org/resource/6923878ec11c4138a12cee1365d3fb8a/
https://www.hydroshare.org/resource/4cda7a99dff04474867e336a9ccb7425/

Interesting to see that one of these res hasn't been edited since 2019
![image](https://user-images.githubusercontent.com/17934193/213777993-3009b33d-0b76-432e-920b-79ee552bf567.png)
So this means we have resources lingering with "pending publication status" that have been pending for YEARS...?


Resolving patch:
diff --git a/hs_core/hydroshare/utils.py b/hs_core/hydroshare/utils.py
--- a/hs_core/hydroshare/utils.py
+++ b/hs_core/hydroshare/utils.py
@@ -1078,7 +1078,7 @@ def add_metadata_element_to_xml(root, md_element, md_fields):
      [('first_name', 'firstName'), 'phone', 'email']
      # xml sub-elements names: firstName, phone, email
     """
-    import defusedxml.ElementTree as etree
+    from lxml import etree
     from hs_core.models import CoreMetaData
 
     name_spaces = CoreMetaData.NAMESPACES
diff --git a/hs_core/models.py b/hs_core/models.py
--- a/hs_core/models.py
+++ b/hs_core/models.py
@@ -30,7 +30,7 @@
 from django.utils.timezone import now
 
 from dominate.tags import div, legend, table, tbody, tr, th, td, h4
-import defusedxml.ElementTree as etree
+from lxml import etree
 from markdown import markdown
 from mezzanine.conf import settings as s
 from mezzanine.core.managers import PublishedManager


Test patch: