From a0b6ce3917ba6bfde9f01edfcde9929e1b7f5c79 Mon Sep 17 00:00:00 2001 From: Jorran Wit <jorrandewit@outlook.com> Date: Wed, 14 Dec 2016 09:33:38 +0100 Subject: [PATCH] Use ModelManagers on Commentary Implemented basic modelmanager on Commentary model. Created `vetted` and `awaiting_vetting` methods and ipmlemented to few view methods. --- commentaries/models.py | 72 +++++++++++++++++++++++++----------------- commentaries/views.py | 45 +++++++++++--------------- 2 files changed, 62 insertions(+), 55 deletions(-) diff --git a/commentaries/models.py b/commentaries/models.py index b7b570150..9bc4ad6fd 100644 --- a/commentaries/models.py +++ b/commentaries/models.py @@ -1,6 +1,5 @@ from django.utils import timezone from django.db import models -from django.contrib.auth.models import User from django.contrib.postgres.fields import JSONField from django.template import Template, Context @@ -8,62 +7,80 @@ from journals.models import SCIPOST_JOURNALS_DOMAINS, SCIPOST_JOURNALS_SPECIALIZ from scipost.models import TimeStampedModel, Contributor from scipost.models import SCIPOST_DISCIPLINES, SCIPOST_SUBJECT_AREAS - - COMMENTARY_TYPES = ( ('published', 'published paper'), ('preprint', 'arXiv preprint'), ) + +class CommentaryManager(models.Manager): + def vetted(self, **kwargs): + return self.filter(vetted=True, **kwargs) + + def awaiting_vetting(self, **kwargs): + return self.filter(vetted=False, **kwargs) + + class Commentary(TimeStampedModel): """ A Commentary contains all the contents of a SciPost Commentary page for a given publication. """ - requested_by = models.ForeignKey (Contributor, blank=True, null=True, - on_delete=models.CASCADE, related_name='requested_by') + requested_by = models.ForeignKey( + Contributor, blank=True, null=True, + on_delete=models.CASCADE, related_name='requested_by') vetted = models.BooleanField(default=False) - vetted_by = models.ForeignKey (Contributor, blank=True, null=True, on_delete=models.CASCADE) - type = models.CharField(max_length=9, choices=COMMENTARY_TYPES) # published paper or arxiv preprint + vetted_by = models.ForeignKey(Contributor, blank=True, null=True, on_delete=models.CASCADE) + type = models.CharField(max_length=9, choices=COMMENTARY_TYPES) discipline = models.CharField(max_length=20, choices=SCIPOST_DISCIPLINES, default='physics') domain = models.CharField(max_length=3, choices=SCIPOST_JOURNALS_DOMAINS) -# specialization = models.CharField(max_length=1, choices=SCIPOST_JOURNALS_SPECIALIZATIONS) - subject_area = models.CharField(max_length=10, choices=SCIPOST_SUBJECT_AREAS, default='Phys:QP') + subject_area = models.CharField( + max_length=10, choices=SCIPOST_SUBJECT_AREAS, + default='Phys:QP') open_for_commenting = models.BooleanField(default=True) pub_title = models.CharField(max_length=300, verbose_name='title') - arxiv_identifier = models.CharField(max_length=100, - verbose_name="arXiv identifier (including version nr)", - blank=True, null=True) + arxiv_identifier = models.CharField( + max_length=100, verbose_name="arXiv identifier (including version nr)", + blank=True, null=True) arxiv_link = models.URLField(verbose_name='arXiv link (including version nr)', blank=True) - pub_DOI = models.CharField(max_length=200, verbose_name='DOI of the original publication', - blank=True, null=True) - pub_DOI_link = models.URLField(verbose_name='DOI link to the original publication', blank=True) + pub_DOI = models.CharField( + max_length=200, verbose_name='DOI of the original publication', + blank=True, null=True) + pub_DOI_link = models.URLField( + verbose_name='DOI link to the original publication', + blank=True) metadata = JSONField(default={}, blank=True, null=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') + 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') journal = models.CharField(max_length=300, blank=True, null=True) volume = models.CharField(max_length=50, blank=True, null=True) pages = models.CharField(max_length=50, blank=True, null=True) - pub_date = models.DateField(verbose_name='date of original publication', blank=True, null=True) + pub_date = models.DateField( + verbose_name='date of original publication', + blank=True, null=True) pub_abstract = models.TextField(verbose_name='abstract') + objects = CommentaryManager() + class Meta: verbose_name_plural = 'Commentaries' - def __str__(self): return self.pub_title - def header_as_table(self): # for display in Commentary page itself header = ('<table>' @@ -92,8 +109,8 @@ class Commentary(TimeStampedModel): header += '</table>' template = Template(header) context = Context({ - 'pub_title': self.pub_title, 'author_list': self.author_list, - }) + 'pub_title': self.pub_title, 'author_list': self.author_list, + }) if self.type == 'published': context['journal'] = self.journal context['volume'] = self.volume @@ -104,7 +121,6 @@ class Commentary(TimeStampedModel): context['arxiv_link'] = self.arxiv_link return template.render(context) - def header_as_li(self): # for display in search lists context = Context({'scipost_url': self.scipost_url(), 'pub_title': self.pub_title, @@ -135,7 +151,6 @@ class Commentary(TimeStampedModel): return template.render(context) - def simple_header_as_li(self): # for display in Lists context = Context({'scipost_url': self.scipost_url(), 'pub_title': self.pub_title, @@ -157,7 +172,6 @@ class Commentary(TimeStampedModel): template = Template(header) return template.render(context) - def parse_links_into_urls(self): """ Takes the arXiv nr or DOI and turns it into the urls """ if self.pub_DOI: diff --git a/commentaries/views.py b/commentaries/views.py index 88f4d7c69..0797b8fd7 100644 --- a/commentaries/views.py +++ b/commentaries/views.py @@ -61,7 +61,7 @@ def request_commentary(request): 'errormessage': errormessage, 'existing_commentary': existing_commentary} return render(request, 'commentaries/request_commentary.html', context) - + # Otherwise we can create the Commentary contributor = Contributor.objects.get(user=request.user) commentary = Commentary ( @@ -84,7 +84,7 @@ def request_commentary(request): ) commentary.parse_links_into_urls() commentary.save() - + context = {'ack_header': 'Thank you for your request for a Commentary Page', 'ack_message': 'Your request will soon be handled by an Editor. ', 'followup_message': 'Return to your ', @@ -120,7 +120,7 @@ def prefill_using_DOI(request): 'errormessage': errormessage, 'existing_commentary': existing_commentary} return render(request, 'commentaries/request_commentary.html', context) - + # Otherwise we query Crossref for the information: try: queryurl = 'http://api.crossref.org/works/%s' % doiform.cleaned_data['doi'] @@ -133,12 +133,12 @@ def prefill_using_DOI(request): for author in doiqueryJSON['message']['author'][1:]: authorlist += ', ' + author['given'] + ' ' + author['family'] journal = doiqueryJSON['message']['container-title'][0] - + try: volume = doiqueryJSON['message']['volume'] except KeyError: volume = '' - + pages = '' try: pages = doiqueryJSON['message']['article-number'] # for Phys Rev @@ -148,7 +148,7 @@ def prefill_using_DOI(request): pages = doiqueryJSON['message']['page'] except KeyError: pass - + pub_date = '' try: pub_date = (str(doiqueryJSON['message']['issued']['date-parts'][0][0]) + '-' + @@ -209,7 +209,7 @@ def prefill_using_identifier(request): queryurl = ('http://export.arxiv.org/api/query?id_list=%s' % identifierform.cleaned_data['identifier']) arxivquery = feedparser.parse(queryurl) - + # If paper has been published, should comment on published version try: arxiv_journal_ref = arxivquery['entries'][0]['arxiv_journal_ref'] @@ -223,7 +223,7 @@ def prefill_using_identifier(request): + '. Please comment on the published version.') except (IndexError, KeyError): pass - + if errormessage: form = RequestCommentaryForm() doiform = DOIToQueryForm() @@ -231,7 +231,7 @@ def prefill_using_identifier(request): 'errormessage': errormessage, 'existing_commentary': existing_commentary} return render(request, 'commentaries/request_commentary.html', context) - + # otherwise prefill the form: metadata = arxivquery pub_title = arxivquery['entries'][0]['title'] @@ -265,7 +265,7 @@ def prefill_using_identifier(request): @permission_required('scipost.can_vet_commentary_requests', raise_exception=True) def vet_commentary_requests(request): contributor = Contributor.objects.get(user=request.user) - commentary_to_vet = Commentary.objects.filter(vetted=False).first() # only handle one at a time + commentary_to_vet = Commentary.objects.awaiting_vetting().first() # only handle one at a time form = VetCommentaryForm() context = {'contributor': contributor, 'commentary_to_vet': commentary_to_vet, 'form': form } return render(request, 'commentaries/vet_commentary_requests.html', context) @@ -353,12 +353,10 @@ def commentaries(request): if request.method == 'POST': form = CommentarySearchForm(request.POST) if form.is_valid() and form.has_changed(): - commentary_search_list = Commentary.objects.filter( + commentary_search_list = Commentary.objects.vetted( pub_title__icontains=form.cleaned_data['pub_title_keyword'], author_list__icontains=form.cleaned_data['pub_author'], - pub_abstract__icontains=form.cleaned_data['pub_abstract_keyword'], - vetted=True, - ) + pub_abstract__icontains=form.cleaned_data['pub_abstract_keyword']) commentary_search_list.order_by('-pub_date') else: commentary_search_list = [] @@ -367,11 +365,9 @@ def commentaries(request): form = CommentarySearchForm() commentary_search_list = [] - comment_recent_list = (Comment.objects.filter(status='1') - .order_by('-date_submitted')[:10]) + comment_recent_list = Comment.objects.filter(status='1').order_by('-date_submitted')[:10] - commentary_recent_list = (Commentary.objects.filter(vetted=True) - .order_by('-latest_activity')[:10]) + commentary_recent_list = Commentary.objects.vetted().order_by('-latest_activity')[:10] context = {'form': form, 'commentary_search_list': commentary_search_list, 'comment_recent_list': comment_recent_list, 'commentary_recent_list': commentary_recent_list } @@ -382,12 +378,10 @@ def browse(request, discipline, nrweeksback): if request.method == 'POST': form = CommentarySearchForm(request.POST) if form.is_valid() and form.has_changed(): - commentary_search_list = Commentary.objects.filter( + commentary_search_list = Commentary.objects.vetted( pub_title__icontains=form.cleaned_data['pub_title_keyword'], author_list__icontains=form.cleaned_data['pub_author'], - pub_abstract__icontains=form.cleaned_data['pub_abstract_keyword'], - vetted=True, - ) + pub_abstract__icontains=form.cleaned_data['pub_abstract_keyword']) commentary_search_list.order_by('-pub_date') else: commentary_search_list = [] @@ -395,10 +389,9 @@ def browse(request, discipline, nrweeksback): return HttpResponseRedirect(request, 'commentaries/commentaries.html', context) else: form = CommentarySearchForm() - commentary_browse_list = Commentary.objects.filter( - vetted=True, discipline=discipline, - latest_activity__gte=timezone.now() + datetime.timedelta(weeks=-int(nrweeksback)) - ) + commentary_browse_list = Commentary.objects.vetted( + discipline=discipline, + latest_activity__gte=timezone.now() + datetime.timedelta(weeks=-int(nrweeksback))) context = {'form': form, 'discipline': discipline, 'nrweeksback': nrweeksback, 'commentary_browse_list': commentary_browse_list } return render(request, 'commentaries/commentaries.html', context) -- GitLab