Permalink
Browse files

Added facet-by-month

  • Loading branch information...
simonw committed Oct 3, 2017
1 parent 0b361c7 commit 1322ada238369fc803a5c670cf0528bb1dab18df
Showing with 35 additions and 1 deletion.
  1. +24 −1 blog/views.py
  2. +11 −0 templates/search.html
View
@@ -6,7 +6,7 @@
from django.contrib.postgres.search import SearchQuery, SearchRank
from django.views.decorators.cache import never_cache
from django.db import models
from django.db.models.functions import TruncYear
from django.db.models.functions import TruncYear, TruncMonth
from django.conf import settings
from django.core.paginator import (
Paginator,
@@ -403,6 +403,7 @@ def search_results(request, q):
excluded_tags = request.GET.getlist('exclude.tag')
selected_type = request.GET.get('type', '')
selected_year = request.GET.get('year', '')
selected_month = request.GET.get('month', '')
values = ('pk', 'type', 'created', 'rank')
@@ -413,6 +414,8 @@ def make_queryset(klass, type_name):
)
if selected_year:
qs = qs.filter(created__year=int(selected_year))
if selected_month:
qs = qs.filter(created__month=int(selected_month))
if q:
qs = qs.filter(search_document=query)
for tag in selected_tags:
@@ -430,6 +433,7 @@ def make_queryset(klass, type_name):
type_counts_raw = {}
tag_counts_raw = {}
year_counts_raw = {}
month_counts_raw = {}
for klass, type_name in (
(Entry, 'entry'),
@@ -454,6 +458,14 @@ def make_queryset(klass, type_name):
year_counts_raw[row['year']] = year_counts_raw.get(
row['year'], 0
) + row['n']
# Only do month counts if a year is selected
if selected_year:
for row in klass_qs.order_by().annotate(
month=TruncMonth('created')
).values('month').annotate(n=models.Count('pk')):
month_counts_raw[row['month']] = month_counts_raw.get(
row['month'], 0
) + row['n']
qs = qs.union(klass_qs.values(*values))
qs = qs.order_by('-rank')
@@ -480,6 +492,14 @@ def make_queryset(klass, type_name):
key=lambda t: t['year']
)
month_counts = sorted(
[
{'month': month, 'n': value}
for month, value in month_counts_raw.items()
],
key=lambda t: t['month']
)
paginator = Paginator(qs, 30)
page_number = request.GET.get('page') or '1'
try:
@@ -506,10 +526,13 @@ def make_queryset(klass, type_name):
'type_counts': type_counts,
'tag_counts': tag_counts,
'year_counts': year_counts,
'month_counts': month_counts,
'selected_tags': selected_tags,
'excluded_tags': excluded_tags,
'selected_type': selected_type,
'selected_year': selected_year,
'selected_month': selected_month,
'selected_month_name': MONTHS_3_REV_REV.get(selected_month and int(selected_month) or '', '').title(),
})
View
@@ -22,6 +22,9 @@ <h2>Search{% if q %} for “{{ q }}”{% endif %}</h2>
{% if selected_year %}
<a class="selected-tag" href="{% remove_qsarg "year" selected_year %}">Year: {{ selected_year }} <strong>&#x00D7;</strong></a>
{% endif %}
{% if selected_month %}
<a class="selected-tag" href="{% remove_qsarg "month" selected_month %}">Month: {{ selected_month_name }} <strong>&#x00D7;</strong></a>
{% endif %}
{% for selected_tag in selected_tags %}
<a class="selected-tag" href="{% remove_qsarg "tag" selected_tag %}">{{ selected_tag }} <strong>&#x00D7;</strong></a>
{% endfor %}
@@ -56,6 +59,14 @@ <h3>Years</h3>
{% endfor %}
</ul>
{% endif %}
{% if month_counts %}
<h3>Months</h3>
<ul>
{% for t in month_counts %}
<li><a href="{% add_qsarg "month" t.month|date:"n" %}">{{ t.month|date:"F" }}</a> {{ t.n }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% if tag_counts %}
<h3>Tags</h3>
<ul>

0 comments on commit 1322ada

Please sign in to comment.