2009-05-13 05:24:37 +01:00
import csv
import settings
from expo . models import QM , LogbookEntry , Cave
from datetime import *
import re
#sorry that the below code is ugly. I'll fix it sometime, really! - AC
QM . objects . all ( ) . delete ( )
2009-05-13 05:25:17 +01:00
def parseCaveQMs ( cave , pathToCSV ) :
if cave == ' stein ' :
try :
steinBr = Cave . objects . get ( official_name = " Steinbrückenhöhle " )
except Cave . DoesNotExist :
print " Steinbruckenhoehle is not in the database. Please run parsers.cavetab first. "
return
elif cave == ' hauch ' :
try :
hauchHl = Cave . objects . get ( official_name = " Hauchhöhle " )
except Cave . DoesNotExist :
print " Steinbruckenhoehle is not in the database. Please run parsers.cavetab first. "
return
2009-05-13 05:24:37 +01:00
2009-05-13 05:25:17 +01:00
qmPath = settings . EXPOWEB + pathToCSV
qmCSVContents = open ( qmPath , ' r ' )
dialect = csv . Sniffer ( ) . sniff ( qmCSVContents . read ( ) )
qmCSVContents . seek ( 0 , 0 )
qmReader = csv . reader ( qmCSVContents , dialect = dialect )
2009-05-13 05:24:37 +01:00
qmReader . next ( ) # Skip header row
for line in qmReader :
2009-05-13 05:25:17 +01:00
try :
year = int ( line [ 0 ] [ 1 : 5 ] )
#check if placeholder exists for given year, create it if not
if cave == ' stein ' :
2009-05-13 06:08:04 +01:00
placeholder , hadToCreate = LogbookEntry . objects . get_or_create ( date__year = year , title = " placeholder for QMs in 204 " , text = " QMs temporarily attached to this should be re-attached to their actual trips " , defaults = { " date " : date ( year , 1 , 1 ) , " cave " : steinBr } )
2009-05-13 05:25:17 +01:00
elif cave == ' hauch ' :
2009-05-13 06:08:04 +01:00
placeholder , hadToCreate = LogbookEntry . objects . get_or_create ( date__year = year , title = " placeholder for QMs in 234 " , text = " QMs temporarily attached to this should be re-attached to their actual trips " , defaults = { " date " : date ( year , 1 , 1 ) , " cave " : hauchHl } )
2009-05-13 05:25:17 +01:00
if hadToCreate :
print cave + " placeholder logbook entry for " + str ( year ) + " added to database "
QMnum = re . match ( r " .*?- \ d*?-X?(?P<numb> \ d*) " , line [ 0 ] ) . group ( " numb " )
newQM = QM ( )
newQM . found_by = placeholder
newQM . number = QMnum
if line [ 1 ] == " Dig " :
newQM . grade = " D "
else :
newQM . grade = line [ 1 ]
newQM . area = line [ 2 ]
newQM . location_description = line [ 3 ]
2009-05-13 05:59:40 +01:00
newQM . completion_description = line [ 4 ]
newQM . nearest_station_description = line [ 5 ]
if newQM . completion_description : # Troggle checks if QMs are completed by checking if they have a ticked_off_by trip. In the table, completion is indicated by the presence of a completion discription.
newQM . ticked_off_by = placeholder
2009-05-13 05:25:17 +01:00
newQM . comment = line [ 6 ]
newQM . save ( )
2009-05-13 05:52:59 +01:00
print " QM " + str ( newQM ) + ' added to database \r ' ,
2009-05-13 05:25:17 +01:00
except KeyError :
continue
# except IndexError:
# print "Index error in " + str(line)
# continue
2009-05-13 05:24:37 +01:00
2009-05-13 05:25:17 +01:00
parseCaveQMs ( cave = ' stein ' , pathToCSV = r " smkridge/204/qm.csv " )
parseCaveQMs ( cave = ' hauch ' , pathToCSV = r " smkridge/234/qm.csv " )