From e64d82cd920af92a4fbd81082d22334ced7a052f Mon Sep 17 00:00:00 2001 From: Sam Wenham Date: Mon, 25 Feb 2019 23:10:24 +0000 Subject: [PATCH] Start of moving databasereset to django management --- core/management/__init__.py | 0 core/management/commands/__init__.py | 0 core/management/commands/reset_db.py | 182 +++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 core/management/__init__.py create mode 100644 core/management/commands/__init__.py create mode 100644 core/management/commands/reset_db.py diff --git a/core/management/__init__.py b/core/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/management/commands/__init__.py b/core/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/management/commands/reset_db.py b/core/management/commands/reset_db.py new file mode 100644 index 0000000..29f8e81 --- /dev/null +++ b/core/management/commands/reset_db.py @@ -0,0 +1,182 @@ +from django.core.management.base import BaseCommand, CommandError +from optparse import make_option +from troggle.core.models import Cave +import settings + +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('--foo', + action='store_true', + dest='foo', + default=False, + help='test'), + ) + + def add_arguments(self, parser): + + parser.add_argument( + '--foo', + action='store_true', + dest='foo', + help='Help text', + ) + + 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 "reset" in args: + self.reset() + 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 "foo" in args: + self.stdout.write('Tesing....') + else: + self.stdout.write("%s not recognised" % args) + self.usage(options) + + def reload_db(): + 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(): + """Make directories that troggle requires""" + # should also deal with permissions here. + if not os.path.isdir(settings.PHOTOS_ROOT): + os.mkdir(settings.PHOTOS_ROOT) + + def import_caves(): + import parsers.caves + print("importing caves") + parsers.caves.readcaves() + + def import_people(): + import parsers.people + parsers.people.LoadPersonsExpos() + + def import_logbooks(): + # 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(): + import parsers.survex + parsers.survex.LoadAllSurvexBlocks() + parsers.survex.LoadPos() + + def import_QMs(): + import parsers.QMs + + def import_surveys(): + import parsers.surveys + parsers.surveys.parseSurveys(logfile=settings.LOGFILE) + + def import_surveyscans(): + import parsers.surveys + parsers.surveys.LoadListScans() + + def import_tunnelfiles(): + import parsers.surveys + parsers.surveys.LoadTunnelFiles() + + def reset(): + """ Wipe the troggle database and import everything from legacy data + """ + reload_db() + make_dirs() + pageredirects() + import_caves() + import_people() + import_surveyscans() + import_survex() + import_logbooks() + import_QMs() + try: + import_tunnelfiles() + except: + print("Tunnel files parser broken.") + + import_surveys() + + def pageredirects(): + for oldURL, newURL in [("indxal.htm", reverse("caveindex"))]: + f = troggle.flatpages.models.Redirect(originalURL=oldURL, newURL=newURL) + f.save() + + def writeCaves(): + for cave in Cave.objects.all(): + cave.writeDataFile() + for entrance in Entrance.objects.all(): + entrance.writeDataFile() + + def usage(self, parser): + 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 + """)