Repository: mathesar-foundation/mathesar

Issue:
Demo mode results in 5XX
## Description
<!-- A clear and concise description of what the bug is. -->

Mathesar is broken (as of 0.1.4) for Demo Mode. It doesn't load, and just says "Server Error (500)" instead.

## Expected behavior
<!-- A clear and concise description of what you expected to happen. -->

Demo mode should work.

## To Reproduce
<!-- How can we recreate this bug? Please try to provide a Minimal, Complete, and Verifiable (http://stackoverflow.com/help/mcve) example if code-related. -->

Change the `.env` file according to the comment to use the demo mode settings, try to build and start mathesar (dev environment is fine)


Discussion:
The settings module of the demo mode imports settings from `config.settings.production` which has `MATHESAR_MODE=PRODUCTION` & `DEBUG=FALSE`, now while using our dev container we have the `MODE=DEVELOPMENT` and `DEBUG=TRUE` this was leading to some weird behavior for `STATICFILES_DIRS` and hence the demo app was having trouble locating static files which was, in turn, causing the 500.

Setting `MODE=PRODUCTION` & `DEBUG=FALSE`, for demo mode fixes this issue.
@Anish9901 what is the underlying issue that's causing the "weird behavior"? Can / should we fix that?
@kgodey We have different setting modules for dev(common_settings), production, and demo. Out of these, the common_settings are passed around in production which is passed to demo setting module with alterations made to a couple settings as mentioned in my previous comment for prod. We could fix the underlying issue by having a single settings file for all the modes and having a flag to switch between them. 

Alternatively, we could also keep running the demo in development mode which is the easiest.
@Anish9901 Okay, thanks. I think it's fine to run in development mode unless @mathemancer disagrees. I actually [linked the DRF browseable API on demo](https://www.reddit.com/r/selfhosted/comments/18yd85c/just_heard_airplanedev_shutting_down_what_are_the/kgo63b6/) to someone on Reddit recently and it's nice to be able to do that. We'll lose that if we turn `DEBUG` off.
It depends on the intended audience for the demo mode. For now, I agree it makes sense to run the demo mode using the dev settings, since most of the people we're directing there might be interested in the debug ouput and so on.

Resolving patch:
diff --git a/demo/management/commands/setup_demo_template_db.py b/demo/management/commands/setup_demo_template_db.py
--- a/demo/management/commands/setup_demo_template_db.py
+++ b/demo/management/commands/setup_demo_template_db.py
@@ -20,7 +20,7 @@ def _setup_demo_template_db():
     print("Initializing demo template database...")
 
     template_db_name = settings.MATHESAR_DEMO_TEMPLATE
-    django_model = Database.current_objects.get(name=settings.DATABASES["default"]["NAME"])
+    django_model = Database.create_from_settings_key("default")
     root_engine = create_mathesar_engine(django_model)
     with root_engine.connect() as conn:
         conn.execution_options(isolation_level="AUTOCOMMIT")
diff --git a/demo/settings.py b/demo/settings.py
--- a/demo/settings.py
+++ b/demo/settings.py
@@ -1,5 +1,4 @@
-from config.settings.production import *  # noqa
-from config.settings import *  # noqa
+from config.settings.common_settings import *  # noqa
 from decouple import config as decouple_config
 
 INSTALLED_APPS += [  # noqa
@@ -10,6 +9,7 @@
     "demo.middleware.LiveDemoModeMiddleware",
 ]
 
+MATHESAR_MODE = 'PRODUCTION'
 MATHESAR_LIVE_DEMO = True
 MATHESAR_LIVE_DEMO_USERNAME = decouple_config('MATHESAR_LIVE_DEMO_USERNAME', default=None)
 MATHESAR_LIVE_DEMO_PASSWORD = decouple_config('MATHESAR_LIVE_DEMO_PASSWORD', default=None)
diff --git a/mathesar/install.py b/mathesar/install.py
--- a/mathesar/install.py
+++ b/mathesar/install.py
@@ -37,6 +37,14 @@ def main(skip_static_collection=False):
             install_on_db_with_key(database_key, skip_confirm)
         except IntegrityError:
             continue
+    if getattr(settings, 'MATHESAR_LIVE_DEMO', False) is True:
+        management.call_command(
+            'createsuperuser',
+            '--no-input',
+            '--username', 'demo',
+            '--email', 'admin@example.com',
+        )
+        management.call_command('setup_demo_template_db')
 
 
 def install_on_db_with_key(database_key, skip_confirm):


Test patch: