SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 70b8926d authored by Geert Kapteijns's avatar Geert Kapteijns
Browse files

Extract comment query logic to manager

parent 25151c15
No related branches found
No related tags found
No related merge requests found
from django.db import models
class CommentManager(models.Manager):
def vetted(self):
return self.filter(status__gte=1)
...@@ -10,6 +10,8 @@ from scipost.models import TimeStampedModel, Contributor ...@@ -10,6 +10,8 @@ from scipost.models import TimeStampedModel, Contributor
from submissions.models import Submission, Report from submissions.models import Submission, Report
from theses.models import ThesisLink from theses.models import ThesisLink
from .managers import CommentManager
COMMENT_CATEGORIES = ( COMMENT_CATEGORIES = (
('ERR', 'erratum'), ('ERR', 'erratum'),
('REM', 'remark'), ('REM', 'remark'),
...@@ -37,19 +39,24 @@ class Comment(TimeStampedModel): ...@@ -37,19 +39,24 @@ class Comment(TimeStampedModel):
on a particular publication or in reply to an earlier Comment. """ on a particular publication or in reply to an earlier Comment. """
status = models.SmallIntegerField(default=0) status = models.SmallIntegerField(default=0)
vetted_by = models.ForeignKey(Contributor, blank=True, null=True, vetted_by = models.ForeignKey(
on_delete=models.CASCADE, Contributor,
related_name='comment_vetted_by') blank=True,
file_attachment = models.FileField(upload_to='uploads/comments/%Y/%m/%d/', blank=True, null=True,
validators=[validate_file_extension, on_delete=models.CASCADE,
validate_max_file_size]) 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. # a Comment is either for a Commentary or Submission or a ThesisLink.
commentary = models.ForeignKey(Commentary, blank=True, null=True, on_delete=models.CASCADE) commentary = models.ForeignKey(Commentary, blank=True, null=True, on_delete=models.CASCADE)
submission = models.ForeignKey(Submission, 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) thesislink = models.ForeignKey(ThesisLink, blank=True, null=True, on_delete=models.CASCADE)
is_author_reply = models.BooleanField(default=False) is_author_reply = models.BooleanField(default=False)
in_reply_to_comment = models.ForeignKey('self', blank=True, null=True, in_reply_to_comment = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE)
on_delete=models.CASCADE)
in_reply_to_report = models.ForeignKey(Report, 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) author = models.ForeignKey(Contributor, on_delete=models.CASCADE)
anonymous = models.BooleanField(default=False, verbose_name='Publish anonymously') anonymous = models.BooleanField(default=False, verbose_name='Publish anonymously')
...@@ -64,19 +71,20 @@ class Comment(TimeStampedModel): ...@@ -64,19 +71,20 @@ class Comment(TimeStampedModel):
is_lit = models.BooleanField(default=False, verbose_name='pointer to related literature') is_lit = models.BooleanField(default=False, verbose_name='pointer to related literature')
is_sug = models.BooleanField(default=False, verbose_name='suggestion for further work') is_sug = models.BooleanField(default=False, verbose_name='suggestion for further work')
comment_text = models.TextField() comment_text = models.TextField()
remarks_for_editors = models.TextField(default='', blank=True, remarks_for_editors = models.TextField(default='', blank=True, verbose_name='optional remarks for the Editors only')
verbose_name='optional remarks for the Editors only')
date_submitted = models.DateTimeField('date submitted') date_submitted = models.DateTimeField('date submitted')
# Opinions # Opinions
nr_A = models.PositiveIntegerField(default=0) nr_A = models.PositiveIntegerField(default=0)
in_agreement = models.ManyToManyField(Contributor, in_agreement = models.ManyToManyField(Contributor, related_name='in_agreement', blank=True)
related_name='in_agreement', blank=True)
nr_N = models.PositiveIntegerField(default=0) nr_N = models.PositiveIntegerField(default=0)
in_notsure = models.ManyToManyField(Contributor, in_notsure = models.ManyToManyField(Contributor, related_name='in_notsure', blank=True)
related_name='in_notsure', blank=True)
nr_D = models.PositiveIntegerField(default=0) nr_D = models.PositiveIntegerField(default=0)
in_disagreement = models.ManyToManyField(Contributor, in_disagreement = models.ManyToManyField(
related_name='in_disagreement', blank=True) Contributor,
related_name='in_disagreement',
blank=True
)
objects = CommentManager()
def __str__(self): def __str__(self):
return ('by ' + self.author.user.first_name + ' ' + self.author.user.last_name + return ('by ' + self.author.user.first_name + ' ' + self.author.user.last_name +
......
...@@ -30,23 +30,6 @@ class TestThesisDetail(TestCase): ...@@ -30,23 +30,6 @@ class TestThesisDetail(TestCase):
response = client.post(target) response = client.post(target)
self.assertEqual(response.status_code, 200) 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): class TestRequestThesisLink(TestCase):
......
...@@ -131,14 +131,12 @@ def browse(request, discipline, nrweeksback): ...@@ -131,14 +131,12 @@ def browse(request, discipline, nrweeksback):
def thesis_detail(request, thesislink_id): def thesis_detail(request, thesislink_id):
thesislink = get_object_or_404(ThesisLink, pk=thesislink_id) thesislink = get_object_or_404(ThesisLink, pk=thesislink_id)
comments = thesislink.comment_set.all()
form = CommentForm() form = CommentForm()
try:
author_replies = Comment.objects.filter(thesislink=thesislink, is_author_reply=True) comments = thesislink.comment_set
except Comment.DoesNotExist: author_replies = comments.filter(is_author_reply=True)
author_replies = ()
# TODO: make manager for horribly obfuscating 'status__gte=1'
context = {'thesislink': thesislink, 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} 'author_replies': author_replies, 'form': form}
return render(request, 'theses/thesis_detail.html', context) return render(request, 'theses/thesis_detail.html', context)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment