2009-05-13 05:13:38 +01:00
# -*- coding: utf-8 -*-
2009-07-02 20:43:18 +01:00
import troggle . core . models as models
2009-05-13 05:52:59 +01:00
from django . conf import settings
2009-05-22 06:17:24 +01:00
import csv , time , re , os , logging
2009-07-03 05:31:49 +01:00
from utils import save_carefully
2011-07-11 00:15:59 +01:00
from django . core . urlresolvers import reverse
import flatpages . models
2009-05-13 06:15:48 +01:00
2009-05-13 05:13:38 +01:00
##format of CAVETAB2.CSV is
KatasterNumber = 0
KatStatusCode = 1
Entrances = 2
UnofficialNumber = 3
MultipleEntrances = 4
AutogenFile = 5
LinkFile = 6
LinkEntrance = 7
Name = 8
UnofficialName = 9
Comment = 10
Area = 11
Explorers = 12
UndergroundDescription = 13
Equipment = 14
QMList = 15
KatasterStatus = 16
References = 17
UndergroundCentreLine = 18
UndergroundDrawnSurvey = 19
SurvexFile = 20
Length = 21
Depth = 22
Extent = 23
Notes = 24
EntranceName = 25
TagPoint = 26
OtherPoint = 27
DescriptionOfOtherPoint = 28
ExactEntrance = 29
TypeOfFix = 30
GPSpreSA = 31
GPSpostSA = 32
Northing = 33
Easting = 34
Altitude = 35
Bearings = 36
Map = 37
Location = 38
Approach = 39
EntranceDescription = 40
PhotoOfLocation = 41
Marking = 42
MarkingComment = 43
Findability = 44
FindabilityComment = 45
2009-05-22 06:17:24 +01:00
def LoadCaveTab ( ) :
2011-07-11 00:15:59 +01:00
2009-05-19 06:32:42 +01:00
cavetab = open ( os . path . join ( settings . EXPOWEB , " noinfo " , " CAVETAB2.CSV " ) , ' rU ' )
2009-05-13 05:46:12 +01:00
caveReader = csv . reader ( cavetab )
caveReader . next ( ) # Strip out column headers
2009-05-13 06:15:48 +01:00
2009-05-22 06:17:24 +01:00
logging . info ( " Beginning to import caves from " + str ( cavetab ) + " \n " + " - " * 60 + " \n " )
2009-05-13 06:15:48 +01:00
2009-05-13 05:46:12 +01:00
for katArea in [ ' 1623 ' , ' 1626 ' ] :
if not models . Area . objects . filter ( short_name = katArea ) :
newArea = models . Area ( short_name = katArea )
newArea . save ( )
2009-05-22 06:17:24 +01:00
logging . info ( " Added area " + str ( newArea . short_name ) + " \n " )
2009-05-13 05:46:12 +01:00
area1626 = models . Area . objects . filter ( short_name = ' 1626 ' ) [ 0 ]
area1623 = models . Area . objects . filter ( short_name = ' 1623 ' ) [ 0 ]
counter = 0
for line in caveReader :
if line [ Area ] == ' nonexistent ' :
continue
entranceLetters = [ ] #Used in caves that have mulitlple entrances, which are not described on seperate lines
2009-05-13 06:15:48 +01:00
if line [ MultipleEntrances ] == ' yes ' or line [ MultipleEntrances ] == ' ' : #When true, this line contains an actual cave, otherwise it is an extra entrance.
2009-05-13 05:46:12 +01:00
args = { }
2009-05-13 06:15:48 +01:00
defaultArgs = { }
2011-07-11 00:15:59 +01:00
2009-05-13 05:46:12 +01:00
def addToArgs ( CSVname , modelName ) :
if line [ CSVname ] :
2011-07-11 00:15:59 +01:00
args [ modelName ] = line [ CSVname ]
2009-05-13 06:15:48 +01:00
def addToDefaultArgs ( CSVname , modelName ) : #This has to do with the non-destructive import. These arguments will be passed as the "default" dictionary in a get_or_create
if line [ CSVname ] :
2011-07-11 00:15:59 +01:00
defaultArgs [ modelName ] = line [ CSVname ]
2009-05-13 06:15:48 +01:00
# The attributes added using "addToArgs" will be used to look up an existing cave. Those added using "addToDefaultArgs" will not.
2009-05-13 05:46:12 +01:00
addToArgs ( KatasterNumber , " kataster_number " )
2009-05-13 06:15:48 +01:00
addToDefaultArgs ( KatStatusCode , " kataster_code " )
2009-05-13 05:46:12 +01:00
addToArgs ( UnofficialNumber , " unofficial_number " )
addToArgs ( Name , " official_name " )
2009-05-13 06:15:48 +01:00
addToDefaultArgs ( Comment , " notes " )
addToDefaultArgs ( Explorers , " explorers " )
addToDefaultArgs ( UndergroundDescription , " underground_description " )
addToDefaultArgs ( Equipment , " equipment " )
addToDefaultArgs ( KatasterStatus , " kataster_status " )
addToDefaultArgs ( References , " references " )
addToDefaultArgs ( UndergroundCentreLine , " underground_centre_line " )
addToDefaultArgs ( UndergroundDrawnSurvey , " survey " )
addToDefaultArgs ( Length , " length " )
addToDefaultArgs ( Depth , " depth " )
addToDefaultArgs ( Extent , " extent " )
addToDefaultArgs ( SurvexFile , " survex_file " )
addToDefaultArgs ( Notes , " notes " )
2011-07-11 00:15:59 +01:00
addToDefaultArgs ( AutogenFile , " url " )
2011-06-02 19:16:16 +01:00
if line [ Area ] == " 1626 " :
if line [ KatasterNumber ] != " " :
args [ " slug " ] = line [ Area ] + " - " + line [ KatasterNumber ]
else :
args [ " slug " ] = line [ Area ] + " - " + line [ UnofficialNumber ]
else :
if line [ KatasterNumber ] != " " :
args [ " slug " ] = " 1623 " + " - " + line [ KatasterNumber ]
else :
args [ " slug " ] = " 1623 " + " - " + line [ UnofficialNumber ]
2009-06-14 04:33:19 +01:00
#The following adds the legacy_file_path. This is always in either Autogen file or Link file
for header in ( AutogenFile , LinkFile ) :
if line [ header ] :
addToDefaultArgs ( header , " description_file " )
break
2009-05-13 05:13:38 +01:00
2009-06-10 17:47:05 +01:00
#The following checks if this cave is non-public i.e. we don't have rights to display it online.
#Noinfo was the name of the old password protected directory, so if it has that then we will
#set the non_public field of the model instance to true.
defaultArgs [ " non_public " ] = line [ AutogenFile ] . startswith ( ' noinfo ' ) or line [ LinkFile ] . startswith ( ' noinfo ' )
2009-05-13 06:15:48 +01:00
newCave , created = save_carefully ( models . Cave , lookupAttribs = args , nonLookupAttribs = defaultArgs )
2009-05-22 06:17:24 +01:00
logging . info ( " Added cave " + str ( newCave ) + " \n " )
2009-05-13 06:15:48 +01:00
#If we created a new cave, add the area to it. This does mean that if a cave's identifying features have not changed, areas will not be updated from csv.
if created and line [ Area ] :
2009-05-13 05:46:12 +01:00
if line [ Area ] == " 1626 " :
newCave . area . add ( area1626 )
2009-05-13 05:13:38 +01:00
else :
2009-05-13 05:46:12 +01:00
area = models . Area . objects . filter ( short_name = line [ Area ] )
if area :
newArea = area [ 0 ]
else :
newArea = models . Area ( short_name = line [ Area ] , parent = area1623 )
newArea . save ( )
newCave . area . add ( newArea )
2011-07-11 00:15:59 +01:00
newCave . area . add ( area1623 )
2009-05-13 06:15:48 +01:00
elif created :
2009-05-13 05:46:12 +01:00
newCave . area . add ( area1623 )
2009-05-13 06:15:48 +01:00
2009-05-13 05:46:12 +01:00
newCave . save ( )
2009-05-22 06:17:24 +01:00
logging . info ( " Added area " + line [ Area ] + " to cave " + str ( newCave ) + " \n " )
2009-05-13 05:13:38 +01:00
2009-05-13 06:15:48 +01:00
if created and line [ UnofficialName ] :
newUnofficialName = models . OtherCaveName ( cave = newCave , name = line [ UnofficialName ] )
newUnofficialName . save ( )
2009-07-04 17:08:48 +01:00
2009-05-22 06:17:24 +01:00
logging . info ( " Added unofficial name " + str ( newUnofficialName ) + " to cave " + str ( newCave ) + " \n " )
2011-07-11 00:15:59 +01:00
2009-05-13 06:15:48 +01:00
if created and line [ MultipleEntrances ] == ' ' or \
2009-05-13 05:46:12 +01:00
line [ MultipleEntrances ] == ' entrance ' or \
line [ MultipleEntrances ] == ' last entrance ' :
args = { }
2011-06-02 19:16:16 +01:00
if line [ Entrances ] :
entrance_letter = line [ Entrances ]
else :
entrance_letter = ' '
2009-05-13 05:46:12 +01:00
def addToArgs ( CSVname , modelName ) :
if line [ CSVname ] :
2011-07-11 00:15:59 +01:00
args [ modelName ] = line [ CSVname ]
2009-05-13 05:46:12 +01:00
def addToArgsViaDict ( CSVname , modelName , dictionary ) :
if line [ CSVname ] :
2011-07-11 00:15:59 +01:00
args [ modelName ] = dictionary [ line [ CSVname ] ]
2009-05-13 05:46:12 +01:00
addToArgs ( EntranceName , ' name ' )
addToArgs ( Explorers , ' explorers ' )
addToArgs ( Map , ' map_description ' )
addToArgs ( Location , ' location_description ' )
addToArgs ( Approach , ' approach ' )
addToArgs ( EntranceDescription , ' entrance_description ' )
addToArgs ( UndergroundDescription , ' underground_description ' )
addToArgs ( PhotoOfLocation , ' photo ' )
addToArgsViaDict ( Marking , ' marking ' , { " Paint " : " P " ,
" Paint (?) " : " P? " ,
" Tag " : " T " ,
" Tag (?) " : " T? " ,
" Retagged " : " R " ,
" Retag " : " R " ,
" Spit " : " S " ,
" Spit (?) " : " S? " ,
" Unmarked " : " U " ,
" " : " ? " ,
} )
2011-07-11 00:15:59 +01:00
2009-05-13 05:46:12 +01:00
addToArgs ( MarkingComment , ' marking_comment ' )
addToArgsViaDict ( Findability , ' findability ' , { " Surveyed " : " S " ,
" Lost " : " L " ,
" Refindable " : " R " ,
" " : " ? " ,
" ? " : " ? " ,
} )
addToArgs ( FindabilityComment , ' findability_description ' )
addToArgs ( Easting , ' easting ' )
addToArgs ( Northing , ' northing ' )
addToArgs ( Altitude , ' alt ' )
addToArgs ( DescriptionOfOtherPoint , ' other_description ' )
2011-07-11 00:15:59 +01:00
addToArgs ( TagPoint , ' tag_station ' )
addToArgs ( ExactEntrance , ' exact_station ' )
addToArgs ( OtherPoint , ' other_station ' )
2009-05-13 05:46:12 +01:00
addToArgs ( OtherPoint , ' other_description ' )
if line [ GPSpreSA ] :
2011-07-11 00:15:59 +01:00
addToArgs ( GPSpreSA , ' other_station ' )
2009-05-13 05:46:12 +01:00
args [ ' other_description ' ] = ' pre selective availability GPS '
if line [ GPSpostSA ] :
2011-07-11 00:15:59 +01:00
addToArgs ( GPSpostSA , ' other_station ' )
2009-05-13 05:46:12 +01:00
args [ ' other_description ' ] = ' post selective availability GPS '
addToArgs ( Bearings , ' bearings ' )
2011-06-02 19:16:16 +01:00
args [ ' slug ' ] = newCave . slug + entrance_letter
2009-05-13 05:46:12 +01:00
newEntrance = models . Entrance ( * * args )
newEntrance . save ( )
2009-07-04 17:08:48 +01:00
logging . info ( " Added entrance " + str ( newEntrance ) + " \n " )
2011-06-02 19:16:16 +01:00
2009-05-13 05:46:12 +01:00
newCaveAndEntrance = models . CaveAndEntrance ( cave = newCave , entrance = newEntrance , entrance_letter = entrance_letter )
newCaveAndEntrance . save ( )
2009-05-22 06:17:24 +01:00
logging . info ( " Added CaveAndEntrance " + str ( newCaveAndEntrance ) + " \n " )
2011-07-11 00:15:59 +01:00
f = flatpages . models . EntranceRedirect ( originalURL = line [ AutogenFile ] , entrance = newEntrance )
f . save ( )
2009-05-13 05:43:20 +01:00
# lookup function modelled on GetPersonExpeditionNameLookup
Gcavelookup = None
def GetCaveLookup ( ) :
global Gcavelookup
if Gcavelookup :
return Gcavelookup
Gcavelookup = { " NONEPLACEHOLDER " : None }
for cave in models . Cave . objects . all ( ) :
Gcavelookup [ cave . official_name . lower ( ) ] = cave
if cave . kataster_number :
Gcavelookup [ cave . kataster_number ] = cave
if cave . unofficial_number :
Gcavelookup [ cave . unofficial_number ] = cave
2009-05-13 05:46:12 +01:00
Gcavelookup [ " tunnocks " ] = Gcavelookup [ " 258 " ]
Gcavelookup [ " hauchhole " ] = Gcavelookup [ " 234 " ]
2009-05-13 05:43:20 +01:00
return Gcavelookup
2009-05-13 05:49:50 +01:00