Added filters

This commit is contained in:
Zak Timson 2017-09-05 22:13:47 -04:00
parent ee6273670d
commit 2dec223f8b
2 changed files with 41 additions and 21 deletions

View File

@ -15,13 +15,13 @@
<div class="container"> <div class="container">
<div class="row py-3"> <div class="row py-3">
{% for header in allHeaders %} {% for header in allHeaders %}
<div class="col-4"> <div class="col-xs-12 col-sm-6 col-md-4">
<input id="input-{{ header.id }}" class="curs-pointer" type="checkbox"> <input id="input-{{ header.id }}" class="curs-pointer filters" name="filter" type="checkbox" value="{{ header }}">
<label for="input-{{ header.id }}" class="curs-pointer">{{ header }}</label> <label for="input-{{ header.id }}" class="curs-pointer">{{ header }}</label>
</div> </div>
{% endfor %} {% endfor %}
<div class="col-12"> <div class="col-12">
<button class="btn btn-secondary d-block mx-auto"><i class="fa fa-search"></i> Search</button> <button id="search" class="btn btn-secondary d-block mx-auto"><i class="fa fa-search"></i> Search</button>
</div> </div>
</div> </div>
</div> </div>
@ -42,16 +42,24 @@
{% endfor %} {% endfor %}
</ol> </ol>
{% endif %} {% endif %}
{% if filter %}
<div class="col-12 p-3" style="background-color: #e9ecef;">
Filters:
{% for item in filter %}
<span class="badge badge-primary">{{ item }}</span>
{% endfor %}
<span class="badge badge-danger"><a class="text-white" href="{% url 'browser' %}">&times; Clear Filters</a></span>
</div>
{% endif %}
<div class="row p-3"> <div class="row p-3">
<div class="col-lg-12 ml-auto text-dark-primary"> <div class="col-lg-12 ml-auto text-dark-primary">
{% if headersCount > 1 %} {% if headersCount > 1 and decisionsCount > 1 and not filter %}
<h4>Headings</h4> <h4>Headings</h4>
<hr> <hr>
{% for header in headers %} {% for header in headers %}
<div> <div>
<a href="{% url 'browser' %}?path={% if url %}{{ url }}/{% endif %}{{ header }}"> <a href="{% url 'browser' %}?path={% if url %}{{ url }}/{% endif %}{{ header }}">
<i class="fa fa-folder-open-o fa-2x"></i> <h4 class="d-inline-block"><i class="fa fa-folder-open-o"></i> {{ header }}</h4>
<h4 class="d-inline-block"> {{ header }}</h4>
</a> </a>
</div> </div>
{% endfor %} {% endfor %}
@ -62,8 +70,7 @@
{% for decision in decisions %} {% for decision in decisions %}
<div> <div>
<a href="{% url 'case' decision.id %}"> <a href="{% url 'case' decision.id %}">
<i class="fa fa-file-text-o fa-2x"></i> <h4 class="d-inline-block"><i class="fa fa-file-text-o"></i> {{ decision.synopsis }}</h4>
<h4 class="d-inline-block"> {{ decision.synopsis }}</h4>
</a> </a>
</div> </div>
{% endfor %} {% endfor %}
@ -76,4 +83,16 @@
{% if headersCount %}{{ headersCount }} Headers, {% endif %}{{ decisionsCount }} Decision{% if decisionsCount > 1 %}s{% endif %} {% if headersCount %}{{ headersCount }} Headers, {% endif %}{{ decisionsCount }} Decision{% if decisionsCount > 1 %}s{% endif %}
</div> </div>
</div> </div>
<script>
$('#search').click(function() {
var selected = $('input.filters').filter(function(i, e) {
return $(e).is(':checked')
}).map(function(i, e) {
return $(e).val()
});
window.location.href = '{% url 'browser' %}?filter=' + selected.toArray().join('/');
})
</script>
{% endblock %} {% endblock %}

View File

@ -4,31 +4,32 @@ from .models import Decision, Subtitle
def browser(request): def browser(request):
headers = {}
path = request.GET.get('path')
filter = request.GET.get('filter') filter = request.GET.get('filter')
decisions = Decision.objects.all() decisions = Decision.objects.all()
if filter: if filter:
headers = {}
filter = filter.split('/') filter = filter.split('/')
ids = Subtitle.objects.filter(name__in=filter) print(filter)
ids = Subtitle.objects.filter(name__in=filter).values_list('id')
for id in ids:
decisions = decisions.filter(headers__in=id)
elif path:
path = path.split('/')
ids = Subtitle.objects.filter(name__in=path).values_list('id')
for id in ids: for id in ids:
decisions = decisions.filter(headers__in=id) decisions = decisions.filter(headers__in=id)
else:
path = request.GET.get('path')
if path:
path = path.split('/')
ids = Subtitle.objects.filter(name__in=path).values_list('id')
for id in ids:
decisions = decisions.filter(headers__in=id)
headers = set() headers = set()
for decision in decisions: for decision in decisions:
headers = headers.union(decision.headers.all().values_list('name', flat=True)) headers = headers.union(decision.headers.all().values_list('name', flat=True))
if path: headers = headers.difference(path) if path: headers = headers.difference(path)
return render(request, 'browser.html', { return render(request, 'browser.html', {
'allHeaders': Subtitle.objects.all().order_by('name'), 'allHeaders': Subtitle.objects.all().order_by('name'),
'decisions': decisions.order_by('synopsis'), 'decisions': decisions.order_by('synopsis'),
'decisionsCount': len(decisions), 'decisionsCount': len(decisions),
'filter': filter,
'headers': sorted(headers), 'headers': sorted(headers),
'headersCount': len(headers), 'headersCount': len(headers),
'url': request.GET.get('path'), 'url': request.GET.get('path'),