import os from pathlib import Path from decouple import Config, Csv, RepositoryEnv from datetime import timedelta # This is for the JWT token expiration time # from pymongo import MongoClient #this is mongo from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi from dotenv import load_dotenv #i'm having problems getting in env # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent env_path = BASE_DIR / '.env' config = Config(RepositoryEnv(env_path)) # 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 = config('DJANGO_SECRET_KEY') # config(): get the value of the key from the .env file. Config() is the class itself # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config('DJANGO_DEBUG', default=False, cast=bool) ALLOWED_HOSTS = config('DJANGO_ALLOWED_HOSTS', default='localhost', cast=Csv()) # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'rest_framework_simplejwt', 'corsheaders', 'users', 'drf_yasg', 'rest_framework_simplejwt.token_blacklist', # Invalidates tokens (e.g., after logout) 'api_keys', 'map', ] CORS_ALLOW_CREDENTIALS = config('CORS_ALLOW_CREDENTIALS', default=True, cast=bool) CORS_ALLOW_HEADERS = [ 'content-type', 'x-api-key', ] # Update by adding to env template when needed CORS_ALLOWED_ORIGINS = config('CORS_ALLOWED_ORIGINS', default='http://localhost:3000', cast=Csv()) MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', '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 = 'sightseeing_map.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(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 = 'sightseeing_map.wsgi.application' # Database: See .env.template for the database configuration # https://docs.djangoproject.com/en/5.1/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } load_dotenv() #try to load from env MONGO_URI = os.getenv("MONGO_URI") client = MongoClient(MONGO_URI) MONGO_DB = client["sightseeing_maps"] #database_name # 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 = config('DJANGO_TIME_ZONE', default='UTC') USE_I18N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.1/howto/static-files/ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/' MEDIA_URL = '/media/' MEDIA_ROOT = 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' # REST Framework settings REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), # This is to make sure that all endpoints are protected by default } # Simple JWT settings SIMPLE_JWT = { "ACCESS_TOKEN_LIFETIME": timedelta(minutes=5), "REFRESH_TOKEN_LIFETIME": timedelta(days=1), "AUTH_HEADER_TYPES": ("Bearer",), "ROTATE_REFRESH_TOKENS": True, "BLACKLIST_AFTER_ROTATION": True, "SIGNING_KEY": SECRET_KEY, "ALGORITHM": "HS256", } # Email settings EMAIL_BACKEND = config('DJANGO_EMAIL_BACKEND', default='django.core.mail.backends.smtp.EmailBackend') EMAIL_HOST = config('DJANGO_EMAIL_HOST', default='localhost') EMAIL_PORT = config('DJANGO_EMAIL_PORT', default=587, cast=int) EMAIL_USE_TLS = config('DJANGO_EMAIL_USE_TLS', default=True, cast=bool) EMAIL_USE_SSL = config('DJANGO_EMAIL_USE_SSL', default=False, cast=bool) EMAIL_HOST_USER = config('DJANGO_EMAIL_HOST_USER', default='') EMAIL_HOST_PASSWORD = config('DJANGO_EMAIL_HOST_PASSWORD', default='') DEFAULT_FROM_EMAIL = config('DJANGO_DEFAULT_FROM_EMAIL', default='webmaster@localhost') # Frontend URL FRONTEND_URL = config('FRONTEND_URL', default='http://localhost:3000')