mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-16 21:12:13 +00:00
Dedupe generic search logic
This commit is contained in:
@@ -134,3 +134,28 @@ class SecureAPIRequest(generic.View):
|
||||
return HttpResponse(json, content_type="application/json") # Always json
|
||||
|
||||
return HttpResponse(model)
|
||||
|
||||
|
||||
class GenericListView(generic.ListView):
|
||||
paginate_by = 20
|
||||
|
||||
def get_queryset(self):
|
||||
q = self.request.GET.get('q', "")
|
||||
|
||||
filter = Q(name__icontains=q) | Q(email__icontains=q) | Q(address__icontains=q) | Q(notes__icontains=q) | Q(
|
||||
phone__startswith=q) | Q(phone__endswith=q)
|
||||
|
||||
# try and parse an int
|
||||
try:
|
||||
val = int(q)
|
||||
filter = filter | Q(pk=val)
|
||||
except: # noqa
|
||||
# not an integer
|
||||
pass
|
||||
|
||||
object_list = self.model.objects.filter(filter)
|
||||
|
||||
orderBy = self.request.GET.get('orderBy', "name")
|
||||
if orderBy is not "":
|
||||
object_list = object_list.order_by(orderBy)
|
||||
return object_list
|
||||
|
||||
@@ -20,6 +20,8 @@ from RIGS import models, forms
|
||||
from assets import models as asset_models
|
||||
from functools import reduce
|
||||
|
||||
from PyRIGS.views import GenericListView
|
||||
|
||||
"""
|
||||
Displays the current rig count along with a few other bits and pieces
|
||||
"""
|
||||
@@ -52,31 +54,9 @@ class CloseModal(generic.TemplateView):
|
||||
return {'messages': messages.get_messages(self.request)}
|
||||
|
||||
|
||||
class PersonList(generic.ListView):
|
||||
class PersonList(GenericListView):
|
||||
template_name = 'person_list.html'
|
||||
model = models.Person
|
||||
paginate_by = 20
|
||||
|
||||
def get_queryset(self):
|
||||
q = self.request.GET.get('query', "")
|
||||
|
||||
filter = Q(name__icontains=q) | Q(email__icontains=q) | Q(address__icontains=q) | Q(notes__icontains=q) | Q(
|
||||
phone__startswith=q) | Q(phone__endswith=q)
|
||||
|
||||
# try and parse an int
|
||||
try:
|
||||
val = int(q)
|
||||
filter = filter | Q(pk=val)
|
||||
except: # noqa
|
||||
# not an integer
|
||||
pass
|
||||
|
||||
object_list = self.model.objects.filter(filter)
|
||||
|
||||
orderBy = self.request.GET.get('orderBy', 'name')
|
||||
if orderBy is not None:
|
||||
object_list = object_list.order_by(orderBy)
|
||||
return object_list
|
||||
|
||||
|
||||
class PersonDetail(generic.DetailView):
|
||||
@@ -120,31 +100,9 @@ class PersonUpdate(generic.UpdateView):
|
||||
return url
|
||||
|
||||
|
||||
class OrganisationList(generic.ListView):
|
||||
class OrganisationList(GenericListView):
|
||||
template_name = 'organisation_list.html'
|
||||
model = models.Organisation
|
||||
paginate_by = 20
|
||||
|
||||
def get_queryset(self):
|
||||
q = self.request.GET.get('query', "")
|
||||
|
||||
filter = Q(name__icontains=q) | Q(email__icontains=q) | Q(address__icontains=q) | Q(notes__icontains=q) | Q(
|
||||
phone__startswith=q) | Q(phone__endswith=q)
|
||||
|
||||
# try and parse an int
|
||||
try:
|
||||
val = int(q)
|
||||
filter = filter | Q(pk=val)
|
||||
except: # noqa
|
||||
# not an integer
|
||||
pass
|
||||
|
||||
object_list = self.model.objects.filter(filter)
|
||||
|
||||
orderBy = self.request.GET.get('orderBy', "name")
|
||||
if orderBy is not "":
|
||||
object_list = object_list.order_by(orderBy)
|
||||
return object_list
|
||||
|
||||
|
||||
class OrganisationDetail(generic.DetailView):
|
||||
@@ -188,31 +146,9 @@ class OrganisationUpdate(generic.UpdateView):
|
||||
return url
|
||||
|
||||
|
||||
class VenueList(generic.ListView):
|
||||
class VenueList(GenericListView):
|
||||
template_name = "venue_list.html"
|
||||
model = models.Venue
|
||||
paginate_by = 20
|
||||
|
||||
def get_queryset(self):
|
||||
q = self.request.GET.get('query', "")
|
||||
|
||||
filter = Q(name__icontains=q) | Q(email__icontains=q) | Q(address__icontains=q) | Q(notes__icontains=q) | Q(
|
||||
phone__startswith=q) | Q(phone__endswith=q)
|
||||
|
||||
# try and parse an int
|
||||
try:
|
||||
val = int(q)
|
||||
filter = filter | Q(pk=val)
|
||||
except: # noqa
|
||||
# not an integer
|
||||
pass
|
||||
|
||||
object_list = self.model.objects.filter(filter)
|
||||
|
||||
orderBy = self.request.GET.get('orderBy', "name")
|
||||
if orderBy is not "":
|
||||
object_list = object_list.order_by(orderBy)
|
||||
return object_list
|
||||
|
||||
|
||||
class VenueDetail(generic.DetailView):
|
||||
|
||||
@@ -40,10 +40,6 @@ class SupplierForm(forms.ModelForm):
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class SupplierSearchForm(forms.Form):
|
||||
query = forms.CharField(required=False)
|
||||
|
||||
|
||||
class CableTypeForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = models.CableType
|
||||
|
||||
@@ -14,6 +14,7 @@ from django.utils.decorators import method_decorator
|
||||
from django.views import generic
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from versioning import versioning
|
||||
from PyRIGS.views import GenericListView
|
||||
|
||||
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
@@ -205,36 +206,11 @@ class AssetAudit(AssetEdit):
|
||||
return super().get_success_url()
|
||||
|
||||
|
||||
class SupplierList(generic.ListView):
|
||||
class SupplierList(GenericListView):
|
||||
model = models.Supplier
|
||||
template_name = 'supplier_list.html'
|
||||
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
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<form class="form-inline pr-3">
|
||||
{% csrf_token %}
|
||||
<div class="input-group pull-right">
|
||||
<input type="search" name="query" placeholder="Search" value="{{ request.GET.query }}"
|
||||
<input type="search" name="q" placeholder="Search" value="{{ request.GET.q }}"
|
||||
class="form-control"/>
|
||||
<span class="input-group-append"><button type="submit" class="btn btn-primary" id="id_search">Search</button></span>
|
||||
</div>
|
||||
@@ -31,9 +31,9 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for object in object_list %}
|
||||
<tr>
|
||||
<tr id="row_item">
|
||||
<th scope="row" class="align-middle">{{ object.pk }}</th>
|
||||
<td class="align-middle">{{ object.name }}</td>
|
||||
<td class="align-middle" id="cell_name">{{ object.name }}</td>
|
||||
<td class="align-middle">{{ object.email }}</td>
|
||||
<td class="align-middle">{% if object.phone %}<a href="tel:{{ object.phone }}">{%endif%}{{ object.phone }}{% if object.phone %}</a>{%endif%}</td>
|
||||
<td class="align-middle">{{ object.notes|yesno|capfirst }}</td>
|
||||
|
||||
Reference in New Issue
Block a user