settings.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. __package__ = 'archivebox.core'
  2. import os
  3. import sys
  4. import inspect
  5. from pathlib import Path
  6. from django.utils.crypto import get_random_string
  7. import abx
  8. import abx.archivebox
  9. import abx.archivebox.use
  10. import abx.django.use
  11. from archivebox.config import DATA_DIR, PACKAGE_DIR, ARCHIVE_DIR, CONSTANTS
  12. from archivebox.config.common import SHELL_CONFIG, SERVER_CONFIG # noqa
  13. IS_MIGRATING = 'makemigrations' in sys.argv[:3] or 'migrate' in sys.argv[:3]
  14. IS_TESTING = 'test' in sys.argv[:3] or 'PYTEST_CURRENT_TEST' in os.environ
  15. IS_SHELL = 'shell' in sys.argv[:3] or 'shell_plus' in sys.argv[:3]
  16. ################################################################################
  17. ### ArchiveBox Plugin Settings
  18. ################################################################################
  19. PLUGIN_HOOKSPECS = [
  20. 'abx.django.hookspec',
  21. 'abx.pydantic_pkgr.hookspec',
  22. 'abx.archivebox.hookspec',
  23. ]
  24. abx.register_hookspecs(PLUGIN_HOOKSPECS)
  25. BUILTIN_PLUGIN_DIRS = {
  26. 'archivebox': PACKAGE_DIR,
  27. 'plugins_pkg': PACKAGE_DIR / 'plugins_pkg',
  28. 'plugins_auth': PACKAGE_DIR / 'plugins_auth',
  29. 'plugins_search': PACKAGE_DIR / 'plugins_search',
  30. 'plugins_extractor': PACKAGE_DIR / 'plugins_extractor',
  31. }
  32. USER_PLUGIN_DIRS = {
  33. # 'user_plugins': DATA_DIR / 'user_plugins',
  34. }
  35. # Discover ArchiveBox plugins
  36. BUILTIN_PLUGINS = abx.get_plugins_in_dirs(BUILTIN_PLUGIN_DIRS)
  37. PIP_PLUGINS = abx.get_pip_installed_plugins(group='archivebox')
  38. USER_PLUGINS = abx.get_plugins_in_dirs(USER_PLUGIN_DIRS)
  39. ALL_PLUGINS = {**BUILTIN_PLUGINS, **PIP_PLUGINS, **USER_PLUGINS}
  40. # Load ArchiveBox plugins
  41. PLUGIN_MANAGER = abx.pm
  42. abx.archivebox.load_archivebox_plugins(PLUGIN_MANAGER, ALL_PLUGINS)
  43. PLUGINS = abx.archivebox.use.get_PLUGINS()
  44. # Load ArchiveBox config from plugins
  45. CONFIGS = abx.archivebox.use.get_CONFIGS()
  46. CONFIG = FLAT_CONFIG = abx.archivebox.use.get_FLAT_CONFIG()
  47. BINPROVIDERS = abx.archivebox.use.get_BINPROVIDERS()
  48. BINARIES = abx.archivebox.use.get_BINARIES()
  49. EXTRACTORS = abx.archivebox.use.get_EXTRACTORS()
  50. SEARCHBACKENDS = abx.archivebox.use.get_SEARCHBACKENDS()
  51. # REPLAYERS = abx.archivebox.use.get_REPLAYERS()
  52. # ADMINDATAVIEWS = abx.archivebox.use.get_ADMINDATAVIEWS()
  53. ################################################################################
  54. ### Django Core Settings
  55. ################################################################################
  56. WSGI_APPLICATION = 'core.wsgi.application'
  57. ASGI_APPLICATION = "core.asgi.application"
  58. ROOT_URLCONF = 'core.urls'
  59. LOGIN_URL = '/accounts/login/'
  60. LOGOUT_REDIRECT_URL = os.environ.get('LOGOUT_REDIRECT_URL', '/')
  61. PASSWORD_RESET_URL = '/accounts/password_reset/'
  62. APPEND_SLASH = True
  63. DEBUG = SHELL_CONFIG.DEBUG or ('--debug' in sys.argv)
  64. INSTALLED_APPS = [
  65. 'daphne',
  66. # Django default apps
  67. 'django.contrib.auth',
  68. 'django.contrib.contenttypes',
  69. 'django.contrib.sessions',
  70. 'django.contrib.messages',
  71. 'django.contrib.staticfiles',
  72. 'django.contrib.admin',
  73. # 3rd-party apps from PyPI
  74. 'django_jsonform', # handles rendering Pydantic models to Django HTML widgets/forms https://github.com/bhch/django-jsonform
  75. 'signal_webhooks', # handles REST API outbound webhooks https://github.com/MrThearMan/django-signal-webhooks
  76. 'django_object_actions', # provides easy Django Admin action buttons on change views https://github.com/crccheck/django-object-actions
  77. # Our ArchiveBox-provided apps
  78. 'config', # ArchiveBox config settings (loaded as a plugin, don't need to add it here)
  79. 'machine', # handles collecting and storing information about the host machine, network interfaces, installed binaries, etc.
  80. 'queues', # handles starting and managing background workers and processes
  81. 'abid_utils', # handles ABID ID creation, handling, and models
  82. 'core', # core django model with Snapshot, ArchiveResult, etc.
  83. 'api', # Django-Ninja-based Rest API interfaces, config, APIToken model, etc.
  84. # ArchiveBox plugins
  85. *abx.django.use.get_INSTALLED_APPS(), # all plugin django-apps found in archivebox/plugins_* and data/user_plugins,
  86. # 3rd-party apps from PyPI that need to be loaded last
  87. 'admin_data_views', # handles rendering some convenient automatic read-only views of data in Django admin
  88. 'django_extensions', # provides Django Debug Toolbar (and other non-debug helpers)
  89. 'django_huey', # provides multi-queue support for django huey https://github.com/gaiacoop/django-huey
  90. 'bx_django_utils', # needed for huey_monitor https://github.com/boxine/bx_django_utils
  91. 'huey_monitor', # adds an admin UI for monitoring background huey tasks https://github.com/boxine/django-huey-monitor
  92. # load plugins last so all other apps are already .ready() when we call plugins.ready()
  93. 'abx',
  94. ]
  95. MIDDLEWARE = [
  96. 'core.middleware.TimezoneMiddleware',
  97. 'django.middleware.security.SecurityMiddleware',
  98. 'django.contrib.sessions.middleware.SessionMiddleware',
  99. 'django.middleware.common.CommonMiddleware',
  100. 'django.middleware.csrf.CsrfViewMiddleware',
  101. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  102. 'core.middleware.ReverseProxyAuthMiddleware',
  103. 'django.contrib.messages.middleware.MessageMiddleware',
  104. 'core.middleware.CacheControlMiddleware',
  105. *abx.django.use.get_MIDDLEWARES(),
  106. ]
  107. ################################################################################
  108. ### Authentication Settings
  109. ################################################################################
  110. # AUTH_USER_MODEL = 'auth.User' # cannot be easily changed unfortunately
  111. AUTHENTICATION_BACKENDS = [
  112. 'django.contrib.auth.backends.RemoteUserBackend',
  113. 'django.contrib.auth.backends.ModelBackend',
  114. *abx.django.use.get_AUTHENTICATION_BACKENDS(),
  115. ]
  116. # from ..plugins_auth.ldap.settings import LDAP_CONFIG
  117. # if LDAP_CONFIG.LDAP_ENABLED:
  118. # AUTH_LDAP_BIND_DN = LDAP_CONFIG.LDAP_BIND_DN
  119. # AUTH_LDAP_SERVER_URI = LDAP_CONFIG.LDAP_SERVER_URI
  120. # AUTH_LDAP_BIND_PASSWORD = LDAP_CONFIG.LDAP_BIND_PASSWORD
  121. # AUTH_LDAP_USER_ATTR_MAP = LDAP_CONFIG.LDAP_USER_ATTR_MAP
  122. # AUTH_LDAP_USER_SEARCH = LDAP_CONFIG.AUTH_LDAP_USER_SEARCH
  123. # AUTHENTICATION_BACKENDS = LDAP_CONFIG.AUTHENTICATION_BACKENDS
  124. ################################################################################
  125. ### Staticfile and Template Settings
  126. ################################################################################
  127. STATIC_URL = '/static/'
  128. TEMPLATES_DIR_NAME = 'templates'
  129. CUSTOM_TEMPLATES_ENABLED = os.access(CONSTANTS.CUSTOM_TEMPLATES_DIR, os.R_OK) and CONSTANTS.CUSTOM_TEMPLATES_DIR.is_dir()
  130. STATICFILES_DIRS = [
  131. *([str(CONSTANTS.CUSTOM_TEMPLATES_DIR / 'static')] if CUSTOM_TEMPLATES_ENABLED else []),
  132. # *[
  133. # str(plugin_dir / 'static')
  134. # for plugin_dir in PLUGIN_DIRS.values()
  135. # if (plugin_dir / 'static').is_dir()
  136. # ],
  137. *abx.django.use.get_STATICFILES_DIRS(),
  138. str(PACKAGE_DIR / TEMPLATES_DIR_NAME / 'static'),
  139. ]
  140. TEMPLATE_DIRS = [
  141. *([str(CONSTANTS.CUSTOM_TEMPLATES_DIR)] if CUSTOM_TEMPLATES_ENABLED else []),
  142. # *[
  143. # str(plugin_dir / 'templates')
  144. # for plugin_dir in PLUGIN_DIRS.values()
  145. # if (plugin_dir / 'templates').is_dir()
  146. # ],
  147. *abx.django.use.get_TEMPLATE_DIRS(),
  148. str(PACKAGE_DIR / TEMPLATES_DIR_NAME / 'core'),
  149. str(PACKAGE_DIR / TEMPLATES_DIR_NAME / 'admin'),
  150. str(PACKAGE_DIR / TEMPLATES_DIR_NAME),
  151. ]
  152. TEMPLATES = [
  153. {
  154. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  155. 'DIRS': TEMPLATE_DIRS,
  156. 'APP_DIRS': True,
  157. 'OPTIONS': {
  158. 'context_processors': [
  159. 'django.template.context_processors.debug',
  160. 'django.template.context_processors.request',
  161. 'django.contrib.auth.context_processors.auth',
  162. 'django.contrib.messages.context_processors.messages',
  163. ],
  164. },
  165. },
  166. ]
  167. ################################################################################
  168. ### External Service Settings
  169. ################################################################################
  170. # CACHE_DB_FILENAME = 'cache.sqlite3'
  171. # CACHE_DB_PATH = CONSTANTS.CACHE_DIR / CACHE_DB_FILENAME
  172. # CACHE_DB_TABLE = 'django_cache'
  173. DATABASE_NAME = os.environ.get("ARCHIVEBOX_DATABASE_NAME", str(CONSTANTS.DATABASE_FILE))
  174. SQLITE_CONNECTION_OPTIONS = {
  175. "ENGINE": "django.db.backends.sqlite3",
  176. "TIME_ZONE": CONSTANTS.TIMEZONE,
  177. "OPTIONS": {
  178. # https://gcollazo.com/optimal-sqlite-settings-for-django/
  179. # https://litestream.io/tips/#busy-timeout
  180. # https://docs.djangoproject.com/en/5.1/ref/databases/#setting-pragma-options
  181. "timeout": 5,
  182. "check_same_thread": False,
  183. "transaction_mode": "IMMEDIATE",
  184. "init_command": (
  185. "PRAGMA foreign_keys=ON;"
  186. "PRAGMA journal_mode = WAL;"
  187. "PRAGMA synchronous = NORMAL;"
  188. "PRAGMA temp_store = MEMORY;"
  189. "PRAGMA mmap_size = 134217728;"
  190. "PRAGMA journal_size_limit = 67108864;"
  191. "PRAGMA cache_size = 2000;"
  192. ),
  193. },
  194. }
  195. DATABASES = {
  196. "default": {
  197. "NAME": DATABASE_NAME,
  198. **SQLITE_CONNECTION_OPTIONS,
  199. },
  200. "queue": {
  201. "NAME": CONSTANTS.QUEUE_DATABASE_FILE,
  202. **SQLITE_CONNECTION_OPTIONS,
  203. },
  204. # 'cache': {
  205. # 'NAME': CACHE_DB_PATH,
  206. # **SQLITE_CONNECTION_OPTIONS,
  207. # },
  208. }
  209. MIGRATION_MODULES = {'signal_webhooks': None}
  210. # as much as I'd love this to be a UUID or ULID field, it's not supported yet as of Django 5.0
  211. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  212. HUEY = {
  213. "huey_class": "huey.SqliteHuey",
  214. "filename": CONSTANTS.QUEUE_DATABASE_FILENAME,
  215. "name": "system_tasks",
  216. "results": True,
  217. "store_none": True,
  218. "immediate": False,
  219. "utc": True,
  220. "consumer": {
  221. "workers": 1,
  222. "worker_type": "thread",
  223. "initial_delay": 0.1, # Smallest polling interval, same as -d.
  224. "backoff": 1.15, # Exponential backoff using this rate, -b.
  225. "max_delay": 10.0, # Max possible polling interval, -m.
  226. "scheduler_interval": 1, # Check schedule every second, -s.
  227. "periodic": True, # Enable crontab feature.
  228. "check_worker_health": True, # Enable worker health checks.
  229. "health_check_interval": 1, # Check worker health every second.
  230. },
  231. }
  232. # https://huey.readthedocs.io/en/latest/contrib.html#setting-things-up
  233. # https://github.com/gaiacoop/django-huey
  234. DJANGO_HUEY = {
  235. "default": "system_tasks",
  236. "queues": {
  237. HUEY["name"]: HUEY.copy(),
  238. # more registered here at plugin import-time by BaseQueue.register()
  239. **abx.django.use.get_DJANGO_HUEY_QUEUES(QUEUE_DATABASE_NAME=CONSTANTS.QUEUE_DATABASE_FILENAME),
  240. },
  241. }
  242. class HueyDBRouter:
  243. """
  244. A router to store all the Huey result k:v / Huey Monitor models in the queue.sqlite3 database.
  245. We keep the databases separate because the queue database receives many more reads/writes per second
  246. and we want to avoid single-write lock contention with the main database. Also all the in-progress task
  247. data is ephemeral/not-important-long-term. This makes it easier to for the user to clear non-critical
  248. temp data by just deleting queue.sqlite3 and leaving index.sqlite3.
  249. """
  250. route_app_labels = {"huey_monitor", "django_huey", "djhuey"}
  251. def db_for_read(self, model, **hints):
  252. if model._meta.app_label in self.route_app_labels:
  253. return "queue"
  254. return 'default'
  255. def db_for_write(self, model, **hints):
  256. if model._meta.app_label in self.route_app_labels:
  257. return "queue"
  258. return 'default'
  259. def allow_relation(self, obj1, obj2, **hints):
  260. if obj1._meta.app_label in self.route_app_labels or obj2._meta.app_label in self.route_app_labels:
  261. return obj1._meta.app_label == obj2._meta.app_label
  262. return None
  263. def allow_migrate(self, db, app_label, model_name=None, **hints):
  264. if app_label in self.route_app_labels:
  265. return db == "queue"
  266. return db == "default"
  267. DATABASE_ROUTERS = ['core.settings.HueyDBRouter']
  268. CACHES = {
  269. 'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'},
  270. # 'sqlite': {'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'cache'},
  271. # 'dummy': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
  272. # 'filebased': {"BACKEND": "django.core.cache.backends.filebased.FileBasedCache", "LOCATION": CACHE_DIR / 'cache_filebased'},
  273. }
  274. EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
  275. STORAGES = {
  276. "default": {
  277. "BACKEND": "django.core.files.storage.FileSystemStorage",
  278. },
  279. "staticfiles": {
  280. "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
  281. },
  282. "archive": {
  283. "BACKEND": "django.core.files.storage.FileSystemStorage",
  284. "OPTIONS": {
  285. "base_url": "/archive/",
  286. "location": ARCHIVE_DIR,
  287. },
  288. },
  289. # "personas": {
  290. # "BACKEND": "django.core.files.storage.FileSystemStorage",
  291. # "OPTIONS": {
  292. # "base_url": "/personas/",
  293. # "location": PERSONAS_DIR,
  294. # },
  295. # },
  296. }
  297. ################################################################################
  298. ### Security Settings
  299. ################################################################################
  300. SECRET_KEY = SERVER_CONFIG.SECRET_KEY or get_random_string(50, 'abcdefghijklmnopqrstuvwxyz0123456789_')
  301. ALLOWED_HOSTS = SERVER_CONFIG.ALLOWED_HOSTS.split(',')
  302. CSRF_TRUSTED_ORIGINS = list(set(SERVER_CONFIG.CSRF_TRUSTED_ORIGINS.split(',')))
  303. # automatically fix case when user sets ALLOWED_HOSTS (e.g. to archivebox.example.com)
  304. # but forgets to add https://archivebox.example.com to CSRF_TRUSTED_ORIGINS
  305. for hostname in ALLOWED_HOSTS:
  306. https_endpoint = f'https://{hostname}'
  307. if hostname != '*' and https_endpoint not in CSRF_TRUSTED_ORIGINS:
  308. print(f'[!] WARNING: {https_endpoint} from ALLOWED_HOSTS should be added to CSRF_TRUSTED_ORIGINS')
  309. CSRF_TRUSTED_ORIGINS.append(https_endpoint)
  310. SECURE_BROWSER_XSS_FILTER = True
  311. SECURE_CONTENT_TYPE_NOSNIFF = True
  312. SECURE_REFERRER_POLICY = 'strict-origin-when-cross-origin'
  313. CSRF_COOKIE_SECURE = False
  314. SESSION_COOKIE_SECURE = False
  315. SESSION_COOKIE_HTTPONLY = True
  316. SESSION_COOKIE_DOMAIN = None
  317. SESSION_COOKIE_AGE = 1209600 # 2 weeks
  318. SESSION_EXPIRE_AT_BROWSER_CLOSE = False
  319. SESSION_SAVE_EVERY_REQUEST = False
  320. SESSION_ENGINE = "django.contrib.sessions.backends.db"
  321. AUTH_PASSWORD_VALIDATORS = [
  322. {'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'},
  323. {'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'},
  324. {'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
  325. {'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
  326. ]
  327. DATA_UPLOAD_MAX_NUMBER_FIELDS = None
  328. DATA_UPLOAD_MAX_MEMORY_SIZE = 26_214_400 # 25MB
  329. ################################################################################
  330. ### Shell Settings
  331. ################################################################################
  332. SHELL_PLUS = 'ipython'
  333. SHELL_PLUS_PRINT_SQL = False
  334. IPYTHON_ARGUMENTS = ['--no-confirm-exit', '--no-banner']
  335. IPYTHON_KERNEL_DISPLAY_NAME = 'ArchiveBox Django Shell'
  336. if IS_SHELL:
  337. os.environ['PYTHONSTARTUP'] = str(PACKAGE_DIR / 'misc' / 'shell_welcome_message.py')
  338. ################################################################################
  339. ### Internationalization & Localization Settings
  340. ################################################################################
  341. LANGUAGE_CODE = 'en-us'
  342. USE_I18N = True
  343. USE_TZ = True
  344. DATETIME_FORMAT = 'Y-m-d h:i:s A'
  345. SHORT_DATETIME_FORMAT = 'Y-m-d h:i:s A'
  346. TIME_ZONE = CONSTANTS.TIMEZONE # django convention is TIME_ZONE, archivebox config uses TIMEZONE, they are equivalent
  347. from django.conf.locale.en import formats as en_formats # type: ignore
  348. en_formats.DATETIME_FORMAT = DATETIME_FORMAT # monkey patch en_format default with our preferred format
  349. en_formats.SHORT_DATETIME_FORMAT = SHORT_DATETIME_FORMAT
  350. ################################################################################
  351. ### Logging Settings
  352. ################################################################################
  353. from .settings_logging import SETTINGS_LOGGING, LOGS_DIR, ERROR_LOG
  354. LOGGING = SETTINGS_LOGGING
  355. ################################################################################
  356. ### REST API Outbound Webhooks settings
  357. ################################################################################
  358. # Add default webhook configuration to the User model
  359. SIGNAL_WEBHOOKS_CUSTOM_MODEL = 'api.models.OutboundWebhook'
  360. SIGNAL_WEBHOOKS = {
  361. "HOOKS": {
  362. # ... is a special sigil value that means "use the default autogenerated hooks"
  363. "django.contrib.auth.models.User": ...,
  364. "core.models.Snapshot": ...,
  365. "core.models.ArchiveResult": ...,
  366. "core.models.Tag": ...,
  367. "api.models.APIToken": ...,
  368. },
  369. }
  370. ################################################################################
  371. ### Admin Data View Settings
  372. ################################################################################
  373. ADMIN_DATA_VIEWS = {
  374. "NAME": "Environment",
  375. "URLS": [
  376. {
  377. "route": "config/",
  378. "view": "core.views.live_config_list_view",
  379. "name": "Configuration",
  380. "items": {
  381. "route": "<str:key>/",
  382. "view": "core.views.live_config_value_view",
  383. "name": "config_val",
  384. },
  385. },
  386. {
  387. "route": "binaries/",
  388. "view": "archivebox.config.views.binaries_list_view",
  389. "name": "Dependencies",
  390. "items": {
  391. "route": "<str:key>/",
  392. "view": "archivebox.config.views.binary_detail_view",
  393. "name": "binary",
  394. },
  395. },
  396. {
  397. "route": "plugins/",
  398. "view": "archivebox.config.views.plugins_list_view",
  399. "name": "Plugins",
  400. "items": {
  401. "route": "<str:key>/",
  402. "view": "archivebox.config.views.plugin_detail_view",
  403. "name": "plugin",
  404. },
  405. },
  406. {
  407. "route": "workers/",
  408. "view": "archivebox.config.views.worker_list_view",
  409. "name": "Workers",
  410. "items": {
  411. "route": "<str:key>/",
  412. "view": "archivebox.config.views.worker_detail_view",
  413. "name": "worker",
  414. },
  415. },
  416. {
  417. "route": "logs/",
  418. "view": "archivebox.config.views.log_list_view",
  419. "name": "Logs",
  420. "items": {
  421. "route": "<str:key>/",
  422. "view": "archivebox.config.views.log_detail_view",
  423. "name": "log",
  424. },
  425. },
  426. *abx.django.use.get_ADMIN_DATA_VIEWS_URLS(),
  427. ],
  428. }
  429. ################################################################################
  430. ### Debug Settings
  431. ################################################################################
  432. # only enable debug toolbar when in DEBUG mode with --nothreading (it doesnt work in multithreaded mode)
  433. DEBUG_TOOLBAR = False
  434. DEBUG_TOOLBAR = DEBUG_TOOLBAR and DEBUG and ('--nothreading' in sys.argv) and ('--reload' not in sys.argv)
  435. if DEBUG_TOOLBAR:
  436. try:
  437. import debug_toolbar # noqa
  438. DEBUG_TOOLBAR = True
  439. except ImportError:
  440. DEBUG_TOOLBAR = False
  441. if DEBUG_TOOLBAR:
  442. INSTALLED_APPS = [*INSTALLED_APPS, 'debug_toolbar']
  443. INTERNAL_IPS = ['0.0.0.0', '127.0.0.1', '*']
  444. DEBUG_TOOLBAR_CONFIG = {
  445. "SHOW_TOOLBAR_CALLBACK": lambda request: True,
  446. "RENDER_PANELS": True,
  447. }
  448. DEBUG_TOOLBAR_PANELS = [
  449. 'debug_toolbar.panels.history.HistoryPanel',
  450. 'debug_toolbar.panels.versions.VersionsPanel',
  451. 'debug_toolbar.panels.timer.TimerPanel',
  452. 'debug_toolbar.panels.settings.SettingsPanel',
  453. 'debug_toolbar.panels.headers.HeadersPanel',
  454. 'debug_toolbar.panels.request.RequestPanel',
  455. 'debug_toolbar.panels.sql.SQLPanel',
  456. 'debug_toolbar.panels.staticfiles.StaticFilesPanel',
  457. # 'debug_toolbar.panels.templates.TemplatesPanel',
  458. 'debug_toolbar.panels.cache.CachePanel',
  459. 'debug_toolbar.panels.signals.SignalsPanel',
  460. 'debug_toolbar.panels.logging.LoggingPanel',
  461. 'debug_toolbar.panels.redirects.RedirectsPanel',
  462. 'debug_toolbar.panels.profiling.ProfilingPanel',
  463. 'djdt_flamegraph.FlamegraphPanel',
  464. ]
  465. MIDDLEWARE = [*MIDDLEWARE, 'debug_toolbar.middleware.DebugToolbarMiddleware']
  466. if DEBUG:
  467. from django_autotyping.typing import AutotypingSettingsDict
  468. INSTALLED_APPS += ['django_autotyping']
  469. AUTOTYPING: AutotypingSettingsDict = {
  470. "STUBS_GENERATION": {
  471. "LOCAL_STUBS_DIR": PACKAGE_DIR / "typings",
  472. }
  473. }
  474. # https://github.com/bensi94/Django-Requests-Tracker (improved version of django-debug-toolbar)
  475. # Must delete archivebox/templates/admin to use because it relies on some things we override
  476. # visit /__requests_tracker__/ to access
  477. DEBUG_REQUESTS_TRACKER = True
  478. DEBUG_REQUESTS_TRACKER = DEBUG_REQUESTS_TRACKER and DEBUG
  479. if DEBUG_REQUESTS_TRACKER:
  480. import requests_tracker
  481. INSTALLED_APPS += ["requests_tracker"]
  482. MIDDLEWARE += ["requests_tracker.middleware.requests_tracker_middleware"]
  483. INTERNAL_IPS = ["127.0.0.1", "10.0.2.2", "0.0.0.0", "*"]
  484. TEMPLATE_DIRS.insert(0, str(Path(inspect.getfile(requests_tracker)).parent / "templates"))
  485. REQUESTS_TRACKER_CONFIG = {
  486. "TRACK_SQL": True,
  487. "ENABLE_STACKTRACES": False,
  488. "IGNORE_PATHS_PATTERNS": (
  489. r".*/favicon\.ico",
  490. r".*\.png",
  491. r"/admin/jsi18n/",
  492. ),
  493. "IGNORE_SQL_PATTERNS": (
  494. r"^SELECT .* FROM django_migrations WHERE app = 'requests_tracker'",
  495. r"^SELECT .* FROM django_migrations WHERE app = 'auth'",
  496. ),
  497. }
  498. # # https://docs.pydantic.dev/logfire/integrations/django/ (similar to DataDog / NewRelic / etc.)
  499. # DEBUG_LOGFIRE = False
  500. # DEBUG_LOGFIRE = DEBUG_LOGFIRE and os.access(DATA_DIR / '.logfire', os.W_OK) and (DATA_DIR / '.logfire').is_dir()
  501. # For usage with https://www.jetadmin.io/integrations/django
  502. # INSTALLED_APPS += ['jet_django']
  503. # JET_PROJECT = 'archivebox'
  504. # JET_TOKEN = 'some-api-token-here'
  505. abx.django.use.register_checks()
  506. # abx.archivebox.use.register_all_hooks(globals())
  507. # import ipdb; ipdb.set_trace()