From e88f52d6a32acad7e1b4cf8ac5469fc197431b76 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Fri, 1 Apr 2016 06:41:25 +0200
Subject: [PATCH] Use DOI or arXiv identifiers for commentaries

---
 commentaries/models.py                        | 32 +++++++++++++++++--
 .../templates/commentaries/commentaries.html  |  7 ----
 .../commentaries/commentary_detail.html       |  2 +-
 .../commentaries/vet_commentary_requests.html |  1 +
 commentaries/urls.py                          | 10 ++++--
 commentaries/views.py                         |  9 ++++--
 .../comments/vet_submitted_comments.html      |  2 +-
 7 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/commentaries/models.py b/commentaries/models.py
index 0c179327d..ae60176f1 100644
--- a/commentaries/models.py
+++ b/commentaries/models.py
@@ -27,12 +27,13 @@ class Commentary(models.Model):
     pub_title = models.CharField(max_length=300, verbose_name='title')
     arxiv_link = models.URLField(verbose_name='arXiv link (including version nr)', blank=True)
     pub_DOI_link = models.URLField(verbose_name='DOI link to the original publication', blank=True)
+    arxiv_or_DOI_string = models.CharField(max_length=100, verbose_name='string form of arxiv nr or DOI for commentary url', default='')
     author_list = models.CharField(max_length=1000)
     # Authors which have been mapped to contributors:
     authors = models.ManyToManyField (Contributor, blank=True, related_name='authors_com')
     authors_claims = models.ManyToManyField (Contributor, blank=True, related_name='authors_com_claims')
     authors_false_claims = models.ManyToManyField (Contributor, blank=True, related_name='authors_com_false_claims')
-    pub_date = models.DateField(verbose_name='date of original publication')
+    pub_date = models.DateField(verbose_name='date of original publication', blank=True, null=True)
     pub_abstract = models.TextField(verbose_name='abstract')
     latest_activity = models.DateTimeField(default=timezone.now)
 
@@ -55,7 +56,34 @@ class Commentary(models.Model):
     def header_as_li (self):
         # for display in search lists
         header = '<li><div class="flex-container">'
-        header += '<div class="flex-whitebox0"><p><a href="/commentary/' + str(self.id) + '" class="pubtitleli">' + self.pub_title + '</a></p>'
+        #header += '<div class="flex-whitebox0"><p><a href="/commentary/' + str(self.id) + '" class="pubtitleli">' + self.pub_title + '</a></p>'
+        header += '<div class="flex-whitebox0"><p><a href="' + self.scipost_url() + '" class="pubtitleli">' + self.pub_title + '</a></p>'
         header += '<p>by ' + self.author_list + '</p><p> (published ' + str(self.pub_date) + ') - latest activity: ' + self.latest_activity.strftime('%Y-%m-%d %H:%M') + '</p></div>'
         header += '</div></li>'
         return header
+
+    def parse_link_into_url (self):
+        """ Takes the arXiv nr or DOI and turns it into the url suffix """
+        if self.pub_DOI_link:
+            self.arxiv_or_DOI_string = str(self.pub_DOI_link)
+            self.arxiv_or_DOI_string = self.arxiv_or_DOI_string.replace('http://dx.doi.org/', '')
+        else:
+            self.arxiv_or_DOI_string = str(self.arxiv_link)
+            # Format required: either identifier arXiv:1234.56789v10 or old-style arXiv:cond-mat/9712001v1
+            # strip: 
+            self.arxiv_or_DOI_string = self.arxiv_or_DOI_string.replace('http://', '')
+            # Old style: from arxiv.org/abs/1234.5678 into arXiv:1234.5678 (new identifier style)
+            self.arxiv_or_DOI_string = self.arxiv_or_DOI_string.replace('arxiv.org/', '')
+            self.arxiv_or_DOI_string = self.arxiv_or_DOI_string.replace('abs/', '')
+            self.arxiv_or_DOI_string = self.arxiv_or_DOI_string.replace('pdf/', '')
+            # make sure arXiv prefix is there:
+            self.arxiv_or_DOI_string = 'arXiv:' + self.arxiv_or_DOI_string
+        self.save()
+
+    def scipost_url (self):
+        """ Returns the url of the SciPost Commentary Page """
+        return '/commentary/' + self.arxiv_or_DOI_string 
+
+    def scipost_url_full (self):
+        """ Returns the url of the SciPost Commentary Page """
+        return 'https://scipost.org/commentary/' + self.arxiv_or_DOI_string 
diff --git a/commentaries/templates/commentaries/commentaries.html b/commentaries/templates/commentaries/commentaries.html
index 7a1092752..570c1ef46 100644
--- a/commentaries/templates/commentaries/commentaries.html
+++ b/commentaries/templates/commentaries/commentaries.html
@@ -42,13 +42,6 @@
   <h3>Search results:</h3>
   <ul>
     {% for commentary in commentary_search_list %}
