summaryrefslogtreecommitdiff
path: root/archaeological_operations/lookups.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-08-04 15:31:31 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-08-04 15:37:23 +0200
commitb35c53bbf266153e478a776209e35aad36e9cd5b (patch)
treefeb7c9b7566209b175e0a6734b91692e0317b311 /archaeological_operations/lookups.py
parent6044b3522b2511eae088a40338115c902a9b0f82 (diff)
downloadIshtar-b35c53bbf266153e478a776209e35aad36e9cd5b.tar.bz2
Ishtar-b35c53bbf266153e478a776209e35aad36e9cd5b.zip
Admin: improve parcel, parcel owner forms, add a lookup for parcel
Diffstat (limited to 'archaeological_operations/lookups.py')
-rw-r--r--archaeological_operations/lookups.py41
1 files changed, 39 insertions, 2 deletions
diff --git a/archaeological_operations/lookups.py b/archaeological_operations/lookups.py
index 690b3c566..83cf6fa3a 100644
--- a/archaeological_operations/lookups.py
+++ b/archaeological_operations/lookups.py
@@ -1,7 +1,11 @@
from ajax_select import register, LookupChannel
from django.db.models import Q
-from archaeological_operations.models import Operation, ArchaeologicalSite
+from django.utils.encoding import force_text
+from django.utils.html import escape
+
+from archaeological_operations.models import Operation, ArchaeologicalSite, \
+ Parcel
@register('operation')
@@ -33,7 +37,40 @@ class ArchaeologicalSiteLookup(LookupChannel):
Q(name__icontains=term)
)
query &= subquery
- return self.model.objects.filter(query).order_by('cached_label')[:20]
+ return self.model.objects.filter(query).order_by('reference',
+ 'name')[:20]
def format_item_display(self, item):
return u"<span class='ajax-label'>%s</span>" % unicode(item)
+
+
+@register('parcel')
+class ParcelLookup(LookupChannel):
+ model = Parcel
+
+ def get_query(self, q, request):
+ query = Q()
+ for term in q.strip().split(' '):
+ subquery = (
+ Q(associated_file__cached_label__icontains=term) |
+ Q(operation__cached_label__icontains=term) |
+ Q(section__icontains=term) |
+ Q(parcel_number__icontains=term) |
+ Q(town__name__icontains=term)
+ )
+ try:
+ subquery |= Q(year=int(term))
+ except ValueError:
+ pass
+ query &= subquery
+ return self.model.objects.filter(
+ query).order_by('-associated_file__cached_label',
+ '-operation__cached_label',
+ 'section',
+ 'parcel_number')[:20]
+
+ def format_match(self, obj):
+ return escape(force_text(obj.long_label()))
+
+ def format_item_display(self, item):
+ return u"<span class='ajax-label'>%s</span>" % item.long_label()