settings.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import logging
  2. import os
  3. import socket
  4. from django.contrib.messages import constants as messages
  5. from django.core.exceptions import ImproperlyConfigured
  6. try:
  7. from netbox import configuration
  8. except ImportError:
  9. raise ImproperlyConfigured("Configuration file is not present. Please define netbox/netbox/configuration.py per "
  10. "the documentation.")
  11. VERSION = '1.9.2-dev'
  12. # Import local configuration
  13. for setting in ['ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY']:
  14. try:
  15. globals()[setting] = getattr(configuration, setting)
  16. except AttributeError:
  17. raise ImproperlyConfigured("Mandatory setting {} is missing from configuration.py. Please define it per the "
  18. "documentation.".format(setting))
  19. # Default configurations
  20. ADMINS = getattr(configuration, 'ADMINS', [])
  21. DEBUG = getattr(configuration, 'DEBUG', False)
  22. EMAIL = getattr(configuration, 'EMAIL', {})
  23. LOGIN_REQUIRED = getattr(configuration, 'LOGIN_REQUIRED', False)
  24. BASE_PATH = getattr(configuration, 'BASE_PATH', '')
  25. if BASE_PATH:
  26. BASE_PATH = BASE_PATH.strip('/') + '/' # Enforce trailing slash only
  27. MAINTENANCE_MODE = getattr(configuration, 'MAINTENANCE_MODE', False)
  28. PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
  29. NETBOX_USERNAME = getattr(configuration, 'NETBOX_USERNAME', '')
  30. NETBOX_PASSWORD = getattr(configuration, 'NETBOX_PASSWORD', '')
  31. TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
  32. DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y')
  33. SHORT_DATE_FORMAT = getattr(configuration, 'SHORT_DATE_FORMAT', 'Y-m-d')
  34. TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a')
  35. SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s')
  36. DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
  37. SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i')
  38. BANNER_TOP = getattr(configuration, 'BANNER_TOP', False)
  39. BANNER_BOTTOM = getattr(configuration, 'BANNER_BOTTOM', False)
  40. PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
  41. ENFORCE_GLOBAL_UNIQUE = getattr(configuration, 'ENFORCE_GLOBAL_UNIQUE', False)
  42. CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS
  43. # Attempt to import LDAP configuration if it has been defined
  44. LDAP_IGNORE_CERT_ERRORS = False
  45. try:
  46. from netbox.ldap_config import *
  47. LDAP_CONFIGURED = True
  48. except ImportError:
  49. LDAP_CONFIGURED = False
  50. # LDAP configuration (optional)
  51. if LDAP_CONFIGURED:
  52. try:
  53. import ldap
  54. import django_auth_ldap
  55. # Prepend LDAPBackend to the default ModelBackend
  56. AUTHENTICATION_BACKENDS = [
  57. 'django_auth_ldap.backend.LDAPBackend',
  58. 'django.contrib.auth.backends.ModelBackend',
  59. ]
  60. # Optionally disable strict certificate checking
  61. if LDAP_IGNORE_CERT_ERRORS:
  62. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
  63. # Enable logging for django_auth_ldap
  64. logger = logging.getLogger('django_auth_ldap')
  65. logger.addHandler(logging.StreamHandler())
  66. logger.setLevel(logging.DEBUG)
  67. except ImportError:
  68. raise ImproperlyConfigured("LDAP authentication has been configured, but django-auth-ldap is not installed. "
  69. "You can remove netbox/ldap_config.py to disable LDAP.")
  70. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  71. # Database
  72. configuration.DATABASE.update({'ENGINE': 'django.db.backends.postgresql'})
  73. DATABASES = {
  74. 'default': configuration.DATABASE,
  75. }
  76. # Email
  77. EMAIL_HOST = EMAIL.get('SERVER')
  78. EMAIL_PORT = EMAIL.get('PORT', 25)
  79. EMAIL_HOST_USER = EMAIL.get('USERNAME')
  80. EMAIL_HOST_PASSWORD = EMAIL.get('PASSWORD')
  81. EMAIL_TIMEOUT = EMAIL.get('TIMEOUT', 10)
  82. SERVER_EMAIL = EMAIL.get('FROM_EMAIL')
  83. EMAIL_SUBJECT_PREFIX = '[NetBox] '
  84. # Installed applications
  85. INSTALLED_APPS = (
  86. 'django.contrib.admin',
  87. 'django.contrib.auth',
  88. 'django.contrib.contenttypes',
  89. 'django.contrib.sessions',
  90. 'django.contrib.messages',
  91. 'django.contrib.staticfiles',
  92. 'django.contrib.humanize',
  93. 'debug_toolbar',
  94. 'django_tables2',
  95. 'mptt',
  96. 'rest_framework',
  97. 'rest_framework_swagger',
  98. 'circuits',
  99. 'dcim',
  100. 'ipam',
  101. 'extras',
  102. 'secrets',
  103. 'tenancy',
  104. 'users',
  105. 'utilities',
  106. )
  107. # Middleware
  108. MIDDLEWARE = (
  109. 'debug_toolbar.middleware.DebugToolbarMiddleware',
  110. 'django.contrib.sessions.middleware.SessionMiddleware',
  111. 'django.middleware.common.CommonMiddleware',
  112. 'django.middleware.csrf.CsrfViewMiddleware',
  113. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  114. 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  115. 'django.contrib.messages.middleware.MessageMiddleware',
  116. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  117. 'django.middleware.security.SecurityMiddleware',
  118. 'utilities.middleware.LoginRequiredMiddleware',
  119. )
  120. ROOT_URLCONF = 'netbox.urls'
  121. TEMPLATES = [
  122. {
  123. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  124. 'DIRS': [BASE_DIR + '/templates/'],
  125. 'APP_DIRS': True,
  126. 'OPTIONS': {
  127. 'context_processors': [
  128. 'django.template.context_processors.debug',
  129. 'django.template.context_processors.request',
  130. 'django.contrib.auth.context_processors.auth',
  131. 'django.contrib.messages.context_processors.messages',
  132. 'utilities.context_processors.settings',
  133. ],
  134. },
  135. },
  136. ]
  137. # WSGI
  138. WSGI_APPLICATION = 'netbox.wsgi.application'
  139. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  140. USE_X_FORWARDED_HOST = True
  141. # Internationalization
  142. # https://docs.djangoproject.com/en/1.8/topics/i18n/
  143. LANGUAGE_CODE = 'en-us'
  144. USE_I18N = True
  145. USE_TZ = True
  146. # Static files (CSS, JavaScript, Images)
  147. # https://docs.djangoproject.com/en/1.8/howto/static-files/
  148. STATIC_ROOT = BASE_DIR + '/static/'
  149. STATIC_URL = '/{}static/'.format(BASE_PATH)
  150. STATICFILES_DIRS = (
  151. os.path.join(BASE_DIR, "project-static"),
  152. )
  153. # Disable default limit of 1000 fields per request. Needed for bulk deletion of objects. (Added in Django 1.10.)
  154. DATA_UPLOAD_MAX_NUMBER_FIELDS = None
  155. # Messages
  156. MESSAGE_TAGS = {
  157. messages.ERROR: 'danger',
  158. }
  159. # Authentication URLs
  160. LOGIN_URL = '/{}login/'.format(BASE_PATH)
  161. # Secrets
  162. SECRETS_MIN_PUBKEY_SIZE = 2048
  163. # Django REST framework
  164. REST_FRAMEWORK = {
  165. 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',)
  166. }
  167. if LOGIN_REQUIRED:
  168. REST_FRAMEWORK['DEFAULT_PERMISSION_CLASSES'] = ('rest_framework.permissions.IsAuthenticated',)
  169. # Django debug toolbar
  170. INTERNAL_IPS = (
  171. '127.0.0.1',
  172. '::1',
  173. )
  174. try:
  175. HOSTNAME = socket.gethostname()
  176. except:
  177. HOSTNAME = 'localhost'