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
|
2020-10-31 07:08:03 +00:00
|
|
|
|
2020-09-30 19:54:32 +00:00
|
|
|
from pathlib import Path
|
2021-02-16 06:23:01 +00:00
|
|
|
from datetime import datetime
|
2020-04-23 01:14:43 +00:00
|
|
|
from django.utils.crypto import get_random_string
|
2019-04-02 22:53:21 +00:00
|
|
|
|
2020-07-28 03:26:45 +00:00
|
|
|
from ..config import ( # noqa: F401
|
|
|
|
DEBUG,
|
2021-02-16 06:23:01 +00:00
|
|
|
IS_TTY,
|
|
|
|
VERSION,
|
|
|
|
IN_DOCKER,
|
2019-05-02 23:15:16 +00:00
|
|
|
SECRET_KEY,
|
|
|
|
ALLOWED_HOSTS,
|
2020-10-31 07:08:03 +00:00
|
|
|
PACKAGE_DIR,
|
2020-12-11 14:21:09 +00:00
|
|
|
TEMPLATES_DIR_NAME,
|
2019-05-02 23:15:16 +00:00
|
|
|
SQL_INDEX_FILENAME,
|
2020-07-28 03:26:45 +00:00
|
|
|
OUTPUT_DIR,
|
2021-02-16 01:51:23 +00:00
|
|
|
LOGS_DIR,
|
2019-05-02 23:15:16 +00:00
|
|
|
)
|
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/'
|
|
|
|
LOGOUT_REDIRECT_URL = '/'
|
|
|
|
PASSWORD_RESET_URL = '/accounts/password_reset/'
|
|
|
|
APPEND_SLASH = True
|
2020-04-23 01:14:43 +00:00
|
|
|
|
2021-01-30 11:09:26 +00:00
|
|
|
DEBUG = DEBUG or ('--debug' in sys.argv)
|
2021-01-30 11:07:35 +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',
|
2019-04-02 22:53:21 +00:00
|
|
|
|
|
|
|
'core',
|
2019-04-17 09:42:09 +00:00
|
|
|
|
2019-04-17 07:49:18 +00:00
|
|
|
'django_extensions',
|
2019-04-02 22:53:21 +00:00
|
|
|
]
|
|
|
|
|
2019-04-22 23:07:39 +00:00
|
|
|
|
2019-04-02 22:53:21 +00:00
|
|
|
MIDDLEWARE = [
|
|
|
|
'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',
|
|
|
|
]
|
|
|
|
|
2020-10-31 07:08:03 +00:00
|
|
|
AUTHENTICATION_BACKENDS = [
|
|
|
|
'django.contrib.auth.backends.ModelBackend',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Staticfile and Template Settings
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
STATIC_URL = '/static/'
|
|
|
|
|
|
|
|
STATICFILES_DIRS = [
|
2021-01-30 10:34:19 +00:00
|
|
|
str(Path(PACKAGE_DIR) / TEMPLATES_DIR_NAME / 'static'),
|
2020-10-31 07:08:03 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
TEMPLATE_DIRS = [
|
2021-01-30 10:34:19 +00:00
|
|
|
str(Path(PACKAGE_DIR) / TEMPLATES_DIR_NAME / 'core'),
|
|
|
|
str(Path(PACKAGE_DIR) / TEMPLATES_DIR_NAME / 'admin'),
|
2020-12-11 14:21:09 +00:00
|
|
|
str(Path(PACKAGE_DIR) / 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
|
|
|
|
2020-10-25 02:12:39 +00:00
|
|
|
DATABASE_FILE = Path(OUTPUT_DIR) / 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-02-16 01:51:23 +00:00
|
|
|
# modified to be in-memory or sqlite3+wal by setup_django() in config.py
|
2019-04-02 22:53:21 +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
|
|
|
|
2019-04-22 23:07:39 +00:00
|
|
|
################################################################################
|
|
|
|
### Security Settings
|
|
|
|
################################################################################
|
2020-10-31 07:08:03 +00:00
|
|
|
|
2021-02-16 01:45:42 +00:00
|
|
|
SECRET_KEY = SECRET_KEY or get_random_string(50, 'abcdefghijklmnopqrstuvwxyz0123456789_')
|
2020-10-31 07:08:03 +00:00
|
|
|
|
|
|
|
ALLOWED_HOSTS = ALLOWED_HOSTS.split(',')
|
|
|
|
|
2019-04-22 23:07:39 +00:00
|
|
|
SECURE_BROWSER_XSS_FILTER = True
|
|
|
|
SECURE_CONTENT_TYPE_NOSNIFF = True
|
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
|
|
|
|
|
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'},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### 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:
|
2020-10-31 07:08:03 +00:00
|
|
|
os.environ['PYTHONSTARTUP'] = str(Path(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'
|
|
|
|
TIME_ZONE = 'UTC'
|
2020-07-28 09:51:18 +00:00
|
|
|
USE_I18N = False
|
|
|
|
USE_L10N = False
|
2020-07-28 10:50:03 +00:00
|
|
|
USE_TZ = False
|
2020-07-28 09:51:18 +00:00
|
|
|
|
|
|
|
DATETIME_FORMAT = 'Y-m-d g:iA'
|
|
|
|
SHORT_DATETIME_FORMAT = 'Y-m-d h:iA'
|
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$'),
|
|
|
|
]
|
|
|
|
|
|
|
|
class NoisyRequestsFilter(logging.Filter):
|
|
|
|
def filter(self, record):
|
|
|
|
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):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# ignore staticfile requests that 200 or 30*
|
|
|
|
ignoreable_200_log_pattern = re.compile(r'"GET /static/.* HTTP/.*" (200|30.) .+', re.I | re.M)
|
|
|
|
if ignoreable_200_log_pattern.match(logline):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
2021-02-16 06:23:01 +00:00
|
|
|
ERROR_LOG = LOGS_DIR / 'errors.log'
|
|
|
|
|
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'],
|
|
|
|
'level': 'DEBUG',
|
|
|
|
'filters': ['noisyrequestsfilter'],
|
|
|
|
},
|
|
|
|
'django.server': {
|
|
|
|
'handlers': ['console', 'logfile'],
|
|
|
|
'level': 'DEBUG',
|
|
|
|
'filters': ['noisyrequestsfilter'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-16 06:23:01 +00:00
|
|
|
|
|
|
|
# log startup message to the error log
|
|
|
|
with open(ERROR_LOG, "a+") as f:
|
|
|
|
command = ' '.join(sys.argv)
|
|
|
|
ts = datetime.now().strftime('%Y-%m-%d__%H:%M:%S')
|
|
|
|
f.write(f"\n> {command}; ts={ts} version={VERSION} docker={IN_DOCKER} is_tty={IS_TTY}\n")
|