From 973ee29644d86c445371d25a4e40614d83789443 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Tue, 11 Jul 2017 09:50:23 +0200
Subject: [PATCH] Ensure unique deposit to Crossref for a given metadata
 timestamp

---
 journals/views.py | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/journals/views.py b/journals/views.py
index b4c25fbbf..94fdc4ba6 100644
--- a/journals/views.py
+++ b/journals/views.py
@@ -587,6 +587,16 @@ def metadata_xml_deposit(request, doi_label, option='test'):
     Makes use of the python requests module.
     """
     publication = get_object_or_404(Publication, doi_label=doi_label)
+    timestamp = (publication.metadata_xml.partition(
+        '<timestamp>'))[2].partition('</timestamp>')[0]
+    doi_batch_id = (publication.metadata_xml.partition(
+        '<doi_batch_id>'))[2].partition('</doi_batch_id>')[0]
+    path = (settings.MEDIA_ROOT + publication.in_issue.path + '/'
+            + publication.get_paper_nr() + '/' + publication.doi_label.replace('.', '_')
+            + '_' + timestamp + '.xml')
+    if os.path.isfile(path):
+        errormessage = 'The metadata file for this metadata timestamp already exists'
+        return render(request, 'scipost/error.html', context={'errormessage': errormessage})
     if option == 'deposit':
         url = 'http://doi.crossref.org/servlet/deposit'
     elif option == 'test':
@@ -614,13 +624,6 @@ def metadata_xml_deposit(request, doi_label, option='test'):
     # Then create the associated Deposit object (saving the metadata to a file)
     if option == 'deposit':
         content = ContentFile(publication.metadata_xml)
-        timestamp = (publication.metadata_xml.partition(
-            '<timestamp>'))[2].partition('</timestamp>')[0]
-        doi_batch_id = (publication.metadata_xml.partition(
-            '<doi_batch_id>'))[2].partition('</doi_batch_id>')[0]
-        path = (settings.MEDIA_ROOT + publication.in_issue.path + '/'
-                + publication.get_paper_nr() + '/' + publication.doi_label.replace('.', '_')
-                + '_' + timestamp + '.xml')
         deposit = Deposit(publication=publication, timestamp=timestamp, doi_batch_id=doi_batch_id,
                           metadata_xml=publication.metadata_xml, deposition_date=timezone.now())
         deposit.metadata_xml_file.save(path, content)
-- 
GitLab