2019-04-11 07:40:37 +00:00
|
|
|
__package__ = 'archivebox.core'
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2019-04-17 07:49:18 +00:00
|
|
|
import os
|
2019-04-22 23:07:39 +00:00
|
|
|
import sys
|
2021-02-16 01:51:23 +00:00
|
|
|
import re
|
|
|
|
import logging
|
2024-08-27 03:14:47 +00:00
|
|
|
import inspect
|
2021-03-27 06:17:12 +00:00
|
|
|
import tempfile
|
2024-08-23 09:01:40 +00:00
|
|
|
from typing import Any, Dict
|
2020-10-31 07:08:03 +00:00
|
|
|
|
2020-09-30 19:54:32 +00:00
|
|
|
from pathlib import Path
|
2020-04-23 01:14:43 +00:00
|
|
|
from django.utils.crypto import get_random_string
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
from ..config import CONFIG
|
|
|
|
from ..config_stubs import AttrDict
|
|
|
|
assert isinstance(CONFIG, AttrDict)
|
2019-04-17 09:42:09 +00:00
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
IS_MIGRATING = 'makemigrations' in sys.argv[:3] or 'migrate' in sys.argv[:3]
|
|
|
|
IS_TESTING = 'test' in sys.argv[:3] or 'PYTEST_CURRENT_TEST' in os.environ
|
2019-04-22 23:07:39 +00:00
|
|
|
IS_SHELL = 'shell' in sys.argv[:3] or 'shell_plus' in sys.argv[:3]
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
################################################################################
|
|
|
|
### Django Core Settings
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
WSGI_APPLICATION = 'core.wsgi.application'
|
|
|
|
ROOT_URLCONF = 'core.urls'
|
|
|
|
|
|
|
|
LOGIN_URL = '/accounts/login/'
|
2022-03-31 19:40:14 +00:00
|
|
|
LOGOUT_REDIRECT_URL = os.environ.get('LOGOUT_REDIRECT_URL', '/')
|
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
PASSWORD_RESET_URL = '/accounts/password_reset/'
|
|
|
|
APPEND_SLASH = True
|
2020-04-23 01:14:43 +00:00
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
DEBUG = CONFIG.DEBUG or ('--debug' in sys.argv)
|
2021-01-30 11:07:35 +00:00
|
|
|
|
2024-05-18 03:48:11 +00:00
|
|
|
|
2024-08-23 09:02:34 +00:00
|
|
|
BUILTIN_PLUGINS_DIR = CONFIG.PACKAGE_DIR / 'builtin_plugins'
|
|
|
|
USER_PLUGINS_DIR = CONFIG.OUTPUT_DIR / 'user_plugins'
|
|
|
|
|
|
|
|
def find_plugins(plugins_dir, prefix: str) -> Dict[str, Any]:
|
|
|
|
plugins = {
|
|
|
|
f'{prefix}.{plugin_entrypoint.parent.name}': plugin_entrypoint.parent
|
2024-05-18 03:48:11 +00:00
|
|
|
for plugin_entrypoint in plugins_dir.glob('*/apps.py')
|
|
|
|
}
|
2024-08-23 09:02:34 +00:00
|
|
|
# print(f'Found {prefix} plugins:\n', '\n '.join(plugins.keys()))
|
|
|
|
return plugins
|
2024-05-18 03:48:11 +00:00
|
|
|
|
|
|
|
INSTALLED_PLUGINS = {
|
2024-08-23 09:02:34 +00:00
|
|
|
**find_plugins(BUILTIN_PLUGINS_DIR, prefix='builtin_plugins'),
|
|
|
|
**find_plugins(USER_PLUGINS_DIR, prefix='user_plugins'),
|
2024-05-18 03:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-02 22:53:21 +00:00
|
|
|
INSTALLED_APPS = [
|
2019-04-17 07:49:18 +00:00
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
'django.contrib.sessions',
|
|
|
|
'django.contrib.messages',
|
|
|
|
'django.contrib.staticfiles',
|
2020-07-28 03:26:45 +00:00
|
|
|
'django.contrib.admin',
|
2024-05-18 03:13:54 +00:00
|
|
|
'django_jsonform',
|
2024-08-20 08:58:19 +00:00
|
|
|
|
2024-05-13 12:12:12 +00:00
|
|
|
'signal_webhooks',
|
2024-05-13 09:37:48 +00:00
|
|
|
'abid_utils',
|
2024-05-18 03:13:54 +00:00
|
|
|
'plugantic',
|
2019-04-02 22:53:21 +00:00
|
|
|
'core',
|
2024-04-09 23:29:24 +00:00
|
|
|
'api',
|
2024-08-23 09:02:34 +00:00
|
|
|
'pkgs',
|
2019-04-17 09:42:09 +00:00
|
|
|
|
2024-05-18 03:48:11 +00:00
|
|
|
*INSTALLED_PLUGINS.keys(),
|
|
|
|
|
2024-05-06 18:06:42 +00:00
|
|
|
'admin_data_views',
|
2019-04-17 07:49:18 +00:00
|
|
|
'django_extensions',
|
2019-04-02 22:53:21 +00:00
|
|
|
]
|
2021-02-16 21:23:09 +00:00
|
|
|
|
2023-09-14 09:41:27 +00:00
|
|
|
|
|
|
|
# For usage with https://www.jetadmin.io/integrations/django
|
|
|
|
# INSTALLED_APPS += ['jet_django']
|
|
|
|
# JET_PROJECT = 'archivebox'
|
|
|
|
# JET_TOKEN = 'some-api-token-here'
|
|
|
|
|
|
|
|
|
2021-02-16 21:23:09 +00:00
|
|
|
MIDDLEWARE = [
|
2021-04-10 08:16:12 +00:00
|
|
|
'core.middleware.TimezoneMiddleware',
|
2021-02-16 21:23:09 +00:00
|
|
|
'django.middleware.security.SecurityMiddleware',
|
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
2021-09-30 15:40:13 +00:00
|
|
|
'core.middleware.ReverseProxyAuthMiddleware',
|
2021-02-16 21:23:09 +00:00
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
2021-04-10 08:16:12 +00:00
|
|
|
'core.middleware.CacheControlMiddleware',
|
2021-02-16 21:23:09 +00:00
|
|
|
]
|
|
|
|
|
2023-08-17 02:53:49 +00:00
|
|
|
################################################################################
|
|
|
|
### Authentication Settings
|
|
|
|
################################################################################
|
|
|
|
|
2024-08-20 08:58:36 +00:00
|
|
|
# AUTH_USER_MODEL = 'auth.User' # cannot be easily changed unfortunately
|
|
|
|
|
2021-02-16 21:23:09 +00:00
|
|
|
AUTHENTICATION_BACKENDS = [
|
2021-09-30 15:40:13 +00:00
|
|
|
'django.contrib.auth.backends.RemoteUserBackend',
|
2021-02-16 21:23:09 +00:00
|
|
|
'django.contrib.auth.backends.ModelBackend',
|
|
|
|
]
|
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
if CONFIG.LDAP:
|
2023-10-20 01:05:59 +00:00
|
|
|
try:
|
|
|
|
import ldap
|
|
|
|
from django_auth_ldap.config import LDAPSearch
|
|
|
|
|
|
|
|
global AUTH_LDAP_SERVER_URI
|
|
|
|
global AUTH_LDAP_BIND_DN
|
|
|
|
global AUTH_LDAP_BIND_PASSWORD
|
2023-10-20 11:08:38 +00:00
|
|
|
global AUTH_LDAP_USER_SEARCH
|
|
|
|
global AUTH_LDAP_USER_ATTR_MAP
|
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
AUTH_LDAP_SERVER_URI = CONFIG.LDAP_SERVER_URI
|
|
|
|
AUTH_LDAP_BIND_DN = CONFIG.LDAP_BIND_DN
|
|
|
|
AUTH_LDAP_BIND_PASSWORD = CONFIG.LDAP_BIND_PASSWORD
|
2023-10-20 01:05:59 +00:00
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
assert AUTH_LDAP_SERVER_URI and CONFIG.LDAP_USERNAME_ATTR and CONFIG.LDAP_USER_FILTER, 'LDAP_* config options must all be set if LDAP=True'
|
2023-10-20 11:08:38 +00:00
|
|
|
|
2023-10-20 01:05:59 +00:00
|
|
|
AUTH_LDAP_USER_SEARCH = LDAPSearch(
|
2024-08-21 01:31:21 +00:00
|
|
|
CONFIG.LDAP_USER_BASE,
|
2023-10-20 01:05:59 +00:00
|
|
|
ldap.SCOPE_SUBTREE,
|
2024-08-21 01:31:21 +00:00
|
|
|
'(&(' + CONFIG.LDAP_USERNAME_ATTR + '=%(user)s)' + CONFIG.LDAP_USER_FILTER + ')',
|
2023-10-20 01:05:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP = {
|
2024-08-21 01:31:21 +00:00
|
|
|
'username': CONFIG.LDAP_USERNAME_ATTR,
|
|
|
|
'first_name': CONFIG.LDAP_FIRSTNAME_ATTR,
|
|
|
|
'last_name': CONFIG.LDAP_LASTNAME_ATTR,
|
|
|
|
'email': CONFIG.LDAP_EMAIL_ATTR,
|
2023-10-20 01:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUTHENTICATION_BACKENDS = [
|
2023-12-06 12:16:51 +00:00
|
|
|
'django.contrib.auth.backends.ModelBackend',
|
2023-10-20 01:05:59 +00:00
|
|
|
'django_auth_ldap.backend.LDAPBackend',
|
|
|
|
]
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
sys.stderr.write('[X] Error: Found LDAP=True config but LDAP packages not installed. You may need to run: pip install archivebox[ldap]\n\n')
|
|
|
|
# dont hard exit here. in case the user is just running "archivebox version" or "archivebox help", we still want those to work despite broken ldap
|
|
|
|
# sys.exit(1)
|
|
|
|
|
2023-08-17 02:53:49 +00:00
|
|
|
|
2024-04-26 00:59:54 +00:00
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
################################################################################
|
|
|
|
### Staticfile and Template Settings
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
STATIC_URL = '/static/'
|
|
|
|
|
|
|
|
STATICFILES_DIRS = [
|
2024-08-21 01:31:21 +00:00
|
|
|
*([str(CONFIG.CUSTOM_TEMPLATES_DIR / 'static')] if CONFIG.CUSTOM_TEMPLATES_DIR else []),
|
|
|
|
str(Path(CONFIG.PACKAGE_DIR) / CONFIG.TEMPLATES_DIR_NAME / 'static'),
|
2020-10-31 07:08:03 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
TEMPLATE_DIRS = [
|
2024-08-21 01:31:21 +00:00
|
|
|
*([str(CONFIG.CUSTOM_TEMPLATES_DIR)] if CONFIG.CUSTOM_TEMPLATES_DIR else []),
|
|
|
|
str(Path(CONFIG.PACKAGE_DIR) / CONFIG.TEMPLATES_DIR_NAME / 'core'),
|
|
|
|
str(Path(CONFIG.PACKAGE_DIR) / CONFIG.TEMPLATES_DIR_NAME / 'admin'),
|
|
|
|
str(Path(CONFIG.PACKAGE_DIR) / CONFIG.TEMPLATES_DIR_NAME),
|
2020-10-31 07:08:03 +00:00
|
|
|
]
|
|
|
|
|
2019-04-02 22:53:21 +00:00
|
|
|
TEMPLATES = [
|
|
|
|
{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
2020-10-31 07:08:03 +00:00
|
|
|
'DIRS': TEMPLATE_DIRS,
|
2019-04-02 22:53:21 +00:00
|
|
|
'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',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### External Service Settings
|
|
|
|
################################################################################
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2024-05-12 08:42:34 +00:00
|
|
|
|
|
|
|
CACHE_DB_FILENAME = 'cache.sqlite3'
|
2024-08-21 01:31:21 +00:00
|
|
|
CACHE_DB_PATH = CONFIG.CACHE_DIR / CACHE_DB_FILENAME
|
2024-05-12 08:42:34 +00:00
|
|
|
CACHE_DB_TABLE = 'django_cache'
|
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
DATABASE_FILE = Path(CONFIG.OUTPUT_DIR) / CONFIG.SQL_INDEX_FILENAME
|
2021-01-20 23:42:10 +00:00
|
|
|
DATABASE_NAME = os.environ.get("ARCHIVEBOX_DATABASE_NAME", str(DATABASE_FILE))
|
2020-12-08 23:05:37 +00:00
|
|
|
|
2019-04-02 22:53:21 +00:00
|
|
|
DATABASES = {
|
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
2020-12-08 23:05:37 +00:00
|
|
|
'NAME': DATABASE_NAME,
|
2021-03-01 03:55:00 +00:00
|
|
|
'OPTIONS': {
|
|
|
|
'timeout': 60,
|
|
|
|
'check_same_thread': False,
|
|
|
|
},
|
2024-08-21 01:31:21 +00:00
|
|
|
'TIME_ZONE': CONFIG.TIMEZONE,
|
2021-02-16 11:18:39 +00:00
|
|
|
# DB setup is sometimes modified at runtime by setup_django() in config.py
|
2024-05-12 08:42:34 +00:00
|
|
|
},
|
2024-05-13 12:12:12 +00:00
|
|
|
# 'cache': {
|
|
|
|
# 'ENGINE': 'django.db.backends.sqlite3',
|
|
|
|
# 'NAME': CACHE_DB_PATH,
|
|
|
|
# 'OPTIONS': {
|
|
|
|
# 'timeout': 60,
|
|
|
|
# 'check_same_thread': False,
|
|
|
|
# },
|
2024-08-21 01:31:21 +00:00
|
|
|
# 'TIME_ZONE': CONFIG.TIMEZONE,
|
2024-05-13 12:12:12 +00:00
|
|
|
# },
|
2019-04-02 22:53:21 +00:00
|
|
|
}
|
2024-05-13 12:12:12 +00:00
|
|
|
MIGRATION_MODULES = {'signal_webhooks': None}
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2024-05-13 09:37:48 +00:00
|
|
|
# as much as I'd love this to be a UUID or ULID field, it's not supported yet as of Django 5.0
|
|
|
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|
|
|
|
2021-02-17 23:25:23 +00:00
|
|
|
|
|
|
|
CACHES = {
|
2024-05-13 12:12:12 +00:00
|
|
|
'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'},
|
|
|
|
# 'sqlite': {'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'cache'},
|
|
|
|
# 'dummy': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
|
|
|
|
# 'filebased': {"BACKEND": "django.core.cache.backends.filebased.FileBasedCache", "LOCATION": CACHE_DIR / 'cache_filebased'},
|
2021-02-17 23:25:23 +00:00
|
|
|
}
|
2021-02-16 11:18:39 +00:00
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
|
|
|
|
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2024-05-06 13:58:03 +00:00
|
|
|
STORAGES = {
|
2024-05-06 14:14:01 +00:00
|
|
|
"default": {
|
|
|
|
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
|
|
|
},
|
|
|
|
"staticfiles": {
|
|
|
|
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
|
|
|
|
},
|
2024-05-06 13:58:03 +00:00
|
|
|
"archive": {
|
|
|
|
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
|
|
|
"OPTIONS": {
|
|
|
|
"base_url": "/archive/",
|
2024-08-21 01:31:21 +00:00
|
|
|
"location": CONFIG.ARCHIVE_DIR,
|
2024-05-06 13:58:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
# "personas": {
|
|
|
|
# "BACKEND": "django.core.files.storage.FileSystemStorage",
|
|
|
|
# "OPTIONS": {
|
|
|
|
# "base_url": "/personas/",
|
|
|
|
# "location": PERSONAS_DIR,
|
|
|
|
# },
|
|
|
|
# },
|
|
|
|
}
|
|
|
|
|
2019-04-22 23:07:39 +00:00
|
|
|
################################################################################
|
|
|
|
### Security Settings
|
|
|
|
################################################################################
|
2020-10-31 07:08:03 +00:00
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
SECRET_KEY = CONFIG.SECRET_KEY or get_random_string(50, 'abcdefghijklmnopqrstuvwxyz0123456789_')
|
2020-10-31 07:08:03 +00:00
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
ALLOWED_HOSTS = CONFIG.ALLOWED_HOSTS.split(',')
|
2024-08-23 09:01:40 +00:00
|
|
|
CSRF_TRUSTED_ORIGINS = list(set(CONFIG.CSRF_TRUSTED_ORIGINS.split(',')))
|
2024-08-23 01:40:47 +00:00
|
|
|
|
|
|
|
# automatically fix case when user sets ALLOWED_HOSTS (e.g. to archivebox.example.com)
|
|
|
|
# but forgets to add https://archivebox.example.com to CSRF_TRUSTED_ORIGINS
|
2024-08-23 09:01:40 +00:00
|
|
|
for hostname in ALLOWED_HOSTS:
|
|
|
|
https_endpoint = f'https://{hostname}'
|
|
|
|
if hostname != '*' and https_endpoint not in CSRF_TRUSTED_ORIGINS:
|
|
|
|
print(f'[!] WARNING: {https_endpoint} from ALLOWED_HOSTS should be added to CSRF_TRUSTED_ORIGINS')
|
|
|
|
CSRF_TRUSTED_ORIGINS.append(https_endpoint)
|
2020-10-31 07:08:03 +00:00
|
|
|
|
2019-04-22 23:07:39 +00:00
|
|
|
SECURE_BROWSER_XSS_FILTER = True
|
|
|
|
SECURE_CONTENT_TYPE_NOSNIFF = True
|
2021-04-10 08:19:30 +00:00
|
|
|
SECURE_REFERRER_POLICY = 'strict-origin-when-cross-origin'
|
2020-10-31 07:08:03 +00:00
|
|
|
|
2019-04-22 23:07:39 +00:00
|
|
|
CSRF_COOKIE_SECURE = False
|
2020-10-31 07:08:03 +00:00
|
|
|
SESSION_COOKIE_SECURE = False
|
2019-04-22 23:07:39 +00:00
|
|
|
SESSION_COOKIE_DOMAIN = None
|
2020-10-31 07:08:03 +00:00
|
|
|
SESSION_COOKIE_AGE = 1209600 # 2 weeks
|
2019-04-22 23:07:39 +00:00
|
|
|
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
|
|
|
|
SESSION_SAVE_EVERY_REQUEST = True
|
|
|
|
|
2021-02-16 11:18:39 +00:00
|
|
|
SESSION_ENGINE = "django.contrib.sessions.backends.db"
|
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
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'},
|
|
|
|
]
|
|
|
|
|
2024-08-27 03:14:47 +00:00
|
|
|
DATA_UPLOAD_MAX_NUMBER_FIELDS = None
|
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
################################################################################
|
|
|
|
### Shell Settings
|
|
|
|
################################################################################
|
2019-04-22 23:07:39 +00:00
|
|
|
|
|
|
|
SHELL_PLUS = 'ipython'
|
|
|
|
SHELL_PLUS_PRINT_SQL = False
|
|
|
|
IPYTHON_ARGUMENTS = ['--no-confirm-exit', '--no-banner']
|
|
|
|
IPYTHON_KERNEL_DISPLAY_NAME = 'ArchiveBox Django Shell'
|
|
|
|
if IS_SHELL:
|
2024-08-21 01:31:21 +00:00
|
|
|
os.environ['PYTHONSTARTUP'] = str(Path(CONFIG.PACKAGE_DIR) / 'core' / 'welcome_message.py')
|
2019-04-22 23:07:39 +00:00
|
|
|
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
################################################################################
|
|
|
|
### Internationalization & Localization Settings
|
|
|
|
################################################################################
|
|
|
|
|
2019-04-02 22:53:21 +00:00
|
|
|
LANGUAGE_CODE = 'en-us'
|
2021-04-10 08:19:30 +00:00
|
|
|
USE_I18N = True
|
|
|
|
USE_TZ = True
|
2020-07-28 09:51:18 +00:00
|
|
|
DATETIME_FORMAT = 'Y-m-d g:iA'
|
|
|
|
SHORT_DATETIME_FORMAT = 'Y-m-d h:iA'
|
2024-08-21 01:31:21 +00:00
|
|
|
TIME_ZONE = CONFIG.TIMEZONE # django convention is TIME_ZONE, archivebox config uses TIMEZONE, they are equivalent
|
2022-06-09 03:00:29 +00:00
|
|
|
|
2021-02-16 01:51:23 +00:00
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
from django.conf.locale.en import formats as en_formats # type: ignore
|
2021-04-10 08:19:30 +00:00
|
|
|
|
|
|
|
en_formats.DATETIME_FORMAT = DATETIME_FORMAT
|
|
|
|
en_formats.SHORT_DATETIME_FORMAT = SHORT_DATETIME_FORMAT
|
|
|
|
|
2021-02-16 01:51:23 +00:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Logging Settings
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
IGNORABLE_404_URLS = [
|
|
|
|
re.compile(r'apple-touch-icon.*\.png$'),
|
|
|
|
re.compile(r'favicon\.ico$'),
|
|
|
|
re.compile(r'robots\.txt$'),
|
|
|
|
re.compile(r'.*\.(css|js)\.map$'),
|
|
|
|
]
|
2024-08-27 03:14:47 +00:00
|
|
|
IGNORABLE_200_URLS = [
|
|
|
|
re.compile(r'^"GET /static/.* HTTP/.*" (200|30.) .+', re.I | re.M),
|
|
|
|
re.compile(r'^"GET /admin/jsi18n/ HTTP/.*" (200|30.) .+', re.I | re.M),
|
|
|
|
]
|
2021-02-16 01:51:23 +00:00
|
|
|
|
|
|
|
class NoisyRequestsFilter(logging.Filter):
|
2024-08-23 00:57:33 +00:00
|
|
|
def filter(self, record) -> bool:
|
2021-02-16 01:51:23 +00:00
|
|
|
logline = record.getMessage()
|
|
|
|
|
|
|
|
# ignore harmless 404s for the patterns in IGNORABLE_404_URLS
|
|
|
|
for ignorable_url_pattern in IGNORABLE_404_URLS:
|
|
|
|
ignorable_log_pattern = re.compile(f'^"GET /.*/?{ignorable_url_pattern.pattern[:-1]} HTTP/.*" (200|30.|404) .+$', re.I | re.M)
|
|
|
|
if ignorable_log_pattern.match(logline):
|
2024-08-23 00:57:33 +00:00
|
|
|
return False
|
2021-02-16 01:51:23 +00:00
|
|
|
|
2024-08-27 03:14:47 +00:00
|
|
|
ignorable_log_pattern = re.compile(f'^Not Found: /.*/?{ignorable_url_pattern.pattern}', re.I | re.M)
|
|
|
|
if ignorable_log_pattern.match(logline):
|
|
|
|
return False
|
2021-02-16 01:51:23 +00:00
|
|
|
|
2024-08-27 03:14:47 +00:00
|
|
|
# ignore staticfile requests that 200 or 30*
|
|
|
|
for ignorable_url_pattern in IGNORABLE_200_URLS:
|
|
|
|
if ignorable_log_pattern.match(logline):
|
|
|
|
return False
|
|
|
|
|
2024-08-23 00:57:33 +00:00
|
|
|
return True
|
2021-02-16 01:51:23 +00:00
|
|
|
|
2024-08-27 03:14:47 +00:00
|
|
|
|
|
|
|
ERROR_LOG = tempfile.NamedTemporaryFile().name
|
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
if CONFIG.LOGS_DIR.exists():
|
|
|
|
ERROR_LOG = (CONFIG.LOGS_DIR / 'errors.log')
|
2021-03-27 06:17:12 +00:00
|
|
|
else:
|
2023-09-14 09:41:27 +00:00
|
|
|
# historically too many edge cases here around creating log dir w/ correct permissions early on
|
|
|
|
# if there's an issue on startup, we trash the log and let user figure it out via stdout/stderr
|
2024-08-27 03:14:47 +00:00
|
|
|
print(f'[!] WARNING: data/logs dir does not exist. Logging to temp file: {ERROR_LOG}')
|
2021-02-16 06:23:01 +00:00
|
|
|
|
2021-02-16 01:51:23 +00:00
|
|
|
LOGGING = {
|
|
|
|
'version': 1,
|
|
|
|
'disable_existing_loggers': False,
|
|
|
|
'handlers': {
|
|
|
|
'console': {
|
|
|
|
'class': 'logging.StreamHandler',
|
|
|
|
},
|
|
|
|
'logfile': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'class': 'logging.handlers.RotatingFileHandler',
|
2021-02-16 06:23:01 +00:00
|
|
|
'filename': ERROR_LOG,
|
2021-02-16 01:51:23 +00:00
|
|
|
'maxBytes': 1024 * 1024 * 25, # 25 MB
|
|
|
|
'backupCount': 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'filters': {
|
|
|
|
'noisyrequestsfilter': {
|
|
|
|
'()': NoisyRequestsFilter,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'django': {
|
|
|
|
'handlers': ['console', 'logfile'],
|
2021-02-16 07:50:05 +00:00
|
|
|
'level': 'INFO',
|
2021-02-16 01:51:23 +00:00
|
|
|
'filters': ['noisyrequestsfilter'],
|
|
|
|
},
|
|
|
|
'django.server': {
|
|
|
|
'handlers': ['console', 'logfile'],
|
2021-02-16 07:50:05 +00:00
|
|
|
'level': 'INFO',
|
2021-02-16 01:51:23 +00:00
|
|
|
'filters': ['noisyrequestsfilter'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2024-05-06 13:58:03 +00:00
|
|
|
|
|
|
|
|
2024-08-27 03:14:47 +00:00
|
|
|
################################################################################
|
|
|
|
### REST API Outbound Webhooks settings
|
|
|
|
################################################################################
|
|
|
|
|
2024-05-06 13:58:03 +00:00
|
|
|
# Add default webhook configuration to the User model
|
2024-05-13 09:36:15 +00:00
|
|
|
SIGNAL_WEBHOOKS_CUSTOM_MODEL = 'api.models.OutboundWebhook'
|
2024-05-06 13:58:03 +00:00
|
|
|
SIGNAL_WEBHOOKS = {
|
|
|
|
"HOOKS": {
|
2024-05-13 09:36:15 +00:00
|
|
|
# ... is a special sigil value that means "use the default autogenerated hooks"
|
|
|
|
"django.contrib.auth.models.User": ...,
|
2024-05-06 14:13:54 +00:00
|
|
|
"core.models.Snapshot": ...,
|
|
|
|
"core.models.ArchiveResult": ...,
|
|
|
|
"core.models.Tag": ...,
|
|
|
|
"api.models.APIToken": ...,
|
2024-05-06 13:58:03 +00:00
|
|
|
},
|
|
|
|
}
|
2024-05-06 18:06:42 +00:00
|
|
|
|
2024-08-27 03:14:47 +00:00
|
|
|
################################################################################
|
|
|
|
### Admin Data View Settings
|
|
|
|
################################################################################
|
2024-05-06 18:06:42 +00:00
|
|
|
|
|
|
|
ADMIN_DATA_VIEWS = {
|
2024-05-18 03:13:54 +00:00
|
|
|
"NAME": "Environment",
|
2024-05-06 18:06:42 +00:00
|
|
|
"URLS": [
|
|
|
|
{
|
2024-05-18 03:13:54 +00:00
|
|
|
"route": "config/",
|
2024-05-06 18:06:42 +00:00
|
|
|
"view": "core.views.live_config_list_view",
|
2024-05-18 03:13:54 +00:00
|
|
|
"name": "Configuration",
|
2024-05-06 18:06:42 +00:00
|
|
|
"items": {
|
|
|
|
"route": "<str:key>/",
|
|
|
|
"view": "core.views.live_config_value_view",
|
2024-05-18 03:13:54 +00:00
|
|
|
"name": "config_val",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"route": "binaries/",
|
|
|
|
"view": "plugantic.views.binaries_list_view",
|
|
|
|
"name": "Binaries",
|
|
|
|
"items": {
|
|
|
|
"route": "<str:key>/",
|
|
|
|
"view": "plugantic.views.binary_detail_view",
|
|
|
|
"name": "binary",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"route": "plugins/",
|
|
|
|
"view": "plugantic.views.plugins_list_view",
|
|
|
|
"name": "Plugins",
|
|
|
|
"items": {
|
|
|
|
"route": "<str:key>/",
|
|
|
|
"view": "plugantic.views.plugin_detail_view",
|
|
|
|
"name": "plugin",
|
2024-05-06 18:06:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
2024-08-27 03:14:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Debug Settings
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# only enable debug toolbar when in DEBUG mode with --nothreading (it doesnt work in multithreaded mode)
|
|
|
|
DEBUG_TOOLBAR = True
|
|
|
|
DEBUG_TOOLBAR = DEBUG_TOOLBAR and DEBUG and ('--nothreading' in sys.argv) and ('--reload' not in sys.argv)
|
|
|
|
if DEBUG_TOOLBAR:
|
|
|
|
try:
|
|
|
|
import debug_toolbar # noqa
|
|
|
|
DEBUG_TOOLBAR = True
|
|
|
|
except ImportError:
|
|
|
|
DEBUG_TOOLBAR = False
|
|
|
|
|
|
|
|
if DEBUG_TOOLBAR:
|
|
|
|
INSTALLED_APPS = [*INSTALLED_APPS, 'debug_toolbar']
|
|
|
|
INTERNAL_IPS = ['0.0.0.0', '127.0.0.1', '*']
|
|
|
|
DEBUG_TOOLBAR_CONFIG = {
|
|
|
|
"SHOW_TOOLBAR_CALLBACK": lambda request: True,
|
|
|
|
"RENDER_PANELS": True,
|
|
|
|
}
|
|
|
|
DEBUG_TOOLBAR_PANELS = [
|
|
|
|
'debug_toolbar.panels.history.HistoryPanel',
|
|
|
|
'debug_toolbar.panels.versions.VersionsPanel',
|
|
|
|
'debug_toolbar.panels.timer.TimerPanel',
|
|
|
|
'debug_toolbar.panels.settings.SettingsPanel',
|
|
|
|
'debug_toolbar.panels.headers.HeadersPanel',
|
|
|
|
'debug_toolbar.panels.request.RequestPanel',
|
|
|
|
'debug_toolbar.panels.sql.SQLPanel',
|
|
|
|
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
|
|
|
|
# 'debug_toolbar.panels.templates.TemplatesPanel',
|
|
|
|
'debug_toolbar.panels.cache.CachePanel',
|
|
|
|
'debug_toolbar.panels.signals.SignalsPanel',
|
|
|
|
'debug_toolbar.panels.logging.LoggingPanel',
|
|
|
|
'debug_toolbar.panels.redirects.RedirectsPanel',
|
|
|
|
'debug_toolbar.panels.profiling.ProfilingPanel',
|
|
|
|
'djdt_flamegraph.FlamegraphPanel',
|
|
|
|
]
|
|
|
|
MIDDLEWARE = [*MIDDLEWARE, 'debug_toolbar.middleware.DebugToolbarMiddleware']
|
|
|
|
|
|
|
|
if DEBUG:
|
|
|
|
from django_autotyping.typing import AutotypingSettingsDict
|
|
|
|
|
|
|
|
INSTALLED_APPS += ['django_autotyping']
|
|
|
|
AUTOTYPING: AutotypingSettingsDict = {
|
|
|
|
"STUBS_GENERATION": {
|
|
|
|
"LOCAL_STUBS_DIR": Path(CONFIG.PACKAGE_DIR) / "typings",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# https://github.com/bensi94/Django-Requests-Tracker (improved version of django-debug-toolbar)
|
|
|
|
# Must delete archivebox/templates/admin to use because it relies on some things we override
|
|
|
|
# visit /__requests_tracker__/ to access
|
|
|
|
DEBUG_REQUESTS_TRACKER = True
|
|
|
|
if DEBUG_REQUESTS_TRACKER:
|
|
|
|
import requests_tracker
|
|
|
|
|
|
|
|
INSTALLED_APPS += ["requests_tracker"]
|
|
|
|
MIDDLEWARE += ["requests_tracker.middleware.requests_tracker_middleware"]
|
|
|
|
INTERNAL_IPS = ["127.0.0.1", "10.0.2.2", "0.0.0.0", "*"]
|
|
|
|
|
|
|
|
TEMPLATE_DIRS.insert(0, str(Path(inspect.getfile(requests_tracker)).parent / "templates"))
|
|
|
|
|
|
|
|
REQUESTS_TRACKER_CONFIG = {
|
|
|
|
"TRACK_SQL": True,
|
|
|
|
"ENABLE_STACKTRACES": False,
|
|
|
|
"IGNORE_PATHS_PATTERNS": (
|
|
|
|
r".*/favicon\.ico",
|
|
|
|
r".*\.png",
|
|
|
|
r"/admin/jsi18n/",
|
|
|
|
),
|
|
|
|
"IGNORE_SQL_PATTERNS": (
|
|
|
|
r"^SELECT .* FROM django_migrations WHERE app = 'requests_tracker'",
|
|
|
|
r"^SELECT .* FROM django_migrations WHERE app = 'auth'",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
# https://docs.pydantic.dev/logfire/integrations/django/ (similar to DataDog / NewRelic / etc.)
|
|
|
|
DEBUG_LOGFIRE = False
|
|
|
|
DEBUG_LOGFIRE = DEBUG_LOGFIRE and (Path(CONFIG.OUTPUT_DIR) / '.logfire').is_dir()
|