From f7a3842ffbd649d45a89c1262ee53e1fb07ac14b Mon Sep 17 00:00:00 2001 From: Matthew Cengia Date: Wed, 17 Jan 2024 13:25:57 +1100 Subject: [PATCH] ruff format --- django/manage.py | 4 +- django/myproject/asgi.py | 2 +- django/myproject/settings.py | 134 ++++++++++++++++-------------- django/myproject/urls.py | 27 +++--- django/myproject/wsgi.py | 2 +- django/scripts/createsuperuser.py | 13 ++- 6 files changed, 94 insertions(+), 88 deletions(-) diff --git a/django/manage.py b/django/manage.py index 92bb9a3..893db8e 100755 --- a/django/manage.py +++ b/django/manage.py @@ -6,7 +6,7 @@ def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") try: from django.core.management import execute_from_command_line except ImportError as exc: @@ -18,5 +18,5 @@ def main(): execute_from_command_line(sys.argv) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/django/myproject/asgi.py b/django/myproject/asgi.py index 63130be..ab19189 100644 --- a/django/myproject/asgi.py +++ b/django/myproject/asgi.py @@ -11,6 +11,6 @@ from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") application = get_asgi_application() diff --git a/django/myproject/settings.py b/django/myproject/settings.py index c2b856f..ae8477e 100644 --- a/django/myproject/settings.py +++ b/django/myproject/settings.py @@ -13,17 +13,18 @@ from pathlib import Path import environ + env = environ.Env( # set casting, default value DEBUG=(bool, False), - TIME_ZONE=(str, 'UTC') + TIME_ZONE=(str, "UTC"), ) # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # reading .env file -environ.Env.read_env(str(BASE_DIR.parent / '.env')) +environ.Env.read_env(str(BASE_DIR.parent / ".env")) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/ @@ -31,90 +32,93 @@ # SECURITY WARNING: keep the secret key used in production secret! # Raises django's ImproperlyConfigured exception if SECRET_KEY not in # os.environ -SECRET_KEY = env('SECRET_KEY') +SECRET_KEY = env("SECRET_KEY") # False if not in os.environ # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = env('DEBUG') +DEBUG = env("DEBUG") -ALLOWED_HOSTS = ['localhost'] + env('ALLOWED_HOSTS').split(',') -CSRF_TRUSTED_ORIGINS = env('CSRF_TRUSTED_ORIGINS').split(',') +ALLOWED_HOSTS = ["localhost"] + env("ALLOWED_HOSTS").split(",") +CSRF_TRUSTED_ORIGINS = env("CSRF_TRUSTED_ORIGINS").split(",") # Application definition INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", # Add gis module for geospatial projects #'django.contrib.gis', # Include this to build REST APIs - 'rest_framework', + "rest_framework", # Include this to build on top of Boostrap 5 - 'django_bootstrap5', + "django_bootstrap5", # Include this to add history to models - 'simple_history', + "simple_history", # Include extras to make working with Django's CLI etc easier - 'django_extensions', + "django_extensions", # Uncomment the below line and replace 'myapp' with the name of your app #'myapp', ] 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', + "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", # Include this to add history to models - 'simple_history.middleware.HistoryRequestMiddleware', + "simple_history.middleware.HistoryRequestMiddleware", ] -ROOT_URLCONF = 'myproject.urls' +ROOT_URLCONF = "myproject.urls" 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', + "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 = 'myproject.wsgi.application' +WSGI_APPLICATION = "myproject.wsgi.application" # Database # https://docs.djangoproject.com/en/dev/ref/settings/#databases DATABASES = { - 'default': { + "default": { # Use sqlite by default - 'ENGINE': env("DB_ENGINE", default='django.db.backends.sqlite3'), + "ENGINE": env("DB_ENGINE", default="django.db.backends.sqlite3"), # Use spatialite for spatial projects #'ENGINE': env("DB_ENGINE", default='django.contrib.gis.db.backends.spatialite'), # Use regular Postgres in production #'ENGINE': env("DB_ENGINE", default='django.db.backends.postgresql_psycopg2'), # Use PostGIS in production for spatial projects #'ENGINE': env("DB_ENGINE", default='django.contrib.gis.db.backends.postgis'), - 'NAME': env("POSTGRES_DB", default=env("DB_NAME", default=str(BASE_DIR.parent / 'db/db.sqlite3'))), - 'USER': env("POSTGRES_USER", default="nobody"), - 'PASSWORD': env("POSTGRES_PASSWORD", default="insecure"), - 'HOST': env("POSTGRES_HOST", default="localhost"), - 'PORT': env("POSTGRES_PORT", default="5432"), + "NAME": env( + "POSTGRES_DB", + default=env("DB_NAME", default=str(BASE_DIR.parent / "db/db.sqlite3")), + ), + "USER": env("POSTGRES_USER", default="nobody"), + "PASSWORD": env("POSTGRES_PASSWORD", default="insecure"), + "HOST": env("POSTGRES_HOST", default="localhost"), + "PORT": env("POSTGRES_PORT", default="5432"), } } @@ -124,16 +128,16 @@ AUTH_PASSWORD_VALIDATORS = [ { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", }, ] @@ -141,7 +145,7 @@ # Internationalization # https://docs.djangoproject.com/en/dev/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = "en-us" TIME_ZONE = env("TIME_ZONE") @@ -155,24 +159,26 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/dev/howto/static-files/ -STATIC_URL = '/static/' -STATIC_ROOT = str(BASE_DIR.parent / 'static') -MEDIA_URL = '/media/' -MEDIA_ROOT = str(BASE_DIR.parent / 'media') +STATIC_URL = "/static/" +STATIC_ROOT = str(BASE_DIR.parent / "static") +MEDIA_URL = "/media/" +MEDIA_ROOT = str(BASE_DIR.parent / "media") # Default primary key field type # https://docs.djangoproject.com/en/dev/ref/settings/#default-auto-field -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' - -EMAIL_BACKEND = env('EMAIL_BACKEND', default='django.core.mail.backends.console.EmailBackend') -EMAIL_HOST = env('EMAIL_HOST', default='mail') -EMAIL_PORT = env('EMAIL_PORT', default='587') -EMAIL_HOST_USER = env('EMAIL_HOST_USER', default='') -EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD', default='') -EMAIL_USE_TLS = env('EMAIL_USE_TLS', default=False) -EMAIL_USE_SSL = env('EMAIL_USE_SSL', default=False) -EMAIL_TIMEOUT = env('EMAIL_TIMEOUT', default=None) -EMAIL_SSL_KEYFILE = env('EMAIL_SSL_KEYFILE', default=None) -EMAIL_SSL_CERTFILE = env('EMAIL_SSL_CERTFILE', default=None) -DEFAULT_FROM_EMAIL = env('DEFAULT_FROM_EMAIL', default='webmaster@localhost') +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +EMAIL_BACKEND = env( + "EMAIL_BACKEND", default="django.core.mail.backends.console.EmailBackend" +) +EMAIL_HOST = env("EMAIL_HOST", default="mail") +EMAIL_PORT = env("EMAIL_PORT", default="587") +EMAIL_HOST_USER = env("EMAIL_HOST_USER", default="") +EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD", default="") +EMAIL_USE_TLS = env("EMAIL_USE_TLS", default=False) +EMAIL_USE_SSL = env("EMAIL_USE_SSL", default=False) +EMAIL_TIMEOUT = env("EMAIL_TIMEOUT", default=None) +EMAIL_SSL_KEYFILE = env("EMAIL_SSL_KEYFILE", default=None) +EMAIL_SSL_CERTFILE = env("EMAIL_SSL_CERTFILE", default=None) +DEFAULT_FROM_EMAIL = env("DEFAULT_FROM_EMAIL", default="webmaster@localhost") diff --git a/django/myproject/urls.py b/django/myproject/urls.py index a666956..d3a6f02 100644 --- a/django/myproject/urls.py +++ b/django/myproject/urls.py @@ -16,40 +16,41 @@ # Use this for regular Django projects from django.contrib import admin from django.contrib.auth import views as auth_views + # Use this for geospatial projects -#from django.contrib.gis import admin +# from django.contrib.gis import admin from django.urls import include, path from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path( - 'admin/password_reset/', + "admin/password_reset/", auth_views.PasswordResetView.as_view(), - name='admin_password_reset', + name="admin_password_reset", ), path( - 'admin/password_reset/done/', + "admin/password_reset/done/", auth_views.PasswordResetDoneView.as_view(), - name='password_reset_done', + name="password_reset_done", ), path( - 'reset///', + "reset///", auth_views.PasswordResetConfirmView.as_view(), - name='password_reset_confirm', + name="password_reset_confirm", ), path( - 'reset/done/', + "reset/done/", auth_views.PasswordResetCompleteView.as_view(), - name='password_reset_complete', + name="password_reset_complete", ), - path('admin/', admin.site.urls), + path("admin/", admin.site.urls), # Add this to set up authentication for REST framework - #path('api-auth/', include('rest_framework.urls')), + # path('api-auth/', include('rest_framework.urls')), # To set up your first app to manage the homepage of this site, # uncomment the below line, and replace 'myapp' with the name of your app. - #path('', include('myapp.urls')), + # path('', include('myapp.urls')), ] urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) -urlpatterns += static('docs/', document_root='/docs', show_indexes=True) +urlpatterns += static("docs/", document_root="/docs", show_indexes=True) diff --git a/django/myproject/wsgi.py b/django/myproject/wsgi.py index b3f18ee..1de9e2c 100644 --- a/django/myproject/wsgi.py +++ b/django/myproject/wsgi.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") application = get_wsgi_application() diff --git a/django/scripts/createsuperuser.py b/django/scripts/createsuperuser.py index 214b5b6..ac02214 100755 --- a/django/scripts/createsuperuser.py +++ b/django/scripts/createsuperuser.py @@ -3,20 +3,19 @@ def run(): - super_username = environ['SUPERUSER_USERNAME'] - super_password = environ['SUPERUSER_PASSWORD'] - super_email = environ['SUPERUSER_EMAIL'] + super_username = environ["SUPERUSER_USERNAME"] + super_password = environ["SUPERUSER_PASSWORD"] + super_email = environ["SUPERUSER_EMAIL"] User = get_user_model() - if (super_username != '' and super_password != '' and super_email != ''): + if super_username != "" and super_password != "" and super_email != "": has_superuser = False for u in User.objects.values(): - if u['is_superuser']: + if u["is_superuser"]: has_superuser = True break if not has_superuser: print("Creating super user: " + super_username) - User.objects.create_superuser( - super_username, super_email, super_password) + User.objects.create_superuser(super_username, super_email, super_password) else: print("Super user already exists. Not creating another.")