2019-02-25 23:10:24 +00:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from optparse import make_option
|
|
|
|
from troggle.core.models import Cave
|
|
|
|
import settings
|
2019-03-30 13:58:38 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from django.db import connection
|
|
|
|
from django.core import management
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from troggle.core.models import Cave, Entrance
|
|
|
|
import troggle.flatpages.models
|
2019-02-25 23:10:24 +00:00
|
|
|
|
|
|
|
databasename=settings.DATABASES['default']['NAME']
|
|
|
|
expouser=settings.EXPOUSER
|
|
|
|
expouserpass=settings.EXPOUSERPASS
|
|
|
|
expouseremail=settings.EXPOUSER_EMAIL
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'This is normal usage, clear database and reread everything'
|
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
2019-03-30 13:58:38 +00:00
|
|
|
make_option('--reset',
|
2019-02-25 23:10:24 +00:00
|
|
|
action='store_true',
|
2019-03-30 13:58:38 +00:00
|
|
|
dest='reset',
|
2019-02-25 23:10:24 +00:00
|
|
|
default=False,
|
2019-03-30 13:58:38 +00:00
|
|
|
help='Reset the entier DB from files'),
|
2019-02-25 23:10:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
print(args)
|
|
|
|
print(options)
|
|
|
|
if "desc" in args:
|
|
|
|
self.resetdesc()
|
|
|
|
elif "scans" in args:
|
|
|
|
self.import_surveyscans()
|
|
|
|
elif "caves" in args:
|
|
|
|
self.reload_db()
|
|
|
|
self.make_dirs()
|
|
|
|
self.pageredirects()
|
|
|
|
self.import_caves()
|
|
|
|
elif "people" in args:
|
|
|
|
self.import_people()
|
|
|
|
elif "QMs" in args:
|
|
|
|
self.import_QMs()
|
|
|
|
elif "tunnel" in args:
|
|
|
|
self.import_tunnelfiles()
|
2019-03-30 13:58:38 +00:00
|
|
|
elif options['reset']:
|
|
|
|
self.reset(self)
|
2019-02-25 23:10:24 +00:00
|
|
|
elif "survex" in args:
|
|
|
|
self.import_survex()
|
|
|
|
elif "survexpos" in args:
|
|
|
|
import parsers.survex
|
|
|
|
parsers.survex.LoadPos()
|
|
|
|
elif "logbooks" in args:
|
|
|
|
self.import_logbooks()
|
|
|
|
elif "autologbooks" in args:
|
|
|
|
self.import_auto_logbooks()
|
|
|
|
elif "dumplogbooks" in args:
|
|
|
|
self.dumplogbooks()
|
|
|
|
elif "writeCaves" in args:
|
|
|
|
self.writeCaves()
|
2019-03-30 13:58:38 +00:00
|
|
|
elif options['foo']:
|
|
|
|
self.stdout.write(self.style.WARNING('Tesing....'))
|
2019-02-25 23:10:24 +00:00
|
|
|
else:
|
2019-03-30 13:58:38 +00:00
|
|
|
#self.stdout.write("%s not recognised" % args)
|
|
|
|
#self.usage(options)
|
|
|
|
self.stdout.write("poo")
|
|
|
|
#print(args)
|
2019-02-25 23:10:24 +00:00
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def reload_db(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3':
|
|
|
|
try:
|
|
|
|
os.remove(databasename)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute("DROP DATABASE %s" % databasename)
|
|
|
|
cursor.execute("CREATE DATABASE %s" % databasename)
|
|
|
|
cursor.execute("ALTER DATABASE %s CHARACTER SET=utf8" % databasename)
|
|
|
|
cursor.execute("USE %s" % databasename)
|
|
|
|
management.call_command('migrate', interactive=False)
|
|
|
|
# management.call_command('syncdb', interactive=False)
|
|
|
|
user = User.objects.create_user(expouser, expouseremail, expouserpass)
|
|
|
|
user.is_staff = True
|
|
|
|
user.is_superuser = True
|
|
|
|
user.save()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def make_dirs(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
"""Make directories that troggle requires"""
|
|
|
|
# should also deal with permissions here.
|
|
|
|
if not os.path.isdir(settings.PHOTOS_ROOT):
|
|
|
|
os.mkdir(settings.PHOTOS_ROOT)
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_caves(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
import parsers.caves
|
2019-03-30 13:58:38 +00:00
|
|
|
print("Importing Caves")
|
2019-02-25 23:10:24 +00:00
|
|
|
parsers.caves.readcaves()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_people(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
import parsers.people
|
|
|
|
parsers.people.LoadPersonsExpos()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_logbooks(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
# The below line was causing errors I didn't understand (it said LOGFILE was a string), and I couldn't be bothered to figure
|
|
|
|
# what was going on so I just catch the error with a try. - AC 21 May
|
|
|
|
try:
|
|
|
|
settings.LOGFILE.write('\nBegun importing logbooks at ' + time.asctime() + '\n' + '-' * 60)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
import parsers.logbooks
|
|
|
|
parsers.logbooks.LoadLogbooks()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_survex(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
import parsers.survex
|
|
|
|
parsers.survex.LoadAllSurvexBlocks()
|
|
|
|
parsers.survex.LoadPos()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_QMs(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
import parsers.QMs
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_surveys(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
import parsers.surveys
|
|
|
|
parsers.surveys.parseSurveys(logfile=settings.LOGFILE)
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_surveyscans(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
import parsers.surveys
|
|
|
|
parsers.surveys.LoadListScans()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def import_tunnelfiles(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
import parsers.surveys
|
|
|
|
parsers.surveys.LoadTunnelFiles()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def reset(self, mgmt_obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
""" Wipe the troggle database and import everything from legacy data
|
|
|
|
"""
|
2019-03-30 13:58:38 +00:00
|
|
|
self.reload_db()
|
|
|
|
self.make_dirs()
|
|
|
|
self.pageredirects()
|
|
|
|
self.import_caves()
|
|
|
|
self.import_people()
|
|
|
|
self.import_surveyscans()
|
|
|
|
self.import_survex()
|
|
|
|
self.import_logbooks()
|
|
|
|
self.import_QMs()
|
2019-02-25 23:10:24 +00:00
|
|
|
try:
|
2019-03-30 13:58:38 +00:00
|
|
|
self.import_tunnelfiles()
|
2019-02-25 23:10:24 +00:00
|
|
|
except:
|
|
|
|
print("Tunnel files parser broken.")
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
self.import_surveys()
|
2019-02-25 23:10:24 +00:00
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def pageredirects(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
for oldURL, newURL in [("indxal.htm", reverse("caveindex"))]:
|
|
|
|
f = troggle.flatpages.models.Redirect(originalURL=oldURL, newURL=newURL)
|
|
|
|
f.save()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def writeCaves(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
for cave in Cave.objects.all():
|
|
|
|
cave.writeDataFile()
|
|
|
|
for entrance in Entrance.objects.all():
|
|
|
|
entrance.writeDataFile()
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
def troggle_usage(obj):
|
2019-02-25 23:10:24 +00:00
|
|
|
print("""Usage is 'manage.py reset_db <command>'
|
|
|
|
where command is:
|
|
|
|
reset - this is normal usage, clear database and reread everything
|
|
|
|
desc
|
|
|
|
caves - read in the caves
|
|
|
|
logbooks - read in the logbooks
|
|
|
|
autologbooks
|
|
|
|
dumplogbooks
|
|
|
|
people
|
|
|
|
QMs - read in the QM files
|
|
|
|
resetend
|
|
|
|
scans - read in the scanned surveynotes
|
|
|
|
survex - read in the survex files
|
|
|
|
survexpos
|
|
|
|
tunnel - read in the Tunnel files
|
|
|
|
writeCaves
|
|
|
|
""")
|