diff --git a/.hgignore b/.hgignore index 796ade6a22..c53b0e1f52 100644 --- a/.hgignore +++ b/.hgignore @@ -11,3 +11,4 @@ syntax: glob *.pyc *.orig *~ +db.sqlite3 diff --git a/web/default.nix b/web/default.nix index ff779654d4..eee9e54b71 100644 --- a/web/default.nix +++ b/web/default.nix @@ -2,8 +2,9 @@ # # SPDX-License-Identifier: Apache-2.0 -{ pkgs, ... }: +{ pkgs, ... }@args: { int = pkgs.copyPathToStore ./int; logged-out-int = pkgs.copyPathToStore ./logged-out-int; + quotes = import ./quotes args; } diff --git a/web/quotes/.envrc b/web/quotes/.envrc new file mode 100644 index 0000000000..01ac6cbfd3 --- /dev/null +++ b/web/quotes/.envrc @@ -0,0 +1,3 @@ +use_nix + +export PYTHONPATH=$PYTHONPATH${PYTHONPATH:+':'}"$(readlink -f ..)" diff --git a/web/quotes/__init__.py b/web/quotes/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/web/quotes/default.nix b/web/quotes/default.nix new file mode 100644 index 0000000000..f2ffef32a8 --- /dev/null +++ b/web/quotes/default.nix @@ -0,0 +1,45 @@ +{ depot, pkgs, ... }: + +let + python = pkgs.python3.withPackages (ps: with ps; [ + django_3 + gunicorn + (depot.pkgs.django-allauth.override { + django = django_3; + }) + ]); +in +pkgs.stdenvNoCC.mkDerivation rec { + name = "quotes"; + + src = ./.; + + buildInputs = [ pkgs.makeWrapper ]; + propagatedBuildInputs = [ python ]; + + buildPhase = "true"; + installPhase = '' + sitepkgdir="$out/lib/${python.libPrefix}/site-packages" + pkgdir="$sitepkgdir/quotes" + mkdir -p $pkgdir + cp -R \ + $src/quotesapp \ + $src/quotedb \ + $src/templates \ + $src/static \ + $pkgdir + + mkdir "$out/bin" + makeWrapper "${python}/bin/gunicorn" "$out/bin/quotes" \ + --add-flags "quotes.quotesapp.wsgi" \ + --suffix PYTHONPATH : "$sitepkgdir" + + mkdir -p "$out/share/static" + export STATIC_ROOT="$out/share/static" + export DJANGO_SETTINGS_MODULE=quotes.quotesapp.settings + export PYTHONPATH=$PYTHONPATH''${PYTHONPATH:+':'}"$sitepkgdir" + django-admin collectstatic --no-input + ''; + + passthru.pythonEnv = python; +} diff --git a/web/quotes/manage.py b/web/quotes/manage.py new file mode 100755 index 0000000000..6340fc40c1 --- /dev/null +++ b/web/quotes/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quotes.quotesapp.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/web/quotes/pyproject.toml b/web/quotes/pyproject.toml new file mode 100644 index 0000000000..e131e50cf1 --- /dev/null +++ b/web/quotes/pyproject.toml @@ -0,0 +1,2 @@ +[tool.black] +target-version = ['py38'] diff --git a/web/quotes/quotedb/__init__.py b/web/quotes/quotedb/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/web/quotes/quotedb/admin.py b/web/quotes/quotedb/admin.py new file mode 100644 index 0000000000..45f35b9650 --- /dev/null +++ b/web/quotes/quotedb/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from . import models + + +admin.site.register(models.Person) +admin.site.register(models.Quote) diff --git a/web/quotes/quotedb/apps.py b/web/quotes/quotedb/apps.py new file mode 100644 index 0000000000..6199d445df --- /dev/null +++ b/web/quotes/quotedb/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class QuotedbConfig(AppConfig): + name = "quotedb" diff --git a/web/quotes/quotedb/migrations/0001_initial.py b/web/quotes/quotedb/migrations/0001_initial.py new file mode 100644 index 0000000000..c90f6243ef --- /dev/null +++ b/web/quotes/quotedb/migrations/0001_initial.py @@ -0,0 +1,36 @@ +# Generated by Django 3.1.5 on 2021-01-19 03:40 + +import datetime +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + 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)), + ], + ), + migrations.CreateModel( + 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')), + ], + ), + ] diff --git a/web/quotes/quotedb/migrations/__init__.py b/web/quotes/quotedb/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/web/quotes/quotedb/models.py b/web/quotes/quotedb/models.py new file mode 100644 index 0000000000..7dd7137bd5 --- /dev/null +++ b/web/quotes/quotedb/models.py @@ -0,0 +1,25 @@ +import datetime + +from django.db import models +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) + + def __str__(self): + return self.name + + class Meta: + verbose_name_plural = 'people' + + +class Quote(models.Model): + quote = models.TextField(null=False, blank=False) + added_by = models.ForeignKey(auth_models.User, on_delete=models.SET_NULL, null=True) + added_at = models.DateTimeField(default=datetime.datetime.now) + references = models.ManyToManyField(Person) + + def __str__(self): + return self.quote diff --git a/web/quotes/quotedb/tests.py b/web/quotes/quotedb/tests.py new file mode 100644 index 0000000000..7ce503c2dd --- /dev/null +++ b/web/quotes/quotedb/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/web/quotes/quotedb/urls.py b/web/quotes/quotedb/urls.py new file mode 100644 index 0000000000..2588686fd2 --- /dev/null +++ b/web/quotes/quotedb/urls.py @@ -0,0 +1,21 @@ +"""quotes URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.urls import re_path, path +from . import views + +urlpatterns = [ + path("", views.home, name="home"), +] diff --git a/web/quotes/quotedb/views.py b/web/quotes/quotedb/views.py new file mode 100644 index 0000000000..dedc1e5fc1 --- /dev/null +++ b/web/quotes/quotedb/views.py @@ -0,0 +1,5 @@ +from django.shortcuts import render + + +def home(request): + return render(request, "quotedb/home.html", {}) diff --git a/web/quotes/quotesapp/__init__.py b/web/quotes/quotesapp/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/web/quotes/quotesapp/asgi.py b/web/quotes/quotesapp/asgi.py new file mode 100644 index 0000000000..01fa7a2a41 --- /dev/null +++ b/web/quotes/quotesapp/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for quotes project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quotes.quotesapp.settings") + +application = get_asgi_application() diff --git a/web/quotes/quotesapp/settings.py b/web/quotes/quotesapp/settings.py new file mode 100644 index 0000000000..4dac8646cc --- /dev/null +++ b/web/quotes/quotesapp/settings.py @@ -0,0 +1,146 @@ +""" +Django settings for quotes project. + +Generated by 'django-admin startproject' using Django 3.1.5. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +from pathlib import Path +import os + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "&(13b=+n^k3px89=%x24_=2593x2*)p_6l7&wu_xph!t=$o9!1" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +SITE_ID = 1 + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "django.contrib.sites", + + "allauth", + "allauth.account", + "allauth.socialaccount", + "allauth.socialaccount.providers.discord", + + "quotes.quotedb", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "quotes.quotesapp.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [ + BASE_DIR / "templates", + ], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "quotes.quotesapp.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": str(BASE_DIR / "db.sqlite3"), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = "/static/" +STATIC_ROOT = os.environ.get("STATIC_ROOT", None) +STATICFILES_DIRS = [ + BASE_DIR / "static", +] + +SOCIALACCOUNT_PROVIDERS = { + "discord": { + "SCOPE": [ + "identify", + "email", + ], + }, +} diff --git a/web/quotes/quotesapp/urls.py b/web/quotes/quotesapp/urls.py new file mode 100644 index 0000000000..1cfa71ef25 --- /dev/null +++ b/web/quotes/quotesapp/urls.py @@ -0,0 +1,26 @@ +"""quotes URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import include, re_path, path + +import allauth.urls +import quotes.quotedb.urls + +urlpatterns = [ + path("admin/", admin.site.urls), + path('accounts/', include(allauth.urls)), + re_path(r"", include(quotes.quotedb.urls)), +] diff --git a/web/quotes/quotesapp/wsgi.py b/web/quotes/quotesapp/wsgi.py new file mode 100644 index 0000000000..18617dd842 --- /dev/null +++ b/web/quotes/quotesapp/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for quotes project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quotes.quotesapp.settings") + +application = get_wsgi_application() diff --git a/web/quotes/shell.nix b/web/quotes/shell.nix new file mode 100644 index 0000000000..d15b9e03c8 --- /dev/null +++ b/web/quotes/shell.nix @@ -0,0 +1,12 @@ +{ depot ? import {} }: + +let + inherit (depot.web) quotes; + inherit (depot) pkgs; +in pkgs.mkShell { + buildInputs = with pkgs; [ + quotes.pythonEnv + + black + ]; +} diff --git a/web/quotes/templates/quotedb/home.html b/web/quotes/templates/quotedb/home.html new file mode 100644 index 0000000000..45b983be36 --- /dev/null +++ b/web/quotes/templates/quotedb/home.html @@ -0,0 +1 @@ +hi