import os import dj_database_url import djp # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get("DJANGO_SECRET") or "dev-secret-s(p7%ue-l6r^&@y63p*ix*1" SCREENSHOT_SECRET = os.environ.get("SCREENSHOT_SECRET") or "" GH_API_SIMONW_PRIVATE_MONTHLY = os.environ.get( "GH_API_SIMONW_PRIVATE_MONTHLY", "" ).strip() # CSRF trusted origins CSRF_TRUSTED_ORIGINS = [] if os.environ.get("CSRF_TRUSTED_ORIGINS"): CSRF_TRUSTED_ORIGINS = os.environ["CSRF_TRUSTED_ORIGINS"].split(",") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = bool(os.environ.get("DJANGO_DEBUG")) INTERNAL_IPS = ("127.0.0.1",) STAGING = bool(os.environ.get("STAGING")) # Django SQL Dashboard DASHBOARD_ROW_LIMIT = 200 # Cloudflare details CLOUDFLARE_API_TOKEN = os.environ.get("CLOUDFLARE_API_TOKEN", "") CLOUDFLARE_ZONE_ID = os.environ.get("CLOUDFLARE_ZONE_ID", "") # https://github.com/simonw/simonwillisonblog/issues/498 SECURE_CROSS_ORIGIN_OPENER_POLICY = False # https://github.com/simonw/simonwillisonblog/issues/558 SECURE_REFERRER_POLICY = "strict-origin-when-cross-origin" # SESSION_COOKIE_DOMAIN if os.environ.get("SESSION_COOKIE_DOMAIN"): SESSION_COOKIE_DOMAIN = os.environ["SESSION_COOKIE_DOMAIN"] if os.environ.get("SESSION_COOKIE_SECURE"): SESSION_COOKIE_SECURE = True # Application definition INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "django.contrib.postgres", "django_sql_dashboard", "django.contrib.humanize", "django_hosts", "blog", "guides", "monthly", "redirects", "shortener", "feedstats", "django_http_debug", "s3_web_manager_django", ] MIDDLEWARE = [ "redirects.middleware.block_nested_percent_encoding_middleware", "redirects.middleware.herokuapp_redirect_middleware", "django_hosts.middleware.HostsRequestMiddleware", "redirects.middleware.redirect_middleware", "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django_http_debug.middleware.DebugMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "blog.middleware.AmpersandRedirectMiddleware", "django_hosts.middleware.HostsResponseMiddleware", # "django.middleware.clickjacking.XFrameOptionsMiddleware", ] if DEBUG: try: import debug_toolbar MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"] INSTALLED_APPS += ["debug_toolbar"] except ImportError: pass MIDDLEWARE += ["whitenoise.middleware.WhiteNoiseMiddleware"] # Sentry SENTRY_DSN = os.environ.get("SENTRY_DSN") if SENTRY_DSN: INSTALLED_APPS += ("raven.contrib.django.raven_compat",) RAVEN_CONFIG = { "dsn": SENTRY_DSN, "release": os.environ.get("HEROKU_SLUG_COMMIT", ""), } ROOT_URLCONF = "config.urls" ROOT_HOSTCONF = "config.hosts" DEFAULT_HOST = "www" 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", "blog.context_processors.all", ], }, }, ] WSGI_APPLICATION = "config.wsgi.application" # Database # https://docs.djangoproject.com/en/1.8/ref/settings/#databases DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "simonwillisonblog", }, "dashboard": { "ENGINE": "django.db.backends.postgresql", "NAME": "simonwillisonblog", "OPTIONS": { "options": "-c default_transaction_read_only=on -c statement_timeout=3000" }, }, } # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ LANGUAGE_CODE = "en-us" TIME_ZONE = "UTC" USE_I18N = True USE_L10N = True USE_TZ = True DEFAULT_AUTO_FIELD = "django.db.models.AutoField" DASHBOARD_ENABLE_FULL_EXPORT = True if "DATABASE_URL" in os.environ: # Parse database configuration from $DATABASE_URL DATABASES["default"] = dj_database_url.config() DATABASES["dashboard"] = dj_database_url.config() DATABASES["dashboard"]["OPTIONS"] = { "options": "-c default_transaction_read_only=on -c statement_timeout=3000" } if "DISABLE_AUTOCOMMIT" in os.environ: DATABASES["default"]["AUTOCOMMIT"] = False # Honor the 'X-Forwarded-Proto' header for request.is_secure() SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") # Allow all host headers ALLOWED_HOSTS = ["*"] # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.7/howto/static-files/ BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") STATIC_URL = "/static/" STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),) # Simplified static file serving. # https://warehouse.python.org/project/whitenoise/ # STATICFILES_STORAGE was removed in Django 5.1; STORAGES is its replacement STORAGES = { "default": { "BACKEND": "django.core.files.storage.FileSystemStorage", }, "staticfiles": { "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", }, } # urls.W002 # Your URL pattern '^/?archive/(\d{4})/(\d{2})/(\d{2})/$' has a regex beginning # with a '/'. Remove this slash as it is unnecessary. If this pattern is # targeted in an include(), ensure the include() pattern has a trailing '/'. # This is deliberate (we get hits to //archive/ for some reason) so I'm # silencing the warning: SILENCED_SYSTEM_CHECKS = ("urls.W002",) # Caching CACHES = { "default": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", } } S3_WEB_MANAGER_PERMISSION = ( lambda request: request.user.is_authenticated and request.user.is_superuser ) S3_WEB_MANAGER_PUBLIC_URL_BASE = "https://static.simonwillison.net/" for _name in ( "S3_WEB_MANAGER_BUCKET_NAME", "S3_WEB_MANAGER_REGION_NAME", "S3_WEB_MANAGER_AWS_ACCESS_KEY_ID", "S3_WEB_MANAGER_AWS_SECRET_ACCESS_KEY", "S3_WEB_MANAGER_PUBLIC_URL_BASE", ): if _name in os.environ: globals()[_name] = os.environ[_name] djp.settings(globals())