Add ability to search supplier list. Partial #374

This commit is contained in:
2019-11-24 14:13:47 +00:00
parent 0a724e645f
commit 22f0041eaf
5 changed files with 54 additions and 2 deletions

View File

@@ -25,3 +25,7 @@ class SupplierForm(forms.ModelForm):
class Meta:
model = models.Supplier
fields = '__all__'
class SupplierSearchForm(forms.Form):
query = forms.CharField(required=False)

View File

@@ -1,5 +1,5 @@
{% extends 'base_assets.html' %}
{% block title %}List{% endblock %}
{% block title %}Asset List{% endblock %}
{% load paginator from filters %}
{% load widget_tweaks %}

View File

@@ -1,6 +1,7 @@
{% extends 'base_assets.html' %}
{% block title %}List{% endblock %}
{% block title %}Supplier List{% endblock %}
{% load paginator from filters %}
{% load widget_tweaks %}
{% block content %}
@@ -8,6 +9,15 @@
<h1>Supplier List</h1>
</div>
<form id="supplier-search-form" method="get" class="form-inline pull-right">
{% csrf_token %}
<div class="input-group pull-right" style="width: auto;">
{% render_field form.query|add_class:'form-control' placeholder='Search by Name' style="width: 250px"%}
<label for="query" class="sr-only">Name:</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>

View File

@@ -22,5 +22,7 @@ urlpatterns = [
path('supplier/create', permission_required_with_403('assets.create_supplier')(views.SupplierCreate.as_view()), name='supplier_create'),
path('supplier/<int:pk>/edit', permission_required_with_403('assets.edit_supplier')(views.SupplierUpdate.as_view()), name='supplier_update'),
path('supplier/search/', views.SupplierSearch.as_view(), name='supplier_search_json'),
path('', include(router.urls)),
]

View File

@@ -140,6 +140,42 @@ class SupplierList(generic.ListView):
paginate_by = 40
ordering = ['name']
def get_queryset(self):
if self.request.method == 'POST':
self.form = forms.SupplierSearchForm(data=self.request.POST)
elif self.request.method == 'GET':
self.form = forms.SupplierSearchForm(data=self.request.GET)
else:
self.form = forms.SupplierSearchForm(data={})
form = self.form
if not form.is_valid():
return self.model.objects.none()
query_string = form.cleaned_data['query'] or ""
if len(query_string) == 0:
queryset = self.model.objects.all()
else:
queryset = self.model.objects.filter(Q(name__icontains=query_string))
return queryset
def get_context_data(self, **kwargs):
context = super(SupplierList, self).get_context_data(**kwargs)
context["form"] = self.form
return context
class SupplierSearch(SupplierList):
hide_hidden_status = False
def render_to_response(self, context, **response_kwargs):
result = []
for supplier in context["object_list"]:
result.append({"name": supplier.name})
return JsonResponse(result, safe=False)
class SupplierDetail(generic.DetailView):
model = models.Supplier