-    <!--
-    <li>
-      <p><a href="{% url 'commentaries:commentary' commentary.id %}">{{ commentary.pub_title }}</a></p>
-      <p>by {{ commentary.author_list }}</p>
-      <p>(published {{ commentary.pub_date }})</p>
-    </li>
-    -->
     {{ commentary.header_as_li|safe }}
     {% endfor %}
   </ul>
diff --git a/commentaries/templates/commentaries/commentary_detail.html b/commentaries/templates/commentaries/commentary_detail.html
index 032cbc1de..5e5c841a5 100644
--- a/commentaries/templates/commentaries/commentary_detail.html
+++ b/commentaries/templates/commentaries/commentary_detail.html
@@ -57,7 +57,7 @@
   <div class="flex-greybox">
     <h1>Contribute a Comment:</h1>
   </div>
-  <form action="{% url 'commentaries:commentary' commentary.id %}" method="post">
+  <form action="{% url 'commentaries:commentary' arxiv_or_DOI_string=commentary.arxiv_or_DOI_string %}" method="post">
     {% csrf_token %}
     {% load crispy_forms_tags %}
     {% crispy form %}
diff --git a/commentaries/templates/commentaries/vet_commentary_requests.html b/commentaries/templates/commentaries/vet_commentary_requests.html
index 96413a544..cf82c47a3 100644
--- a/commentaries/templates/commentaries/vet_commentary_requests.html
+++ b/commentaries/templates/commentaries/vet_commentary_requests.html
@@ -21,6 +21,7 @@
 	<tr><td>Author(s): </td><td>{{ commentary_to_vet.author_list }}</td></tr>
 	<tr><td>arXiv link (including version nr): </td><td><a href="{{ commentary_to_vet.arxiv_link }}">{{ commentary_to_vet.arxiv_link }}</a></td></tr>
 	<tr><td>DOI Link: </td><td><a href="{{ commentary_to_vet.pub_DOI_link }}">{{ commentary_to_vet.pub_DOI_link }}</a></td></tr>
+	<tr><td>URL suffix for SP use: </td><td>{{ commentary_to_vet.arxiv_or_DOI_string }}</td></tr>
 	<tr><td>Date published: </td><td>{{ commentary_to_vet.pub_date }}</td></tr>
       </table>
       <br />
