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): ...@@ -214,13 +214,13 @@ class VetCommentaryForm(forms.Form):
class CommentarySearchForm(forms.Form): class CommentarySearchForm(forms.Form):
"""Search for Commentary specified by user""" """Search for Commentary specified by user"""
pub_author = forms.CharField(max_length=100, required=False, label="Author(s)") author = forms.CharField(max_length=100, required=False, label="Author(s)")
pub_title_keyword = forms.CharField(max_length=100, required=False, label="Title") title = forms.CharField(max_length=100, required=False, label="Title")
pub_abstract_keyword = forms.CharField(max_length=1000, required=False, label="Abstract") abstract = forms.CharField(max_length=1000, required=False, label="Abstract")
def search_results(self): def search_results(self):
"""Return all Commentary objects according to search""" """Return all Commentary objects according to search"""
return Commentary.objects.vetted( return Commentary.objects.vetted(
pub_title__icontains=self.cleaned_data['pub_title_keyword'], pub_title__icontains=self.cleaned_data['title'],
pub_abstract__icontains=self.cleaned_data['pub_abstract_keyword'], pub_abstract__icontains=self.cleaned_data['abstract'],
author_list__icontains=self.cleaned_data['pub_author']).order_by('-pub_date') author_list__icontains=self.cleaned_data['author']).order_by('-pub_date')
...@@ -5,7 +5,7 @@ from . import views ...@@ -5,7 +5,7 @@ from . import views
urlpatterns = [ urlpatterns = [
# Commentaries # 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'^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'), 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 ...@@ -13,6 +13,7 @@ from django.core.urlresolvers import reverse, reverse_lazy
from django.shortcuts import redirect from django.shortcuts import redirect
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.views.generic.edit import CreateView, FormView from django.views.generic.edit import CreateView, FormView
from django.views.generic.list import ListView
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from .models import Commentary from .models import Commentary
...@@ -267,21 +268,37 @@ def vet_commentary_request_ack(request, commentary_id): ...@@ -267,21 +268,37 @@ def vet_commentary_request_ack(request, commentary_id):
return render(request, 'scipost/acknowledgement.html', context) return render(request, 'scipost/acknowledgement.html', context)
def commentaries(request): class CommentaryListView(ListView):
"""List and search all commentaries""" model = Commentary
form = CommentarySearchForm(request.POST or None) form = CommentarySearchForm
if form.is_valid() and form.has_changed(): paginate_by = 10
commentary_search_list = form.search_results()
else:
commentary_search_list = []
comment_recent_list = Comment.objects.filter(status='1').order_by('-date_submitted')[:10] def get_queryset(self):
commentary_recent_list = Commentary.objects.vetted().order_by('-latest_activity')[:10] '''Perform search form here already to get the right pagination numbers.'''
context = { self.form = self.form(self.request.GET)
'form': form, 'commentary_search_list': commentary_search_list, if self.form.is_valid():
'comment_recent_list': comment_recent_list, return self.form.search_results()
'commentary_recent_list': commentary_recent_list} return self.model.objects.vetted().order_by('-latest_activity')
return render(request, 'commentaries/commentaries.html', context)
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): def browse(request, discipline, nrweeksback):
......
from django.db import models from django.db import models
class CommentManager(models.Manager): class CommentManager(models.Manager):
def vetted(self): def vetted(self):
return self.filter(status__gte=1) 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