mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-02-02 21:32:15 +00:00
Add ability to search supplier list. Partial #374
This commit is contained in:
@@ -25,3 +25,7 @@ class SupplierForm(forms.ModelForm):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = models.Supplier
|
model = models.Supplier
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
class SupplierSearchForm(forms.Form):
|
||||||
|
query = forms.CharField(required=False)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{% extends 'base_assets.html' %}
|
{% extends 'base_assets.html' %}
|
||||||
{% block title %}List{% endblock %}
|
{% block title %}Asset List{% endblock %}
|
||||||
{% load paginator from filters %}
|
{% load paginator from filters %}
|
||||||
{% load widget_tweaks %}
|
{% load widget_tweaks %}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{% extends 'base_assets.html' %}
|
{% extends 'base_assets.html' %}
|
||||||
{% block title %}List{% endblock %}
|
{% block title %}Supplier List{% endblock %}
|
||||||
{% load paginator from filters %}
|
{% load paginator from filters %}
|
||||||
|
{% load widget_tweaks %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
@@ -8,6 +9,15 @@
|
|||||||
<h1>Supplier List</h1>
|
<h1>Supplier List</h1>
|
||||||
</div>
|
</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">
|
<table class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
|||||||
@@ -22,5 +22,7 @@ urlpatterns = [
|
|||||||
path('supplier/create', permission_required_with_403('assets.create_supplier')(views.SupplierCreate.as_view()), name='supplier_create'),
|
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/<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)),
|
path('', include(router.urls)),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -140,6 +140,42 @@ class SupplierList(generic.ListView):
|
|||||||
paginate_by = 40
|
paginate_by = 40
|
||||||
ordering = ['name']
|
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):
|
class SupplierDetail(generic.DetailView):
|
||||||
model = models.Supplier
|
model = models.Supplier
|
||||||
|
|||||||
Reference in New Issue
Block a user