From b7574660ded1e5d66ae961aba05927fb4fb251aa Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Tue, 19 Jan 2021 23:43:43 +0000 Subject: [PATCH] web/quotes: prodify --- ops/nixos/lib/quotes.bfob.gg.nix | 4 ++ web/quotes/default.nix | 4 ++ web/quotes/quotedb/migrations/0001_initial.py | 51 +++++++++++++++---- web/quotes/quotedb/models.py | 6 ++- web/quotes/quotesapp/prod_settings.py | 15 ++++++ web/quotes/quotesapp/settings.py | 2 - web/quotes/quotesapp/urls.py | 2 +- 7 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 web/quotes/quotesapp/prod_settings.py diff --git a/ops/nixos/lib/quotes.bfob.gg.nix b/ops/nixos/lib/quotes.bfob.gg.nix index d794ce5466..7e84777627 100644 --- a/ops/nixos/lib/quotes.bfob.gg.nix +++ b/ops/nixos/lib/quotes.bfob.gg.nix @@ -51,6 +51,10 @@ in systemd.services.quotesdb = { wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; + environment.DJANGO_SETTINGS_MODULE = "quotes.quotesapp.prod_settings"; + preStart = '' + ${pkg}/bin/quotes-manage migrate --no-input + ''; serviceConfig = { EnvironmentFile = secrets.quotesdb.environment; RuntimeDirectory = "quotesdb"; diff --git a/web/quotes/default.nix b/web/quotes/default.nix index f2ffef32a8..fd49b95b56 100644 --- a/web/quotes/default.nix +++ b/web/quotes/default.nix @@ -4,6 +4,7 @@ let python = pkgs.python3.withPackages (ps: with ps; [ django_3 gunicorn + psycopg2 (depot.pkgs.django-allauth.override { django = django_3; }) @@ -33,6 +34,9 @@ pkgs.stdenvNoCC.mkDerivation rec { makeWrapper "${python}/bin/gunicorn" "$out/bin/quotes" \ --add-flags "quotes.quotesapp.wsgi" \ --suffix PYTHONPATH : "$sitepkgdir" + makeWrapper "${python}/bin/django-admin" "$out/bin/quotes-manage" \ + --set DJANGO_SETTINGS_MODULE "quotes.quotesapp.prod_settings" \ + --suffix PYTHONPATH : "$sitepkgdir" mkdir -p "$out/share/static" export STATIC_ROOT="$out/share/static" diff --git a/web/quotes/quotedb/migrations/0001_initial.py b/web/quotes/quotedb/migrations/0001_initial.py index c90f6243ef..fb77b78eb1 100644 --- a/web/quotes/quotedb/migrations/0001_initial.py +++ b/web/quotes/quotedb/migrations/0001_initial.py @@ -16,21 +16,52 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='Person', + name="Person", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=120, unique=True)), - ('user', models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=120, unique=True)), + ( + "user", + models.ForeignKey( + default=None, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), ], ), migrations.CreateModel( - name='Quote', + name="Quote", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('quote', models.TextField()), - ('added_at', models.DateTimeField(default=datetime.datetime.now)), - ('added_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), - ('references', models.ManyToManyField(to='quotedb.Person')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("quote", models.TextField()), + ("added_at", models.DateTimeField(default=datetime.datetime.now)), + ( + "added_by", + models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), + ("references", models.ManyToManyField(to="quotedb.Person")), ], ), ] diff --git a/web/quotes/quotedb/models.py b/web/quotes/quotedb/models.py index 7dd7137bd5..4a5cee9023 100644 --- a/web/quotes/quotedb/models.py +++ b/web/quotes/quotedb/models.py @@ -6,13 +6,15 @@ from django.contrib.auth import models as auth_models class Person(models.Model): name = models.CharField(null=False, unique=True, blank=False, max_length=120) - user = models.ForeignKey(auth_models.User, null=True, default=None, on_delete=models.SET_NULL) + user = models.ForeignKey( + auth_models.User, null=True, default=None, on_delete=models.SET_NULL + ) def __str__(self): return self.name class Meta: - verbose_name_plural = 'people' + verbose_name_plural = "people" class Quote(models.Model): diff --git a/web/quotes/quotesapp/prod_settings.py b/web/quotes/quotesapp/prod_settings.py new file mode 100644 index 0000000000..881f20e1af --- /dev/null +++ b/web/quotes/quotesapp/prod_settings.py @@ -0,0 +1,15 @@ +from quotes.quotesapp.settings import * + +SECRET_KEY = os.environ["SECRET_KEY"] +DEBUG = False + +ALLOWED_HOSTS = [ + "quotes.bfob.gg", +] + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": "quotesdb", + } +} diff --git a/web/quotes/quotesapp/settings.py b/web/quotes/quotesapp/settings.py index 4dac8646cc..e9d52709ac 100644 --- a/web/quotes/quotesapp/settings.py +++ b/web/quotes/quotesapp/settings.py @@ -41,12 +41,10 @@ INSTALLED_APPS = [ "django.contrib.messages", "django.contrib.staticfiles", "django.contrib.sites", - "allauth", "allauth.account", "allauth.socialaccount", "allauth.socialaccount.providers.discord", - "quotes.quotedb", ] diff --git a/web/quotes/quotesapp/urls.py b/web/quotes/quotesapp/urls.py index 1cfa71ef25..48814e7176 100644 --- a/web/quotes/quotesapp/urls.py +++ b/web/quotes/quotesapp/urls.py @@ -21,6 +21,6 @@ import quotes.quotedb.urls urlpatterns = [ path("admin/", admin.site.urls), - path('accounts/', include(allauth.urls)), + path("accounts/", include(allauth.urls)), re_path(r"", include(quotes.quotedb.urls)), ]