diff options
Diffstat (limited to 'expo')
-rw-r--r-- | expo/models_logbooks.py | 9 | ||||
-rw-r--r-- | expo/search.py | 39 | ||||
-rw-r--r-- | expo/views.py | 1 | ||||
-rw-r--r-- | expo/views_caves.py | 18 | ||||
-rw-r--r-- | expo/views_logbooks.py | 11 | ||||
-rw-r--r-- | expo/views_other.py | 13 |
6 files changed, 84 insertions, 7 deletions
diff --git a/expo/models_logbooks.py b/expo/models_logbooks.py index e9cf391..89f2125 100644 --- a/expo/models_logbooks.py +++ b/expo/models_logbooks.py @@ -1,6 +1,6 @@ from django.db import models from django.contrib import admin - +from django.forms import ModelForm class Expedition(models.Model): year = models.CharField(max_length=20, unique=True) @@ -79,9 +79,4 @@ class PersonTrip(models.Model): is_logbook_entry_author = models.BooleanField() def __unicode__(self): - return "%s %s (%s)" % (self.person_expedition, self.place, self.date) - - - - - + return "%s %s (%s)" % (self.person_expedition, self.place, self.date)
\ No newline at end of file diff --git a/expo/search.py b/expo/search.py new file mode 100644 index 0000000..5ec2ce2 --- /dev/null +++ b/expo/search.py @@ -0,0 +1,39 @@ +import re
+
+from django.db.models import Q
+
+# search script from http://www.julienphalip.com/blog/2008/08/16/adding-search-django-site-snap/
+
+def normalize_query(query_string,
+ findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
+ normspace=re.compile(r'\s{2,}').sub):
+ ''' Splits the query string in invidual keywords, getting rid of unecessary spaces
+ and grouping quoted words together.
+ Example:
+
+ >>> normalize_query(' some random words "with quotes " and spaces')
+ ['some', 'random', 'words', 'with quotes', 'and', 'spaces']
+
+ '''
+ return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)]
+
+def get_query(query_string, search_fields):
+ ''' Returns a query, that is a combination of Q objects. That combination
+ aims to search keywords within a model by testing the given search fields.
+
+ '''
+ query = None # Query to search for every search term
+ terms = normalize_query(query_string)
+ for term in terms:
+ or_query = None # Query to search for a given term in each field
+ for field_name in search_fields:
+ q = Q(**{"%s__icontains" % field_name: term})
+ if or_query is None:
+ or_query = q
+ else:
+ or_query = or_query | q
+ if query is None:
+ query = or_query
+ else:
+ query = query & or_query
+ return query
\ No newline at end of file diff --git a/expo/views.py b/expo/views.py index eed5872..718c720 100644 --- a/expo/views.py +++ b/expo/views.py @@ -1,3 +1,4 @@ from views_caves import *
from views_survex import *
from views_logbooks import *
+from views_other import *
diff --git a/expo/views_caves.py b/expo/views_caves.py index b2e1850..1f108f8 100644 --- a/expo/views_caves.py +++ b/expo/views_caves.py @@ -1,12 +1,15 @@ from django.shortcuts import render_to_response
from troggle.expo.models import Cave, CaveAndEntrance
import troggle.settings as settings
+from troggle.expo.forms import CaveForm
+import search
def caveindex(request):
caves = Cave.objects.all()
return render_to_response('caveindex.html', {'caves': caves, 'settings': settings})
def cave(request, cave_id):
+ #hm, we're only choosing by the number within kataster, needs to be fixed. Caves in 1626 will presumably not work. - AC 7DEC08
cave = Cave.objects.filter(kataster_number = cave_id)[0]
return render_to_response('cave.html', {'cave': cave, 'settings': settings})
@@ -17,3 +20,18 @@ def ent(request, cave_id, ent_letter): 'entrance': cave_and_ent.entrance,
'letter': cave_and_ent.entrance_letter,
'settings': settings})
+
+def caveSearch(request):
+ query_string = ''
+ found_entries = None
+ if ('q' in request.GET) and request.GET['q'].strip():
+ query_string = request.GET['q']
+ entry_query = search.get_query(query_string, ['underground_description','official_name',])
+ found_entries = Cave.objects.filter(entry_query)
+
+ return render_to_response('cavesearch.html',
+ { 'query_string': query_string, 'found_entries': found_entries, 'settings': settings})
+ #context_instance=RequestContext(request))
+
+
+
diff --git a/expo/views_logbooks.py b/expo/views_logbooks.py index 9d69099..b44c770 100644 --- a/expo/views_logbooks.py +++ b/expo/views_logbooks.py @@ -1,6 +1,7 @@ from django.shortcuts import render_to_response
from troggle.expo.models import Expedition, Person, PersonExpedition, PersonTrip, LogbookEntry
import troggle.settings as settings
+import search
def personindex(request):
persons = Person.objects.all()
@@ -14,4 +15,14 @@ def logbookentry(request, logbookentry_id): logbookentry = LogbookEntry.objects.filter(id = logbookentry_id)[0]
return render_to_response('logbookentry.html', {'logbookentry': logbookentry, 'settings': settings})
+def logbookSearch(request, extra):
+ query_string = ''
+ found_entries = None
+ if ('q' in request.GET) and request.GET['q'].strip():
+ query_string = request.GET['q']
+ entry_query = search.get_query(query_string, ['text','title',])
+ found_entries = LogbookEntry.objects.filter(entry_query)
+ return render_to_response('logbooksearch.html',
+ { 'query_string': query_string, 'found_entries': found_entries, 'settings': settings})
+ #context_instance=RequestContext(request))
\ No newline at end of file diff --git a/expo/views_other.py b/expo/views_other.py new file mode 100644 index 0000000..808238e --- /dev/null +++ b/expo/views_other.py @@ -0,0 +1,13 @@ +from django.shortcuts import render_to_response
+from troggle.expo.models import Cave, Expedition, Person, LogbookEntry
+import troggle.settings as settings
+from django import forms
+from django.db.models import Q
+
+def stats(request):
+ statsDict={}
+ statsDict['expoCount'] = int(Expedition.objects.count())
+ statsDict['caveCount'] = int(Cave.objects.count())
+ statsDict['personCount'] = int(Person.objects.count())
+ statsDict['logbookEntryCount'] = int(LogbookEntry.objects.count())
+ return render_to_response('statistics.html', statsDict)
\ No newline at end of file |