from django.core.management.base import BaseCommand, CommandError from optparse import make_option from troggle.core.models import Cave import settings 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 """Pretty much all of this is now replaced by databaseRest.py I don't know why this still exists """ 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 + ( make_option('--reset', action='store_true', dest='reset', default=False, help='Reset the entier DB from files'), ) 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() elif options['reset']: self.reset(self) 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() elif options['foo']: self.stdout.write(self.style.WARNING('Tesing....')) else: #self.stdout.write("%s not recognised" % args) #self.usage(options) self.stdout.write("poo") #print(args) def reload_db(obj): 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() def make_dirs(obj): """Make directories that troggle requires""" pass # should also deal with permissions here. #if not os.path.isdir(settings.PHOTOS_ROOT): #os.mkdir(settings.PHOTOS_ROOT) def import_caves(obj): import parsers.caves print("Importing Caves") parsers.caves.readcaves() def import_people(obj): import parsers.people parsers.people.LoadPersonsExpos() def import_logbooks(obj): # 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() def import_survex(obj): import parsers.survex parsers.survex.LoadAllSurvexBlocks() parsers.survex.LoadPos() def import_QMs(obj): import parsers.QMs def import_surveys(obj): import parsers.surveys parsers.surveys.parseSurveys(logfile=settings.LOGFILE) def import_surveyscans(obj): import parsers.surveys parsers.surveys.LoadListScans() def import_tunnelfiles(obj): import parsers.surveys parsers.surveys.LoadTunnelFiles() def reset(self, mgmt_obj): """ Wipe the troggle database and import everything from legacy data """ 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() try: self.import_tunnelfiles() except: print("Tunnel files parser broken.") self.import_surveys() def pageredirects(obj): for oldURL, newURL in [("indxal.htm", reverse("caveindex"))]: f = troggle.flatpages.models.Redirect(originalURL=oldURL, newURL=newURL) f.save() def writeCaves(obj): for cave in Cave.objects.all(): cave.writeDataFile() for entrance in Entrance.objects.all(): entrance.writeDataFile() def troggle_usage(obj): print("""Usage is 'manage.py reset_db ' 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 """)