Start some settings work

This commit is contained in:
Andrew Godwin 2022-11-12 22:10:06 -07:00
parent 878f56b411
commit 143a4a6e8c
17 changed files with 75 additions and 32 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
.PHONY: clean
image:
docker build -t takahe -f docker/Dockerfile .

View File

@ -1,3 +1,20 @@
class Config: import pydantic
pass
class Config(pydantic.BaseModel):
# Basic configuration options
site_name: str = "takahē"
identity_max_age: int = 24 * 60 * 60
# Cached ORM object storage
__singleton__ = None
class Config:
env_prefix = "takahe_"
@classmethod
def load(cls) -> "Config":
if cls.__singleton__ is None:
cls.__singleton__ = cls()
return cls.__singleton__

View File

@ -1,7 +1,7 @@
from django.conf import settings from core.config import Config
def config_context(request): def config_context(request):
return { return {
"config": {"site_name": settings.SITE_NAME}, "config": Config.load(),
} }

View File

@ -1,4 +1,6 @@
FROM python:3.9-bullseye as builder # Build stage
FROM python:3.11.0-buster as builder
RUN mkdir -p /takahe RUN mkdir -p /takahe
RUN python -m venv /takahe/.venv RUN python -m venv /takahe/.venv
@ -12,8 +14,9 @@ RUN . /takahe/.venv/bin/activate \
&& pip install --upgrade pip \ && pip install --upgrade pip \
&& pip install --upgrade -r requirements.txt && pip install --upgrade -r requirements.txt
# Final image stage
FROM python:3.9-slim-bullseye FROM python:3.11.0-slim-buster
RUN apt-get update && apt-get install -y libpq5 RUN apt-get update && apt-get install -y libpq5
@ -23,4 +26,4 @@ COPY . /takahe
WORKDIR /takahe WORKDIR /takahe
EXPOSE 8000 EXPOSE 8000
CMD ["/takahe/scripts/start.sh"] CMD ["/takahe/docker/start.sh"]

View File

@ -19,7 +19,7 @@ services:
build: . build: .
image: tahake:latest image: tahake:latest
environment: environment:
- "DJANGO_SETTINGS_MODULE=takahe.settings" - "DJANGO_SETTINGS_MODULE=takahe.settings.production"
- "SECRET_KEY=insecure_secret" - "SECRET_KEY=insecure_secret"
- "POSTGRES_HOST=db" - "POSTGRES_HOST=db"
- "POSTGRES_DB=tahake" - "POSTGRES_DB=tahake"

View File

@ -6,7 +6,7 @@ import sys
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings.production")
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:

View File

@ -12,3 +12,4 @@ psycopg2~=2.9.5
bleach~=5.0.1 bleach~=5.0.1
pytest-django~=4.5.2 pytest-django~=4.5.2
pytest-httpx~=0.21 pytest-httpx~=0.21
pydantic~=1.10.2

View File

@ -9,7 +9,7 @@ multi_line_output = 3
[tool:pytest] [tool:pytest]
addopts = --tb=short addopts = --tb=short
DJANGO_SETTINGS_MODULE = takahe.settings DJANGO_SETTINGS_MODULE = takahe.settings.testing
filterwarnings = filterwarnings =
ignore:There is no current event loop ignore:There is no current event loop

View File

@ -11,6 +11,6 @@ import os
from django.core.asgi import get_asgi_application from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings.production")
application = get_asgi_application() application = get_asgi_application()

View File

View File

@ -1,17 +1,7 @@
import os import os
from pathlib import Path from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent.parent
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY", "insecure_secret")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ["*"]
CSRF_TRUSTED_ORIGINS = ["http://*", "https://*"]
# Application definition # Application definition
@ -30,7 +20,6 @@ INSTALLED_APPS = [
] ]
MIDDLEWARE = [ MIDDLEWARE = [
"core.middleware.AlwaysSecureMiddleware",
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware", "django.middleware.common.CommonMiddleware",
@ -115,9 +104,4 @@ STATICFILES_DIRS = [
BASE_DIR / "static", BASE_DIR / "static",
] ]
CRISPY_FAIL_SILENTLY = not DEBUG ALLOWED_HOSTS = ["*"]
SITE_NAME = "takahē"
DEFAULT_DOMAIN = "feditest.aeracode.org"
ALLOWED_DOMAINS = ["feditest.aeracode.org"]
IDENTITY_MAX_AGE = 24 * 60 * 60

View File

@ -0,0 +1,13 @@
import os
from .base import * # noqa
# Load secret key from environment with a fallback
SECRET_KEY = os.environ.get("TAKAHE_SECRET_KEY", "insecure_secret")
# Disable the CRSF origin protection
MIDDLEWARE.insert(0, "core.middleware.AlwaysSecureMiddleware")
# Ensure debug features are on
DEBUG = True
CRISPY_FAIL_SILENTLY = False

View File

@ -0,0 +1,17 @@
import os
from .base import * # noqa
# Load secret key from environment
try:
SECRET_KEY = os.environ["TAKAHE_SECRET_KEY"]
except KeyError:
print("You must specify the TAKAHE_SECRET_KEY environment variable!")
os._exit(1)
# Ensure debug features are off
DEBUG = False
CRISPY_FAIL_SILENTLY = True
# TODO: Allow better setting of allowed_hosts, if we need to
ALLOWED_HOSTS = ["*"]

View File

@ -0,0 +1,4 @@
from .base import * # noqa
# Fixed secret key
SECRET_KEY = "testing_secret"

View File

@ -11,6 +11,6 @@ import os
from django.core.wsgi import get_wsgi_application from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings.production")
application = get_wsgi_application() application = get_wsgi_application()

View File

@ -1,13 +1,13 @@
import string import string
from django import forms from django import forms
from django.conf import settings
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import Http404 from django.http import Http404
from django.shortcuts import redirect from django.shortcuts import redirect
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.generic import FormView, TemplateView, View from django.views.generic import FormView, TemplateView, View
from core.config import Config
from core.forms import FormHelper from core.forms import FormHelper
from users.decorators import identity_required from users.decorators import identity_required
from users.models import Domain, Follow, Identity, IdentityStates from users.models import Domain, Follow, Identity, IdentityStates
@ -26,7 +26,7 @@ class ViewIdentity(TemplateView):
fetch=True, fetch=True,
) )
posts = identity.posts.all()[:100] posts = identity.posts.all()[:100]
if identity.data_age > settings.IDENTITY_MAX_AGE: if identity.data_age > Config.load().IDENTITY_MAX_AGE:
identity.transition_perform(IdentityStates.outdated) identity.transition_perform(IdentityStates.outdated)
return { return {
"identity": identity, "identity": identity,