settings.py 12 KB

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