From 99ea0eab0aa22ff3863f460e25bea04ec99b07cd Mon Sep 17 00:00:00 2001 From: j Date: Tue, 21 Aug 2018 20:13:38 +0200 Subject: [PATCH] filter by year --- camp/static/css/main.css | 4 +++- content/templates/results.html | 17 +++++++++++++- content/views.py | 41 ++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/camp/static/css/main.css b/camp/static/css/main.css index 4fa612b..68eb8f8 100644 --- a/camp/static/css/main.css +++ b/camp/static/css/main.css @@ -140,7 +140,9 @@ ul.clearing-thumbs li { } .pagination a:hover, - .pagination button:hover { +.pagination button:hover, +.pagination span.current +{ background: #1779ba !important; } .admin-menu { diff --git a/content/templates/results.html b/content/templates/results.html index 0bb56bb..545f478 100644 --- a/content/templates/results.html +++ b/content/templates/results.html @@ -45,6 +45,21 @@ {% endfor %}

+ {% if years %} +
+
+
    + {% for y in years %} + {% if year == y %} +
  • {{y}}
  • + {% else %} +
  • {{y}}
  • + {% endif %} + {% endfor %} +
+
+
+ {% endif %} {% if content.has_other_pages %}
@@ -69,6 +84,6 @@
-{% endif %} + {% endif %} {% endblock %} diff --git a/content/views.py b/content/views.py index b20d59e..33744c8 100644 --- a/content/views.py +++ b/content/views.py @@ -8,11 +8,12 @@ from django.conf import settings from django.core.exceptions import ObjectDoesNotExist from django.core.mail import EmailMessage 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.shortcuts import get_object_or_404, render, redirect from django.urls import reverse from django.views.generic.list import ListView +from django.db.models.functions.datetime import TruncYear from photologue.views import GalleryListView from photologue.models import Photo, Gallery @@ -69,13 +70,34 @@ def section_index(request, section, max_length=10): '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): content = section_content(section) - q = request.GET.get('q') content = limit_content(content, q) + year = request.GET.get('year', '') + context = filter_by_years(content, year) + ''' page = request.GET.get('page', 1) paginator = Paginator(content, ITEMS_PER_PAGE) try: @@ -84,17 +106,17 @@ def section_list(request, section): content = paginator.page(1) except EmptyPage: content = paginator.page(paginator.num_pages) + ''' base_query = '' if q: base_query = 'q=%s&' % q - - return render(request, 'results.html', { + context.update({ 'base_query': base_query, - 'content': content, 'query': q, 'section': section, }) + return render(request, 'results.html', context) def event(request): now = datetime.now() @@ -202,6 +224,9 @@ def search(request): content = content.exclude(type__name__in=['page', 'homepage']) q = request.GET.get('q') content = limit_content(content, q) + year = request.GET.get('year', '') + context = filter_by_years(content, year) + ''' page = request.GET.get('page', 1) paginator = Paginator(content, ITEMS_PER_PAGE) try: @@ -210,17 +235,19 @@ def search(request): content = paginator.page(1) except EmptyPage: content = paginator.page(paginator.num_pages) + ''' base_query = '' if q: base_query = 'q=%s&' % q - return render(request, 'results.html', { + context.update({ 'base_query': base_query, - 'content': content, 'query': q }) + return render(request, 'results.html', context) + class GalleryListViews(ListView): queryset = Gallery.objects.on_site().is_public()