Skip to content

Commit

Permalink
Better logic for displaying filters
Browse files Browse the repository at this point in the history
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
Simon Willison committed Oct 3, 2017
1 parent 1322ada commit 79b1b13
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
20 changes: 16 additions & 4 deletions blog/views.py
Expand Up @@ -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,
Expand All @@ -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,
})


Expand Down
18 changes: 9 additions & 9 deletions templates/search.html
Expand Up @@ -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 %}
Expand Down

0 comments on commit 79b1b13

Please sign in to comment.