You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
12 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
  1. """
  2. Django settings for app project.
  3. For more information on this file, see
  4. https://docs.djangoproject.com/en/2.0/topics/settings/
  5. For the full list of settings and their values, see
  6. https://docs.djangoproject.com/en/2.0/ref/settings/
  7. Any setting that is configured via an environment variable may
  8. also be set in a `.env` file in the project base directory.
  9. """
  10. import importlib.util
  11. import sys
  12. from os import path
  13. import dj_database_url
  14. from environs import Env
  15. from furl import furl
  16. # Build paths inside the project like this: path.join(BASE_DIR, ...)
  17. BASE_DIR = path.dirname(path.dirname(path.abspath(__file__)))
  18. env = Env()
  19. env.read_env(path.join(BASE_DIR, '.env'), recurse=False)
  20. # Quick-start development settings - unsuitable for production
  21. # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
  22. # SECURITY WARNING: keep the secret key used in production secret!
  23. SECRET_KEY = env('SECRET_KEY',
  24. 'v8sk33sy82!uw3ty=!jjv5vp7=s2phrzw(m(hrn^f7e_#1h2al')
  25. # SECURITY WARNING: don't run with debug turned on in production!
  26. DEBUG = env.bool('DEBUG', True)
  27. # True if you want to allow users to be able to create an account
  28. ALLOW_SIGNUP = env.bool('ALLOW_SIGNUP', True)
  29. # ALLOWED_HOSTS = []
  30. # Application definition
  31. INSTALLED_APPS = [
  32. 'whitenoise.runserver_nostatic',
  33. 'django.contrib.admin',
  34. 'django.contrib.auth',
  35. 'django.contrib.contenttypes',
  36. 'django.contrib.sessions',
  37. 'django.contrib.messages',
  38. 'django.contrib.staticfiles',
  39. 'server.apps.ServerConfig',
  40. 'api.apps.ApiConfig',
  41. # 'widget_tweaks',
  42. 'rest_framework',
  43. 'rest_framework.authtoken',
  44. 'django_filters',
  45. 'social_django',
  46. 'polymorphic',
  47. # 'webpack_loader',
  48. 'corsheaders',
  49. 'drf_yasg'
  50. ]
  51. CLOUD_BROWSER_APACHE_LIBCLOUD_PROVIDER = env('CLOUD_BROWSER_LIBCLOUD_PROVIDER', None)
  52. CLOUD_BROWSER_APACHE_LIBCLOUD_ACCOUNT = env('CLOUD_BROWSER_LIBCLOUD_ACCOUNT', None)
  53. CLOUD_BROWSER_APACHE_LIBCLOUD_SECRET_KEY = env('CLOUD_BROWSER_LIBCLOUD_KEY', None)
  54. if CLOUD_BROWSER_APACHE_LIBCLOUD_PROVIDER:
  55. CLOUD_BROWSER_DATASTORE = 'ApacheLibcloud'
  56. CLOUD_BROWSER_OBJECT_REDIRECT_URL = '/v1/cloud-upload'
  57. INSTALLED_APPS.append('cloud_browser')
  58. MIDDLEWARE = [
  59. 'django.middleware.security.SecurityMiddleware',
  60. 'whitenoise.middleware.WhiteNoiseMiddleware',
  61. 'django.contrib.sessions.middleware.SessionMiddleware',
  62. 'django.middleware.common.CommonMiddleware',
  63. 'django.middleware.csrf.CsrfViewMiddleware',
  64. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  65. 'django.contrib.messages.middleware.MessageMiddleware',
  66. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  67. 'social_django.middleware.SocialAuthExceptionMiddleware',
  68. # 'applicationinsights.django.ApplicationInsightsMiddleware',
  69. 'corsheaders.middleware.CorsMiddleware',
  70. ]
  71. ROOT_URLCONF = 'app.urls'
  72. TEMPLATES = [
  73. {
  74. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  75. 'DIRS': [path.join(BASE_DIR, 'client/dist')],
  76. 'APP_DIRS': True,
  77. 'OPTIONS': {
  78. 'context_processors': [
  79. 'django.template.context_processors.debug',
  80. 'django.template.context_processors.request',
  81. 'django.contrib.auth.context_processors.auth',
  82. 'django.contrib.messages.context_processors.messages',
  83. 'social_django.context_processors.backends',
  84. 'social_django.context_processors.login_redirect',
  85. ],
  86. 'libraries': {
  87. 'analytics': 'server.templatetags.analytics',
  88. 'utils_templating': 'authentification.templatetags.utils_templating',
  89. },
  90. },
  91. },
  92. ]
  93. # Static files (CSS, JavaScript, Images)
  94. # https://docs.djangoproject.com/en/2.0/howto/static-files/
  95. STATIC_URL = '/static/'
  96. STATIC_ROOT = path.join(BASE_DIR, 'staticfiles')
  97. STATICFILES_DIRS = [
  98. path.join(BASE_DIR, 'client/dist/static'),
  99. ]
  100. STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
  101. WEBPACK_LOADER = {
  102. 'DEFAULT': {
  103. 'CACHE': not DEBUG,
  104. 'BUNDLE_DIR_NAME': 'bundle/',
  105. 'STATS_FILE': path.join(BASE_DIR, 'server', 'static', 'webpack-stats.json'),
  106. 'POLL_INTERVAL': 0.1,
  107. 'TIMEOUT': None,
  108. 'IGNORE': [r'.*\.hot-update.js', r'.+\.map']
  109. }
  110. }
  111. WSGI_APPLICATION = 'app.wsgi.application'
  112. AUTHENTICATION_BACKENDS = [
  113. 'social_core.backends.github.GithubOAuth2',
  114. 'social_core.backends.azuread_tenant.AzureADTenantOAuth2',
  115. 'social_core.backends.okta.OktaOAuth2',
  116. 'social_core.backends.okta_openidconnect.OktaOpenIdConnect',
  117. 'django.contrib.auth.backends.ModelBackend',
  118. ]
  119. HEADER_AUTH_USER_NAME = env('HEADER_AUTH_USER_NAME', '')
  120. HEADER_AUTH_USER_GROUPS = env('HEADER_AUTH_USER_GROUPS', '')
  121. HEADER_AUTH_ADMIN_GROUP_NAME = env('HEADER_AUTH_ADMIN_GROUP_NAME', '')
  122. HEADER_AUTH_GROUPS_SEPERATOR = env('HEADER_AUTH_GROUPS_SEPERATOR', default=',')
  123. if HEADER_AUTH_USER_NAME and HEADER_AUTH_USER_GROUPS and HEADER_AUTH_ADMIN_GROUP_NAME:
  124. MIDDLEWARE.append('server.middleware.HeaderAuthMiddleware')
  125. AUTHENTICATION_BACKENDS.append('django.contrib.auth.backends.RemoteUserBackend')
  126. SOCIAL_AUTH_GITHUB_KEY = env('OAUTH_GITHUB_KEY', None)
  127. SOCIAL_AUTH_GITHUB_SECRET = env('OAUTH_GITHUB_SECRET', None)
  128. GITHUB_ADMIN_ORG_NAME = env('GITHUB_ADMIN_ORG_NAME', None)
  129. GITHUB_ADMIN_TEAM_NAME = env('GITHUB_ADMIN_TEAM_NAME', None)
  130. if GITHUB_ADMIN_ORG_NAME and GITHUB_ADMIN_TEAM_NAME:
  131. SOCIAL_AUTH_GITHUB_SCOPE = ['read:org']
  132. SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY = env('OAUTH_AAD_KEY', None)
  133. SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET = env('OAUTH_AAD_SECRET', None)
  134. SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID = env('OAUTH_AAD_TENANT', None)
  135. AZUREAD_ADMIN_GROUP_ID = env('AZUREAD_ADMIN_GROUP_ID', None)
  136. if AZUREAD_ADMIN_GROUP_ID:
  137. SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_RESOURCE = 'https://graph.microsoft.com/'
  138. SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SCOPE = ['Directory.Read.All']
  139. SOCIAL_AUTH_OKTA_OAUTH2_KEY = env('OAUTH_OKTA_OAUTH2_KEY', None)
  140. SOCIAL_AUTH_OKTA_OAUTH2_SECRET = env('OAUTH_OKTA_OAUTH2_SECRET', None)
  141. SOCIAL_AUTH_OKTA_OAUTH2_API_URL = env('OAUTH_OKTA_OAUTH2_API_URL', None)
  142. OKTA_OAUTH2_ADMIN_GROUP_NAME = env('OKTA_OAUTH2_ADMIN_GROUP_NAME', None)
  143. if SOCIAL_AUTH_OKTA_OAUTH2_API_URL:
  144. SOCIAL_AUTH_OKTA_OAUTH2_SCOPE = ["groups"]
  145. SOCIAL_AUTH_OKTA_OPENIDCONNECT_KEY = env('OAUTH_OKTA_OPENIDCONNECT_KEY', None)
  146. SOCIAL_AUTH_OKTA_OPENIDCONNECT_SECRET = env('OAUTH_OKTA_OPENIDCONNECT_SECRET', None)
  147. SOCIAL_AUTH_OKTA_OPENIDCONNECT_API_URL = env('OAUTH_OKTA_OPENIDCONNECT_API_URL', None)
  148. OKTA_OPENIDCONNECT_ADMIN_GROUP_NAME = env('OKTA_OPENIDCONNECT_ADMIN_GROUP_NAME', None)
  149. if SOCIAL_AUTH_OKTA_OPENIDCONNECT_API_URL:
  150. SOCIAL_AUTH_OKTA_OPENIDCONNECT_SCOPE = ["groups"]
  151. SOCIAL_AUTH_PIPELINE = [
  152. 'social_core.pipeline.social_auth.social_details',
  153. 'social_core.pipeline.social_auth.social_uid',
  154. 'social_core.pipeline.social_auth.auth_allowed',
  155. 'social_core.pipeline.social_auth.social_user',
  156. 'social_core.pipeline.user.get_username',
  157. 'social_core.pipeline.user.create_user',
  158. 'social_core.pipeline.social_auth.associate_user',
  159. 'social_core.pipeline.social_auth.load_extra_data',
  160. 'social_core.pipeline.user.user_details',
  161. 'server.social_auth.fetch_github_permissions',
  162. 'server.social_auth.fetch_azuread_permissions',
  163. 'server.social_auth.fetch_okta_oauth2_permissions',
  164. 'server.social_auth.fetch_okta_openidconnect_permissions',
  165. ]
  166. ROLE_PROJECT_ADMIN = env('ROLE_PROJECT_ADMIN', 'project_admin')
  167. ROLE_ANNOTATOR = env('ROLE_ANNOTATOR', 'annotator')
  168. ROLE_ANNOTATION_APPROVER = env('ROLE_ANNOTATION_APPROVER', 'annotation_approver')
  169. # Database
  170. # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
  171. DATABASES = {
  172. 'default': {
  173. 'ENGINE': 'django.db.backends.sqlite3',
  174. 'NAME': path.join(BASE_DIR, 'db.sqlite3'),
  175. }
  176. }
  177. # Password validation
  178. # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
  179. AUTH_PASSWORD_VALIDATORS = [
  180. {
  181. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  182. },
  183. {
  184. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  185. },
  186. {
  187. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  188. },
  189. {
  190. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  191. },
  192. ]
  193. REST_FRAMEWORK = {
  194. # Use Django's standard `django.contrib.auth` permissions,
  195. # or allow read-only access for unauthenticated users.
  196. 'DEFAULT_PERMISSION_CLASSES': [
  197. 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
  198. 'rest_framework.permissions.IsAuthenticated',
  199. ],
  200. 'DEFAULT_AUTHENTICATION_CLASSES': (
  201. 'rest_framework.authentication.SessionAuthentication',
  202. 'rest_framework.authentication.TokenAuthentication',
  203. ),
  204. 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
  205. 'PAGE_SIZE': env.int('DOCCANO_PAGE_SIZE', default=5),
  206. 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
  207. 'SEARCH_PARAM': 'q',
  208. 'DEFAULT_RENDERER_CLASSES': (
  209. 'rest_framework.renderers.JSONRenderer',
  210. 'rest_framework.renderers.BrowsableAPIRenderer',
  211. 'rest_framework_xml.renderers.XMLRenderer'
  212. )
  213. }
  214. # Internationalization
  215. # https://docs.djangoproject.com/en/2.0/topics/i18n/
  216. LANGUAGE_CODE = 'en-us'
  217. TIME_ZONE = 'UTC'
  218. USE_I18N = True
  219. USE_L10N = True
  220. USE_TZ = True
  221. TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
  222. TEST_OUTPUT_DIR = path.join(BASE_DIR, 'junitxml')
  223. LOGIN_URL = '/login/'
  224. LOGIN_REDIRECT_URL = '/projects/'
  225. LOGOUT_REDIRECT_URL = '/'
  226. # dynamic import to avoid installing psycopg2 on pip installation.
  227. name = 'django_heroku'
  228. spec = importlib.util.find_spec(name)
  229. if spec is not None:
  230. module = importlib.util.module_from_spec(spec)
  231. sys.modules[name] = module
  232. spec.loader.exec_module(module)
  233. module.settings(locals(), test_runner=False)
  234. # Change 'default' database configuration with $DATABASE_URL.
  235. DATABASES['default'].update(dj_database_url.config(
  236. env='DATABASE_URL',
  237. conn_max_age=env.int('DATABASE_CONN_MAX_AGE', 500),
  238. ssl_require='sslmode' not in furl(env('DATABASE_URL', '')).args,
  239. ))
  240. # work-around for dj-database-url: explicitly disable ssl for sqlite
  241. if DATABASES['default'].get('ENGINE') == 'django.db.backends.sqlite3':
  242. DATABASES['default'].get('OPTIONS', {}).pop('sslmode', None)
  243. # work-around for dj-database-url: patch ssl for mysql
  244. if DATABASES['default'].get('ENGINE') == 'django.db.backends.mysql':
  245. DATABASES['default'].get('OPTIONS', {}).pop('sslmode', None)
  246. if env('MYSQL_SSL_CA', None):
  247. DATABASES['default'].setdefault('OPTIONS', {})\
  248. .setdefault('ssl', {}).setdefault('ca', env('MYSQL_SSL_CA', None))
  249. # default to a sensible modern driver for Azure SQL
  250. if DATABASES['default'].get('ENGINE') == 'sql_server.pyodbc':
  251. DATABASES['default'].setdefault('OPTIONS', {})\
  252. .setdefault('driver', 'ODBC Driver 17 for SQL Server')
  253. # Honor the 'X-Forwarded-Proto' header for request.is_secure()
  254. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  255. SESSION_COOKIE_SECURE = env.bool('SESSION_COOKIE_SECURE', False)
  256. CSRF_COOKIE_SECURE = env.bool('CSRF_COOKIE_SECURE', False)
  257. CSRF_TRUSTED_ORIGINS = env.list('CSRF_TRUSTED_ORIGINS', [])
  258. # Allow all host headers
  259. ALLOWED_HOSTS = ['*']
  260. # Size of the batch for creating documents
  261. # on the import phase
  262. IMPORT_BATCH_SIZE = env.int('IMPORT_BATCH_SIZE', 500)
  263. GOOGLE_TRACKING_ID = env('GOOGLE_TRACKING_ID', 'UA-125643874-2').strip()
  264. AZURE_APPINSIGHTS_IKEY = env('AZURE_APPINSIGHTS_IKEY', None)
  265. APPLICATION_INSIGHTS = {
  266. 'ikey': AZURE_APPINSIGHTS_IKEY if AZURE_APPINSIGHTS_IKEY else None,
  267. 'endpoint': env('AZURE_APPINSIGHTS_ENDPOINT', None),
  268. }
  269. # necessary for email verification of new accounts
  270. EMAIL_USE_TLS = env.bool('EMAIL_USE_TLS', False)
  271. EMAIL_HOST = env('EMAIL_HOST', None)
  272. EMAIL_HOST_USER = env('EMAIL_HOST_USER', None)
  273. EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD', None)
  274. EMAIL_PORT = env.int('EMAIL_PORT', 587)
  275. DEFAULT_FROM_EMAIL = env('DEFAULT_FROM_EMAIL', 'webmaster@localhost')
  276. if not EMAIL_HOST:
  277. EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
  278. if DEBUG:
  279. CORS_ORIGIN_WHITELIST = (
  280. 'http://127.0.0.1:3000',
  281. 'http://0.0.0.0:3000',
  282. 'http://localhost:3000'
  283. )