2009-05-13 05:14:51 +01:00
from django . shortcuts import render_to_response
2011-05-01 19:32:41 +01:00
from troggle . core . models import Expedition , Person , PersonExpedition , PersonTrip , LogbookEntry , SurvexBlock
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
2011-05-01 19:32:41 +01:00
from troggle . core . forms import PersonForm , getTripForm
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
2011-05-01 19:32:41 +01:00
from django . template import Context , loader
2009-08-29 16:23:11 +01:00
from utils import render_with_context
2011-05-01 19:32:41 +01:00
import os . path
import troggle . parsers . logbooks as logbookparsers
from django . template . defaultfilters import slugify
2009-08-29 16:23:11 +01:00
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 ) :
2009-09-11 23:56:47 +01:00
expedition = Expedition . objects . get ( year = int ( expeditionname ) )
expeditions = Expedition . objects . all ( )
personexpeditiondays = [ ]
2011-05-01 19:32:41 +01:00
dateditems = list ( expedition . logbookentry_set . all ( ) ) + list ( expedition . survexblock_set . all ( ) )
dates = list ( set ( [ item . date for item in dateditems ] ) )
dates . sort ( )
2009-09-11 23:56:47 +01:00
for personexpedition in expedition . personexpedition_set . all ( ) :
prow = [ ]
2011-05-01 19:32:41 +01:00
for date in dates :
pcell = { " persontrips " : PersonTrip . objects . filter ( personexpedition = personexpedition ,
logbook_entry__date = date ) }
pcell [ " survexblocks " ] = set ( SurvexBlock . objects . filter ( survexpersonrole__personexpedition = personexpedition ,
date = date ) )
2009-09-11 23:56:47 +01:00
prow . append ( pcell )
personexpeditiondays . append ( { " personexpedition " : personexpedition , " personrow " : prow } )
message = " "
2009-05-13 05:35:59 +01:00
if " reload " in request . GET :
message = LoadLogbookForExpedition ( expedition )
2011-05-01 19:32:41 +01:00
return render_with_context ( request , ' expedition.html ' , { ' expedition ' : expedition , ' expeditions ' : expeditions , ' personexpeditiondays ' : personexpeditiondays , ' message ' : message , ' settings ' : settings , ' dateditems ' : dateditems } )
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 )
2009-09-11 09:04:59 +01:00
for personrole in personexpedition . survexpersonrole_set . all ( ) :
2009-08-05 11:58:36 +01:00
a = res . setdefault ( personrole . survexblock . date , { } )
2009-09-11 09:04:59 +01:00
a . setdefault ( " personroles " , [ ] ) . append ( personrole . survexblock )
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 " , [ ] )
2009-09-11 09:04:59 +01:00
personroles = res [ rdate ] . get ( " personroles " , [ ] )
2009-07-27 13:43:43 +01:00
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
2011-05-01 19:32:41 +01:00
def newLogbookEntry ( request , expeditionyear , pdate = None , pslug = None ) :
expedition = Expedition . objects . get ( year = expeditionyear )
PersonTripFormSet , TripForm = getTripForm ( expedition )
if pslug and pdate :
previousdate = datetime . date ( * [ int ( x ) for x in pdate . split ( " - " ) ] )
previouslbe = LogbookEntry . objects . get ( slug = pslug , date = previousdate , expedition__year = year )
assert previouslbe . filename
if request . method == ' POST ' : # If the form has been submitted...
tripForm = TripForm ( request . POST ) # A form bound to the POST data
personTripFormSet = PersonTripFormSet ( request . POST )
dateStr = tripForm . cleaned_data [ " date " ] . strftime ( " % Y- % m- %d " )
directory = os . path . join ( settings . EXPOWEB ,
" years " ,
expedition . year ,
" autologbook " )
filename = os . path . join ( directory ,
dateStr + " . " + slugify ( tripForm . cleaned_data [ " title " ] ) [ : 50 ] + " .html " )
if tripForm . is_valid ( ) and personTripFormSet . is_valid ( ) : # All validation rules pass
if not os . path . isdir ( directory ) :
os . mkdir ( directory )
if pslug and pdate :
delLogbookEntry ( previouslbe )
f = open ( filename , " w " )
template = loader . get_template ( ' dataformat/logbookentry.html ' )
context = Context ( { ' trip ' : tripForm . cleaned_data ,
' persons ' : personTripFormSet . cleaned_data ,
' date ' : dateStr ,
' expeditionyear ' : expeditionyear } )
f . write ( template . render ( context ) )
f . close ( )
print logbookparsers . parseAutoLogBookEntry ( filename )
return HttpResponseRedirect ( reverse ( ' expedition ' , args = [ expedition . year ] ) ) # Redirect after POST
else :
2011-05-01 23:11:18 +01:00
if pslug and date :
2011-05-01 19:32:41 +01:00
if lbe . cave :
tripForm = TripForm ( date = previousdate ,
title = previouslbe . title ,
cave = previouslbe . cave . reference ( ) ,
location = None ,
caveOrLocation = " cave " ,
html = previouslbe . text )
else :
tripForm = TripForm ( date = previousdate ,
title = previouslbe . title ,
cave = None ,
location = previouslbe . location ,
caveOrLocation = " location " ,
html = previouslbe . text )
personTripFormSet = PersonTripFormSet ( initial = [ { " name " : py . personexpedition . name ( ) ,
" TU " : py . time_underground ,
" author " : py . is_logbook_entry_author }
for py in previouslbe . persontrip_set . all ( ) ] )
else :
tripForm = TripForm ( ) # An unbound form
personTripFormSet = PersonTripFormSet ( )
return render_with_context ( request , ' newlogbookentry.html ' , {
' tripForm ' : tripForm ,
' personTripFormSet ' : personTripFormSet ,
} )
def deleteLogbookEntry ( request , expeditionyear , date = None , slug = None ) :
expedition = Expedition . objects . get ( year = expeditionyear )
previousdate = datetime . date ( * [ int ( x ) for x in pdate . split ( " - " ) ] )
previouslbe = LogbookEntry . objects . get ( slug = pslug , date = previousdate , expedition__year = year )
delLogbookEntry ( previouslbe )
return HttpResponseRedirect ( reverse ( ' expedition ' , args = [ expedition . year ] ) ) # Redirect after POST
def delLogbookEntry ( lbe ) :
for pt in lbe . persontrip_set . all ( ) :
pt . delete ( )
lbe . delete ( )
os . delete ( lbe . filename )