"""
Django settings for shoply project.
Generated by 'django-admin startproject' using Django 5.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
from pathlib import Path
from logging.handlers import RotatingFileHandler
import os
import dj_database_url
# Load the environment variables from .env file
if os.path.exists(".env"):
from dotenv import load_dotenv
load_dotenv()
# Access the environment variables using os.getenv()
KAGGLE_USERNAME = os.getenv('KAGGLE_USERNAME')
KAGGLE_KEY = os.getenv('KAGGLE_KEY')
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DEBUG", False)
ALLOWED_HOSTS = []
CSRF_TRUSTED_ORIGINS = []
host = os.environ.get("HOST")
if host:
ALLOWED_HOSTS.append(host)
CSRF_TRUSTED_ORIGINS.append(f"https://{host}")
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'allauth', # required for 'allauth' functionality
'allauth.account', # required for 'allauth' account management
'allauth.socialaccount', # required for 'allauth' social media auth
# Project Applications
'shoply',
'home',
'products',
'bag',
'checkout',
'profiles',
'user_activity',
# Other
'crispy_forms',
'crispy_bootstrap5',
]
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',
# Allauth account middleware
"allauth.account.middleware.AccountMiddleware",
]
ROOT_URLCONF = 'shoply.urls'
CRISPY_TEMPLATE_PACK = 'bootstrap5'
CRISPY_ALLOWED_TEMPLATE_PACKS = 'bootstrap5'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'templates', 'allauth'),
],
'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',
'django.template.context_processors.media',
'products.contexts.categories_processor',
'bag.contexts.bag_contents',
],
'builtins': [
'crispy_forms.templatetags.crispy_forms_tags',
'crispy_forms.templatetags.crispy_forms_field',
]
},
},
]
# Message Storing
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by email
'allauth.account.auth_backends.AuthenticationBackend',
]
# Allauth regular account configuration
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_SIGNUP_EMAIL_ENTER_TWICE = True
TrueACCOUNT_USERNAME_MIN_LENGTH = 4
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
# Web Server Gateway Interface
WSGI_APPLICATION = 'shoply.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
if "DATABASE_URL" in os.environ:
DATABASES = {
'default': dj_database_url.parse(os.environ.get("DATABASE_URL"))
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/5.1/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/5.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), )
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Loggers Settings
# Define the logs directory relative to BASE_DIR
LOGGING_DIR = os.path.join(BASE_DIR, 'logs')
if not os.path.exists(LOGGING_DIR):
os.makedirs(LOGGING_DIR) # Create logs directory if it doesn't exist
# Loggers Configuration
LOGGING = {
'version': 1, # Use version 1 for Django logging configuration
'disable_existing_loggers': False, # Keeps the default Django loggers
'formatters': {
'verbose': { # Defines a format for more detailed logging output
'format': '[{asctime}] {levelname} {module}: {message}',
'style': '{', # Allows Python 3's `{}` string formatting
'datefmt': '%d/%m/%Y %H:%M:%S', # UK format: DD/MM/YYYY HH:MM:SS
},
'simple': { # Simpler format for quick debugging
'format': '{levelname}: {message}',
'style': '{',
},
},
'handlers': {
'console': { # Console handler to output logs to the terminal
'level': 'DEBUG', # Capture all logs at set level and below
'class': 'logging.StreamHandler',
'formatter': 'simple', # Uses the 'simple' formatter defined above
},
'file': { # File handler to write logs to a file
'level': 'WARNING', # Capture all logs at set level and above
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs/django.log'),
'maxBytes': 1024 * 1024 * 10, # 10 MB
'backupCount': 5, # Keep 5 old log files
'formatter': 'verbose',
},
},
'loggers': {
'': { # Root logger - captures all logs
'handlers': ['console', 'file'], # Logs to both console and file
'level': 'INFO', # Default log level
},
'django': { # Logger specifically for Django logs
'handlers': ['console', 'file'],
'level': 'INFO',
'propagate': False, # Stops logs from propagating to the root
},
'django.request': { # Logger specifically for HTTP request logs
'handlers': ['file'],
'level': 'ERROR', # Log only errors related to requests
'propagate': False,
},
'shoply': { # Custom logger you can use in your applications
'handlers': ['console', 'file'],
'level': 'DEBUG', # Set lower for detailed logs when debugging
'propagate': False,
},
},
}
# Delivery Variables
FREE_DELIVERY_THRESHOLD = 50
STANDARD_DELIVERY_PERCENTAGE = 10
# Stripe
STRIPE_CURRENCY = 'gbp'
STRIPE_PUBLISHABLE_KEY = os.getenv('STRIPE_PUBLISHABLE_KEY', '')
STRIPE_SECRET_KEY = os.getenv('STRIPE_SECRET_KEY', '')
if 'DEVELOPMENT' in os.environ:
STRIPE_WH_SECRET = os.getenv('STRIPE_WH_SECRET', '')
else:
STRIPE_WH_SECRET = os.getenv('STRIPE_WH_SECRET_DEP', '')
# Back-End Email Config
if 'DEVELOPMENT' in os.environ:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DEFAULT_FROM_EMAIL = 'shoply@example.com'
else:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASS')
DEFAULT_FROM_EMAIL = os.environ.get('EMAIL_HOST_USER')