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.

268 lines
9.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 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. from os import path
  11. import dj_database_url
  12. from environs import Env, EnvError
  13. from furl import furl
  14. # Build paths inside the project like this: path.join(BASE_DIR, ...)
  15. BASE_DIR = path.dirname(path.dirname(path.dirname(path.abspath(__file__))))
  16. env = Env()
  17. env.read_env(path.join(BASE_DIR, ".env"), recurse=False)
  18. # Quick-start development settings - unsuitable for production
  19. # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
  20. # SECURITY WARNING: keep the secret key used in production secret!
  21. SECRET_KEY = env("SECRET_KEY", "v8sk33sy82!uw3ty=!jjv5vp7=s2phrzw(m(hrn^f7e_#1h2al")
  22. # SECURITY WARNING: don't run with debug turned on in production!
  23. DEBUG = env.bool("DEBUG", True)
  24. # Application definition
  25. INSTALLED_APPS = [
  26. "whitenoise.runserver_nostatic",
  27. "django.contrib.admin",
  28. "django.contrib.auth",
  29. "django.contrib.contenttypes",
  30. "django.contrib.sessions",
  31. "django.contrib.messages",
  32. "django.contrib.staticfiles",
  33. "api",
  34. "roles",
  35. "projects",
  36. "metrics",
  37. "users",
  38. "data_import",
  39. "data_export",
  40. "auto_labeling",
  41. "labels",
  42. "label_types",
  43. "examples",
  44. "rest_framework",
  45. "rest_framework.authtoken",
  46. "django_filters",
  47. "polymorphic",
  48. "corsheaders",
  49. "drf_yasg",
  50. "dj_rest_auth",
  51. "django_celery_results",
  52. "django_drf_filepond",
  53. "health_check",
  54. "health_check.cache",
  55. "health_check.storage",
  56. "health_check.contrib.migrations",
  57. "health_check.contrib.celery",
  58. ]
  59. MIDDLEWARE = [
  60. "django.middleware.security.SecurityMiddleware",
  61. "whitenoise.middleware.WhiteNoiseMiddleware",
  62. "django.contrib.sessions.middleware.SessionMiddleware",
  63. "django.middleware.common.CommonMiddleware",
  64. "django.middleware.csrf.CsrfViewMiddleware",
  65. "django.contrib.auth.middleware.AuthenticationMiddleware",
  66. "django.contrib.messages.middleware.MessageMiddleware",
  67. "django.middleware.clickjacking.XFrameOptionsMiddleware",
  68. "corsheaders.middleware.CorsMiddleware",
  69. ]
  70. ROOT_URLCONF = "config.urls"
  71. WSGI_APPLICATION = "config.wsgi.application"
  72. # Django templates
  73. TEMPLATES = [
  74. {
  75. "BACKEND": "django.template.backends.django.DjangoTemplates",
  76. "DIRS": [path.join(BASE_DIR, "client/dist")],
  77. "APP_DIRS": True,
  78. "OPTIONS": {
  79. "context_processors": [
  80. "django.template.context_processors.debug",
  81. "django.template.context_processors.request",
  82. "django.contrib.auth.context_processors.auth",
  83. "django.contrib.messages.context_processors.messages",
  84. ],
  85. },
  86. },
  87. ]
  88. # Static files (CSS, JavaScript, Images)
  89. # https://docs.djangoproject.com/en/2.0/howto/static-files/
  90. STATIC_URL = "/static/"
  91. STATIC_ROOT = path.join(BASE_DIR, "staticfiles")
  92. STATICFILES_DIRS = [
  93. path.join(BASE_DIR, "client/dist/static"),
  94. ]
  95. # STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
  96. STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
  97. # Auth settings
  98. AUTHENTICATION_BACKENDS = [
  99. "django.contrib.auth.backends.ModelBackend",
  100. ]
  101. HEADER_AUTH_USER_NAME = env("HEADER_AUTH_USER_NAME", "")
  102. HEADER_AUTH_USER_GROUPS = env("HEADER_AUTH_USER_GROUPS", "")
  103. HEADER_AUTH_ADMIN_GROUP_NAME = env("HEADER_AUTH_ADMIN_GROUP_NAME", "")
  104. HEADER_AUTH_GROUPS_SEPERATOR = env("HEADER_AUTH_GROUPS_SEPERATOR", default=",")
  105. if HEADER_AUTH_USER_NAME and HEADER_AUTH_USER_GROUPS and HEADER_AUTH_ADMIN_GROUP_NAME:
  106. MIDDLEWARE.append("api.middleware.HeaderAuthMiddleware")
  107. AUTHENTICATION_BACKENDS.append("django.contrib.auth.backends.RemoteUserBackend")
  108. # Role settings
  109. ROLE_PROJECT_ADMIN = env("ROLE_PROJECT_ADMIN", "project_admin")
  110. ROLE_ANNOTATOR = env("ROLE_ANNOTATOR", "annotator")
  111. ROLE_ANNOTATION_APPROVER = env("ROLE_ANNOTATION_APPROVER", "annotation_approver")
  112. # Password validation
  113. # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
  114. AUTH_PASSWORD_VALIDATORS = [
  115. {
  116. "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
  117. },
  118. {
  119. "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
  120. },
  121. {
  122. "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
  123. },
  124. {
  125. "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
  126. },
  127. ]
  128. REST_FRAMEWORK = {
  129. # Use Django's standard `django.contrib.auth` permissions,
  130. # or allow read-only access for unauthenticated users.
  131. "DEFAULT_PERMISSION_CLASSES": [
  132. "rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly",
  133. "rest_framework.permissions.IsAuthenticated",
  134. ],
  135. "DEFAULT_AUTHENTICATION_CLASSES": (
  136. "rest_framework.authentication.SessionAuthentication",
  137. "rest_framework.authentication.TokenAuthentication",
  138. ),
  139. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
  140. "PAGE_SIZE": env.int("DOCCANO_PAGE_SIZE", default=5),
  141. "DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
  142. "SEARCH_PARAM": "q",
  143. "DEFAULT_RENDERER_CLASSES": (
  144. "rest_framework.renderers.JSONRenderer",
  145. "rest_framework.renderers.BrowsableAPIRenderer",
  146. "rest_framework_xml.renderers.XMLRenderer",
  147. ),
  148. }
  149. # Internationalization
  150. # https://docs.djangoproject.com/en/2.0/topics/i18n/
  151. LANGUAGE_CODE = "en-us"
  152. TIME_ZONE = "UTC"
  153. USE_I18N = True
  154. USE_L10N = True
  155. USE_TZ = True
  156. # Testing
  157. TEST_RUNNER = "xmlrunner.extra.djangotestrunner.XMLTestRunner"
  158. TEST_OUTPUT_DIR = path.join(BASE_DIR, "junitxml")
  159. LOGIN_URL = "/login/"
  160. LOGIN_REDIRECT_URL = "/projects/"
  161. LOGOUT_REDIRECT_URL = "/"
  162. # Database
  163. # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
  164. DATABASES = {
  165. "default": {
  166. "ENGINE": "django.db.backends.sqlite3",
  167. "NAME": path.join(BASE_DIR, "db.sqlite3"),
  168. }
  169. }
  170. # Change 'default' database configuration with $DATABASE_URL.
  171. DATABASES["default"].update(
  172. dj_database_url.config(
  173. env="DATABASE_URL",
  174. conn_max_age=env.int("DATABASE_CONN_MAX_AGE", 500),
  175. ssl_require="sslmode" not in furl(env("DATABASE_URL", "")).args,
  176. )
  177. )
  178. # work-around for dj-database-url: explicitly disable ssl for sqlite
  179. if DATABASES["default"].get("ENGINE") == "django.db.backends.sqlite3":
  180. DATABASES["default"].get("OPTIONS", {}).pop("sslmode", None)
  181. # work-around for dj-database-url: patch ssl for mysql
  182. if DATABASES["default"].get("ENGINE") == "django.db.backends.mysql":
  183. DATABASES["default"].get("OPTIONS", {}).pop("sslmode", None)
  184. if env("MYSQL_SSL_CA", None):
  185. DATABASES["default"].setdefault("OPTIONS", {}).setdefault("ssl", {}).setdefault("ca", env("MYSQL_SSL_CA", None))
  186. # default to a sensible modern driver for Azure SQL
  187. if DATABASES["default"].get("ENGINE") == "sql_server.pyodbc":
  188. DATABASES["default"].setdefault("OPTIONS", {}).setdefault("driver", "ODBC Driver 17 for SQL Server")
  189. # Sessions and CSRF
  190. # Honor the 'X-Forwarded-Proto' header for request.is_secure()
  191. SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
  192. SESSION_COOKIE_SECURE = env.bool("SESSION_COOKIE_SECURE", False)
  193. CSRF_COOKIE_SECURE = env.bool("CSRF_COOKIE_SECURE", False)
  194. CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS", [])
  195. # Allow all host headers
  196. ALLOWED_HOSTS = ["*"]
  197. # Batch size for importing data
  198. IMPORT_BATCH_SIZE = env.int("IMPORT_BATCH_SIZE", 1000)
  199. # Necessary for email verification of new accounts
  200. EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS", False)
  201. EMAIL_HOST = env("EMAIL_HOST", None)
  202. EMAIL_HOST_USER = env("EMAIL_HOST_USER", None)
  203. EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD", None)
  204. EMAIL_PORT = env.int("EMAIL_PORT", 587)
  205. DEFAULT_FROM_EMAIL = env("DEFAULT_FROM_EMAIL", "webmaster@localhost")
  206. if not EMAIL_HOST:
  207. EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
  208. # User media files
  209. MEDIA_ROOT = env("MEDIA_ROOT", path.join(BASE_DIR, "media"))
  210. MEDIA_URL = "/media/"
  211. # Filepond settings
  212. DJANGO_DRF_FILEPOND_UPLOAD_TMP = path.join(BASE_DIR, "filepond-temp-uploads")
  213. DJANGO_DRF_FILEPOND_FILE_STORE_PATH = MEDIA_ROOT
  214. # Celery settings
  215. DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH = 191
  216. CELERY_RESULT_BACKEND = "django-db"
  217. try:
  218. CELERY_BROKER_URL = env("CELERY_BROKER_URL")
  219. except EnvError:
  220. try:
  221. # quickfix for Heroku.
  222. # See https://github.com/doccano/doccano/issues/1327.
  223. uri = env("DATABASE_URL")
  224. if uri.startswith("postgres://"):
  225. uri = uri.replace("postgres://", "postgresql://", 1)
  226. CELERY_BROKER_URL = "sqla+{}".format(uri)
  227. except EnvError:
  228. CELERY_BROKER_URL = "sqla+sqlite:///{}".format(DATABASES["default"]["NAME"])
  229. CELERY_ACCEPT_CONTENT = ["application/json"]
  230. CELERY_TASK_SERIALIZER = "json"
  231. CELERY_RESULT_SERIALIZER = "json"
  232. DEFAULT_AUTO_FIELD = "django.db.models.AutoField"