Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # BitBake Toaster Implementation |
| 3 | # |
| 4 | # Copyright (C) 2013 Intel Corporation |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | # Django settings for Toaster project. |
| 10 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 11 | import os |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 12 | from pathlib import Path |
| 13 | from toastermain.logs import LOGGING_SETTINGS |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | |
| 15 | DEBUG = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
| 17 | # Set to True to see the SQL queries in console |
| 18 | SQL_DEBUG = False |
| 19 | if os.environ.get("TOASTER_SQLDEBUG", None) is not None: |
| 20 | SQL_DEBUG = True |
| 21 | |
| 22 | |
| 23 | ADMINS = ( |
| 24 | # ('Your Name', 'your_email@example.com'), |
| 25 | ) |
| 26 | |
| 27 | MANAGERS = ADMINS |
| 28 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 29 | TOASTER_SQLITE_DEFAULT_DIR = os.environ.get('TOASTER_DIR') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 30 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | DATABASES = { |
| 32 | 'default': { |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 33 | # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. |
| 34 | 'ENGINE': 'django.db.backends.sqlite3', |
| 35 | # DB name or full path to database file if using sqlite3. |
| 36 | 'NAME': "%s/toaster.sqlite" % TOASTER_SQLITE_DEFAULT_DIR, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | 'USER': '', |
| 38 | 'PASSWORD': '', |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 39 | #'HOST': '127.0.0.1', # e.g. mysql server |
| 40 | #'PORT': '3306', # e.g. mysql port |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 44 | # New in Django 3.2 |
| 45 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' |
| 46 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | # Needed when Using sqlite especially to add a longer timeout for waiting |
| 48 | # for the database lock to be released |
| 49 | # https://docs.djangoproject.com/en/1.6/ref/databases/#database-is-locked-errors |
| 50 | if 'sqlite' in DATABASES['default']['ENGINE']: |
| 51 | DATABASES['default']['OPTIONS'] = { 'timeout': 20 } |
| 52 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 53 | # Update as of django 1.8.16 release, the '*' is needed to allow us to connect while running |
| 54 | # on hosts without explicitly setting the fqdn for the toaster server. |
| 55 | # See https://docs.djangoproject.com/en/dev/ref/settings/ for info on ALLOWED_HOSTS |
| 56 | # Previously this setting was not enforced if DEBUG was set but it is now. |
| 57 | # The previous behavior was such that ALLOWED_HOSTS defaulted to ['localhost','127.0.0.1','::1'] |
| 58 | # and if you bound to 0.0.0.0:<port #> then accessing toaster as localhost or fqdn would both work. |
| 59 | # To have that same behavior, with a fqdn explicitly enabled you would set |
| 60 | # ALLOWED_HOSTS= ['localhost','127.0.0.1','::1','myserver.mycompany.com'] for |
| 61 | # Django >= 1.8.16. By default, we are not enforcing this restriction in |
| 62 | # DEBUG mode. |
| 63 | if DEBUG is True: |
| 64 | # this will allow connection via localhost,hostname, or fqdn |
| 65 | ALLOWED_HOSTS = ['*'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | |
| 67 | # Local time zone for this installation. Choices can be found here: |
| 68 | # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name |
| 69 | # although not all choices may be available on all operating systems. |
| 70 | # In a Windows environment this must be set to your system time zone. |
| 71 | |
| 72 | # Always use local computer's time zone, find |
| 73 | import hashlib |
| 74 | if 'TZ' in os.environ: |
| 75 | TIME_ZONE = os.environ['TZ'] |
| 76 | else: |
| 77 | # need to read the /etc/localtime file which is the libc standard |
| 78 | # and do a reverse-mapping to /usr/share/zoneinfo/; |
| 79 | # since the timezone may match any number of identical timezone definitions, |
| 80 | |
| 81 | zonefilelist = {} |
| 82 | ZONEINFOPATH = '/usr/share/zoneinfo/' |
| 83 | for dirpath, dirnames, filenames in os.walk(ZONEINFOPATH): |
| 84 | for fn in filenames: |
| 85 | filepath = os.path.join(dirpath, fn) |
| 86 | zonename = filepath.lstrip(ZONEINFOPATH).strip() |
| 87 | try: |
| 88 | import pytz |
| 89 | from pytz.exceptions import UnknownTimeZoneError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | try: |
| 91 | if pytz.timezone(zonename) is not None: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 92 | zonefilelist[hashlib.md5(open(filepath, 'rb').read()).hexdigest()] = zonename |
| 93 | except UnknownTimeZoneError as ValueError: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | # we expect timezone failures here, just move over |
| 95 | pass |
| 96 | except ImportError: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 97 | zonefilelist[hashlib.md5(open(filepath, 'rb').read()).hexdigest()] = zonename |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 99 | TIME_ZONE = zonefilelist[hashlib.md5(open('/etc/localtime', 'rb').read()).hexdigest()] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
| 101 | # Language code for this installation. All choices can be found here: |
| 102 | # http://www.i18nguy.com/unicode/language-identifiers.html |
| 103 | LANGUAGE_CODE = 'en-us' |
| 104 | |
| 105 | SITE_ID = 1 |
| 106 | |
| 107 | # If you set this to False, Django will make some optimizations so as not |
| 108 | # to load the internationalization machinery. |
| 109 | USE_I18N = True |
| 110 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | # If you set this to False, Django will not use timezone-aware datetimes. |
| 112 | USE_TZ = True |
| 113 | |
| 114 | # Absolute filesystem path to the directory that will hold user-uploaded files. |
| 115 | # Example: "/var/www/example.com/media/" |
| 116 | MEDIA_ROOT = '' |
| 117 | |
| 118 | # URL that handles the media served from MEDIA_ROOT. Make sure to use a |
| 119 | # trailing slash. |
| 120 | # Examples: "http://example.com/media/", "http://media.example.com/" |
| 121 | MEDIA_URL = '' |
| 122 | |
| 123 | # Absolute path to the directory static files should be collected to. |
| 124 | # Don't put anything in this directory yourself; store your static files |
| 125 | # in apps' "static/" subdirectories and in STATICFILES_DIRS. |
| 126 | # Example: "/var/www/example.com/static/" |
| 127 | STATIC_ROOT = '' |
| 128 | |
| 129 | # URL prefix for static files. |
| 130 | # Example: "http://example.com/static/", "http://static.example.com/" |
| 131 | STATIC_URL = '/static/' |
| 132 | |
| 133 | # Additional locations of static files |
| 134 | STATICFILES_DIRS = ( |
| 135 | # Put strings here, like "/home/html/static" or "C:/www/django/static". |
| 136 | # Always use forward slashes, even on Windows. |
| 137 | # Don't forget to use absolute paths, not relative paths. |
| 138 | ) |
| 139 | |
| 140 | # List of finder classes that know how to find static files in |
| 141 | # various locations. |
| 142 | STATICFILES_FINDERS = ( |
| 143 | 'django.contrib.staticfiles.finders.FileSystemFinder', |
| 144 | 'django.contrib.staticfiles.finders.AppDirectoriesFinder', |
| 145 | # 'django.contrib.staticfiles.finders.DefaultStorageFinder', |
| 146 | ) |
| 147 | |
| 148 | # Make this unique, and don't share it with anybody. |
| 149 | SECRET_KEY = 'NOT_SUITABLE_FOR_HOSTED_DEPLOYMENT' |
| 150 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 151 | TMPDIR = os.environ.get('TOASTER_DJANGO_TMPDIR', '/tmp') |
| 152 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 153 | class InvalidString(str): |
| 154 | def __mod__(self, other): |
| 155 | from django.template.base import TemplateSyntaxError |
| 156 | raise TemplateSyntaxError( |
| 157 | "Undefined variable or unknown value for: \"%s\"" % other) |
| 158 | |
| 159 | TEMPLATES = [ |
| 160 | { |
| 161 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', |
| 162 | 'DIRS': [ |
| 163 | # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". |
| 164 | # Always use forward slashes, even on Windows. |
| 165 | # Don't forget to use absolute paths, not relative paths. |
| 166 | ], |
| 167 | 'OPTIONS': { |
| 168 | 'context_processors': [ |
| 169 | # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this |
| 170 | # list if you haven't customized them: |
| 171 | 'django.contrib.auth.context_processors.auth', |
| 172 | 'django.template.context_processors.debug', |
| 173 | 'django.template.context_processors.i18n', |
| 174 | 'django.template.context_processors.media', |
| 175 | 'django.template.context_processors.static', |
| 176 | 'django.template.context_processors.tz', |
| 177 | 'django.contrib.messages.context_processors.messages', |
| 178 | # Custom |
| 179 | 'django.template.context_processors.request', |
| 180 | 'toastergui.views.managedcontextprocessor', |
| 181 | |
| 182 | ], |
| 183 | 'loaders': [ |
| 184 | # List of callables that know how to import templates from various sources. |
| 185 | 'django.template.loaders.filesystem.Loader', |
| 186 | 'django.template.loaders.app_directories.Loader', |
| 187 | #'django.template.loaders.eggs.Loader', |
| 188 | ], |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 189 | # https://docs.djangoproject.com/en/4.2/ref/templates/api/#how-invalid-variables-are-handled |
| 190 | # Generally, string_if_invalid should only be enabled in order to debug |
| 191 | # a specific template problem, then cleared once debugging is complete. |
| 192 | # If you assign a value other than '' to string_if_invalid, |
| 193 | # you will experience rendering problems with these templates and sites. |
| 194 | # 'string_if_invalid': InvalidString("%s"), |
| 195 | 'string_if_invalid': "", |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 196 | 'debug': DEBUG, |
| 197 | }, |
| 198 | }, |
| 199 | ] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 200 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 201 | MIDDLEWARE = [ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 202 | 'django.middleware.common.CommonMiddleware', |
| 203 | 'django.contrib.sessions.middleware.SessionMiddleware', |
| 204 | 'django.middleware.csrf.CsrfViewMiddleware', |
| 205 | 'django.contrib.auth.middleware.AuthenticationMiddleware', |
| 206 | 'django.contrib.messages.middleware.MessageMiddleware', |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 207 | 'django.contrib.auth.middleware.AuthenticationMiddleware', |
| 208 | 'django.contrib.messages.middleware.MessageMiddleware', |
| 209 | 'django.contrib.sessions.middleware.SessionMiddleware', |
| 210 | ] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 211 | |
| 212 | CACHES = { |
| 213 | # 'default': { |
| 214 | # 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', |
| 215 | # 'LOCATION': '127.0.0.1:11211', |
| 216 | # }, |
| 217 | 'default': { |
| 218 | 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 219 | 'LOCATION': '%s/toaster_cache_%d' % (TMPDIR, os.getuid()), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 220 | 'TIMEOUT': 1, |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | from os.path import dirname as DN |
| 226 | SITE_ROOT=DN(DN(os.path.abspath(__file__))) |
| 227 | |
| 228 | import subprocess |
| 229 | TOASTER_BRANCH = subprocess.Popen('git branch | grep "^* " | tr -d "* "', cwd = SITE_ROOT, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] |
| 230 | TOASTER_REVISION = subprocess.Popen('git rev-parse HEAD ', cwd = SITE_ROOT, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] |
| 231 | |
| 232 | ROOT_URLCONF = 'toastermain.urls' |
| 233 | |
| 234 | # Python dotted path to the WSGI application used by Django's runserver. |
| 235 | WSGI_APPLICATION = 'toastermain.wsgi.application' |
| 236 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 237 | |
| 238 | INSTALLED_APPS = ( |
| 239 | 'django.contrib.auth', |
| 240 | 'django.contrib.contenttypes', |
| 241 | 'django.contrib.messages', |
| 242 | 'django.contrib.sessions', |
| 243 | 'django.contrib.admin', |
| 244 | 'django.contrib.staticfiles', |
| 245 | |
| 246 | # Uncomment the next line to enable admin documentation: |
| 247 | # 'django.contrib.admindocs', |
| 248 | 'django.contrib.humanize', |
| 249 | 'bldcollector', |
| 250 | 'toastermain', |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 251 | |
| 252 | # 3rd-lib |
| 253 | "log_viewer", |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 254 | ) |
| 255 | |
| 256 | |
| 257 | INTERNAL_IPS = ['127.0.0.1', '192.168.2.28'] |
| 258 | |
| 259 | # Load django-fresh is TOASTER_DEVEL is set, and the module is available |
| 260 | FRESH_ENABLED = False |
| 261 | if os.environ.get('TOASTER_DEVEL', None) is not None: |
| 262 | try: |
| 263 | import fresh |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 264 | MIDDLEWARE = ["fresh.middleware.FreshMiddleware",] + MIDDLEWARE |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 265 | INSTALLED_APPS = INSTALLED_APPS + ('fresh',) |
| 266 | FRESH_ENABLED = True |
| 267 | except: |
| 268 | pass |
| 269 | |
| 270 | DEBUG_PANEL_ENABLED = False |
| 271 | if os.environ.get('TOASTER_DEVEL', None) is not None: |
| 272 | try: |
| 273 | import debug_toolbar, debug_panel |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 274 | MIDDLEWARE = ['debug_panel.middleware.DebugPanelMiddleware',] + MIDDLEWARE |
| 275 | #MIDDLEWARE = MIDDLEWARE + ['debug_toolbar.middleware.DebugToolbarMiddleware',] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 276 | INSTALLED_APPS = INSTALLED_APPS + ('debug_toolbar','debug_panel',) |
| 277 | DEBUG_PANEL_ENABLED = True |
| 278 | |
| 279 | # this cache backend will be used by django-debug-panel |
| 280 | CACHES['debug-panel'] = { |
| 281 | 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', |
| 282 | 'LOCATION': '/var/tmp/debug-panel-cache', |
| 283 | 'TIMEOUT': 300, |
| 284 | 'OPTIONS': { |
| 285 | 'MAX_ENTRIES': 200 |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | except: |
| 290 | pass |
| 291 | |
| 292 | |
| 293 | SOUTH_TESTS_MIGRATE = False |
| 294 | |
| 295 | |
| 296 | # We automatically detect and install applications here if |
| 297 | # they have a 'models.py' or 'views.py' file |
| 298 | import os |
| 299 | currentdir = os.path.dirname(__file__) |
| 300 | for t in os.walk(os.path.dirname(currentdir)): |
| 301 | modulename = os.path.basename(t[0]) |
| 302 | #if we have a virtualenv skip it to avoid incorrect imports |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 303 | if 'VIRTUAL_ENV' in os.environ and os.environ['VIRTUAL_ENV'] in t[0]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 304 | continue |
| 305 | |
| 306 | if ("views.py" in t[2] or "models.py" in t[2]) and not modulename in INSTALLED_APPS: |
| 307 | INSTALLED_APPS = INSTALLED_APPS + (modulename,) |
| 308 | |
| 309 | # A sample logging configuration. The only tangible logging |
| 310 | # performed by this configuration is to send an email to |
| 311 | # the site admins on every HTTP 500 error when DEBUG=False. |
| 312 | # See http://docs.djangoproject.com/en/dev/topics/logging for |
| 313 | # more details on how to customize your logging configuration. |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 314 | LOGGING = LOGGING_SETTINGS |
| 315 | |
| 316 | # Build paths inside the project like this: BASE_DIR / 'subdir'. |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 317 | BUILDDIR = os.environ.get("BUILDDIR", TMPDIR) |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 318 | |
| 319 | # LOG VIEWER |
| 320 | # https://pypi.org/project/django-log-viewer/ |
| 321 | LOG_VIEWER_FILES_PATTERN = '*.log*' |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 322 | LOG_VIEWER_FILES_DIR = os.path.join(BUILDDIR, "toaster_logs/") |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 323 | LOG_VIEWER_PAGE_LENGTH = 25 # total log lines per-page |
| 324 | LOG_VIEWER_MAX_READ_LINES = 100000 # total log lines will be read |
| 325 | LOG_VIEWER_PATTERNS = ['INFO', 'DEBUG', 'WARNING', 'ERROR', 'CRITICAL'] |
| 326 | |
| 327 | # Optionally you can set the next variables in order to customize the admin: |
| 328 | LOG_VIEWER_FILE_LIST_TITLE = "Logs list" |
| 329 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 330 | if DEBUG and SQL_DEBUG: |
| 331 | LOGGING['loggers']['django.db.backends'] = { |
| 332 | 'level': 'DEBUG', |
| 333 | 'handlers': ['console'], |
| 334 | } |
| 335 | |
| 336 | |
| 337 | # If we're using sqlite, we need to tweak the performance a bit |
| 338 | from django.db.backends.signals import connection_created |
| 339 | def activate_synchronous_off(sender, connection, **kwargs): |
| 340 | if connection.vendor == 'sqlite': |
| 341 | cursor = connection.cursor() |
| 342 | cursor.execute('PRAGMA synchronous = 0;') |
| 343 | connection_created.connect(activate_synchronous_off) |
| 344 | # |
| 345 | |