2009-05-13 05:14:51 +01:00
from django . shortcuts import render_to_response
from troggle . expo . models import Expedition , Person , PersonExpedition , PersonTrip , LogbookEntry
import troggle . settings as settings
2009-05-13 05:52:59 +01:00
from django . db import models
2009-05-13 05:35:59 +01:00
from troggle . parsers . logbooks import LoadLogbookForExpedition
2009-05-13 05:39:52 +01:00
from troggle . parsers . people import GetPersonExpeditionNameLookup
2009-05-13 05:48:10 +01:00
from troggle . expo . forms import PersonForm
2009-05-13 05:39:52 +01:00
2009-05-13 05:52:15 +01:00
# Django uses Context, not RequestContext when you call render_to_response. We always want to use RequestContext, so that django adds the context from settings.TEMPLATE_CONTEXT_PROCESSORS. This way we automatically get necessary settings variables passed to each template. So we use a custom method, render_response instead of render_to_response. Hopefully future Django releases will make this unnecessary.
from troggle . alwaysUseRequestContext import render_response
2009-05-13 05:22:14 +01:00
import search
2009-05-13 05:24:37 +01:00
import re
2009-05-13 05:14:51 +01:00
2009-05-13 05:52:59 +01:00
@models.permalink #this allows the nice get_absolute_url syntax we are using
2009-05-13 05:55:00 +01:00
def getNotablePersons ( ) :
notablepersons = [ ]
for person in Person . objects . all ( ) :
if person . bisnotable ( ) :
notablepersons . append ( person )
return notablepersons
2009-05-13 05:14:51 +01:00
def personindex ( request ) :
persons = Person . objects . all ( )
2009-05-13 05:52:15 +01:00
# From what I can tell, "persons" seems to be the table rows, while "personss" is the table columns. - AC 16 Feb 09
2009-05-13 05:44:35 +01:00
personss = [ ]
ncols = 5
nc = ( len ( persons ) + ncols - 1 ) / ncols
for i in range ( ncols ) :
personss . append ( persons [ i * nc : ( i + 1 ) * nc ] )
2009-05-13 05:48:47 +01:00
2009-05-13 05:55:00 +01:00
notablepersons = [ ]
for person in Person . objects . all ( ) :
if person . bisnotable ( ) :
notablepersons . append ( person )
2009-05-13 05:52:15 +01:00
return render_response ( request , ' personindex.html ' , { ' persons ' : persons , ' personss ' : personss , ' notablepersons ' : notablepersons , } )
2009-05-13 05:14:51 +01:00
2009-05-13 05:35:59 +01:00
def expedition ( request , expeditionname ) :
year = int ( expeditionname )
expedition = Expedition . objects . get ( year = year )
expedition_next = Expedition . objects . filter ( year = year + 1 ) and Expedition . objects . get ( year = year + 1 ) or None
expedition_prev = Expedition . objects . filter ( year = year - 1 ) and Expedition . objects . get ( year = year - 1 ) or None
message = " No message "
if " reload " in request . GET :
message = LoadLogbookForExpedition ( expedition )
2009-05-13 05:39:52 +01:00
#message = str(GetPersonExpeditionNameLookup(expedition).keys())
2009-05-13 05:35:59 +01:00
logbookentries = expedition . logbookentry_set . order_by ( ' date ' )
2009-05-13 05:52:15 +01:00
return render_response ( request , ' expedition.html ' , { ' expedition ' : expedition , ' expedition_next ' : expedition_next , ' expedition_prev ' : expedition_prev , ' logbookentries ' : logbookentries , ' message ' : message , } )
2009-05-13 05:52:59 +01:00
def get_absolute_url ( self ) :
return ( ' expedition ' , ( expedition . year ) )
2009-05-13 05:52:15 +01:00
def person ( request , first_name = ' ' , last_name = ' ' ) :
person = Person . objects . get ( first_name = first_name , last_name = last_name )
return render_response ( request , ' person.html ' , { ' person ' : person , } )
def get_absolute_url ( self ) :
return settings . URL_ROOT + self . first_name + ' _ ' + self . last_name
2009-05-13 05:35:59 +01:00
2009-05-13 05:52:15 +01:00
#def person(request, name):
# person = Person.objects.get(href=name)
#
2009-05-13 05:14:51 +01:00
2009-05-13 06:02:42 +01:00
def personexpedition ( request , first_name = ' ' , last_name = ' ' , year = ' ' ) :
person = Person . objects . get ( first_name = first_name , last_name = last_name )
2009-05-13 05:38:18 +01:00
expedition = Expedition . objects . get ( year = year )
personexpedition = person . personexpedition_set . get ( expedition = expedition )
2009-05-13 05:52:15 +01:00
return render_response ( request , ' personexpedition.html ' , { ' personexpedition ' : personexpedition , } )
2009-05-13 05:38:18 +01:00
2009-05-13 06:09:14 +01:00
def newQMlink ( logbookentry ) :
2009-05-13 06:08:04 +01:00
biggestQMnumber = 0
2009-05-13 06:09:14 +01:00
if logbookentry . cave :
for log in logbookentry . cave . logbookentry_set . all ( ) :
try :
2009-05-13 06:08:04 +01:00
biggestQMnumberInLog = logbookentry . QMs_found . order_by ( ' -number ' ) [ 0 ] . number
except IndexError :
biggestQMnumberInLog = 0
if biggestQMnumberInLog > biggestQMnumber :
biggestQMnumber = biggestQMnumberInLog
2009-05-13 06:09:14 +01:00
else :
return None
2009-05-13 06:08:04 +01:00
nextQMnumber = biggestQMnumber + 1
2009-05-13 06:09:14 +01:00
return settings . URL_ROOT + r ' /admin/expo/qm/add/? ' + r ' found_by= ' + str ( logbookentry . pk ) + ' &number= ' + str ( nextQMnumber )
def logbookentry ( request , logbookentry_pk ) :
logbookentry = LogbookEntry . objects . get ( pk = logbookentry_pk )
return render_response ( request , ' logbookentry.html ' , { ' logbookentry ' : logbookentry , ' newQMlink ' : newQMlink ( logbookentry ) } )
2009-05-13 05:15:49 +01:00
2009-05-13 05:22:14 +01:00
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 )
2009-05-13 05:15:49 +01:00
2009-05-13 05:52:15 +01:00
return render_response ( request , ' logbooksearch.html ' ,
{ ' query_string ' : query_string , ' found_entries ' : found_entries , } )
2009-05-13 05:24:37 +01:00
#context_instance=RequestContext(request))
2009-05-13 05:48:10 +01:00
def personForm ( request , pk ) :
person = Person . objects . get ( pk = pk )
form = PersonForm ( instance = person )
2009-05-13 05:52:15 +01:00
return render_response ( request , ' personform.html ' , { ' form ' : form , } )