From e64e1cab424ea099717ae3a7133a422f937b01ed Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Sun, 25 Jun 2017 13:56:11 +0200
Subject: [PATCH] Alter Report numbering and add PDF functionality

PDF functionality is now read-only. Auto generating pdf's
function to be craeted.
---
 .../comments/_comment_identifier.html         |  2 +-
 .../migrations/0012_auto_20170625_1253.py     | 20 +++++++++++
 submissions/admin.py                          |  1 +
 .../migrations/0046_auto_20170625_1311.py     | 34 +++++++++++++++++++
 .../migrations/0047_auto_20170625_1331.py     | 29 ++++++++++++++++
 submissions/models.py                         | 21 ++++++++++++
 ...single_public_report_without_comments.html | 10 ++++--
 submissions/urls.py                           |  2 ++
 submissions/views.py                          | 12 ++++++-
 9 files changed, 127 insertions(+), 4 deletions(-)
 create mode 100644 partners/migrations/0012_auto_20170625_1253.py
 create mode 100644 submissions/migrations/0046_auto_20170625_1311.py
 create mode 100644 submissions/migrations/0047_auto_20170625_1331.py

diff --git a/comments/templates/comments/_comment_identifier.html b/comments/templates/comments/_comment_identifier.html
index f30c460f3..3247be4b6 100644
--- a/comments/templates/comments/_comment_identifier.html
+++ b/comments/templates/comments/_comment_identifier.html
@@ -16,7 +16,7 @@
             {% if comment.in_reply_to_comment %}
                 (in reply to <a href="#comment_id{{comment.in_reply_to_comment.id}}">{{comment.in_reply_to_comment.comment.get_author_str}}</a> on {{comment.in_reply_to_comment.date_submitted|date:'Y-m-d'}})
             {% elif comment.in_reply_to_report %}
-                (in reply to <a href="#report_id{{comment.in_reply_to_report.id}}">
+                (in reply to <a href="#report_{{comment.in_reply_to_report.report_nr}}">
 
                 {% if not comment.in_reply_to_report.anonymous %}
                     {{comment.in_reply_to_report.get_author_str}}
diff --git a/partners/migrations/0012_auto_20170625_1253.py b/partners/migrations/0012_auto_20170625_1253.py
new file mode 100644
index 000000000..30925ad95
--- /dev/null
+++ b/partners/migrations/0012_auto_20170625_1253.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2017-06-25 10:53
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('partners', '0011_auto_20170609_2234'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='prospectivepartnerevent',
+            name='event',
+            field=models.CharField(choices=[('requested', 'Requested (from online form)'), ('comment', 'Comment added'), ('email_sent', 'Email sent'), ('negotiating', 'Initiated negotiation'), ('marked_as_uninterested', 'Marked as uninterested'), ('promoted', 'Promoted to Partner')], max_length=64),
+        ),
+    ]
diff --git a/submissions/admin.py b/submissions/admin.py
index dfc3dff7b..67dcb372e 100644
--- a/submissions/admin.py
+++ b/submissions/admin.py
@@ -97,6 +97,7 @@ class ReportAdmin(admin.ModelAdmin):
     list_display_links = ('author',)
     date_hierarchy = 'date_submitted'
     list_filter = ('status',)
+    readonly_fields = ('report_nr',)
     form = ReportAdminForm
 
 
diff --git a/submissions/migrations/0046_auto_20170625_1311.py b/submissions/migrations/0046_auto_20170625_1311.py
new file mode 100644
index 000000000..d33f029e9
--- /dev/null
+++ b/submissions/migrations/0046_auto_20170625_1311.py
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2017-06-25 11:11
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+def set_report_counters(apps, schema_editor):
+    Report = apps.get_model('submissions', 'Report')
+    for report in Report.objects.order_by('date_submitted'):
+        if not report.report_nr:
+            report.report_nr = report.submission.reports.filter(report_nr__gte=1).count() + 1
+        report.save()
+    print('Updated all Report counters.')
+
+
+def do_nothing(*args):
+    pass
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('submissions', '0045_auto_20170608_1710'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='report',
+            name='report_nr',
+            field=models.PositiveSmallIntegerField(default=0),
+        ),
+        migrations.RunPython(set_report_counters, do_nothing)
+    ]
diff --git a/submissions/migrations/0047_auto_20170625_1331.py b/submissions/migrations/0047_auto_20170625_1331.py
new file mode 100644
index 000000000..151166d05
--- /dev/null
+++ b/submissions/migrations/0047_auto_20170625_1331.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2017-06-25 11:31
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('submissions', '0046_auto_20170625_1311'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='report',
+            name='pdf_report',
+            field=models.FileField(blank=True, max_length=200, upload_to='UPLOADS/REPORTS/%Y/%m/'),
+        ),
+        migrations.AlterField(
+            model_name='report',
+            name='report_nr',
+            field=models.PositiveSmallIntegerField(default=0, help_text='This number is a unique number refeering to the Report nr. of the Submission'),
+        ),
+        migrations.AlterUniqueTogether(
+            name='report',
+            unique_together=set([('submission', 'report_nr')]),
+        ),
+    ]
diff --git a/submissions/models.py b/submissions/models.py
index 17a364344..f6465d359 100644
--- a/submissions/models.py
+++ b/submissions/models.py
@@ -238,6 +238,10 @@ class Report(models.Model):
     status = models.CharField(max_length=16, choices=REPORT_STATUSES, default=STATUS_UNVETTED)
     submission = models.ForeignKey('submissions.Submission', related_name='reports',
                                    on_delete=models.CASCADE)
