SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit b5a6e93a authored by Jorran de Wit's avatar Jorran de Wit
Browse files

Commentaries list view to CBV

parent bd6d8c23
No related branches found
No related tags found
No related merge requests found
......@@ -214,13 +214,13 @@ class VetCommentaryForm(forms.Form):
class CommentarySearchForm(forms.Form):
"""Search for Commentary specified by user"""
pub_author = forms.CharField(max_length=100, required=False, label="Author(s)")
pub_title_keyword = forms.CharField(max_length=100, required=False, label="Title")
pub_abstract_keyword = forms.CharField(max_length=1000, required=False, label="Abstract")
author = forms.CharField(max_length=100, required=False, label="Author(s)")
title = forms.CharField(max_length=100, required=False, label="Title")
abstract = forms.CharField(max_length=1000, required=False, label="Abstract")
def search_results(self):
"""Return all Commentary objects according to search"""
return Commentary.objects.vetted(
pub_title__icontains=self.cleaned_data['pub_title_keyword'],
pub_abstract__icontains=self.cleaned_data['pub_abstract_keyword'],
author_list__icontains=self.cleaned_data['pub_author']).order_by('-pub_date')
pub_title__icontains=self.cleaned_data['title'],
pub_abstract__icontains=self.cleaned_data['abstract'],
author_list__icontains=self.cleaned_data['author']).order_by('-pub_date')
......@@ -5,7 +5,7 @@ from . import views
urlpatterns = [
# Commentaries
url(r'^$', views.commentaries, name='commentaries'),
url(r'^$', views.CommentaryListView.as_view(), 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'),
......
......@@ -13,6 +13,7 @@ from django.core.urlresolvers import reverse, reverse_lazy
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.views.generic.edit import CreateView, FormView
from django.views.generic.list import ListView
from django.utils.decorators import method_decorator
from .models import Commentary
......@@ -267,21 +268,37 @@ def vet_commentary_request_ack(request, commentary_id):
return render(request, 'scipost/acknowledgement.html', context)
def commentaries(request):
"""List and search all commentaries"""
form = CommentarySearchForm(request.POST or None)
if form.is_valid() and form.has_changed():
commentary_search_list = form.search_results()
else:
commentary_search_list = []
class CommentaryListView(ListView):
model = Commentary
form = CommentarySearchForm
paginate_by = 10
comment_recent_list = Comment.objects.filter(status='1').order_by('-date_submitted')[: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}
return render(request, 'commentaries/commentaries.html', context)
def get_queryset(self):
'''Perform search form here already to get the right pagination numbers.'''
self.form = self.form(self.request.GET)
if self.form.is_valid():
return self.form.search_results()
return self.model.objects.vetted().order_by('-latest_activity')
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Get newest comments
context['comment_list'] = Comment.objects.vetted().order_by('-date_submitted')[:10]
# Form into the context!
context['form'] = self.form
# To customize display in the template
if 'discipline' in self.kwargs:
context['discipline'] = self.kwargs['discipline']
context['nrweeksback'] = self.kwargs['nrweeksback']
context['browse'] = True
elif not any(argument in ['title', 'author', 'abstract'] for argument in self.request.GET):
context['recent'] = True
return context
def browse(request, discipline, nrweeksback):
......
from django.db import models
class CommentManager(models.Manager):
def vetted(self):
return self.filter(status__gte=1)
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