settings.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.5.2'
  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. MAINTENANCE_MODE = getattr(configuration, 'MAINTENANCE_MODE', False)
  25. PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
  26. NETBOX_USERNAME = getattr(configuration, 'NETBOX_USERNAME', '')
  27. NETBOX_PASSWORD = getattr(configuration, 'NETBOX_PASSWORD', '')
  28. TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
  29. DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y')
  30. SHORT_DATE_FORMAT = getattr(configuration, 'SHORT_DATE_FORMAT', 'Y-m-d')
  31. TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a')
  32. SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s')
  33. DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
  34. SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i')
  35. BANNER_TOP = getattr(configuration, 'BANNER_TOP', False)
  36. BANNER_BOTTOM = getattr(configuration, 'BANNER_BOTTOM', False)
  37. PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
  38. ENFORCE_GLOBAL_UNIQUE = getattr(configuration, 'ENFORCE_GLOBAL_UNIQUE', False)
  39. CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS
  40. # Attempt to import LDAP configuration if it has been defined
  41. LDAP_IGNORE_CERT_ERRORS = False
  42. try:
  43. from ldap_config import *
  44. LDAP_CONFIGURED = True
  45. except ImportError:
  46. LDAP_CONFIGURED = False
  47. # LDAP configuration (optional)
  48. if LDAP_CONFIGURED:
  49. try:
  50. import ldap
  51. import django_auth_ldap
  52. # Prepend LDAPBackend to the default ModelBackend
  53. AUTHENTICATION_BACKENDS = [
  54. 'django_auth_ldap.backend.LDAPBackend',
  55. 'django.contrib.auth.backends.ModelBackend',
  56. ]
  57. # Optionally disable strict certificate checking
  58. if LDAP_IGNORE_CERT_ERRORS:
  59. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
  60. # Enable logging for django_auth_ldap
  61. logger = logging.getLogger('django_auth_ldap')
  62. logger.addHandler(logging.StreamHandler())
  63. logger.setLevel(logging.DEBUG)
  64. except ImportError:
  65. raise ImproperlyConfigured("LDAP authentication has been configured, but django-auth-ldap is not installed. "
  66. "You can remove netbox/ldap.py to disable LDAP.")
  67. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  68. # Database
  69. configuration.DATABASE.update({'ENGINE': 'django.db.backends.postgresql'})
  70. DATABASES = {
  71. 'default': configuration.DATABASE,
  72. }
  73. # Email
  74. EMAIL_HOST = EMAIL.get('SERVER')
  75. EMAIL_PORT = EMAIL.get('PORT', 25)
  76. EMAIL_HOST_USER = EMAIL.get('USERNAME')
  77. EMAIL_HOST_PASSWORD = EMAIL.get('PASSWORD')
  78. EMAIL_TIMEOUT = EMAIL.get('TIMEOUT', 10)
  79. SERVER_EMAIL = EMAIL.get('FROM_EMAIL')
  80. EMAIL_SUBJECT_PREFIX = '[NetBox] '
  81. # Installed applications
  82. INSTALLED_APPS = (
  83. 'django.contrib.admin',
  84. 'django.contrib.auth',
  85. 'django.contrib.contenttypes',
  86. 'django.contrib.sessions',
  87. 'django.contrib.messages',
  88. 'django.contrib.staticfiles',
  89. 'django.contrib.humanize',
  90. 'debug_toolbar',
  91. 'django_tables2',
  92. 'rest_framework',
  93. 'rest_framework_swagger',
  94. 'circuits',
  95. 'dcim',
  96. 'ipam',
  97. 'extras',
  98. 'secrets',
  99. 'tenancy',
  100. 'users',
  101. 'utilities',
  102. )
  103. # Middleware
  104. MIDDLEWARE_CLASSES = (
  105. 'django.contrib.sessions.middleware.SessionMiddleware',
  106. 'django.middleware.common.CommonMiddleware',
  107. 'django.middleware.csrf.CsrfViewMiddleware',
  108. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  109. 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  110. 'django.contrib.messages.middleware.MessageMiddleware',
  111. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  112. 'django.middleware.security.SecurityMiddleware',
  113. 'utilities.middleware.LoginRequiredMiddleware',
  114. )
  115. ROOT_URLCONF = 'netbox.urls'
  116. TEMPLATES = [
  117. {
  118. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  119. 'DIRS': [BASE_DIR + '/templates/'],
  120. 'APP_DIRS': True,
  121. 'OPTIONS': {
  122. 'context_processors': [
  123. 'django.template.context_processors.debug',
  124. 'django.template.context_processors.request',
  125. 'django.contrib.auth.context_processors.auth',
  126. 'django.contrib.messages.context_processors.messages',
  127. 'utilities.context_processors.settings',
  128. ],
  129. },
  130. },
  131. ]
  132. # WSGI
  133. WSGI_APPLICATION = 'netbox.wsgi.application'
  134. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  135. USE_X_FORWARDED_HOST = True
  136. # Internationalization
  137. # https://docs.djangoproject.com/en/1.8/topics/i18n/
  138. LANGUAGE_CODE = 'en-us'
  139. USE_I18N = True
  140. USE_TZ = True
  141. # Static files (CSS, JavaScript, Images)
  142. # https://docs.djangoproject.com/en/1.8/howto/static-files/
  143. STATIC_ROOT = BASE_DIR + '/static/'
  144. STATIC_URL = '/static/'
  145. STATICFILES_DIRS = (
  146. os.path.join(BASE_DIR, "project-static"),
  147. )
  148. # Messages
  149. MESSAGE_TAGS = {
  150. messages.ERROR: 'danger',
  151. }
  152. # Authentication URLs
  153. LOGIN_URL = '/login/'
  154. LOGIN_REDIRECT_URL = '/'
  155. # Secrets
  156. SECRETS_MIN_PUBKEY_SIZE = 2048
  157. # Django REST framework
  158. REST_FRAMEWORK = {
  159. 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',)
  160. }
  161. # Swagger settings (API docs)
  162. SWAGGER_SETTINGS = {
  163. 'base_path': '{}/api/docs'.format(ALLOWED_HOSTS[0]),
  164. }
  165. try:
  166. HOSTNAME = socket.gethostname()
  167. except:
  168. HOSTNAME = 'localhost'