2009-07-11 01:36:00 +01:00
import urllib , urlparse , string , os , datetime , logging , re
2009-05-13 05:24:37 +01:00
from django . forms import ModelForm
from django . db import models
from django . contrib import admin
2009-05-13 05:27:43 +01:00
from django . core . files . storage import FileSystemStorage
2009-05-13 05:48:10 +01:00
from django . contrib . auth . models import User
2009-05-13 05:56:56 +01:00
from django . contrib . contenttypes . models import ContentType
2009-09-11 23:56:47 +01:00
from django . db . models import Min , Max
2009-05-13 05:52:59 +01:00
from django . conf import settings
2009-05-13 06:05:37 +01:00
from decimal import Decimal , getcontext
2009-05-13 06:28:36 +01:00
from django . core . urlresolvers import reverse
from imagekit . models import ImageModel
2009-05-13 06:05:37 +01:00
getcontext ( ) . prec = 2 #use 2 significant figures for decimal calculations
2009-05-13 05:39:52 +01:00
2009-05-13 05:13:38 +01:00
from models_survex import *
2009-05-13 05:24:37 +01:00
2009-08-05 11:58:36 +01:00
2009-07-11 01:36:00 +01:00
def get_related_by_wikilinks ( wiki_text ) :
found = re . findall ( settings . QM_PATTERN , wiki_text )
res = [ ]
for wikilink in found :
qmdict = { ' urlroot ' : settings . URL_ROOT , ' cave ' : wikilink [ 2 ] , ' year ' : wikilink [ 1 ] , ' number ' : wikilink [ 3 ] }
try :
qm = QM . objects . get ( found_by__cave__kataster_number = qmdict [ ' cave ' ] ,
found_by__date__year = qmdict [ ' year ' ] ,
number = qmdict [ ' number ' ] )
res . append ( qm )
except QM . DoesNotExist :
print ' fail on ' + str ( wikilink )
return res
2009-05-22 06:17:24 +01:00
logging . basicConfig ( level = logging . DEBUG ,
filename = settings . LOGFILE ,
filemode = ' w ' )
2009-07-04 08:27:49 +01:00
#This class is for adding fields and methods which all of our models will have.
2009-05-13 05:56:56 +01:00
class TroggleModel ( models . Model ) :
2009-05-13 06:00:16 +01:00
new_since_parsing = models . BooleanField ( default = False , editable = False )
2009-06-10 06:34:50 +01:00
non_public = models . BooleanField ( default = False )
2009-05-15 03:29:19 +01:00
def object_name ( self ) :
return self . _meta . object_name
2009-05-13 05:56:56 +01:00
def get_admin_url ( self ) :
2009-07-02 23:02:42 +01:00
return urlparse . urljoin ( settings . URL_ROOT , " /admin/core/ " + self . object_name ( ) . lower ( ) + " / " + str ( self . pk ) )
2009-05-13 05:56:56 +01:00
class Meta :
2009-07-04 08:27:49 +01:00
abstract = True
class TroggleImageModel ( ImageModel ) :
new_since_parsing = models . BooleanField ( default = False , editable = False )
def object_name ( self ) :
return self . _meta . object_name
def get_admin_url ( self ) :
return urlparse . urljoin ( settings . URL_ROOT , " /admin/core/ " + self . object_name ( ) . lower ( ) + " / " + str ( self . pk ) )
class Meta :
abstract = True
2009-05-13 06:23:57 +01:00
2009-07-27 13:43:43 +01:00
#
# single Expedition, usually seen by year
#
2009-05-13 05:56:56 +01:00
class Expedition ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
year = models . CharField ( max_length = 20 , unique = True )
name = models . CharField ( max_length = 100 )
2009-09-11 23:56:47 +01:00
2009-05-13 05:24:37 +01:00
def __unicode__ ( self ) :
return self . year
2009-05-13 05:44:35 +01:00
class Meta :
2009-05-15 03:29:19 +01:00
ordering = ( ' -year ' , )
2009-09-11 23:56:47 +01:00
get_latest_by = ' year '
2009-05-13 05:52:15 +01:00
def get_absolute_url ( self ) :
2009-07-27 13:43:43 +01:00
return urlparse . urljoin ( settings . URL_ROOT , reverse ( ' expedition ' , args = [ self . year ] ) )
2009-07-04 08:27:49 +01:00
2009-09-11 23:56:47 +01:00
# construction function. should be moved out
2009-09-11 09:04:59 +01:00
def get_expedition_day ( self , date ) :
expeditiondays = self . expeditionday_set . filter ( date = date )
if expeditiondays :
assert len ( expeditiondays ) == 1
return expeditiondays [ 0 ]
res = ExpeditionDay ( expedition = self , date = date )
res . save ( )
return res
2009-09-11 23:56:47 +01:00
def day_min ( self ) :
res = self . expeditionday_set . all ( )
return res and res [ 0 ] or None
def day_max ( self ) :
res = self . expeditionday_set . all ( )
return res and res [ len ( res ) - 1 ] or None
2009-09-11 09:04:59 +01:00
2009-09-11 23:56:47 +01:00
2009-09-11 09:04:59 +01:00
class ExpeditionDay ( TroggleModel ) :
expedition = models . ForeignKey ( " Expedition " )
date = models . DateField ( )
2009-07-04 08:27:49 +01:00
2009-09-11 23:56:47 +01:00
class Meta :
ordering = ( ' date ' , )
def GetPersonTrip ( self , personexpedition ) :
personexpeditions = self . persontrip_set . filter ( expeditionday = self )
return personexpeditions and personexpeditions [ 0 ] or None
2009-07-27 13:43:43 +01:00
#
# single Person, can go on many years
#
2009-05-13 05:56:56 +01:00
class Person ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
first_name = models . CharField ( max_length = 100 )
last_name = models . CharField ( max_length = 100 )
2009-05-13 05:25:17 +01:00
is_vfho = models . BooleanField ( help_text = " VFHO is the Vereines für Höhlenkunde in Obersteier, a nearby Austrian caving club. " )
2009-07-04 08:27:49 +01:00
mug_shot = models . CharField ( max_length = 100 , blank = True , null = True )
2009-05-13 05:25:17 +01:00
blurb = models . TextField ( blank = True , null = True )
2009-05-13 05:48:47 +01:00
2009-07-04 08:27:49 +01:00
#href = models.CharField(max_length=200)
orderref = models . CharField ( max_length = 200 ) # for alphabetic
#the below have been removed and made methods. I'm not sure what the b in bisnotable stands for. - AC 16 Feb
#notability = models.FloatField() # for listing the top 20 people
#bisnotable = models.BooleanField()
2009-05-13 05:54:17 +01:00
user = models . OneToOneField ( User , null = True , blank = True )
2009-05-13 05:52:15 +01:00
def get_absolute_url ( self ) :
2009-06-08 20:16:18 +01:00
return urlparse . urljoin ( settings . URL_ROOT , reverse ( ' person ' , kwargs = { ' first_name ' : self . first_name , ' last_name ' : self . last_name } ) )
2009-05-13 05:52:15 +01:00
2009-05-13 05:25:39 +01:00
class Meta :
2009-07-04 08:27:49 +01:00
verbose_name_plural = " People "
class Meta :
2009-05-13 05:48:47 +01:00
ordering = ( ' orderref ' , ) # "Wookey" makes too complex for: ('last_name', 'first_name')
2009-05-13 05:35:59 +01:00
2009-05-13 05:24:37 +01:00
def __unicode__ ( self ) :
2009-05-13 05:35:59 +01:00
if self . last_name :
return " %s %s " % ( self . first_name , self . last_name )
return self . first_name
2009-07-04 08:27:49 +01:00
2009-05-13 05:44:35 +01:00
2009-05-13 05:55:00 +01:00
def notability ( self ) :
2009-05-13 06:05:37 +01:00
notability = Decimal ( 0 )
for personexpedition in self . personexpedition_set . all ( ) :
2009-05-13 05:55:00 +01:00
if not personexpedition . is_guest :
2009-05-13 06:05:37 +01:00
notability + = Decimal ( 1 ) / ( 2012 - int ( personexpedition . expedition . year ) )
2009-05-13 05:55:00 +01:00
return notability
2009-05-13 05:52:59 +01:00
2009-05-13 05:55:00 +01:00
def bisnotable ( self ) :
2009-05-13 06:05:37 +01:00
return self . notability ( ) > Decimal ( 1 ) / Decimal ( 3 )
2009-07-04 08:27:49 +01:00
2009-08-05 11:58:36 +01:00
def surveyedleglength ( self ) :
return sum ( [ personexpedition . surveyedleglength ( ) for personexpedition in self . personexpedition_set . all ( ) ] )
def first ( self ) :
return self . personexpedition_set . order_by ( ' -expedition ' ) [ 0 ]
def last ( self ) :
return self . personexpedition_set . order_by ( ' expedition ' ) [ 0 ]
2009-07-04 08:27:49 +01:00
#def Sethref(self):
#if self.last_name:
#self.href = self.first_name.lower() + "_" + self.last_name.lower()
#self.orderref = self.last_name + " " + self.first_name
#else:
# self.href = self.first_name.lower()
#self.orderref = self.first_name
#self.notability = 0.0 # set temporarily
2009-05-13 05:24:37 +01:00
2009-07-27 13:43:43 +01:00
#
# Person's attenance to one Expo
#
2009-05-13 05:56:56 +01:00
class PersonExpedition ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
expedition = models . ForeignKey ( Expedition )
person = models . ForeignKey ( Person )
2009-09-11 23:56:47 +01:00
2009-05-13 05:24:37 +01:00
is_guest = models . BooleanField ( default = False )
2009-05-13 05:52:15 +01:00
COMMITTEE_CHOICES = (
( ' leader ' , ' Expo leader ' ) ,
( ' medical ' , ' Expo medical officer ' ) ,
( ' treasurer ' , ' Expo treasurer ' ) ,
( ' sponsorship ' , ' Expo sponsorship coordinator ' ) ,
( ' research ' , ' Expo research coordinator ' ) ,
)
expo_committee_position = models . CharField ( blank = True , null = True , choices = COMMITTEE_CHOICES , max_length = 200 )
2009-05-13 05:24:37 +01:00
nickname = models . CharField ( max_length = 100 , blank = True , null = True )
2009-05-13 05:43:20 +01:00
def GetPersonroles ( self ) :
res = [ ]
2009-08-05 11:58:36 +01:00
for personrole in self . personrole_set . order_by ( ' survexblock ' ) :
if res and res [ - 1 ] [ ' survexpath ' ] == personrole . survexblock . survexpath :
2009-05-13 05:43:20 +01:00
res [ - 1 ] [ ' roles ' ] + = " , " + str ( personrole . role )
else :
2009-08-05 11:58:36 +01:00
res . append ( { ' date ' : personrole . survexblock . date , ' survexpath ' : personrole . survexblock . survexpath , ' roles ' : str ( personrole . role ) } )
2009-05-13 05:43:20 +01:00
return res
2009-05-13 05:44:35 +01:00
class Meta :
2009-09-11 09:04:59 +01:00
ordering = ( ' -expedition ' , )
2009-05-14 06:19:46 +01:00
#order_with_respect_to = 'expedition'
2009-05-13 05:35:29 +01:00
2009-05-13 05:24:37 +01:00
def __unicode__ ( self ) :
return " %s : ( %s ) " % ( self . person , self . expedition )
2009-05-13 05:35:59 +01:00
2009-07-27 13:43:43 +01:00
2009-05-13 05:52:15 +01:00
#why is the below a function in personexpedition, rather than in person? - AC 14 Feb 09
2009-05-13 05:35:59 +01:00
def name ( self ) :
if self . nickname :
return " %s ( %s ) %s " % ( self . person . first_name , self . nickname , self . person . last_name )
if self . person . last_name :
return " %s %s " % ( self . person . first_name , self . person . last_name )
return self . person . first_name
2009-05-13 05:24:37 +01:00
2009-05-13 05:52:15 +01:00
def get_absolute_url ( self ) :
2009-07-27 13:43:43 +01:00
return urlparse . urljoin ( settings . URL_ROOT , reverse ( ' personexpedition ' , kwargs = { ' first_name ' : self . person . first_name , ' last_name ' : self . person . last_name , ' year ' : self . expedition . year } ) )
2009-05-13 05:24:37 +01:00
2009-08-05 11:58:36 +01:00
def surveyedleglength ( self ) :
survexblocks = [ personrole . survexblock for personrole in self . personrole_set . all ( ) ]
return sum ( [ survexblock . totalleglength for survexblock in set ( survexblocks ) ] )
2009-07-27 13:43:43 +01:00
2009-09-11 23:56:47 +01:00
# would prefer to return actual person trips so we could link to first and last ones
def day_min ( self ) :
res = self . persontrip_set . aggregate ( day_min = Min ( " expeditionday__date " ) )
return res [ " day_min " ]
def day_max ( self ) :
res = self . persontrip_set . all ( ) . aggregate ( day_max = Max ( " expeditionday__date " ) )
return res [ " day_max " ]
2009-07-27 13:43:43 +01:00
#
# Single parsed entry from Logbook
#
2009-05-13 05:56:56 +01:00
class LogbookEntry ( TroggleModel ) :
2011-05-01 19:32:41 +01:00
date = models . DateField ( ) #MJG wants to turn this into a datetime such that multiple Logbook entries on the same day can be ordered.
expeditionday = models . ForeignKey ( " ExpeditionDay " , null = True ) #MJG wants to KILL THIS (redundant information)
2009-07-04 08:27:49 +01:00
expedition = models . ForeignKey ( Expedition , blank = True , null = True ) # yes this is double-
2011-05-01 19:32:41 +01:00
#author = models.ForeignKey(PersonExpedition,blank=True,null=True) # the person who writes it up doesn't have to have been on the trip.
2009-06-19 15:38:32 +01:00
# Re: the above- so this field should be "typist" or something, not "author". - AC 15 jun 09
2011-05-01 19:32:41 +01:00
#MJG wants to KILL THIS, as it is typically redundant with PersonTrip.is_logbook_entry_author, in the rare it was not redundanty and of actually interest it could be added to the text.
title = models . CharField ( max_length = settings . MAX_LOGBOOK_ENTRY_TITLE_LENGTH )
2009-05-13 05:48:47 +01:00
cave = models . ForeignKey ( ' Cave ' , blank = True , null = True )
2009-06-09 00:29:00 +01:00
place = models . CharField ( max_length = 100 , blank = True , null = True , help_text = " Only use this if you haven ' t chosen a cave " )
2009-05-13 05:48:47 +01:00
text = models . TextField ( )
2009-05-13 06:10:29 +01:00
slug = models . SlugField ( max_length = 50 )
2011-05-01 19:32:41 +01:00
filename = models . CharField ( max_length = 200 , null = True )
2009-05-13 05:52:15 +01:00
2009-05-13 05:48:47 +01:00
class Meta :
2011-05-01 19:32:41 +01:00
verbose_name_plural = " Logbook Entries "
2009-07-04 08:27:49 +01:00
# several PersonTrips point in to this object
2009-05-13 05:48:47 +01:00
ordering = ( ' -date ' , )
2009-05-13 05:52:15 +01:00
2011-05-01 19:32:41 +01:00
def isLogbookEntry ( self ) : # Function used in templates
return True
2009-05-13 05:52:15 +01:00
def get_absolute_url ( self ) :
2009-06-08 20:16:18 +01:00
return urlparse . urljoin ( settings . URL_ROOT , reverse ( ' logbookentry ' , kwargs = { ' date ' : self . date , ' slug ' : self . slug } ) )
2009-05-13 05:52:15 +01:00
2009-05-13 05:48:47 +01:00
def __unicode__ ( self ) :
return " %s : ( %s ) " % ( self . date , self . title )
2009-05-13 05:52:59 +01:00
def get_next_by_id ( self ) :
2009-05-13 06:15:48 +01:00
LogbookEntry . objects . get ( id = self . id + 1 )
2009-05-13 05:48:47 +01:00
2009-05-13 05:52:59 +01:00
def get_previous_by_id ( self ) :
2009-05-13 06:15:48 +01:00
LogbookEntry . objects . get ( id = self . id - 1 )
2009-05-13 05:52:15 +01:00
2009-07-04 08:27:49 +01:00
def new_QM_number ( self ) :
""" Returns """
if self . cave :
nextQMnumber = self . cave . new_QM_number ( self . date . year )
else :
return none
return nextQMnumber
def new_QM_found_link ( self ) :
""" Produces a link to a new QM with the next number filled in and this LogbookEntry set as ' found by ' """
return settings . URL_ROOT + r ' /admin/core/qm/add/? ' + r ' found_by= ' + str ( self . pk ) + ' &number= ' + str ( self . new_QM_number ( ) )
2009-09-11 23:56:47 +01:00
def DayIndex ( self ) :
return list ( self . expeditionday . logbookentry_set . all ( ) ) . index ( self )
2009-07-27 13:43:43 +01:00
#
# Single Person going on a trip, which may or may not be written up (accounts for different T/U for people in same logbook entry)
#
2009-05-13 05:56:56 +01:00
class PersonTrip ( TroggleModel ) :
2009-09-11 23:56:47 +01:00
personexpedition = models . ForeignKey ( " PersonExpedition " , null = True )
2009-05-13 05:48:47 +01:00
2011-05-01 19:32:41 +01:00
#expeditionday = models.ForeignKey("ExpeditionDay")#MJG wants to KILL THIS (redundant information)
#date = models.DateField() #MJG wants to KILL THIS (redundant information)
2009-06-09 00:29:00 +01:00
time_underground = models . FloatField ( help_text = " In decimal hours " )
2009-05-13 05:48:47 +01:00
logbook_entry = models . ForeignKey ( LogbookEntry )
is_logbook_entry_author = models . BooleanField ( )
2009-07-27 13:43:43 +01:00
# sequencing by person (difficult to solve locally)
2011-05-01 19:32:41 +01:00
#persontrip_next = models.ForeignKey('PersonTrip', related_name='pnext', blank=True,null=True)#MJG wants to KILL THIS (and use funstion persontrip_next_auto)
#persontrip_prev = models.ForeignKey('PersonTrip', related_name='pprev', blank=True,null=True)#MJG wants to KILL THIS(and use funstion persontrip_prev_auto)
2009-07-27 13:43:43 +01:00
2011-05-01 19:32:41 +01:00
def persontrip_next ( self ) :
futurePTs = PersonTrip . objects . filter ( personexpedition = self . personexpedition , logbook_entry__date__gt = self . logbook_entry . date ) . order_by ( ' logbook_entry__date ' ) . all ( )
if len ( futurePTs ) > 0 :
return futurePTs [ 0 ]
else :
return None
def persontrip_prev ( self ) :
pastPTs = PersonTrip . objects . filter ( personexpedition = self . personexpedition , logbook_entry__date__lt = self . logbook_entry . date ) . order_by ( ' -logbook_entry__date ' ) . all ( )
if len ( pastPTs ) > 0 :
return pastPTs [ 0 ]
else :
return None
2009-06-09 00:29:00 +01:00
def place ( self ) :
2009-07-27 13:43:43 +01:00
return self . logbook_entry . cave and self . logbook_entry . cave or self . logbook_entry . place
2009-07-04 08:27:49 +01:00
2009-05-13 05:48:47 +01:00
def __unicode__ ( self ) :
2011-05-01 19:32:41 +01:00
return " %s ( %s ) " % ( self . personexpedition , self . logbook_entry . date )
2009-09-11 23:56:47 +01:00
2009-05-13 05:48:47 +01:00
2009-07-27 13:43:43 +01:00
##########################################
2009-05-13 05:48:47 +01:00
# move following classes into models_cave
2009-07-27 13:43:43 +01:00
##########################################
2009-05-13 05:24:37 +01:00
2009-05-13 05:56:56 +01:00
class Area ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
short_name = models . CharField ( max_length = 100 )
name = models . CharField ( max_length = 200 , blank = True , null = True )
description = models . TextField ( blank = True , null = True )
parent = models . ForeignKey ( ' Area ' , blank = True , null = True )
def __unicode__ ( self ) :
if self . parent :
return unicode ( self . parent ) + u " - " + unicode ( self . short_name )
else :
return unicode ( self . short_name )
def kat_area ( self ) :
if self . short_name in [ " 1623 " , " 1626 " ] :
return self . short_name
elif self . parent :
return self . parent . kat_area ( )
2009-05-13 05:56:56 +01:00
class CaveAndEntrance ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
cave = models . ForeignKey ( ' Cave ' )
entrance = models . ForeignKey ( ' Entrance ' )
entrance_letter = models . CharField ( max_length = 20 , blank = True , null = True )
def __unicode__ ( self ) :
return unicode ( self . cave ) + unicode ( self . entrance_letter )
2009-05-13 05:56:56 +01:00
class Cave ( TroggleModel ) :
2009-05-13 05:46:12 +01:00
# too much here perhaps
2009-05-13 05:24:37 +01:00
official_name = models . CharField ( max_length = 160 )
area = models . ManyToManyField ( Area , blank = True , null = True )
kataster_code = models . CharField ( max_length = 20 , blank = True , null = True )
kataster_number = models . CharField ( max_length = 10 , blank = True , null = True )
unofficial_number = models . CharField ( max_length = 60 , blank = True , null = True )
entrances = models . ManyToManyField ( ' Entrance ' , through = ' CaveAndEntrance ' )
explorers = models . TextField ( blank = True , null = True )
underground_description = models . TextField ( blank = True , null = True )
equipment = models . TextField ( blank = True , null = True )
references = models . TextField ( blank = True , null = True )
survey = models . TextField ( blank = True , null = True )
kataster_status = models . TextField ( blank = True , null = True )
underground_centre_line = models . TextField ( blank = True , null = True )
notes = models . TextField ( blank = True , null = True )
length = models . CharField ( max_length = 100 , blank = True , null = True )
depth = models . CharField ( max_length = 100 , blank = True , null = True )
extent = models . CharField ( max_length = 100 , blank = True , null = True )
2009-06-14 04:33:19 +01:00
survex_file = models . CharField ( max_length = 100 , blank = True , null = True )
description_file = models . CharField ( max_length = 200 , blank = True , null = True )
2009-05-13 05:46:12 +01:00
2011-05-01 19:32:41 +01:00
#class Meta:
# unique_together = (("area", "kataster_number"), ("area", "unofficial_number"))
# FIXME Kataster Areas and CUCC defined sub areas need seperating
2009-05-13 05:54:17 +01:00
#href = models.CharField(max_length=100)
2011-05-01 19:32:41 +01:00
def reference ( self ) :
if self . kataster_number :
return " %s - %s " % ( self . kat_area ( ) , self . kataster_number )
else :
return " %s - %s " % ( self . kat_area ( ) , self . unofficial_number )
2009-05-13 05:46:12 +01:00
2009-05-13 05:52:15 +01:00
def get_absolute_url ( self ) :
2009-05-13 05:46:12 +01:00
if self . kataster_number :
2009-05-13 05:52:15 +01:00
href = self . kataster_number
2009-05-13 05:46:12 +01:00
elif self . unofficial_number :
2009-05-13 05:52:15 +01:00
href = self . unofficial_number
2009-05-13 05:46:12 +01:00
else :
2009-05-13 05:52:15 +01:00
href = official_name . lower ( )
2009-05-13 06:13:31 +01:00
#return settings.URL_ROOT + '/cave/' + href + '/'
2009-06-10 05:37:53 +01:00
return urlparse . urljoin ( settings . URL_ROOT , reverse ( ' cave ' , kwargs = { ' cave_id ' : href , } ) )
2009-05-13 06:13:31 +01:00
2009-05-13 05:24:37 +01:00
def __unicode__ ( self ) :
if self . kataster_number :
if self . kat_area ( ) :
return self . kat_area ( ) + u " : " + self . kataster_number
else :
return unicode ( " l " ) + u " : " + self . kataster_number
else :
if self . kat_area ( ) :
return self . kat_area ( ) + u " : " + self . unofficial_number
else :
return self . unofficial_number
2009-05-13 05:46:12 +01:00
2009-05-13 06:00:16 +01:00
def get_QMs ( self ) :
return QM . objects . filter ( found_by__cave = self )
2009-07-04 08:27:49 +01:00
def new_QM_number ( self , year = datetime . date . today ( ) . year ) :
""" Given a cave and the current year, returns the next QM number. """
try :
res = QM . objects . filter ( found_by__date__year = year , found_by__cave = self ) . order_by ( ' -number ' ) [ 0 ]
except IndexError :
return 1
return res . number + 1
2009-05-13 05:24:37 +01:00
def kat_area ( self ) :
for a in self . area . all ( ) :
if a . kat_area ( ) :
return a . kat_area ( )
2009-06-10 05:37:53 +01:00
2009-05-13 05:24:37 +01:00
def entrances ( self ) :
return CaveAndEntrance . objects . filter ( cave = self )
2009-06-10 05:37:53 +01:00
2009-05-13 05:24:37 +01:00
def entrancelist ( self ) :
rs = [ ]
res = " "
for e in CaveAndEntrance . objects . filter ( cave = self ) :
rs . append ( e . entrance_letter )
rs . sort ( )
prevR = None
n = 0
for r in rs :
if prevR :
if chr ( ord ( prevR ) + 1 ) == r :
prevR = r
n + = 1
else :
if n == 0 :
res + = " , " + prevR
else :
res + = " – " + prevR
else :
prevR = r
n = 0
res + = r
if n == 0 :
res + = " , " + prevR
else :
res + = " – " + prevR
return res
2011-05-01 19:32:41 +01:00
def getCaveByReference ( reference ) :
print reference
areaname , code = reference . split ( " - " , 1 )
area = Area . objects . get ( short_name = areaname )
foundCaves = list ( Cave . objects . filter ( area = area , kataster_number = code ) . all ( ) ) + list ( Cave . objects . filter ( area = area , unofficial_number = code ) . all ( ) )
assert len ( foundCaves ) == 1
return foundCaves [ 0 ]
2009-05-13 05:56:56 +01:00
class OtherCaveName ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
name = models . CharField ( max_length = 160 )
cave = models . ForeignKey ( Cave )
def __unicode__ ( self ) :
return unicode ( self . name )
2009-05-13 05:56:56 +01:00
class SurveyStation ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
name = models . CharField ( max_length = 200 )
def __unicode__ ( self ) :
return unicode ( self . name )
2009-05-13 05:56:56 +01:00
class Entrance ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
name = models . CharField ( max_length = 100 , blank = True , null = True )
entrance_description = models . TextField ( blank = True , null = True )
explorers = models . TextField ( blank = True , null = True )
map_description = models . TextField ( blank = True , null = True )
location_description = models . TextField ( blank = True , null = True )
approach = models . TextField ( blank = True , null = True )
underground_description = models . TextField ( blank = True , null = True )
photo = models . TextField ( blank = True , null = True )
MARKING_CHOICES = (
( ' P ' , ' Paint ' ) ,
( ' P? ' , ' Paint (?) ' ) ,
( ' T ' , ' Tag ' ) ,
( ' T? ' , ' Tag (?) ' ) ,
( ' R ' , ' Retagged ' ) ,
( ' S ' , ' Spit ' ) ,
( ' S? ' , ' Spit (?) ' ) ,
( ' U ' , ' Unmarked ' ) ,
( ' ? ' , ' Unknown ' ) )
marking = models . CharField ( max_length = 2 , choices = MARKING_CHOICES )
marking_comment = models . TextField ( blank = True , null = True )
FINDABLE_CHOICES = (
( ' ? ' , ' To be confirmed ... ' ) ,
( ' S ' , ' Surveyed ' ) ,
( ' L ' , ' Lost ' ) ,
( ' R ' , ' Refindable ' ) )
findability = models . CharField ( max_length = 1 , choices = FINDABLE_CHOICES , blank = True , null = True )
findability_description = models . TextField ( blank = True , null = True )
alt = models . TextField ( blank = True , null = True )
northing = models . TextField ( blank = True , null = True )
easting = models . TextField ( blank = True , null = True )
tag_station = models . ForeignKey ( SurveyStation , blank = True , null = True , related_name = " tag_station " )
exact_station = models . ForeignKey ( SurveyStation , blank = True , null = True , related_name = " exact_station " )
other_station = models . ForeignKey ( SurveyStation , blank = True , null = True , related_name = " other_station " )
other_description = models . TextField ( blank = True , null = True )
bearings = models . TextField ( blank = True , null = True )
def __unicode__ ( self ) :
a = CaveAndEntrance . objects . filter ( entrance = self )
name = u ' '
if self . name :
name = unicode ( self . name ) + u ' '
if len ( a ) == 1 :
return name + unicode ( a [ 0 ] )
return name + unicode ( a )
def marking_val ( self ) :
for m in self . MARKING_CHOICES :
if m [ 0 ] == self . marking :
return m [ 1 ]
def findability_val ( self ) :
for f in self . FINDABLE_CHOICES :
if f [ 0 ] == self . findability :
return f [ 1 ]
2009-05-13 05:45:26 +01:00
2009-05-22 01:50:16 +01:00
def get_absolute_url ( self ) :
ancestor_titles = ' / ' . join ( [ subcave . title for subcave in self . get_ancestors ( ) ] )
if ancestor_titles :
res = ' / ' . join ( ( self . get_root ( ) . cave . get_absolute_url ( ) , ancestor_titles , self . title ) )
else :
res = ' / ' . join ( ( self . get_root ( ) . cave . get_absolute_url ( ) , self . title ) )
return res
2009-07-04 16:42:17 +01:00
2009-07-04 00:28:28 +01:00
class CaveDescription ( TroggleModel ) :
2009-07-04 16:42:17 +01:00
short_name = models . CharField ( max_length = 50 , unique = True )
long_name = models . CharField ( max_length = 200 , blank = True , null = True )
2009-07-04 00:28:28 +01:00
description = models . TextField ( blank = True , null = True )
2009-07-09 05:08:21 +01:00
linked_subcaves = models . ManyToManyField ( " NewSubCave " , blank = True , null = True )
linked_entrances = models . ManyToManyField ( " Entrance " , blank = True , null = True )
linked_qms = models . ManyToManyField ( " QM " , blank = True , null = True )
2009-07-04 00:28:28 +01:00
def __unicode__ ( self ) :
2009-07-09 05:08:21 +01:00
if self . long_name :
return unicode ( self . long_name )
else :
return unicode ( self . short_name )
def get_absolute_url ( self ) :
return urlparse . urljoin ( settings . URL_ROOT , reverse ( ' cavedescription ' , args = ( self . short_name , ) ) )
2009-07-11 01:36:00 +01:00
def save ( self ) :
2009-07-12 06:30:24 +01:00
"""
Overridden save method which stores wikilinks in text as links in database .
"""
2009-07-11 01:36:00 +01:00
super ( CaveDescription , self ) . save ( )
qm_list = get_related_by_wikilinks ( self . description )
for qm in qm_list :
self . linked_qms . add ( qm )
super ( CaveDescription , self ) . save ( )
2009-07-04 00:28:28 +01:00
class NewSubCave ( TroggleModel ) :
2009-07-04 16:42:17 +01:00
name = models . CharField ( max_length = 200 , unique = True )
2009-07-04 00:28:28 +01:00
def __unicode__ ( self ) :
return unicode ( self . name )
2009-05-13 05:24:37 +01:00
2009-05-13 05:56:56 +01:00
class QM ( TroggleModel ) :
2009-05-13 05:24:37 +01:00
#based on qm.csv in trunk/expoweb/smkridge/204 which has the fields:
#"Number","Grade","Area","Description","Page reference","Nearest station","Completion description","Comment"
found_by = models . ForeignKey ( LogbookEntry , related_name = ' QMs_found ' , blank = True , null = True )
ticked_off_by = models . ForeignKey ( LogbookEntry , related_name = ' QMs_ticked_off ' , null = True , blank = True )
2009-07-04 08:27:49 +01:00
number = models . IntegerField ( help_text = " this is the sequential number in the year " , )
2009-05-13 05:24:37 +01:00
GRADE_CHOICES = (
( ' A ' , ' A: Large obvious lead ' ) ,
( ' B ' , ' B: Average lead ' ) ,
( ' C ' , ' C: Tight unpromising lead ' ) ,
( ' D ' , ' D: Dig ' ) ,
( ' X ' , ' X: Unclimbable aven ' )
)
grade = models . CharField ( max_length = 1 , choices = GRADE_CHOICES )
location_description = models . TextField ( blank = True )
#should be a foreignkey to surveystation
nearest_station_description = models . CharField ( max_length = 400 , null = True , blank = True )
2009-05-13 06:15:48 +01:00
nearest_station = models . CharField ( max_length = 200 , blank = True , null = True )
2009-05-13 05:24:37 +01:00
area = models . CharField ( max_length = 100 , blank = True , null = True )
completion_description = models . TextField ( blank = True , null = True )
comment = models . TextField ( blank = True , null = True )
2009-07-04 08:27:49 +01:00
2009-06-10 00:05:02 +01:00
def __unicode__ ( self ) :
2009-07-04 19:35:06 +01:00
return u " %s %s " % ( self . code ( ) , self . grade )
def code ( self ) :
2009-07-06 08:31:24 +01:00
return u " %s - %s - %s " % ( unicode ( self . found_by . cave ) [ 6 : ] , self . found_by . date . year , self . number )
2009-05-13 05:53:37 +01:00
2009-05-13 06:00:16 +01:00
def get_absolute_url ( self ) :
2009-05-13 06:13:31 +01:00
#return settings.URL_ROOT + '/cave/' + self.found_by.cave.kataster_number + '/' + str(self.found_by.date.year) + '-' + '%02d' %self.number
2009-06-08 20:16:18 +01:00
return urlparse . urljoin ( settings . URL_ROOT , reverse ( ' qm ' , kwargs = { ' cave_id ' : self . found_by . cave . kataster_number , ' year ' : self . found_by . date . year , ' qm_id ' : self . number , ' grade ' : self . grade } ) )
2009-05-13 06:00:16 +01:00
def get_next_by_id ( self ) :
return QM . objects . get ( id = self . id + 1 )
def get_previous_by_id ( self ) :
return QM . objects . get ( id = self . id - 1 )
2009-05-13 06:13:31 +01:00
def wiki_link ( self ) :
2009-07-06 08:31:24 +01:00
return u " %s %s %s " % ( ' [[QM: ' , self . code ( ) , ' ]] ' )
2009-05-13 06:13:31 +01:00
2009-05-13 06:00:51 +01:00
photoFileStorage = FileSystemStorage ( location = settings . PHOTOS_ROOT , base_url = settings . PHOTOS_URL )
2009-08-23 23:29:05 +01:00
class DPhoto ( TroggleImageModel ) :
2009-05-13 05:25:17 +01:00
caption = models . CharField ( max_length = 1000 , blank = True , null = True )
2009-05-13 06:13:31 +01:00
contains_logbookentry = models . ForeignKey ( LogbookEntry , blank = True , null = True )
2009-05-13 05:25:17 +01:00
contains_person = models . ManyToManyField ( Person , blank = True , null = True )
2009-05-13 05:53:37 +01:00
file = models . ImageField ( storage = photoFileStorage , upload_to = ' . ' , )
2009-05-13 05:25:17 +01:00
is_mugshot = models . BooleanField ( default = False )
contains_cave = models . ForeignKey ( Cave , blank = True , null = True )
contains_entrance = models . ForeignKey ( Entrance , related_name = " photo_file " , blank = True , null = True )
nearest_survey_point = models . ForeignKey ( SurveyStation , blank = True , null = True )
nearest_QM = models . ForeignKey ( QM , blank = True , null = True )
lon_utm = models . FloatField ( blank = True , null = True )
lat_utm = models . FloatField ( blank = True , null = True )
2009-05-13 06:23:57 +01:00
class IKOptions :
2009-07-02 20:43:18 +01:00
spec_module = ' core.imagekit_specs '
2009-05-13 06:23:57 +01:00
cache_dir = ' thumbs '
image_field = ' file '
2009-05-13 05:25:17 +01:00
#content_type = models.ForeignKey(ContentType)
#object_id = models.PositiveIntegerField()
#location = generic.GenericForeignKey('content_type', 'object_id')
2009-06-10 00:05:02 +01:00
def __unicode__ ( self ) :
2009-05-13 05:25:17 +01:00
return self . caption
2009-05-13 05:25:39 +01:00
2009-05-13 06:22:53 +01:00
scansFileStorage = FileSystemStorage ( location = settings . SURVEY_SCANS , base_url = settings . SURVEYS_URL )
2009-05-13 05:27:43 +01:00
def get_scan_path ( instance , filename ) :
2009-05-13 05:31:21 +01:00
year = instance . survey . expedition . year
2009-05-13 05:38:37 +01:00
print " WN: " , type ( instance . survey . wallet_number ) , instance . survey . wallet_number
2009-05-13 05:31:21 +01:00
number = " %02d " % instance . survey . wallet_number + str ( instance . survey . wallet_letter ) #using %02d string formatting because convention was 2009#01
2009-05-13 05:27:43 +01:00
return os . path . join ( ' ./ ' , year , year + r ' # ' + number , instance . contents + str ( instance . number_in_wallet ) + r ' .jpg ' )
2009-07-04 08:27:49 +01:00
class ScannedImage ( TroggleImageModel ) :
2009-05-13 05:27:43 +01:00
file = models . ImageField ( storage = scansFileStorage , upload_to = get_scan_path )
2009-05-13 05:29:31 +01:00
scanned_by = models . ForeignKey ( Person , blank = True , null = True )
scanned_on = models . DateField ( null = True )
2009-05-13 05:27:43 +01:00
survey = models . ForeignKey ( ' Survey ' )
contents = models . CharField ( max_length = 20 , choices = ( ( ' notes ' , ' notes ' ) , ( ' plan ' , ' plan_sketch ' ) , ( ' elevation ' , ' elevation_sketch ' ) ) )
number_in_wallet = models . IntegerField ( null = True )
lon_utm = models . FloatField ( blank = True , null = True )
lat_utm = models . FloatField ( blank = True , null = True )
2009-05-13 06:23:57 +01:00
class IKOptions :
2009-07-02 20:43:18 +01:00
spec_module = ' core.imagekit_specs '
2009-05-13 06:23:57 +01:00
cache_dir = ' thumbs '
image_field = ' file '
2009-05-13 05:27:43 +01:00
#content_type = models.ForeignKey(ContentType)
#object_id = models.PositiveIntegerField()
#location = generic.GenericForeignKey('content_type', 'object_id')
2009-05-13 05:28:57 +01:00
#This is an ugly hack to deal with the #s in our survey scan paths. The correct thing is to write a custom file storage backend which calls urlencode on the name for making file.url but not file.path.
2009-05-13 05:27:43 +01:00
def correctURL ( self ) :
2009-05-13 05:28:57 +01:00
return string . replace ( self . file . url , r ' # ' , r ' % 23 ' )
2009-05-13 05:27:43 +01:00
2009-06-10 00:05:02 +01:00
def __unicode__ ( self ) :
2009-05-13 05:27:43 +01:00
return get_scan_path ( self , ' ' )
2009-05-13 05:56:56 +01:00
class Survey ( TroggleModel ) :
2009-05-21 19:47:19 +01:00
expedition = models . ForeignKey ( ' Expedition ' ) #REDUNDANT (logbook_entry)
2009-05-13 05:27:43 +01:00
wallet_number = models . IntegerField ( blank = True , null = True )
wallet_letter = models . CharField ( max_length = 1 , blank = True , null = True )
comments = models . TextField ( blank = True , null = True )
2009-05-21 19:47:19 +01:00
location = models . CharField ( max_length = 400 , blank = True , null = True ) #REDUNDANT
2009-07-04 19:26:51 +01:00
subcave = models . ForeignKey ( ' NewSubCave ' , blank = True , null = True )
2009-05-13 05:27:43 +01:00
#notes_scan = models.ForeignKey('ScannedImage',related_name='notes_scan',blank=True, null=True) #Replaced by contents field of ScannedImage model
2009-05-21 19:47:19 +01:00
survex_block = models . OneToOneField ( ' SurvexBlock ' , blank = True , null = True )
logbook_entry = models . ForeignKey ( ' LogbookEntry ' )
2009-05-13 05:27:43 +01:00
centreline_printed_on = models . DateField ( blank = True , null = True )
centreline_printed_by = models . ForeignKey ( ' Person ' , related_name = ' centreline_printed_by ' , blank = True , null = True )
#sketch_scan = models.ForeignKey(ScannedImage,blank=True, null=True) #Replaced by contents field of ScannedImage model
tunnel_file = models . FileField ( upload_to = ' surveyXMLfiles ' , blank = True , null = True )
tunnel_main_sketch = models . ForeignKey ( ' Survey ' , blank = True , null = True )
integrated_into_main_sketch_on = models . DateField ( blank = True , null = True )
integrated_into_main_sketch_by = models . ForeignKey ( ' Person ' , related_name = ' integrated_into_main_sketch_by ' , blank = True , null = True )
rendered_image = models . ImageField ( upload_to = ' renderedSurveys ' , blank = True , null = True )
2009-06-10 00:05:02 +01:00
def __unicode__ ( self ) :
2009-05-21 19:47:19 +01:00
return self . expedition . year + " # " + " %02d " % int ( self . wallet_number )
2009-05-13 05:31:21 +01:00
def notes ( self ) :
return self . scannedimage_set . filter ( contents = ' notes ' )
def plans ( self ) :
return self . scannedimage_set . filter ( contents = ' plan ' )
def elevations ( self ) :
2009-05-13 05:46:12 +01:00
return self . scannedimage_set . filter ( contents = ' elevation ' )