web: init quotes

This commit is contained in:
Luke Granger-Brown 2021-01-19 03:51:22 +00:00
parent 1d41593ae2
commit 169c243369
23 changed files with 393 additions and 1 deletions

View file

@ -11,3 +11,4 @@ syntax: glob
*.pyc
*.orig
*~
db.sqlite3

View file

@ -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;
}

3
web/quotes/.envrc Normal file
View file

@ -0,0 +1,3 @@
use_nix
export PYTHONPATH=$PYTHONPATH${PYTHONPATH:+':'}"$(readlink -f ..)"

0
web/quotes/__init__.py Normal file
View file

45
web/quotes/default.nix Normal file
View file

@ -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;
}

22
web/quotes/manage.py Executable file
View file

@ -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()

View file

@ -0,0 +1,2 @@
[tool.black]
target-version = ['py38']

View file

View file

@ -0,0 +1,6 @@
from django.contrib import admin
from . import models
admin.site.register(models.Person)
admin.site.register(models.Quote)

View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class QuotedbConfig(AppConfig):
name = "quotedb"

View file

@ -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')),
],
),
]

View file

@ -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

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -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"),
]

View file

@ -0,0 +1,5 @@
from django.shortcuts import render
def home(request):
return render(request, "quotedb/home.html", {})

View file

View file

@ -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()

View file

@ -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",
],
},
}

View file

@ -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)),
]

View file

@ -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()

12
web/quotes/shell.nix Normal file
View file

@ -0,0 +1,12 @@
{ depot ? import <depot> {} }:
let
inherit (depot.web) quotes;
inherit (depot) pkgs;
in pkgs.mkShell {
buildInputs = with pkgs; [
quotes.pythonEnv
black
];
}

View file

@ -0,0 +1 @@
hi