+    report_nr = models.PositiveSmallIntegerField(default=0,
+                                                 help_text='This number is a unique number '
+                                                           'refeering to the Report nr. of '
+                                                           'the Submission')
     vetted_by = models.ForeignKey('scipost.Contributor', related_name="report_vetted_by",
                                   blank=True, null=True, on_delete=models.CASCADE)
     # `invited' filled from RefereeInvitation objects at moment of report submission
@@ -268,13 +272,30 @@ class Report(models.Model):
     remarks_for_editors = models.TextField(default='', blank=True,
                                            verbose_name='optional remarks for the Editors only')
     anonymous = models.BooleanField(default=True, verbose_name='Publish anonymously')
+    pdf_report = models.FileField(upload_to='UPLOADS/REPORTS/%Y/%m/', max_length=200, blank=True)
 
     objects = ReportManager()
 
+    class Meta:
+        unique_together = ('submission', 'report_nr')
+
     def __str__(self):
         return (self.author.user.first_name + ' ' + self.author.user.last_name + ' on ' +
                 self.submission.title[:50] + ' by ' + self.submission.author_list[:50])
 
+    def get_absolute_url(self):
+        return self.submission.get_absolute_url() + '#report_' + str(self.report_nr)
+
+    def save(self, *args, **kwargs):
+        # Control Report count per Submission.
+        if not self.report_nr:
+            self.report_nr = self.submission.reports.count() + 1
+        return super().save(*args, **kwargs)
+
+    def report_default_counter(self):
+        print(self)
+        raise
+
 
 ##########################
 # EditorialCommunication #
diff --git a/submissions/templates/submissions/_single_public_report_without_comments.html b/submissions/templates/submissions/_single_public_report_without_comments.html
index bad492424..0d6e9a92f 100644
--- a/submissions/templates/submissions/_single_public_report_without_comments.html
+++ b/submissions/templates/submissions/_single_public_report_without_comments.html
@@ -3,13 +3,16 @@
 
 <div class="row">
     <div class="col-12">
-        <div class="report">
+        <div class="report" id="report_{{report.report_nr}}">
             {% if user.contributor == submission.editor_in_charge or user|is_in_group:'Editorial Administrators' and user|is_not_author_of_submission:submission.arxiv_identifier_w_vn_nr %}
 
                 <div class="reportid">
                     <h3>{% if report.anonymous %}(chose public anonymity) {% endif %}<a href="{% url 'scipost:contributor_info' report.author.id %}">{{ report.author.user.first_name }} {{ report.author.user.last_name }}</a>
                         on {{ report.date_submitted|date:'Y-n-j' }}</h3>
                     </h3>
