First pass at filters

They're currently exclusive with each other and search, which ain't quite right!

Also stopped search from searching comments.
This commit is contained in:
2019-10-02 22:49:54 +01:00
parent 8599a2ae5b
commit 5bfa737f6b
4 changed files with 77 additions and 40 deletions

View File

@@ -4,39 +4,67 @@
{% block content %} {% block content %}
<div class="page-header"> <div class="page-header">
<h1 class="text-center">Asset List</h1> <h1 class="text-center">Asset List</h1>
</div> </div>
<form id="asset-filter-form" onsubmit=> <form id="asset-search-form">
{% csrf_token %} {% csrf_token %}
<div class="input-group"> <div class="input-group">
<input type="q" name="q" placeholder="Search" class="form-control" value="{{searchName}}"> <input type="query" name="query" placeholder="Search by Asset ID/Description" class="form-control" value="{{searchName}}">
<label for="asset_id" class="sr-only">Asset ID</label> <label for="asset_id" class="sr-only">Asset ID/Description:</label>
<span class="input-group-btn"><button type="submit" class="btn">Search</button></span> <span class="input-group-btn"><button type="submit" class="btn btn-default">Search</button></span>
</div> </div>
</form> </form>
<table class="table table-striped"> <form class="form-inline" id="asset-filter-form">
<thead> {% csrf_token %}
<tr> <div class="form-group">
<th>Asset ID</th> <label for="cat">Category:</label>
<th>Description</th> <select name="cat" class="form-control">
<th>Category</th> <option>None</option>
<th>Status</th> {% for name in categories %}
<th>Quick Links</th> <option>
</tr> {{ name }}
</thead> </option>
<tbody id="asset_table_body"> {% endfor %}
{% include 'asset_list_table_body.html' %} </select>
</tbody> </div>
</table> <div class="form-group">
<label for="status">Status:</label>
<select name="status" class="form-control">
<option>None</option>
{% for name in statuses %}
<option>
{{ name }}
</option>
{% endfor %}
</select>
</div>
<!---TODO: Auto filter whenever an option is selected, instead of using a button --->
<button type="submit" class="btn btn-default">Filter</button>
</form>
{% if is_paginated %} <table class="table">
<div class="text-center"> <thead>
{% paginator %} <tr>
</div> <th>Asset ID</th>
{% endif %} <th>Description</th>
<th>Category</th>
<th>Status</th>
<th>Quick Links</th>
</tr>
</thead>
<tbody id="asset_table_body">
{% include 'partials/asset_list_table_body.html' %}
</tbody>
</table>
{% if is_paginated %}
<div class="text-center">
{% paginator %}
</div>
{% endif %}
{% endblock %} {% endblock %}

View File

@@ -9,13 +9,13 @@
<div class="page-header"> <div class="page-header">
<h1> <h1>
{% if edit and object %} {% if edit and object %}
Edit Asset: {{ object.asset_id }} {{ object.description }} Edit Asset: {{ object.asset_id }} - {{ object.description }}
{% elif duplicate %} {% elif duplicate %}
Duplication of Asset: {{ previous_asset_id }} Duplication of Asset: {{ previous_asset_id }}
{% elif not object %} {% elif not object %}
Create Asset Create Asset
{% else %} {% else %}
Asset: {{ object.asset_id }} {{ object.description }} Asset: {{ object.asset_id }} - {{ object.description }}
{% endif %} {% endif %}
</h1> </h1>
</div> </div>

View File

@@ -20,18 +20,27 @@ class AssetList(LoginRequiredMixin, generic.ListView):
ordering = ['-pk'] ordering = ['-pk']
def get_queryset(self): def get_queryset(self):
q = self.request.GET.get('q', "") #TODO Feedback to user when search fails
if len(q) >= 3: query = self.request.GET.get('query', "")
return self.model.objects.filter(Q(asset_id__exact=q) | Q(description__icontains=q) | Q(comments__icontains=q)) if len(query) >= 3:
elif q != "": return self.model.objects.filter(Q(asset_id__exact=query) | Q(description__icontains=query))
return self.model.objects.filter(Q(asset_id__exact=q)) elif query != "":
return self.model.objects.filter(Q(asset_id__exact=query))
else: else:
return self.model.objects.all() cat = self.request.GET.get('cat', "")
status = self.request.GET.get('status', "")
if cat != "None":
return self.model.objects.filter(category__name__exact=cat)
elif status != "None":
return self.model.objects.filter(status__name__exact=status)
else:
return self.model.objects.all()
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
q = self.request.GET.get('q', "")
context = super(AssetList, self).get_context_data(**kwargs) context = super(AssetList, self).get_context_data(**kwargs)
context["searchName"] = q context["search_name"] = self.request.GET.get('query', "")
context["categories"] = models.AssetCategory.objects.all()
context["statuses"] = models.AssetStatus.objects.all()
return context; return context;
class AssetDetail(LoginRequiredMixin, generic.DetailView): class AssetDetail(LoginRequiredMixin, generic.DetailView):