settings.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 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.7.4-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 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. 'rest_framework',
  96. 'rest_framework_swagger',
  97. 'circuits',
  98. 'dcim',
  99. 'ipam',
  100. 'extras',
  101. 'secrets',
  102. 'tenancy',
  103. 'users',
  104. 'utilities',
  105. )
  106. # Middleware
  107. MIDDLEWARE = (
  108. 'debug_toolbar.middleware.DebugToolbarMiddleware',
  109. 'django.contrib.sessions.middleware.SessionMiddleware',
  110. 'django.middleware.common.CommonMiddleware',
  111. 'django.middleware.csrf.CsrfViewMiddleware',
  112. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  113. 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  114. 'django.contrib.messages.middleware.MessageMiddleware',
  115. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  116. 'django.middleware.security.SecurityMiddleware',
  117. 'utilities.middleware.LoginRequiredMiddleware',
  118. )
  119. ROOT_URLCONF = 'netbox.urls'
  120. TEMPLATES = [
  121. {
  122. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  123. 'DIRS': [BASE_DIR + '/templates/'],
  124. 'APP_DIRS': True,
  125. 'OPTIONS': {
  126. 'context_processors': [
  127. 'django.template.context_processors.debug',
  128. 'django.template.context_processors.request',
  129. 'django.contrib.auth.context_processors.auth',
  130. 'django.contrib.messages.context_processors.messages',
  131. 'utilities.context_processors.settings',
  132. ],
  133. },
  134. },
  135. ]
  136. # WSGI
  137. WSGI_APPLICATION = 'netbox.wsgi.application'
  138. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  139. USE_X_FORWARDED_HOST = True
  140. # Internationalization
  141. # https://docs.djangoproject.com/en/1.8/topics/i18n/
  142. LANGUAGE_CODE = 'en-us'
  143. USE_I18N = True
  144. USE_TZ = True
  145. # Static files (CSS, JavaScript, Images)
  146. # https://docs.djangoproject.com/en/1.8/howto/static-files/
  147. STATIC_ROOT = BASE_DIR + '/static/'
  148. STATIC_URL = '/{}static/'.format(BASE_PATH)
  149. STATICFILES_DIRS = (
  150. os.path.join(BASE_DIR, "project-static"),
  151. )
  152. # Disable default limit of 1000 fields per request. Needed for bulk deletion of objects. (Added in Django 1.10.)
  153. DATA_UPLOAD_MAX_NUMBER_FIELDS = None
  154. # Messages
  155. MESSAGE_TAGS = {
  156. messages.ERROR: 'danger',
  157. }
  158. # Authentication URLs
  159. LOGIN_URL = '/{}login/'.format(BASE_PATH)
  160. # Secrets
  161. SECRETS_MIN_PUBKEY_SIZE = 2048
  162. # Django REST framework
  163. REST_FRAMEWORK = {
  164. 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',)
  165. }
  166. if LOGIN_REQUIRED:
  167. REST_FRAMEWORK['DEFAULT_PERMISSION_CLASSES'] = ('rest_framework.permissions.IsAuthenticated',)
  168. # Swagger settings (API docs)
  169. SWAGGER_SETTINGS = {
  170. 'base_path': '{}/{}api/docs'.format(ALLOWED_HOSTS[0], BASE_PATH),
  171. }
  172. try:
  173. HOSTNAME = socket.gethostname()
  174. except:
  175. HOSTNAME = 'localhost'