diff --git a/commentaries/urls.py b/commentaries/urls.py
index a3ef4e929..8d3f3961d 100644
--- a/commentaries/urls.py
+++ b/commentaries/urls.py
@@ -8,8 +8,14 @@ urlpatterns = [
     url(r'^$', views.commentaries, name='commentaries'),
     url(r'^browse/(?P<discipline>[a-z]+)/(?P<nrweeksback>[0-9]+)/$', views.browse, name='browse'),
     url(r'^howto$', TemplateView.as_view(template_name='commentaries/howto.html'), name='howto'),
-    #url(r'^commentary/(?P<commentary_id>[0-9]+)/$', views.commentary_detail, name='commentary'),
-    url(r'^(?P<commentary_id>[0-9]+)/$', views.commentary_detail, name='commentary'),
+    #url(r'^(?P<commentary_id>[0-9]+)/$', views.commentary_detail, name='commentary'),
+    # Match a DOI-based link:
+    url(r'^(?P<arxiv_or_DOI_string>10.[0-9]{4,9}/[-._;()/:a-zA-Z0-9]+)/$', views.commentary_detail, name='commentary'),
+    # Match an arxiv-based link:
+    # new style identifiers:
+    url(r'^(?P<arxiv_or_DOI_string>arXiv:[0-9]{4,}.[0-9]{5,}(v[0-9]+)?)/$', views.commentary_detail, name='commentary'),
+    # old style identifiers:
+    url(r'^(?P<arxiv_or_DOI_string>arXiv:[a-z-]+/[0-9]{7,}(v[0-9]+)?)/$', views.commentary_detail, name='commentary'),
     url(r'^request_commentary$', views.request_commentary, name='request_commentary'),
     url(r'^request_commentary_ack$', TemplateView.as_view(template_name='commentaries/request_commentary_ack.html'), name='request_commentary_ack'),
     url(r'^vet_commentary_requests$', views.vet_commentary_requests, name='vet_commentary_requests'),
diff --git a/commentaries/views.py b/commentaries/views.py
index 4c0b619a6..8b1f684c1 100644
--- a/commentaries/views.py
+++ b/commentaries/views.py
@@ -45,6 +45,7 @@ def request_commentary(request):
                     pub_abstract = form.cleaned_data['pub_abstract'],
                     latest_activity = timezone.now(),
                     )
+                commentary.parse_link_into_url()
                 commentary.save()
                 return HttpResponseRedirect('request_commentary_ack')
         else:
@@ -77,7 +78,7 @@ def vet_commentary_request_ack(request, commentary_id):
                 email_text = ('Dear ' + title_dict[commentary.requested_by.title] + ' ' + 
                               commentary.requested_by.user.last_name + ', \n\nThe Commentary Page you have requested, concerning publication with title ' + 
                               commentary.pub_title + ' by ' + commentary.author_list + 
-                              ', has been activated. You are now welcome to submit your comments.' + 
+                              ', has been activated at https://scipost.org/commentary/' + str(commentary.id) + '. You are now welcome to submit your comments.' + 
                               '\n\nThank you for your contribution, \nThe SciPost Team.')
                 emailmessage = EmailMessage('SciPost Commentary Page activated', email_text, 
                                             'SciPost commentaries <commentaries@scipost.org>', [commentary.requested_by.user.email, 'commentaries@scipost.org'], 
@@ -165,8 +166,10 @@ def browse(request, discipline, nrweeksback):
     return render(request, 'commentaries/commentaries.html', context)
 
 
-def commentary_detail(request, commentary_id):
-    commentary = get_object_or_404(Commentary, pk=commentary_id)
+#def commentary_detail(request, commentary_id):
+#    commentary = get_object_or_404(Commentary, pk=commentary_id)
+def commentary_detail(request, arxiv_or_DOI_string):
+    commentary = get_object_or_404(Commentary, arxiv_or_DOI_string=arxiv_or_DOI_string)
     comments = commentary.comment_set.all()
     if request.method == 'POST':
         form = CommentForm(request.POST)
diff --git a/comments/templates/comments/vet_submitted_comments.html b/comments/templates/comments/vet_submitted_comments.html
index 6f82c410a..b67175c11 100644
--- a/comments/templates/comments/vet_submitted_comments.html
+++ b/comments/templates/comments/vet_submitted_comments.html
@@ -16,7 +16,7 @@
   {% for comment_to_vet in comments_to_vet %}
 
   {% if comment_to_vet.commentary %}
-  <h3>From Commentary (<a href="{% url 'commentaries:commentary' commentary_id=comment_to_vet.commentary.id %}">link</a>)</h3>
+  <h3>From Commentary (<a href="{% url 'commentaries:commentary' arxiv_or_DOI_string=comment_to_vet.commentary.arxiv_or_DOI_string %}">link</a>)</h3>
   {{ comment_to_vet.commentary.header_as_table|safe }}
   <br />
   {% endif %}
-- 
GitLab