forked from expo/troggle
118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
|
import os
|
||
|
import time
|
||
|
import settings
|
||
|
os.environ['PYTHONPATH'] = settings.PYTHON_PATH
|
||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
|
||
|
from django.core import management
|
||
|
from django.db import connection
|
||
|
from django.contrib.auth.models import User
|
||
|
from django.http import HttpResponse
|
||
|
from django.core.urlresolvers import reverse
|
||
|
from troggle.core.models import Cave, Entrance
|
||
|
from troggle.core.models import PersonM, SurveyM, CaveM, ExpeditionM, Logbook_entryM
|
||
|
import troggle.flatpages.models
|
||
|
|
||
|
databasename=settings.DATABASES['default']['NAME']
|
||
|
expouser=settings.EXPOUSER
|
||
|
expouserpass=settings.EXPOUSERPASS
|
||
|
expouseremail=settings.EXPOUSER_EMAIL
|
||
|
|
||
|
def destroy():
|
||
|
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('syncdb', interactive=False)
|
||
|
user = User.objects.create_user(expouser, expouseremail, expouserpass)
|
||
|
user.is_staff = True
|
||
|
user.is_superuser = True
|
||
|
user.save()
|
||
|
print('Nuked the database and rebuilt it. You savage monster')
|
||
|
|
||
|
def gracefull_flush():
|
||
|
CaveM.objects.all().delete()
|
||
|
PersonM.objects.all().delete()
|
||
|
SurveyM.objects.all().delete()
|
||
|
ExpeditionM.objects.all().delete()
|
||
|
Logbook_entryM.objects.all().delete()
|
||
|
print('Deleted contents of the database, ready to load new stuff :)')
|
||
|
|
||
|
def load_redirects():
|
||
|
for oldURL, newURL in [("indxal.htm", reverse("caveindex"))]:
|
||
|
f = troggle.flatpages.models.Redirect(originalURL = oldURL, newURL = newURL)
|
||
|
f.save()
|
||
|
|
||
|
def load_caves():
|
||
|
import troggle.parsers.cavesM
|
||
|
troggle.parsers.cavesM.load()
|
||
|
|
||
|
def load_all():
|
||
|
load_caves()
|
||
|
load_surveys()
|
||
|
load_people()
|
||
|
load_redirects()
|
||
|
load_links()
|
||
|
print('Loaded everything. Your database is ready to go :)')
|
||
|
|
||
|
|
||
|
def help():
|
||
|
print("""Usage is 'python databaseResetM.py <command>'
|
||
|
where command is:
|
||
|
UNLOADERS:
|
||
|
gracefull_flush - flushes new (M-style) databases contents but keeps tables existing
|
||
|
destroy - destroys entire database and builds empty tables
|
||
|
|
||
|
LOADERS:
|
||
|
load_all - loads all tables and links
|
||
|
load_caves - loads all caves
|
||
|
load_surveys - loads all surveys (corresponds to .svx files)
|
||
|
load_people - loads all people
|
||
|
load_redirects - load page redirects
|
||
|
load_links - loads links between classes (run last! can't link non-existent things)
|
||
|
|
||
|
OTHER:
|
||
|
help - displays this page
|
||
|
----------------
|
||
|
This is a new version of database management written by RW 2019
|
||
|
----------------
|
||
|
""")
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
import troggle.core.models
|
||
|
import sys
|
||
|
import django
|
||
|
django.setup()
|
||
|
if "destroy" in sys.argv:
|
||
|
destroy()
|
||
|
elif "gracefull_flush" in sys.argv:
|
||
|
gracefull_flush()
|
||
|
|
||
|
elif "load_all" in sys.argv:
|
||
|
load_all()
|
||
|
elif "load_caves" in sys.argv:
|
||
|
load_caves()
|
||
|
elif "load_surveys" in sys.argv:
|
||
|
load_surveys()
|
||
|
elif "load_people" in sys.argv:
|
||
|
load_people()
|
||
|
elif "load_redirects" in sys.argv:
|
||
|
load_redirects()
|
||
|
elif "load_links" in sys.argv:
|
||
|
load_links()
|
||
|
elif "help" in sys.argv:
|
||
|
help()
|
||
|
else:
|
||
|
print("%s not recognised" % sys.argv)
|
||
|
help()
|
||
|
|
||
|
|
||
|
|
||
|
|