From 70b8926d68cc0555aa1845b5cc4317346381d09b Mon Sep 17 00:00:00 2001
From: Geert Kapteijns <ghkapteijns@gmail.com>
Date: Sat, 25 Feb 2017 16:39:58 +0100
Subject: [PATCH] Extract comment query logic to manager

---
 comments/managers.py |  5 +++++
 comments/models.py   | 40 ++++++++++++++++++++++++----------------
 theses/test_views.py | 17 -----------------
 theses/views.py      | 12 +++++-------
 4 files changed, 34 insertions(+), 40 deletions(-)
 create mode 100644 comments/managers.py

diff --git a/comments/managers.py b/comments/managers.py
new file mode 100644
index 000000000..7b71d0454
--- /dev/null
+++ b/comments/managers.py
@@ -0,0 +1,5 @@
+from django.db import models
+
+class CommentManager(models.Manager):
+    def vetted(self):
+        return self.filter(status__gte=1)
diff --git a/comments/models.py b/comments/models.py
index e449d2a73..3851d0737 100644
--- a/comments/models.py
+++ b/comments/models.py
@@ -10,6 +10,8 @@ from scipost.models import TimeStampedModel, Contributor
 from submissions.models import Submission, Report
 from theses.models import ThesisLink
 
+from .managers import CommentManager
+
 COMMENT_CATEGORIES = (
     ('ERR', 'erratum'),
     ('REM', 'remark'),
@@ -37,19 +39,24 @@ class Comment(TimeStampedModel):
     on a particular publication or in reply to an earlier Comment. """
 
     status = models.SmallIntegerField(default=0)
-    vetted_by = models.ForeignKey(Contributor, blank=True, null=True,
-                                  on_delete=models.CASCADE,
-                                  related_name='comment_vetted_by')
-    file_attachment = models.FileField(upload_to='uploads/comments/%Y/%m/%d/', blank=True,
-                                       validators=[validate_file_extension,
-                                                   validate_max_file_size])
+    vetted_by = models.ForeignKey(
+        Contributor,
+        blank=True,
+        null=True,
+        on_delete=models.CASCADE,
+        related_name='comment_vetted_by'
+    )
+    file_attachment = models.FileField(
+        upload_to='uploads/comments/%Y/%m/%d/', blank=True,
+        validators=[validate_file_extension,
+        validate_max_file_size]
+    )
     # a Comment is either for a Commentary or Submission or a ThesisLink.
     commentary = models.ForeignKey(Commentary, blank=True, null=True, on_delete=models.CASCADE)
     submission = models.ForeignKey(Submission, blank=True, null=True, on_delete=models.CASCADE)
     thesislink = models.ForeignKey(ThesisLink, blank=True, null=True, on_delete=models.CASCADE)
     is_author_reply = models.BooleanField(default=False)
-    in_reply_to_comment = models.ForeignKey('self', blank=True, null=True,
-                                            on_delete=models.CASCADE)
+    in_reply_to_comment = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE)
     in_reply_to_report = models.ForeignKey(Report, blank=True, null=True, on_delete=models.CASCADE)
     author = models.ForeignKey(Contributor, on_delete=models.CASCADE)
     anonymous = models.BooleanField(default=False, verbose_name='Publish anonymously')
@@ -64,19 +71,20 @@ class Comment(TimeStampedModel):
     is_lit = models.BooleanField(default=False, verbose_name='pointer to related literature')
     is_sug = models.BooleanField(default=False, verbose_name='suggestion for further work')
     comment_text = models.TextField()
-    remarks_for_editors = models.TextField(default='', blank=True,
-                                           verbose_name='optional remarks for the Editors only')
+    remarks_for_editors = models.TextField(default='', blank=True, verbose_name='optional remarks for the Editors only')
     date_submitted = models.DateTimeField('date submitted')
     # Opinions
     nr_A = models.PositiveIntegerField(default=0)
-    in_agreement = models.ManyToManyField(Contributor,
-                                          related_name='in_agreement', blank=True)
+    in_agreement = models.ManyToManyField(Contributor, related_name='in_agreement', blank=True)
     nr_N = models.PositiveIntegerField(default=0)
-    in_notsure = models.ManyToManyField(Contributor,
-                                        related_name='in_notsure', blank=True)
+    in_notsure = models.ManyToManyField(Contributor, related_name='in_notsure', blank=True)
     nr_D = models.PositiveIntegerField(default=0)
-    in_disagreement = models.ManyToManyField(Contributor,
-                                             related_name='in_disagreement', blank=True)
+    in_disagreement = models.ManyToManyField(
+        Contributor,
+        related_name='in_disagreement',
+        blank=True
+    )
+    objects = CommentManager()
 
     def __str__(self):
         return ('by ' + self.author.user.first_name + ' ' + self.author.user.last_name +
diff --git a/theses/test_views.py b/theses/test_views.py
index 3ee2ccd9e..4cb20b101 100644
--- a/theses/test_views.py
+++ b/theses/test_views.py
@@ -30,23 +30,6 @@ class TestThesisDetail(TestCase):
         response = client.post(target)
         self.assertEqual(response.status_code, 200)
 
-    # def test_submitting_comment_creates_comment(self):
-    #     """ Valid Comment gets saved """
-    #
-    #     contributor = ContributorFactory()
-    #     thesislink = ThesisLinkFactory()
-    #     valid_comment_data = model_form_data(CommentFactory.build(), CommentForm)
-    #     target = reverse('theses:thesis', kwargs={'thesislink_id': thesislink.id})
-    #
-    #     comment_count = Comment.objects.filter(author=contributor).count()
-    #     self.assertEqual(comment_count, 0)
-    #
-    #     request = RequestFactory().post(target, valid_comment_data)
-    #     request.user = contributor.user
-    #     response = thesis_detail(request, thesislink_id=thesislink.id)
-    #
-    #     comment_count = Comment.objects.filter(author=contributor).count()
-    #     self.assertEqual(comment_count, 1)
 
 
 class TestRequestThesisLink(TestCase):
diff --git a/theses/views.py b/theses/views.py
index e11c21f2d..87d39a7f6 100644
--- a/theses/views.py
+++ b/theses/views.py
@@ -131,14 +131,12 @@ def browse(request, discipline, nrweeksback):
 
 def thesis_detail(request, thesislink_id):
     thesislink = get_object_or_404(ThesisLink, pk=thesislink_id)
-    comments = thesislink.comment_set.all()
     form = CommentForm()
-    try:
-        author_replies = Comment.objects.filter(thesislink=thesislink, is_author_reply=True)
-    except Comment.DoesNotExist:
-        author_replies = ()
-    # TODO: make manager for horribly obfuscating 'status__gte=1'
+
+    comments = thesislink.comment_set
+    author_replies = comments.filter(is_author_reply=True)
+
     context = {'thesislink': thesislink,
-               'comments': comments.filter(status__gte=1).order_by('date_submitted'),
+               'comments': comments.vetted().order_by('date_submitted'),
                'author_replies': author_replies, 'form': form}
     return render(request, 'theses/thesis_detail.html', context)
-- 
GitLab