2009-05-13 05:14:51 +01:00
from django . shortcuts import render_to_response
2009-07-02 20:43:18 +01:00
from troggle . core . models import Expedition , Person , PersonExpedition , PersonTrip , LogbookEntry
2009-08-05 11:58:36 +01:00
import troggle . core . models as models
2009-05-13 05:14:51 +01:00
import troggle . settings as settings
2009-08-05 11:58:36 +01:00
import django . db . 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-07-02 20:43:18 +01:00
from troggle . core . forms import PersonForm
2009-07-04 08:27:49 +01:00
from django . core . urlresolvers import reverse
2009-08-29 16:23:11 +01:00
from django . http import HttpResponseRedirect , HttpResponse
from utils import render_with_context
2009-05-13 05:39:52 +01:00
2009-07-04 08:27:49 +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.
2009-08-23 23:29:05 +01:00
#from troggle.alwaysUseRequestContext import render_response
2009-07-03 05:31:49 +01:00
2009-05-13 05:24:37 +01:00
import re
2009-05-13 05:14:51 +01:00
2009-08-05 11:58:36 +01:00
@django.db.models.permalink #this allows the nice get_absolute_url syntax we are using
2009-05-13 05:52:59 +01:00
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-07-27 13:43:43 +01:00
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 = [ ]
2009-08-05 11:58:36 +01:00
ncols = 4
2009-05-13 05:44:35 +01:00
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-08-29 16:23:11 +01:00
return render_with_context ( request , ' personindex.html ' , { ' persons ' : persons , ' personss ' : personss , ' notablepersons ' : notablepersons , } )
2009-05-13 05:14:51 +01:00
2009-07-27 13:43:43 +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-08-29 16:23:11 +01:00
return render_with_context ( request , ' expedition.html ' , { ' expedition ' : expedition , ' expedition_next ' : expedition_next , ' expedition_prev ' : expedition_prev , ' logbookentries ' : logbookentries , ' message ' : message , ' settings ' : settings } )
2009-05-13 05:52:15 +01:00
2009-05-13 05:52:59 +01:00
def get_absolute_url ( self ) :
return ( ' expedition ' , ( expedition . year ) )
2009-07-27 13:43:43 +01:00
2009-05-13 06:15:48 +01:00
def person ( request , first_name = ' ' , last_name = ' ' , ) :
2009-05-13 05:52:15 +01:00
person = Person . objects . get ( first_name = first_name , last_name = last_name )
2009-05-13 06:15:48 +01:00
#This is for removing the reference to the user's profile, in case they set it to the wrong person
if request . method == ' GET ' :
if request . GET . get ( ' clear_profile ' ) == ' True ' :
person . user = None
person . save ( )
return HttpResponseRedirect ( reverse ( ' profiles_select_profile ' ) )
2009-08-29 16:23:11 +01:00
return render_with_context ( request , ' person.html ' , { ' person ' : person , } )
2009-05-13 05:14:51 +01:00
2009-07-27 13:43:43 +01:00
def GetPersonChronology ( personexpedition ) :
res = { }
for persontrip in personexpedition . persontrip_set . all ( ) :
a = res . setdefault ( persontrip . date , { } )
a . setdefault ( " persontrips " , [ ] ) . append ( persontrip )
for personrole in personexpedition . personrole_set . all ( ) :
2009-08-05 11:58:36 +01:00
a = res . setdefault ( personrole . survexblock . date , { } )
2009-07-27 13:43:43 +01:00
b = a . setdefault ( " personroles " , { } )
2009-08-05 11:58:36 +01:00
survexpath = personrole . survexblock . survexpath
2009-07-27 13:43:43 +01:00
if b . get ( survexpath ) :
2009-08-01 07:31:27 +01:00
b [ survexpath ] + = " , " + str ( personrole . nrole )
2009-07-27 13:43:43 +01:00
else :
2009-08-01 07:31:27 +01:00
b [ survexpath ] = str ( personrole . nrole )
2009-07-27 13:43:43 +01:00
# build up the tables
rdates = res . keys ( )
rdates . sort ( )
res2 = [ ]
for rdate in rdates :
persontrips = res [ rdate ] . get ( " persontrips " , [ ] )
personroles = list ( res [ rdate ] . get ( " personroles " , { } ) . items ( ) )
for n in range ( max ( len ( persontrips ) , len ( personroles ) ) ) :
2009-08-01 07:31:27 +01:00
res2 . append ( ( ( n == 0 and rdate or " -- " ) , ( n < len ( persontrips ) and persontrips [ n ] ) , ( n < len ( personroles ) and personroles [ n ] ) ) )
2009-07-27 13:43:43 +01:00
return res2
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-07-27 13:43:43 +01:00
personchronology = GetPersonChronology ( personexpedition )
2009-08-29 16:23:11 +01:00
return render_with_context ( request , ' personexpedition.html ' , { ' personexpedition ' : personexpedition , ' personchronology ' : personchronology } )
2009-07-27 13:43:43 +01:00
2009-05-13 06:09:14 +01:00
2009-05-13 06:09:55 +01:00
def logbookentry ( request , date , slug ) :
2009-06-12 05:39:30 +01:00
logbookentry = LogbookEntry . objects . filter ( date = date , slug = slug )
if len ( logbookentry ) > 1 :
2009-08-29 16:23:11 +01:00
return render_with_context ( request , ' object_list.html ' , { ' object_list ' : logbookentry } )
2009-06-12 05:39:30 +01:00
else :
logbookentry = logbookentry [ 0 ]
2009-08-29 16:23:11 +01:00
return render_with_context ( request , ' logbookentry.html ' , { ' logbookentry ' : logbookentry } )
2009-05-13 05:15:49 +01:00
2009-07-27 13:43:43 +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 ' ]
2009-05-13 06:15:48 +01:00
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-08-29 16:23:11 +01:00
return render_with_context ( request , ' logbooksearch.html ' ,
2009-05-13 05:52:15 +01:00
{ ' 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-08-29 16:23:11 +01:00
return render_with_context ( request , ' personform.html ' , { ' form ' : form , } )
2009-07-27 13:43:43 +01:00
2009-08-05 11:58:36 +01:00
def experimental ( request ) :
legsbyexpo = [ ]
for expedition in Expedition . objects . all ( ) :
survexblocks = expedition . survexblock_set . all ( )
survexlegs = [ ]
survexleglength = 0.0
for survexblock in survexblocks :
survexlegs . extend ( survexblock . survexleg_set . all ( ) )
survexleglength + = survexblock . totalleglength
legsbyexpo . append ( ( expedition , { " nsurvexlegs " : len ( survexlegs ) , " survexleglength " : survexleglength } ) )
legsbyexpo . reverse ( )
survexlegs = models . SurvexLeg . objects . all ( )
totalsurvexlength = sum ( [ survexleg . tape for survexleg in survexlegs ] )
2009-08-29 16:23:11 +01:00
return render_with_context ( request , ' experimental.html ' , { " nsurvexlegs " : len ( survexlegs ) , " totalsurvexlength " : totalsurvexlength , " legsbyexpo " : legsbyexpo } )
2009-08-05 11:58:36 +01:00