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-05-13 05:14:51 +01:00
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-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-05-13 06:15:48 +01:00
from django . http import HttpResponseRedirect
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.
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-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-07-04 08:27:49 +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-07-04 08:27:49 +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:15 +01:00
2009-05-13 05:52:59 +01:00
def get_absolute_url ( self ) :
return ( ' expedition ' , ( expedition . year ) )
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-07-04 08:27:49 +01:00
return render_response ( request , ' person.html ' , { ' person ' : person , } )
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-07-04 08:27:49 +01:00
return render_response ( request , ' personexpedition.html ' , { ' personexpedition ' : personexpedition , } )
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-07-04 08:27:49 +01:00
return render_response ( request , ' object_list.html ' , { ' object_list ' : logbookentry } )
2009-06-12 05:39:30 +01:00
else :
logbookentry = logbookentry [ 0 ]
2009-07-04 08:27:49 +01:00
return render_response ( request , ' logbookentry.html ' , { ' logbookentry ' : 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 ' ]
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-07-04 08:27:49 +01:00
return render_response ( 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-07-04 08:27:49 +01:00
return render_response ( request , ' personform.html ' , { ' form ' : form , } )