Make the migrate passthrough support more checks natively. Resolves #1941

This commit is contained in:
Griatch 2020-01-19 15:08:29 +01:00
parent 31076478b4
commit c2d43770d0
2 changed files with 27 additions and 18 deletions

View file

@ -1428,12 +1428,17 @@ def create_superuser():
django.core.management.call_command("createsuperuser", interactive=True) django.core.management.call_command("createsuperuser", interactive=True)
def check_database(): def check_database(always_return=False):
""" """
Check so the database exists. Check so the database exists.
Args:
always_return (bool, optional): If set, will always return True/False
also on critical errors. No output will be printed.
Returns: Returns:
exists (bool): `True` if the database exists, otherwise `False`. exists (bool): `True` if the database exists, otherwise `False`.
""" """
# Check so a database exists and is accessible # Check so a database exists and is accessible
from django.db import connection from django.db import connection
@ -1450,6 +1455,8 @@ def check_database():
try: try:
AccountDB.objects.get(id=1) AccountDB.objects.get(id=1)
except django.db.utils.OperationalError as e: except django.db.utils.OperationalError as e:
if always_return:
return False
print(ERROR_DATABASE.format(traceback=e)) print(ERROR_DATABASE.format(traceback=e))
sys.exit() sys.exit()
except AccountDB.DoesNotExist: except AccountDB.DoesNotExist:
@ -1484,7 +1491,7 @@ def check_database():
new.save() new.save()
else: else:
create_superuser() create_superuser()
check_database() check_database(always_return=always_return)
return True return True
@ -2246,14 +2253,15 @@ def main():
# pass-through to django manager, but set things up first # pass-through to django manager, but set things up first
check_db = False check_db = False
need_gamedir = True need_gamedir = True
# some commands don't require the presence of a game directory to work
if option in ("makemessages", "compilemessages"):
need_gamedir = False
# handle special django commands # handle special django commands
if option in ("runserver", "testserver"): if option in ("runserver", "testserver"):
# we don't want the django test-webserver
print(WARNING_RUNSERVER) print(WARNING_RUNSERVER)
if option in ("shell", "check"): if option in ("makemessages", "compilemessages"):
# some commands don't require the presence of a game directory to work
need_gamedir = False
if option in ("shell", "check", "makemigrations"):
# some django commands requires the database to exist, # some django commands requires the database to exist,
# or evennia._init to have run before they work right. # or evennia._init to have run before they work right.
check_db = True check_db = True
@ -2263,16 +2271,17 @@ def main():
init_game_directory(CURRENT_DIR, check_db=check_db, need_gamedir=need_gamedir) init_game_directory(CURRENT_DIR, check_db=check_db, need_gamedir=need_gamedir)
if option in ("migrate", "makemigrations"): if option == "migrate":
# we have to launch migrate within the program to make sure migrations # we need to bypass some checks here for the first db creation
# run within the scope of the launcher (otherwise missing a db will cause errors) if not check_database(always_return=True):
django.core.management.call_command(*([option] + unknown_args)) django.core.management.call_command(*([option] + unknown_args))
else: sys.exit(0)
# pass on to the core django manager - re-parse the entire input line
# but keep 'evennia' as the name instead of django-admin. This is # pass on to the core django manager - re-parse the entire input line
# an exit condition. # but keep 'evennia' as the name instead of django-admin. This is
sys.argv[0] = re.sub(r"(-script\.pyw?|\.exe)?$", "", sys.argv[0]) # an exit condition.
sys.exit(execute_from_command_line()) sys.argv[0] = re.sub(r"(-script\.pyw?|\.exe)?$", "", sys.argv[0])
sys.exit(execute_from_command_line())
elif not args.tail_log: elif not args.tail_log:
# no input; print evennia info (don't pring if we're tailing log) # no input; print evennia info (don't pring if we're tailing log)

View file

@ -77,8 +77,8 @@ class WebSocketClient(WebSocketServerProtocol, Session):
client_address = self.transport.client client_address = self.transport.client
client_address = client_address[0] if client_address else None client_address = client_address[0] if client_address else None
if client_address in _UPSTREAM_IPS and 'x-forwarded-for' in self.http_headers: if client_address in _UPSTREAM_IPS and "x-forwarded-for" in self.http_headers:
addresses = [x.strip() for x in self.http_headers['x-forwarded-for'].split(',')] addresses = [x.strip() for x in self.http_headers["x-forwarded-for"].split(",")]
addresses.reverse() addresses.reverse()
for addr in addresses: for addr in addresses: