filter by year

This commit is contained in:
j 2018-08-21 20:13:38 +02:00
parent 480cb4afe5
commit 99ea0eab0a
3 changed files with 53 additions and 9 deletions

View File

@ -140,7 +140,9 @@ ul.clearing-thumbs li {
} }
.pagination a:hover, .pagination a:hover,
.pagination button:hover { .pagination button:hover,
.pagination span.current
{
background: #1779ba !important; } background: #1779ba !important; }
.admin-menu { .admin-menu {

View File

@ -45,6 +45,21 @@
{% endfor %} {% endfor %}
<br> <br>
<br> <br>
{% if years %}
<div class="row">
<div class="medium-8 medium-offset-2 end columns">
<ul class="pagination">
{% for y in years %}
{% if year == y %}
<li><span class="current">{{y}}</a></li>
{% else %}
<li><a href="?{{ base_query }}year={{ y }}">{{y}}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
{% endif %}
{% if content.has_other_pages %} {% if content.has_other_pages %}
<div class="row"> <div class="row">
<div class="medium-8 medium-offset-2 end columns"> <div class="medium-8 medium-offset-2 end columns">
@ -69,6 +84,6 @@
</ul> </ul>
</div> </div>
</div> </div>
{% endif %} {% endif %}
</div> </div>
{% endblock %} {% endblock %}

View File

@ -8,11 +8,12 @@ from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q from django.db.models import Q, Count
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404, render, redirect from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse from django.urls import reverse
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.db.models.functions.datetime import TruncYear
from photologue.views import GalleryListView from photologue.views import GalleryListView
from photologue.models import Photo, Gallery from photologue.models import Photo, Gallery
@ -69,13 +70,34 @@ def section_index(request, section, max_length=10):
'more_content': more_content, 'more_content': more_content,
}) })
def content_years(content):
years = [
year['year'].year
for year in content.annotate(year=TruncYear('datestart')).values('year').annotate(count=Count('shortname')).order_by('-year')
]
return years
def filter_by_years(content, year):
years = content_years(content)
if year.isdigit():
year = int(year)
else:
year = years[0]
content = content.filter(datestart__gte='%s-01-01' % year, datestart__lt='%s-01-01' % (year+1))
return {
'content': content,
'year': year,
'years': years
}
def section_list(request, section): def section_list(request, section):
content = section_content(section) content = section_content(section)
q = request.GET.get('q') q = request.GET.get('q')
content = limit_content(content, q) content = limit_content(content, q)
year = request.GET.get('year', '')
context = filter_by_years(content, year)
'''
page = request.GET.get('page', 1) page = request.GET.get('page', 1)
paginator = Paginator(content, ITEMS_PER_PAGE) paginator = Paginator(content, ITEMS_PER_PAGE)
try: try:
@ -84,17 +106,17 @@ def section_list(request, section):
content = paginator.page(1) content = paginator.page(1)
except EmptyPage: except EmptyPage:
content = paginator.page(paginator.num_pages) content = paginator.page(paginator.num_pages)
'''
base_query = '' base_query = ''
if q: if q:
base_query = 'q=%s&' % q base_query = 'q=%s&' % q
context.update({
return render(request, 'results.html', {
'base_query': base_query, 'base_query': base_query,
'content': content,
'query': q, 'query': q,
'section': section, 'section': section,
}) })
return render(request, 'results.html', context)
def event(request): def event(request):
now = datetime.now() now = datetime.now()
@ -202,6 +224,9 @@ def search(request):
content = content.exclude(type__name__in=['page', 'homepage']) content = content.exclude(type__name__in=['page', 'homepage'])
q = request.GET.get('q') q = request.GET.get('q')
content = limit_content(content, q) content = limit_content(content, q)
year = request.GET.get('year', '')
context = filter_by_years(content, year)
'''
page = request.GET.get('page', 1) page = request.GET.get('page', 1)
paginator = Paginator(content, ITEMS_PER_PAGE) paginator = Paginator(content, ITEMS_PER_PAGE)
try: try:
@ -210,17 +235,19 @@ def search(request):
content = paginator.page(1) content = paginator.page(1)
except EmptyPage: except EmptyPage:
content = paginator.page(paginator.num_pages) content = paginator.page(paginator.num_pages)
'''
base_query = '' base_query = ''
if q: if q:
base_query = 'q=%s&' % q base_query = 'q=%s&' % q
return render(request, 'results.html', { context.update({
'base_query': base_query, 'base_query': base_query,
'content': content,
'query': q 'query': q
}) })
return render(request, 'results.html', context)
class GalleryListViews(ListView): class GalleryListViews(ListView):
queryset = Gallery.objects.on_site().is_public() queryset = Gallery.objects.on_site().is_public()