Permalink
Browse files

Better logic for displaying filters

Also means this works better: https://simonwillison.net/search/?q=python&month=3

Which oddly shows every python item published in ANY month-of-March
  • Loading branch information...
simonw committed Oct 3, 2017
1 parent 1322ada commit 79b1b13d6cad2d23f2330fca7988f03b28280436
Showing with 25 additions and 13 deletions.
  1. +16 −4 blog/views.py
  2. +9 −9 templates/search.html
View
@@ -517,6 +517,21 @@ def make_queryset(klass, type_name):
'obj': obj,
})
end = time.time()
selected = {
'tags': selected_tags,
'year': selected_year,
'month': selected_month,
'type': selected_type,
'month_name': MONTHS_3_REV_REV.get(selected_month and int(selected_month) or '', '').title(),
}
# Remove empty keys
selected = {
key: value
for key, value in selected.items()
if value
}
return render(request, 'search.html', {
'q': q,
'results': results,
@@ -529,10 +544,7 @@ def make_queryset(klass, type_name):
'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(),
'selected': selected,
})
View
@@ -13,20 +13,20 @@ <h2>Search{% if q %} for “{{ q }}”{% endif %}</h2>
<input type="submit" class="search-submit" value="Search">
</form>
{% if selected_tags or selected_type or selected_year %}
{% if selected %}
<p class="search-selections">
Filters:
{% if selected_type %}
<a class="selected-tag" href="{% remove_qsarg "type" selected_type %}">Type: {{ selected_type }} <strong>&#x00D7;</strong></a>
{% if selected.type %}
<a class="selected-tag" href="{% remove_qsarg "type" selected.type %}">Type: {{ selected.type }} <strong>&#x00D7;</strong></a>
{% endif %}
{% if selected_year %}
<a class="selected-tag" href="{% remove_qsarg "year" selected_year %}">Year: {{ selected_year }} <strong>&#x00D7;</strong></a>
{% 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>
{% 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>
{% for tag in selected.tags %}
<a class="selected-tag" href="{% remove_qsarg "tag" tag %}">{{ tag }} <strong>&#x00D7;</strong></a>
{% endfor %}
</p>
{% endif %}

0 comments on commit 79b1b13

Please sign in to comment.