+                    {% if report.pdf_report %}
+                        <a href="{% url 'submissions:report_detail_pdf' report.submission.arxiv_identifier_w_vn_nr report.report_nr %}" target="_blank">Download as PDF</a>
+                    {% endif %}
                 </div>
 
                 {% if report.flagged %}
@@ -39,9 +42,12 @@
                 </div>
             {% else %}
                 <div class="reportid">
-                    <h3 id="report_id{{report.id}}">{% if report.anonymous %}Anonymous Report {{report.id}}{% else %}<a href="{% url 'scipost:contributor_info' report.author.id %}">{{ report.author.user.first_name }} {{ report.author.user.last_name }}</a>{% endif %}
+                    <h3>{% if report.anonymous %}Anonymous Report {{report.report_nr}}{% else %}<a href="{% url 'scipost:contributor_info' report.author.id %}">{{ report.author.user.first_name }} {{ report.author.user.last_name }}</a>{% endif %}
                         on {{ report.date_submitted|date:'Y-n-j' }}</h3>
                     </h3>
+                    {% if report.pdf_report %}
+                        <a href="{% url 'submissions:report_detail_pdf' report.submission.arxiv_identifier_w_vn_nr report.report_nr %}" target="_blank">Download as PDF</a>
+                    {% endif %}
                 </div>
 
                 {% include 'submissions/_single_report_content.html' with report=report %}
diff --git a/submissions/urls.py b/submissions/urls.py
index 6e776fcf7..2b9375939 100644
--- a/submissions/urls.py
+++ b/submissions/urls.py
@@ -18,6 +18,8 @@ urlpatterns = [
         name='submission_wo_vn_nr'),
     url(r'^(?P<arxiv_identifier_w_vn_nr>[0-9]{4,}.[0-9]{5,}v[0-9]{1,2})/$',
         views.submission_detail, name='submission'),
+    url(r'^(?P<arxiv_identifier_w_vn_nr>[0-9]{4,}.[0-9]{5,}v[0-9]{1,2})/reports/(?P<report_nr>[0-9]+)/pdf$',
+        views.report_detail_pdf, name='report_detail_pdf'),
     url(r'^submit_manuscript$', views.RequestSubmission.as_view(), name='submit_manuscript'),
     url(r'^submit_manuscript/prefill$', views.prefill_using_arxiv_identifier,
         name='prefill_using_identifier'),
diff --git a/submissions/views.py b/submissions/views.py
index 6d5cdc5d5..5a84f528b 100644
--- a/submissions/views.py
+++ b/submissions/views.py
@@ -6,7 +6,7 @@ from django.contrib.auth.decorators import login_required, permission_required
 from django.contrib.auth.models import Group
 from django.core.urlresolvers import reverse, reverse_lazy
 from django.db import transaction
-from django.http import Http404, HttpResponseRedirect
+from django.http import Http404, HttpResponse, HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render, redirect
 from django.template import Template, Context
 from django.utils import timezone
@@ -219,6 +219,16 @@ def submission_detail(request, arxiv_identifier_w_vn_nr):
     return render(request, 'submissions/submission_detail.html', context)
 
 
+def report_detail_pdf(request, arxiv_identifier_w_vn_nr, report_nr):
+    report = get_object_or_404(Report.objects.accepted(),
+                               submission__arxiv_identifier_w_vn_nr=arxiv_identifier_w_vn_nr,
+                               pdf_report__isnull=False, report_nr=report_nr)
+    response = HttpResponse(report.pdf_report.read(), content_type='application/pdf')
+    filename = '%s_report-%i.pdf' % (report.submission.arxiv_identifier_w_vn_nr, report.report_nr)
+    response['Content-Disposition'] = ('filename=' + filename)
+    return response
+
+
 ######################
 # Editorial workflow #
 ######################
-- 
GitLab