From 2b10b6f8cab59548ba24f0d534a4539c1c72987a Mon Sep 17 00:00:00 2001 From: Arvind-4 Date: Sat, 31 Dec 2022 16:00:06 +0530 Subject: [PATCH] Updated: backend --- backend/__init__.py | 0 backend/asgi.py | 16 +++++ backend/db/__init__.py | 0 backend/db/postgres_db.py | 31 ++++++++++ backend/settings/__init__.py | 9 +++ backend/settings/base.py | 107 +++++++++++++++++++++++++++++++++ backend/settings/local.py | 32 ++++++++++ backend/settings/production.py | 21 +++++++ backend/urls.py | 25 ++++++++ backend/wsgi.py | 16 +++++ 10 files changed, 257 insertions(+) create mode 100644 backend/__init__.py create mode 100644 backend/asgi.py create mode 100644 backend/db/__init__.py create mode 100644 backend/db/postgres_db.py create mode 100644 backend/settings/__init__.py create mode 100644 backend/settings/base.py create mode 100644 backend/settings/local.py create mode 100644 backend/settings/production.py create mode 100644 backend/urls.py create mode 100644 backend/wsgi.py diff --git a/backend/__init__.py b/backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/asgi.py b/backend/asgi.py new file mode 100644 index 0000000..14a707e --- /dev/null +++ b/backend/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for backend 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', 'backend.settings') + +application = get_asgi_application() diff --git a/backend/db/__init__.py b/backend/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/db/postgres_db.py b/backend/db/postgres_db.py new file mode 100644 index 0000000..21c5b1b --- /dev/null +++ b/backend/db/postgres_db.py @@ -0,0 +1,31 @@ +from decouple import config + +DJANGO_PG_DATABASE = config('DJANGO_PG_DATABASE', cast=str) +DJANGO_PG_USER = config('DJANGO_PG_USER', cast=str) +DJANGO_PG_PASSWORD = config('DJANGO_PG_PASSWORD', cast=str) +DJANGO_PG_HOST = config('DJANGO_PG_HOST', cast=str) +DJANGO_PG_PORT = config('DJANGO_PG_PORT', cast=str, default='') + + +DB_IS_AVAILABLE = all([ + DJANGO_PG_DATABASE, + DJANGO_PG_USER, + DJANGO_PG_PASSWORD, + DJANGO_PG_HOST, + DJANGO_PG_PORT, +]) + +if DB_IS_AVAILABLE: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': DJANGO_PG_DATABASE, + 'USER': DJANGO_PG_USER, + 'PASSWORD': DJANGO_PG_PASSWORD, + 'HOST': DJANGO_PG_HOST, + 'PORT': DJANGO_PG_PORT, + } + } + + DATABASES['default']['CONN_MAX_AGE'] = None + DATABASES['default']['ATOMIC_REQUESTS'] = True \ No newline at end of file diff --git a/backend/settings/__init__.py b/backend/settings/__init__.py new file mode 100644 index 0000000..dcd3660 --- /dev/null +++ b/backend/settings/__init__.py @@ -0,0 +1,9 @@ +from decouple import config + +DJANGO_LIVE = config('DJANGO_LIVE', cast=bool) + +if DJANGO_LIVE: + from .production import * + +else: + from .local import * \ No newline at end of file diff --git a/backend/settings/base.py b/backend/settings/base.py new file mode 100644 index 0000000..3b3f73d --- /dev/null +++ b/backend/settings/base.py @@ -0,0 +1,107 @@ +""" +Django settings for sd project. + +Generated by 'django-admin startproject' using Django 3.2.6. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.2/ref/settings/ +""" + +from pathlib import Path +from django.contrib.messages import constants as messages + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent.parent + +MESSAGE_TAGS = { + messages.DEBUG: 'alert-secondary', + messages.INFO: 'alert-info', + messages.SUCCESS: 'alert-success', + messages.WARNING: 'alert-warning', + messages.ERROR: 'alert-danger', +} + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + 'landing', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'whitenoise.middleware.WhiteNoiseMiddleware', + '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 = 'backend.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 = 'backend.wsgi.application' + +# Password validation +# https://docs.djangoproject.com/en/3.2/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.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'Asia/Kolkata' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +# 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/backend/settings/local.py b/backend/settings/local.py new file mode 100644 index 0000000..fdcdae2 --- /dev/null +++ b/backend/settings/local.py @@ -0,0 +1,32 @@ +from decouple import config + +from .base import * + +SECRET_KEY = "secret_key" + +DEBUG = True + +ALLOWED_HOSTS = ['*'] + +ADMIN_URL = "admin/" + +# Database +# https://docs.djangoproject.com/en/3.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.2/howto/static-files/ + +STATIC_URL = '/static/' + +STATICFILES_DIRS = [ + BASE_DIR / 'static' +] + +STATIC_ROOT = BASE_DIR / 'staticfiles_build' / 'static' \ No newline at end of file diff --git a/backend/settings/production.py b/backend/settings/production.py new file mode 100644 index 0000000..27dee18 --- /dev/null +++ b/backend/settings/production.py @@ -0,0 +1,21 @@ +from decouple import config + +from .base import * + +SECRET_KEY = config('DJANGO_SECRET_KEY', cast=str) + +DEBUG = config('DJANGO_DEBUG', cast=bool) + +ALLOWED_HOSTS = ['*'] + +ADMIN_URL = config('DJANGO_ADMIN_URL', cast=str) + +STATIC_URL = '/static/' + +STATICFILES_DIRS = [ + BASE_DIR / 'static' +] + +STATIC_ROOT = BASE_DIR / 'staticfiles_build' / 'static' + +from backend.db.postgres_db import * # noqa \ No newline at end of file diff --git a/backend/urls.py b/backend/urls.py new file mode 100644 index 0000000..35c0306 --- /dev/null +++ b/backend/urls.py @@ -0,0 +1,25 @@ +"""backend URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.2/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 path +from django.conf import settings + +from landing.views import HomeView + +urlpatterns = [ + path(str(settings.ADMIN_URL), admin.site.urls), + path('', HomeView.as_view()), +] diff --git a/backend/wsgi.py b/backend/wsgi.py new file mode 100644 index 0000000..e40ee1e --- /dev/null +++ b/backend/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for backend 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.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings') + +application = get_wsgi_application()