2009-05-13 06:15:48 +01:00
# -*- coding: UTF-8 -*-
2009-05-13 05:24:37 +01:00
import csv
2009-05-17 04:31:23 +01:00
from django . conf import settings
2009-07-02 20:43:18 +01:00
from core . models import QM , LogbookEntry , Cave
2009-05-13 05:24:37 +01:00
from datetime import *
2009-07-03 05:31:49 +01:00
from utils import save_carefully
2009-05-17 04:31:23 +01:00
import re , os
2009-05-13 05:24:37 +01:00
2009-05-13 06:15:48 +01:00
def deleteQMs ( ) :
QM . objects . all ( ) . delete ( )
2009-05-13 05:24:37 +01:00
2009-05-13 06:15:48 +01:00
def parseCaveQMs ( cave , inputFile ) :
""" Runs through the CSV file at inputFile (which is a relative path from expoweb) and saves each QM as a QM instance. """
2009-05-13 05:24:37 +01:00
2009-05-13 05:25:17 +01:00
if cave == ' stein ' :
try :
2009-05-13 06:15:48 +01:00
steinBr = Cave . objects . get ( official_name = " Steinbrückenhöhle " )
2009-05-13 05:25:17 +01:00
except Cave . DoesNotExist :
print " Steinbruckenhoehle is not in the database. Please run parsers.cavetab first. "
return
elif cave == ' hauch ' :
try :
2009-05-13 06:15:48 +01:00
hauchHl = Cave . objects . get ( official_name = " Hauchhöhle " )
except Cave . DoesNotExist :
2009-05-17 04:31:23 +01:00
print " Hauchhoele is not in the database. Please run parsers.cavetab first. "
2009-05-13 06:15:48 +01:00
return
elif cave == ' kh ' :
try :
kh = Cave . objects . get ( official_name = " Kaninchenhöhle " )
2009-05-13 05:25:17 +01:00
except Cave . DoesNotExist :
2009-05-17 04:31:23 +01:00
print " KH is not in the database. Please run parsers.cavetab first. "
parse_KH_QMs ( kh , inputFile = inputFile )
2009-05-13 06:15:48 +01:00
return
qmPath = settings . EXPOWEB + inputFile
2009-05-19 06:32:42 +01:00
qmCSVContents = open ( qmPath , ' rU ' )
2009-05-13 05:25:17 +01:00
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 ]
2009-05-13 06:15:48 +01:00
try :
preexistingQM = QM . objects . get ( number = QMnum , found_by__date__year = year ) #if we don't have this one in the DB, save it
if preexistingQM . new_since_parsing == False : #if the pre-existing QM has not been modified, overwrite it
preexistingQM . delete ( )
newQM . save ( )
print " overwriting " + str ( preexistingQM ) + " \r " ,
else : # otherwise, print that it was ignored
print " preserving " + str ( preexistingQM ) + " , which was edited in admin \r " ,
except QM . DoesNotExist : #if there is no pre-existing QM, save the new one
newQM . save ( )
print " QM " + str ( newQM ) + ' added to database \r ' ,
except KeyError : #check on this one
2009-05-13 05:25:17 +01:00
continue
2009-07-02 04:10:51 +01:00
except IndexError :
print " Index error in " + str ( line )
continue
2009-05-13 05:24:37 +01:00
2009-05-13 06:15:48 +01:00
def parse_KH_QMs ( kh , inputFile ) :
""" import QMs from the 1623-161 (Kaninchenh<6E> hle) html pages
"""
khQMs = open ( settings . EXPOWEB + inputFile , ' r ' )
khQMs = khQMs . readlines ( )
for line in khQMs :
res = re . search ( ' name= \" [CB](?P<year> \ d*)-(?P<cave> \ d*)-(?P<number> \ d*).*</a> (?P<grade>[ABDCV])<dd>(?P<description>.*) \ [(?P<nearest_station>.*) \ ] ' , line )
if res :
res = res . groupdict ( )
year = int ( res [ ' year ' ] )
#check if placeholder exists for given year, create it if not
placeholder , hadToCreate = LogbookEntry . objects . get_or_create ( date__year = year , title = " placeholder for QMs in 161 " , text = " QMs temporarily attached to this should be re-attached to their actual trips " , defaults = { " date " : date ( ( year ) , 1 , 1 ) , " cave " : kh } )
lookupArgs = {
' found_by ' : placeholder ,
' number ' : res [ ' number ' ]
}
nonLookupArgs = {
' grade ' : res [ ' grade ' ] ,
' nearest_station ' : res [ ' nearest_station ' ] ,
' location_description ' : res [ ' description ' ]
}
2009-05-17 04:31:23 +01:00
2009-05-13 06:15:48 +01:00
save_carefully ( QM , lookupArgs , nonLookupArgs )
2015-04-08 03:40:57 +01:00
parseCaveQMs ( cave = ' stein ' , inputFile = r " 1623/204/qm.csv " )
parseCaveQMs ( cave = ' hauch ' , inputFile = r " 1623/234/qm.csv " )
parseCaveQMs ( cave = ' kh ' , inputFile = " 1623/161/qmtodo.htm " )
2015-06-28 12:28:18 +01:00
#parseCaveQMs(cave='balkonhoehle',inputFile=r"1623/264/qm.csv")