Skip to content

Commit

Permalink
Use .env files to load settings, add .env_example
Browse files Browse the repository at this point in the history
  • Loading branch information
pxwxnvermx committed Jan 10, 2024
1 parent 998e088 commit 2f29c4c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 90 deletions.
8 changes: 8 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SECRET_KEY=changeme
DEBUG=true
DATABASE_URL=postgres:///connect
DJANGO_ALLOWED_HOSTS=
TWILIO_ACCOUNT_SID=
TWILIO_AUTH_TOKEN=
TWILIO_MESSAGING_SERVICE=
FCM_CREDENTIALS=
200 changes: 110 additions & 90 deletions connectid/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,83 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import os
import environ

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

env = environ.Env()
env.read_env(str(BASE_DIR / ".env"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'phonenumber_field',
'users.apps.UsersConfig',
'messaging',
'oauth2_provider',
'rest_framework',
'fcm_django',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"phonenumber_field",
"users.apps.UsersConfig",
"messaging",
"oauth2_provider",
"rest_framework",
"fcm_django",
]

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

ROOT_URLCONF = 'connectid.urls'
ROOT_URLCONF = "connectid.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 = 'connectid.wsgi.application'


WSGI_APPLICATION = "connectid.wsgi.application"


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'users.validators.EntropyPasswordValidator',
'OPTIONS': {
'min_strength': 2
}
"NAME": "users.validators.EntropyPasswordValidator",
"OPTIONS": {"min_strength": 2},
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

Expand All @@ -99,81 +95,77 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

AUTH_USER_MODEL = 'users.ConnectUser'
AUTH_USER_MODEL = "users.ConnectUser"

LOG_DIR = BASE_DIR
DJANGO_LOG_FILE = LOG_DIR / 'django.log'
DJANGO_LOG_FILE = LOG_DIR / "django.log"
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'django.server': {
'()': 'django.utils.log.ServerFormatter',
'format': '[{server_time}] {message}',
'style': '{',
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"django.server": {
"()": "django.utils.log.ServerFormatter",
"format": "[{server_time}] {message}",
"style": "{",
}
},
'handlers': {
'file' : {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': DJANGO_LOG_FILE,
'maxBytes': 10 * 1024 * 1024, # 10 MB
'backupCount': 20 # Backup 200 MB of logs
"handlers": {
"file": {
"level": "INFO",
"class": "logging.handlers.RotatingFileHandler",
"filename": DJANGO_LOG_FILE,
"maxBytes": 10 * 1024 * 1024, # 10 MB
"backupCount": 20, # Backup 200 MB of logs
},
'console': {
'class': 'logging.StreamHandler',
"console": {
"class": "logging.StreamHandler",
},
'django.server': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'django.server',
"django.server": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "django.server",
},
},
'loggers': {
'django': {
'handlers': ['file', 'console'],
'level': 'INFO',
'propagate': True,
"loggers": {
"django": {
"handlers": ["file", "console"],
"level": "INFO",
"propagate": True,
},
'django.server': {
'handlers': ['django.server'],
'level': 'INFO',
'propagate': False,
"django.server": {
"handlers": ["django.server"],
"level": "INFO",
"propagate": False,
},
},
}


REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.BasicAuthentication",
"oauth2_provider.contrib.rest_framework.OAuth2Authentication",
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
],
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
"DEFAULT_THROTTLE_CLASSES": [
"rest_framework.throttling.AnonRateThrottle",
"rest_framework.throttling.UserRateThrottle",
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}

"DEFAULT_THROTTLE_RATES": {"anon": "100/day", "user": "1000/day"},
}


LOGIN_URL = '/admin/login/'
LOGIN_URL = "/admin/login/"

OAUTH2_PROVIDER = {
"OIDC_ENABLED": True,
Expand All @@ -196,10 +188,38 @@
"DELETE_INACTIVE_DEVICES": False,
}

from .localsettings import *
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env(
"SECRET_KEY",
default="django-insecure-yofpqrszrdtv0ftihjd09cuim2al9^n9j^b85%-y0v*^_lj18d",
)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("DEBUG", default=False)

# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
"default": env.db(
"DATABASE_URL",
default="postgres:///connect",
),
}

ALLOWED_HOSTS = ["127.0.0.1", "localhost"] + env.list(
"DJANGO_ALLOWED_HOSTS", default=[]
)

TWILIO_ACCOUNT_SID = env("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = env("TWILIO_AUTH_TOKEN")
TWILIO_MESSAGING_SERVICE = env("TWILIO_MESSAGING_SERVICE")

FCM_CREDENTIALS = env("FCM_CREDENTIALS", default=None)

# Firebase
if FCM_CREDENTIALS:
from firebase_admin import credentials, initialize_app

creds = credentials.Certificate(FCM_CREDENTIALS)
default_app = initialize_app(credential=creds)

0 comments on commit 2f29c4c

Please sign in to comment.