From 02c7cedf4fb42121c264e6d4862e53d92ca3ebbd Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" Date: Fri, 10 Dec 2021 12:37:28 +0900 Subject: [PATCH] update django configuration (fixes #30) --- db/manage.py | 22 ++++++-- db/project_name/asgi.py | 16 ++++++ db/project_name/settings/base.py | 91 +++++++++++++++++++++----------- db/project_name/settings/dev.py | 12 ++--- db/project_name/settings/prod.py | 2 +- db/project_name/urls.py | 2 +- db/project_name/wsgi.py | 4 +- 7 files changed, 102 insertions(+), 47 deletions(-) create mode 100644 db/project_name/asgi.py diff --git a/db/manage.py b/db/manage.py index cd52a38..2c5b9ea 100755 --- a/db/manage.py +++ b/db/manage.py @@ -1,10 +1,22 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" import os import sys -if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.dev") - - from django.core.management import execute_from_command_line +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings.dev') + 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/db/project_name/asgi.py b/db/project_name/asgi.py new file mode 100644 index 0000000..5f5f5d4 --- /dev/null +++ b/db/project_name/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for {{ project_name }} 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.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings.prod') + +application = get_asgi_application() diff --git a/db/project_name/settings/base.py b/db/project_name/settings/base.py index 747d667..ac55dd8 100644 --- a/db/project_name/settings/base.py +++ b/db/project_name/settings/base.py @@ -1,26 +1,25 @@ """ Django settings for {{ project_name }} project. -Based on the Django 2.0 template, with wq-specific modifications noted as such. +Based on the Django 3.2 template, with wq-specific modifications noted as such. Generated by 'wq create' {{ wq_create_version }}. For more information on this file, see -https://docs.djangoproject.com/en/2.0/topics/settings/ +https://docs.djangoproject.com/en/3.2/topics/settings/ For the full list of settings and their values, see -https://docs.djangoproject.com/en/2.0/ref/settings/ +https://docs.djangoproject.com/en/3.2/ref/settings/ For more information about wq.db's Django settings see -http://wq.io/docs/settings +https://wq.io/wq.db/settings """ -import os -from os.path import dirname +from pathlib import Path -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -# wq: extra dirname()s to account for db/ and settings/ folders -BASE_DIR = dirname(dirname(dirname(dirname(os.path.abspath(__file__))))) +# Build paths inside the project like this: BASE_DIR / 'subdir'. +# wq: extra .parent.parent to account for db/ and settings/ folders +BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent # wq: SECRET_KEY, DEBUG, and ALLOWED_HOSTS are defined in dev.py/prod.py @@ -56,20 +55,45 @@ ROOT_URLCONF = '{{ project_name }}.urls' -# wq: Recommended settings for Django and rest_framework -from wq.db.default_settings import ( - TEMPLATES, - SESSION_COOKIE_HTTPONLY, - REST_FRAMEWORK, -) -if TEMPLATES[0]['BACKEND'] == 'django_mustache.Mustache': - TEMPLATES = TEMPLATES[1:] - -# wq: Recommended settings unique to wq.db -from wq.db.default_settings import ( - ANONYMOUS_PERMISSIONS, - SRID, -) +# wq: Leverage wq.db for Django REST Framework defaults +REST_FRAMEWORK = { + + 'DEFAULT_RENDERER_CLASSES': ( + 'wq.db.rest.renderers.HTMLRenderer', + 'wq.db.rest.renderers.JSONRenderer', + 'wq.db.rest.renderers.GeoJSONRenderer', + ), + + 'DEFAULT_PAGINATION_CLASS': 'wq.db.rest.pagination.Pagination', + 'PAGE_SIZE': 50, + + 'DEFAULT_PERMISSION_CLASSES': ( + 'wq.db.rest.permissions.ModelPermissions', + ), + + 'DEFAULT_FILTER_BACKENDS': ( + 'wq.db.rest.filters.FilterBackend', + ), + + 'DEFAULT_CONTENT_NEGOTIATION_CLASS': + 'wq.db.rest.negotiation.ContentNegotiation' +} + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + '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 = '{{ project_name }}.wsgi.application' @@ -77,7 +101,7 @@ # wq: DATABASES is defined in dev.py/prod.py # Password validation -# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators +# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { @@ -96,7 +120,7 @@ # Internationalization -# https://docs.djangoproject.com/en/2.0/topics/i18n/ +# https://docs.djangoproject.com/en/3.2/topics/i18n/ LANGUAGE_CODE = 'en-us' @@ -110,17 +134,22 @@ # Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/2.0/howto/static-files/ +# https://docs.djangoproject.com/en/3.2/howto/static-files/ STATIC_URL = '/static/'{% if not with_npm %} STATICFILES_DIRS = [ - ('app', os.path.join(BASE_DIR, 'app')) + ('app', BASE_DIR / 'app'), ]{% endif %} # wq: Configure paths for default project layout PROJECT_NAME = '{{ title }}' -STATIC_ROOT = os.path.join(BASE_DIR, 'htdocs', 'static') -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') -WQ_APP_TEMPLATE = os.path.join(BASE_DIR, 'htdocs', 'index.html') -VERSION_TXT = os.path.join(BASE_DIR, 'version.txt') +STATIC_ROOT = BASE_DIR / 'htdocs' / 'static' +MEDIA_ROOT = BASE_DIR / 'media' +WQ_APP_TEMPLATE = BASE_DIR / 'htdocs' / 'index.html' +VERSION_TXT = BASE_DIR / 'version.txt' MEDIA_URL = '/media/' + +# Default primary key field type +# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/db/project_name/settings/dev.py b/db/project_name/settings/dev.py index f5f6c30..9f5403d 100644 --- a/db/project_name/settings/dev.py +++ b/db/project_name/settings/dev.py @@ -1,4 +1,3 @@ -import os import sys import mimetypes from .base import * @@ -16,26 +15,25 @@ if DEBUG_WITH_RUNSERVER: {% if with_npm %} STATICFILES_DIRS = [ - os.path.join(BASE_DIR, 'app', 'build', 'static') + BASE_DIR / 'app' / 'build' / 'static' ] - WQ_CONFIG_FILE = os.path.join(BASE_DIR, 'app', 'src', 'data', 'config.js') + WQ_CONFIG_FILE = BASE_DIR / 'app' / 'src' / 'data' / 'config.js' {% else %} - WQ_CONFIG_FILE = os.path.join(BASE_DIR, 'app', 'js', 'data', 'config.js') + WQ_CONFIG_FILE = BASE_DIR / 'app' / 'js' / 'data' / 'config.js' {% endif %} mimetypes.add_type("application/javascript", ".js", True) ALLOWED_HOSTS = [] # Database -# https://docs.djangoproject.com/en/2.0/ref/settings/#databases - +# https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES = { 'default': { {% if with_gis %}'ENGINE': 'django.contrib.gis.db.backends.spatialite', {% else %}'ENGINE': 'django.db.backends.sqlite3', # To enable GeoDjango: # 'ENGINE': 'django.contrib.gis.db.backends.spatialite', - {% endif %}'NAME': os.path.join(BASE_DIR, 'conf', '{{ project_name }}.sqlite3'), + {% endif %}'NAME': BASE_DIR / 'conf' / '{{ project_name }}.sqlite3', } } diff --git a/db/project_name/settings/prod.py b/db/project_name/settings/prod.py index 36b13ad..24a0314 100644 --- a/db/project_name/settings/prod.py +++ b/db/project_name/settings/prod.py @@ -14,7 +14,7 @@ # Database -# https://docs.djangoproject.com/en/2.0/ref/settings/#databases +# https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES = { 'default': { diff --git a/db/project_name/urls.py b/db/project_name/urls.py index be60019..ae275df 100644 --- a/db/project_name/urls.py +++ b/db/project_name/urls.py @@ -1,7 +1,7 @@ """{{ project_name }} URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/2.0/topics/http/urls/ + https://docs.djangoproject.com/en/3.2/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views diff --git a/db/project_name/wsgi.py b/db/project_name/wsgi.py index 7527ff9..103bf17 100644 --- a/db/project_name/wsgi.py +++ b/db/project_name/wsgi.py @@ -4,13 +4,13 @@ It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see -https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ +https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.prod") +os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings.prod') application = get_wsgi_application()