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 %}
<div class="page-header">
<h1 class="text-center">Asset List</h1>
</div>
<div class="page-header">
<h1 class="text-center">Asset List</h1>
</div>
<form id="asset-filter-form" onsubmit=>
{% csrf_token %}
<div class="input-group">
<input type="q" name="q" placeholder="Search" class="form-control" value="{{searchName}}">
<label for="asset_id" class="sr-only">Asset ID</label>
<span class="input-group-btn"><button type="submit" class="btn">Search</button></span>
</div>
</form>
<form id="asset-search-form">
{% csrf_token %}
<div class="input-group">
<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/Description:</label>
<span class="input-group-btn"><button type="submit" class="btn btn-default">Search</button></span>
</div>
</form>
<table class="table table-striped">
<thead>
<tr>
<th>Asset ID</th>
<th>Description</th>
<th>Category</th>
<th>Status</th>
<th>Quick Links</th>
</tr>
</thead>
<tbody id="asset_table_body">
{% include 'asset_list_table_body.html' %}
</tbody>
</table>
<form class="form-inline" id="asset-filter-form">
{% csrf_token %}
<div class="form-group">
<label for="cat">Category:</label>
<select name="cat" class="form-control">
<option>None</option>
{% for name in categories %}
<option>
{{ name }}
</option>
{% endfor %}
</select>
</div>
<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 %}
<div class="text-center">
{% paginator %}
</div>
{% endif %}
<table class="table">
<thead>
<tr>
<th>Asset ID</th>
<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 %}

View File

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

View File

@@ -20,18 +20,27 @@ class AssetList(LoginRequiredMixin, generic.ListView):
ordering = ['-pk']
def get_queryset(self):
q = self.request.GET.get('q', "")
if len(q) >= 3:
return self.model.objects.filter(Q(asset_id__exact=q) | Q(description__icontains=q) | Q(comments__icontains=q))
elif q != "":
return self.model.objects.filter(Q(asset_id__exact=q))
#TODO Feedback to user when search fails
query = self.request.GET.get('query', "")
if len(query) >= 3:
return self.model.objects.filter(Q(asset_id__exact=query) | Q(description__icontains=query))
elif query != "":
return self.model.objects.filter(Q(asset_id__exact=query))
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):
q = self.request.GET.get('q', "")
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;
class AssetDetail(LoginRequiredMixin, generic.DetailView):