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.

277 lines
9.3 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. "django_cleanup",
  59. ]
  60. MIDDLEWARE = [
  61. "django.middleware.security.SecurityMiddleware",
  62. "whitenoise.middleware.WhiteNoiseMiddleware",
  63. "django.contrib.sessions.middleware.SessionMiddleware",
  64. "django.middleware.common.CommonMiddleware",
  65. "django.middleware.csrf.CsrfViewMiddleware",
  66. "django.contrib.auth.middleware.AuthenticationMiddleware",
  67. "django.contrib.messages.middleware.MessageMiddleware",
  68. "django.middleware.clickjacking.XFrameOptionsMiddleware",
  69. "corsheaders.middleware.CorsMiddleware",
  70. ]
  71. ROOT_URLCONF = "config.urls"
  72. WSGI_APPLICATION = "config.wsgi.application"
  73. # Django templates
  74. TEMPLATES = [
  75. {
  76. "BACKEND": "django.template.backends.django.DjangoTemplates",
  77. "DIRS": [path.join(BASE_DIR, "client/dist")],
  78. "APP_DIRS": True,
  79. "OPTIONS": {
  80. "context_processors": [
  81. "django.template.context_processors.debug",
  82. "django.template.context_processors.request",
  83. "django.contrib.auth.context_processors.auth",
  84. "django.contrib.messages.context_processors.messages",
  85. ],
  86. },
  87. },
  88. ]
  89. # Static files (CSS, JavaScript, Images)
  90. # https://docs.djangoproject.com/en/2.0/howto/static-files/
  91. STATIC_URL = "/static/"
  92. STATIC_ROOT = path.join(BASE_DIR, "staticfiles")
  93. STATICFILES_DIRS = [
  94. path.join(BASE_DIR, "client/dist/static"),
  95. ]
  96. # STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
  97. STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
  98. # Auth settings
  99. AUTHENTICATION_BACKENDS = [
  100. "django.contrib.auth.backends.ModelBackend",
  101. ]
  102. HEADER_AUTH_USER_NAME = env("HEADER_AUTH_USER_NAME", "")
  103. HEADER_AUTH_USER_GROUPS = env("HEADER_AUTH_USER_GROUPS", "")
  104. HEADER_AUTH_ADMIN_GROUP_NAME = env("HEADER_AUTH_ADMIN_GROUP_NAME", "")
  105. HEADER_AUTH_GROUPS_SEPERATOR = env("HEADER_AUTH_GROUPS_SEPERATOR", default=",")
  106. if HEADER_AUTH_USER_NAME and HEADER_AUTH_USER_GROUPS and HEADER_AUTH_ADMIN_GROUP_NAME:
  107. MIDDLEWARE.append("api.middleware.HeaderAuthMiddleware")
  108. AUTHENTICATION_BACKENDS.append("django.contrib.auth.backends.RemoteUserBackend")
  109. # Role settings
  110. ROLE_PROJECT_ADMIN = env("ROLE_PROJECT_ADMIN", "project_admin")
  111. ROLE_ANNOTATOR = env("ROLE_ANNOTATOR", "annotator")
  112. ROLE_ANNOTATION_APPROVER = env("ROLE_ANNOTATION_APPROVER", "annotation_approver")
  113. # Password validation
  114. # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
  115. AUTH_PASSWORD_VALIDATORS = [
  116. {
  117. "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
  118. },
  119. {
  120. "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
  121. },
  122. {
  123. "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
  124. },
  125. {
  126. "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
  127. },
  128. ]
  129. REST_FRAMEWORK = {
  130. # Use Django's standard `django.contrib.auth` permissions,
  131. # or allow read-only access for unauthenticated users.
  132. "DEFAULT_PERMISSION_CLASSES": [
  133. "rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly",
  134. "rest_framework.permissions.IsAuthenticated",
  135. ],
  136. "DEFAULT_AUTHENTICATION_CLASSES": (
  137. "rest_framework.authentication.SessionAuthentication",
  138. "rest_framework.authentication.TokenAuthentication",
  139. ),
  140. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
  141. "PAGE_SIZE": env.int("DOCCANO_PAGE_SIZE", default=5),
  142. "DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
  143. "SEARCH_PARAM": "q",
  144. "DEFAULT_RENDERER_CLASSES": (
  145. "rest_framework.renderers.JSONRenderer",
  146. "rest_framework.renderers.BrowsableAPIRenderer",
  147. "rest_framework_xml.renderers.XMLRenderer",
  148. ),
  149. }
  150. # Internationalization
  151. # https://docs.djangoproject.com/en/2.0/topics/i18n/
  152. LANGUAGE_CODE = "en-us"
  153. TIME_ZONE = "UTC"
  154. USE_I18N = True
  155. USE_L10N = True
  156. USE_TZ = True
  157. # Testing
  158. TEST_RUNNER = "xmlrunner.extra.djangotestrunner.XMLTestRunner"
  159. TEST_OUTPUT_DIR = path.join(BASE_DIR, "junitxml")
  160. LOGIN_URL = "/login/"
  161. LOGIN_REDIRECT_URL = "/projects/"
  162. LOGOUT_REDIRECT_URL = "/"
  163. # Database
  164. # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
  165. DATABASES = {
  166. "default": {
  167. "ENGINE": "django.db.backends.sqlite3",
  168. "NAME": path.join(BASE_DIR, "db.sqlite3"),
  169. }
  170. }
  171. # Change 'default' database configuration with $DATABASE_URL.
  172. DATABASES["default"].update(
  173. dj_database_url.config(
  174. env="DATABASE_URL",
  175. conn_max_age=env.int("DATABASE_CONN_MAX_AGE", 500),
  176. ssl_require="sslmode" not in furl(env("DATABASE_URL", "")).args,
  177. )
  178. )
  179. # work-around for dj-database-url: explicitly disable ssl for sqlite
  180. if DATABASES["default"].get("ENGINE") == "django.db.backends.sqlite3":
  181. DATABASES["default"].get("OPTIONS", {}).pop("sslmode", None)
  182. # work-around for dj-database-url: patch ssl for mysql
  183. if DATABASES["default"].get("ENGINE") == "django.db.backends.mysql":
  184. DATABASES["default"].get("OPTIONS", {}).pop("sslmode", None)
  185. if env("MYSQL_SSL_CA", None):
  186. DATABASES["default"].setdefault("OPTIONS", {}).setdefault("ssl", {}).setdefault("ca", env("MYSQL_SSL_CA", None))
  187. # default to a sensible modern driver for Azure SQL
  188. if DATABASES["default"].get("ENGINE") == "sql_server.pyodbc":
  189. DATABASES["default"].setdefault("OPTIONS", {}).setdefault("driver", "ODBC Driver 17 for SQL Server")
  190. # Sessions and CSRF
  191. # Honor the 'X-Forwarded-Proto' header for request.is_secure()
  192. SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
  193. SESSION_COOKIE_SECURE = env.bool("SESSION_COOKIE_SECURE", False)
  194. CSRF_COOKIE_SECURE = env.bool("CSRF_COOKIE_SECURE", False)
  195. CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS", [])
  196. # Allow all host headers
  197. ALLOWED_HOSTS = ["*"]
  198. if DEBUG:
  199. CORS_ORIGIN_WHITELIST = ("http://127.0.0.1:3000", "http://0.0.0.0:3000", "http://localhost:3000")
  200. CSRF_TRUSTED_ORIGINS = CORS_ORIGIN_WHITELIST
  201. # Batch size for importing data
  202. IMPORT_BATCH_SIZE = env.int("IMPORT_BATCH_SIZE", 1000)
  203. # Necessary for email verification of new accounts
  204. EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS", False)
  205. EMAIL_HOST = env("EMAIL_HOST", None)
  206. EMAIL_HOST_USER = env("EMAIL_HOST_USER", None)
  207. EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD", None)
  208. EMAIL_PORT = env.int("EMAIL_PORT", 587)
  209. DEFAULT_FROM_EMAIL = env("DEFAULT_FROM_EMAIL", "webmaster@localhost")
  210. if not EMAIL_HOST:
  211. EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
  212. # User media files
  213. MEDIA_ROOT = env("MEDIA_ROOT", path.join(BASE_DIR, "media"))
  214. MEDIA_URL = "/media/"
  215. # Filepond settings
  216. DJANGO_DRF_FILEPOND_UPLOAD_TMP = path.join(BASE_DIR, "filepond-temp-uploads")
  217. DJANGO_DRF_FILEPOND_FILE_STORE_PATH = MEDIA_ROOT
  218. # File upload setting
  219. MAX_UPLOAD_SIZE = env.int("MAX_UPLOAD_SIZE", pow(1024, 3)) # default: 1GB per a file
  220. ENABLE_FILE_TYPE_CHECK = env.bool("ENABLE_FILE_TYPE_CHECK", False)
  221. # Celery settings
  222. DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH = 191
  223. CELERY_RESULT_BACKEND = "django-db"
  224. try:
  225. CELERY_BROKER_URL = env("CELERY_BROKER_URL")
  226. except EnvError:
  227. try:
  228. # quickfix for Heroku.
  229. # See https://github.com/doccano/doccano/issues/1327.
  230. uri = env("DATABASE_URL")
  231. if uri.startswith("postgres://"):
  232. uri = uri.replace("postgres://", "postgresql://", 1)
  233. CELERY_BROKER_URL = "sqla+{}".format(uri)
  234. except EnvError:
  235. CELERY_BROKER_URL = "sqla+sqlite:///{}".format(DATABASES["default"]["NAME"])
  236. CELERY_ACCEPT_CONTENT = ["application/json"]
  237. CELERY_TASK_SERIALIZER = "json"
  238. CELERY_RESULT_SERIALIZER = "json"
  239. DEFAULT_AUTO_FIELD = "django.db.models.AutoField"