import logging import os import warnings from collections import OrderedDict from datetime import timedelta from mimetypes import add_type from urllib.parse import quote_plus import django.conf.locale import environ from celery.schedules import crontab from django.conf import global_settings from django.urls import reverse_lazy from django.utils.translation import get_language_info from django.utils.translation import gettext_lazy as t from pymongo import MongoClient from kpi.constants import AUTH_SERVER_TAG_CHOICES, PERM_DELETE_ASSET, PERM_MANAGE_ASSET from ..static_lists import EXTRA_LANG_INFO, SECTOR_CHOICE_DEFAULTS from .utils import constance_env, dj_stripe_request_callback_method, get_git_rev env = environ.Env() # Build paths inside the project like this: os.path.join(BASE_DIR, ...) settings_dirname = os.path.dirname(os.path.abspath(__file__)) parent_dirname = os.path.dirname(settings_dirname) BASE_DIR = os.path.abspath(os.path.dirname(parent_dirname)) # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = env.str('DJANGO_SECRET_KEY', '@25)**hc^rjaiagb4#&q*84hr*uscsxwr-cv#0joiwj$))obyk') # Optionally treat proxied connections as secure. # See: https://docs.djangoproject.com/en/1.8/ref/settings/#secure-proxy-ssl-header. # Example environment: `export SECURE_PROXY_SSL_HEADER='HTTP_X_FORWARDED_PROTO, https'`. # SECURITY WARNING: If enabled, outer web server must filter out the `X-Forwarded-Proto` header. SECURE_PROXY_SSL_HEADER = env.tuple('SECURE_PROXY_SSL_HEADER', str, None) public_request_scheme = env.str('PUBLIC_REQUEST_SCHEME', 'https').lower() if public_request_scheme == 'https' or SECURE_PROXY_SSL_HEADER: SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True # These HSTS settings are sometimes overriden via nginx like in the `kobo-helm-chart` # repository or by the AWS ALB/Azure app gateway. If you see the header returned # with other values, check these places first SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool('SECURE_HSTS_INCLUDE_SUBDOMAINS', False) SECURE_HSTS_PRELOAD = env.bool('SECURE_HSTS_PRELOAD', False) SECURE_HSTS_SECONDS = env.int('SECURE_HSTS_SECONDS', 0) # Make Django use NginX $host. Useful when running with ./manage.py runserver_plus # It avoids adding the debugger webserver port (i.e. `:8000`) at the end of urls. USE_X_FORWARDED_HOST = env.bool('USE_X_FORWARDED_HOST', False) # Domain must not exclude KoBoCAT when sharing sessions SESSION_COOKIE_DOMAIN = env.str('SESSION_COOKIE_DOMAIN', None) if SESSION_COOKIE_DOMAIN: SESSION_COOKIE_NAME = env.str('SESSION_COOKIE_NAME', 'kobonaut') # The trusted CSRF origins must encompass Enketo's subdomain. See # https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-CSRF_TRUSTED_ORIGINS trusted_domains = [ f'{public_request_scheme}://*{SESSION_COOKIE_DOMAIN}', ] else: trusted_domains = [] CSRF_TRUSTED_ORIGINS = env.list( # Separate multiple origins with commas, and do not use spaces 'DJANGO_CSRF_TRUSTED_ORIGINS', default=trusted_domains, ) ENKETO_CSRF_COOKIE_NAME = env.str('ENKETO_CSRF_COOKIE_NAME', '__csrf') # Limit sessions to 1 week (the default is 2 weeks) SESSION_COOKIE_AGE = env.int('DJANGO_SESSION_COOKIE_AGE', 604800) # Set language cookie age to same value as session cookie LANGUAGE_COOKIE_AGE = SESSION_COOKIE_AGE # SECURITY WARNING: don't run with debug turned on in production! DEBUG = env.bool('DJANGO_DEBUG', False) ALLOWED_HOSTS = env.str('DJANGO_ALLOWED_HOSTS', '*').split(' ') LOGIN_REDIRECT_URL = 'kpi-root' LOGOUT_REDIRECT_URL = 'kobo_login' # Use URL pattern instead of hard-coded value # Application definition # The order of INSTALLED_APPS is important for template resolution. When two # apps both define templates for the same view, the first app listed receives # precedence INSTALLED_APPS = ( # Always put `contenttypes` before `auth`; see # https://code.djangoproject.com/ticket/10827 'django.contrib.contenttypes', 'django.contrib.admin', 'kobo.apps.kobo_auth.KoboAuthAppConfig', 'django.contrib.auth', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_prometheus', 'private_storage', 'kobo.apps.KpiConfig', 'kobo.apps.accounts', 'kobo.apps.accounts.mfa.apps.MfaAppConfig', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.microsoft', 'allauth.socialaccount.providers.openid_connect', 'allauth.usersessions', 'allauth.mfa', 'allauth.headless', 'hub.HubAppConfig', 'import_export', 'import_export_celery', 'loginas', 'webpack_loader', 'django_extensions', 'django_filters', 'taggit', 'rest_framework', 'rest_framework.authtoken', 'oauth2_provider', 'django_digest', 'kobo.apps.organizations', 'kobo.apps.kobo_scim.apps.KoboScimConfig', 'kobo.apps.superuser_stats.SuperuserStatsAppConfig', 'kobo.apps.service_health', 'kobo.apps.subsequences', 'constance', 'kobo.apps.hook.apps.HookAppConfig', 'django_celery_beat', 'corsheaders', 'kobo.apps.external_integrations.ExternalIntegrationsAppConfig', 'markdownx', 'kobo.apps.help', 'kobo.apps.project_views.apps.ProjectViewAppConfig', 'kobo.apps.languages.apps.LanguageAppConfig', 'kobo.apps.audit_log.AuditLogAppConfig', 'kobo.apps.data_collectors.DataCollectorsConfig', 'kobo.apps.mass_emails.MassEmailsConfig', 'kobo.apps.trackers.TrackersConfig', 'kobo.apps.trash_bin.TrashBinAppConfig', 'kobo.apps.markdownx_uploader.MarkdownxUploaderAppConfig', 'kobo.apps.form_disclaimer.FormDisclaimerAppConfig', 'kobo.apps.openrosa.apps.logger.app.LoggerAppConfig', 'kobo.apps.openrosa.apps.viewer.app.ViewerConfig', 'kobo.apps.openrosa.apps.main.app.MainConfig', 'kobo.apps.openrosa.apps.apps.OpenRosaAppConfig', 'kobo.apps.openrosa.libs', 'kobo.apps.project_ownership.app.ProjectOwnershipAppConfig', 'kobo.apps.long_running_migrations.app.LongRunningMigrationAppConfig', 'kobo.apps.user_reports.apps.UserReportsConfig', 'kobo.apps.support_tools.apps.SupportToolsConfig', 'drf_spectacular', 'csp', ) MIDDLEWARE = [ 'kobo.apps.service_health.middleware.HealthCheckMiddleware', 'kobo.apps.openrosa.koboform.redirect_middleware.ConditionalRedirects', 'django_dont_vary_on.middleware.RemoveUnneededVaryHeadersMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', 'hub.middleware.NeverCacheJSONMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'hub.middleware.LocaleMiddleware', 'allauth.account.middleware.AccountMiddleware', 'allauth.usersessions.middleware.UserSessionsMiddleware', 'django.middleware.common.CommonMiddleware', 'kobo.apps.audit_log.middleware.create_project_history_log_middleware', # Still needed really? 'kobo.apps.openrosa.libs.utils.middleware.LocaleMiddlewareWithTweaks', 'kobo.apps.openrosa.libs.utils.middleware.OpenRosaTrailingSlashMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'kobo.apps.openrosa.libs.utils.middleware.RestrictedAccessMiddleware', 'kobo.apps.openapi_validator.middleware.OpenAPIValidationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'kobo.apps.openrosa.libs.utils.middleware.HTTPResponseNotAllowedMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'hub.middleware.UsernameInResponseHeaderMiddleware', 'django_userforeignkey.middleware.UserForeignKeyMiddleware', 'django_request_cache.middleware.RequestCacheMiddleware', 'author.middlewares.AuthorDefaultBackendMiddleware', ] if os.environ.get('DEFAULT_FROM_EMAIL'): DEFAULT_FROM_EMAIL = env.str('DEFAULT_FROM_EMAIL') SERVER_EMAIL = DEFAULT_FROM_EMAIL # Configuration options that superusers can modify in the Django admin # interface. Please note that it's not as simple as moving a setting into the # `CONSTANCE_CONFIG` dictionary: each place where the setting's value is needed # must use `constance.config.THE_SETTING` instead of # `django.conf.settings.THE_SETTING` CONSTANCE_CONFIG = { # Temporary?: give Enketo `KOBOCAT_INTERNAL_URL` instead of the public URL, so its # server-side calls (form, manifest, media, submissions) stay inside the cluster 'ENKETO_USE_INTERNAL_OPENROSA_URL': ( env.bool('CONSTANCE_ENKETO_USE_INTERNAL_OPENROSA_URL', False), 'Save the internal domain name instead of the public one as the ' 'OpenRosa server URL in Enketo', ), 'REGISTRATION_OPEN': ( True, 'Allow new users to register accounts for themselves', ), 'REGISTRATION_ALLOWED_EMAIL_DOMAINS': ( '', 'Email domains allowed to register new accounts, one per line, ' 'or blank to allow all email domains' ), 'REGISTRATION_DOMAIN_NOT_ALLOWED_ERROR_MESSAGE': ( 'This email domain is not allowed to create an account', 'Error message for emails not listed in REGISTRATION_ALLOWED_EMAIL_DOMAINS ' 'if field is not blank' ), 'REGISTRATION_BLACKLIST_EMAIL_DOMAINS': ( '', 'Email domains to block from registering new accounts, one per line, ' 'or blank to allow all email domains' ), 'REGISTRATION_BLACKLIST_ERROR_MESSAGE': ( 'Account creation restricted for this server. Your organization uses a ' 'separate private KoboToolbox server. Please contact your organization ' 'support team for assistance.', 'Error message for emails blacklisted in REGISTRATION_BLACKLIST_EMAIL_DOMAINS ' 'if field is not blank' ), 'REGISTRATION_SSO_MANAGED_EMAIL_DOMAINS': ( '', 'List of email domains configured across all managed SocialApps. ' 'Note: these domains are managed per-app through the email_domains field in ' 'Account Extras > Social app custom datas.', 'disabled_textarea', ), 'SHOW_KOBOTOOLBOX_LOGO': ( True, 'Show the KoboToolbox logo on the sign-in and account creation pages. ' 'Turn this off to display only the logo uploaded under ' 'Configuration files', ), 'AUTH_SERVER_TAG': ( '', 'Server name tag shown at the top of the sign-in and account creation ' 'cards. Leave as None on private servers to show the logo uploaded ' 'under Configuration files instead', 'server_tag_choice', ), 'TERMS_OF_SERVICE_URL': ('', 'URL for terms of service document'), 'LAST_TOS_UPDATE': ( '', 'Date of the most recent update to the terms of service. ' "DO NOT EDIT HERE. Instead, use the 'Require " "all users to reaccept the Terms of Service' action in " 'Sitewide messages', 'disabled', ), 'PRIVACY_POLICY_URL': ('', 'URL for privacy policy'), 'SOURCE_CODE_URL': ( 'https://github.com/kobotoolbox/', 'URL of source code repository. When empty, a link will not be shown ' 'in the user interface', ), 'SUPPORT_EMAIL': ( constance_env( env.str, 'CONSTANCE_SUPPORT_EMAIL', 'KOBO_SUPPORT_EMAIL', env.str('DEFAULT_FROM_EMAIL', 'help@kobotoolbox.org'), ), 'Email address for users to contact, e.g. when they encounter ' 'unhandled errors in the application', ), 'SUPPORT_URL': ( constance_env( env.str, 'CONSTANCE_SUPPORT_URL', 'KOBO_SUPPORT_URL', 'https://support.kobotoolbox.org/', ), 'URL for "KoboToolbox Help Center"', ), 'ACADEMY_URL': ( constance_env( env.str, 'CONSTANCE_ACADEMY_URL', 'KOBO_ACADEMY_URL', 'https://academy.kobotoolbox.org/', ), 'URL for "KoboToolbox Community Forum"', ), 'COMMUNITY_URL': ( constance_env( env.str, 'CONSTANCE_COMMUNITY_URL', 'KOBO_COMMUNITY_URL', 'https://community.kobotoolbox.org/', ), 'URL for "KoboToolbox Community Forum"', ), 'SYNCHRONOUS_EXPORT_CACHE_MAX_AGE': ( 300, ( 'A synchronous export request will return the last export generated ' 'with the same settings unless it is older than this value (seconds)' ), 'positive_int', ), 'ALLOW_UNSECURED_HOOK_ENDPOINTS': ( True, 'Allow the use of unsecured endpoints for hooks. ' '(e.g http://hook.example.com)', ), 'HOOK_MAX_RETRIES': ( 3, 'Number of times the system will retry to send data to remote server ' 'before giving up', ), 'SSRF_ALLOWED_IP_ADDRESS': ( '', 'Whitelisted IP addresses to bypass SSRF protection\nOne per line', ), 'SSRF_DENIED_IP_ADDRESS': ( '', 'Blacklisted IP addresses to bypass SSRF protection\nOne per line', ), 'EXPOSE_GIT_REV': ( False, 'Display information about the running commit to non-superusers', ), 'FRONTEND_MIN_RETRY_TIME': ( 2, 'Minimum number of seconds the front end waits before retrying a ' 'failed request to the back end', int, ), 'FRONTEND_MAX_RETRY_TIME': ( 120, 'Maximum number of seconds the front end waits before retrying a ' 'failed request to the back end', int, ), 'MFA_ISSUER_NAME': ( 'KoboToolbox', 'Issuer name displayed in multi-factor applications' ), 'MFA_ENABLED': ( True, 'Enable two-factor authentication' ), 'MFA_LOCALIZED_HELP_TEXT': ( { 'default': t( 'If you cannot access your authenticator app, please enter one ' 'of your backup codes instead. If you cannot access those ' 'either, then you will need to request assistance by ' 'contacting [##support email##](mailto:##support email##).' ), 'some-other-language': ( 'This will never appear because `some-other-language` is not ' 'a valid language code, but this entry is here to show you ' 'an example of adding another message in a different language.' ) }, ( 'Guidance message presented when users click the ' '"Problems with the token" link.\n\n' '`##support email##` is a placeholder for the `SUPPORT_EMAIL` ' 'setting.\n' 'Markdown syntax is supported.\n' 'The “default” message will be used if no translations are provided.' ' The “default” should be in English.\n' 'To add messages in other languages, follow the example of ' '“some-other-language“, but replace “some-other-language“ with a ' 'valid language code (e.g. “fr“ for French).' ), # Use custom field for schema validation 'i18n_text_jsonfield_schema' ), 'SUPERUSER_AUTH_ENFORCEMENT': ( False, 'Require MFA for superusers with a usable password', ), 'EMAIL_CONFIRMATION_REQUESTS_PER_HOUR': ( 5, 'Number of times per hour a new account confirmation email may be ' 'requested for any one email address, through ' '/api/v2/email-confirmations/. Limits how much mail an inbox can be made ' 'to receive by someone else. Set to 0 to disable the limit.', ), 'USAGE_LIMIT_ENFORCEMENT': ( constance_env( env.bool, 'CONSTANCE_USAGE_LIMIT_ENFORCEMENT', 'USAGE_LIMIT_ENFORCEMENT', False, ), 'For Stripe-enabled instances, determines whether usage limits will be enforced' 'by blocking submissions/NLP actions or deleting stored files.', ), 'ASR_MT_INVITEE_USERNAMES': ( '', 'List of invited usernames, one per line, who will have access to NLP ' 'ASR/MT processing via external (costly) APIs.\nEnter * to invite ' 'all users.' ), 'ASR_MT_GOOGLE_REQUEST_TIMEOUT': ( 10, ( 'Timeout in seconds for google NLP data processing requests using' ' the operations API. ' ) ), 'ASR_MT_GOOGLE_PROJECT_ID': ( env.str('CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID', 'kobo-asr-mt'), 'ID of the Google Cloud project used to access ASR/MT APIs', ), 'ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX': ( env.str('CONSTANCE_ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX', 'kobo-asr-mt-tmp'), ( 'Prefix for temporary ASR/MT files stored on Google Cloud. Useful' ' for lifecycle rules: files under this prefix can be deleted after' ' one day.\nThe bucket name itself is set by the environment' ' variable `GS_BUCKET_NAME`.' ), ), 'ASR_MT_GOOGLE_REGION': ( env.str('CONSTANCE_ASR_MT_GOOGLE_REGION', 'global'), ( 'Google Cloud region for ASR/MT data residency. ' 'global (default): maximum language support, per-language routing ' 'to the best available Google endpoint for each model. ' 'eu: restrict all processing to EU-hosted Google endpoints; ' 'languages only available outside the EU become unsupported.' ), 'google_region_choice', ), # TODO: remove this default override once Google fixes Speech-to-Text # support for 'sw' and 'sw-KE' language codes 'ASR_LANGUAGE_CODE_OVERRIDES': ( env.str('CONSTANCE_ASR_LANGUAGE_CODE_OVERRIDES', 'sw:auto,sw-KE:auto'), ( 'Comma-separated mappings that override the language code sent to ' 'Google Speech-to-Text. Works around known Google-side pipeline ' 'failures for specific languages. Useful for temporary workarounds ' 'when Google Speech-to-Text does not correctly support a specific ' 'language code. Format: source_language:google_language. ' 'Example: sw:auto,sw-KE:auto' ), str, ), 'ASR_MT_GOOGLE_CREDENTIALS': ( '', 'The JSON content of a private key file generated by the Google Cloud ' 'IAM & Admin console.\nLeave blank to use a different Google ' 'authentication mechanism.' ), 'AUTOMATIC_QA_REQUESTS_PER_SECOND': ( 5, 'Number of allowed automatic Qualitative Analysis requests per user per second.' ), 'USER_METADATA_FIELDS': ( [ {'name': 'name', 'required': True}, {'name': 'organization', 'required': False}, {'name': 'organization_type', 'required': False}, {'name': 'organization_website', 'required': False}, {'name': 'sector', 'required': False}, {'name': 'bio', 'required': False}, {'name': 'city', 'required': False}, {'name': 'country', 'required': False}, {'name': 'twitter', 'required': False}, {'name': 'linkedin', 'required': False}, {'name': 'instagram', 'required': False}, {'name': 'newsletter_subscription', 'required': False}, ], # The available fields are hard-coded in the front end 'Display (and optionally require) these metadata fields for users.\n' "Possible fields are:\n" "'organization', 'organization_type', 'organization_website', 'sector', 'gender', 'bio', " "'city', 'country', 'twitter', 'linkedin', 'instagram', and 'newsletter_subscription'.\n\n" 'To add another language, follow the example below.\n\n' '{"name": "name", "required": False, "label": ' '{"default": "Full Name", "fr": "Nom Complet"}}\n' "'default' is a required field within the 'label' dict, but 'label' is optional.", # Use custom field for schema validation 'long_metadata_fields_jsonschema' ), 'PROJECT_METADATA_FIELDS': ( [ {'name': 'sector', 'required': False}, {'name': 'country', 'required': False}, {'name': 'description', 'required': False}, ], # The available fields are hard-coded in the front end 'Display (and optionally require) these metadata fields for projects.\n' "Possible fields are:\n" "'sector', 'country', 'operational_purpose', 'collects_pii', " "and 'description'\n\n" 'To add another language, follow the example below.\n\n' '{"name": "sector", "required": False, "label": ' '{"default": "Sector", "fr": "Secteur"}}\n' "'default' is a required field within the 'label' dict, but 'label' is optional.", # Use custom field for schema validation 'metadata_fields_jsonschema' ), 'SECTOR_CHOICES': ( '\n'.join(s[0] for s in SECTOR_CHOICE_DEFAULTS), "Options available for the 'sector' metadata field, one per line.", 'long_textfield' ), 'OPERATIONAL_PURPOSE_CHOICES': ( '', "Options available for the 'operational purpose of data' metadata " 'field, one per line.' ), 'ORGANIZATION_INVITE_EXPIRY': ( 14, 'Number of days before organization invites expire.', 'positive_int', ), 'ASSET_SNAPSHOT_DAYS_RETENTION': ( 30, 'Number of days to keep asset snapshots', 'positive_int' ), 'IMPORT_TASK_DAYS_RETENTION': ( 90, 'Number of days to keep import tasks', 'positive_int', ), 'SUBMISSION_HISTORY_RETENTION': ( 180, 'Number of days to keep submission history', 'positive_int', ), 'PROJECT_TRASH_RETENTION': ( 7, 'Number of days to keep projects in trash after users (soft-)deleted ' 'them and before automatically hard-deleting them by the system', 'positive_int', ), 'ACCOUNT_TRASH_RETENTION': ( 30 * 6, 'Number of days to keep deactivated accounts in trash before ' 'automatically hard-deleting all their projects and data.\n' 'Use -1 to require a superuser to empty the trash manually instead of ' 'having the system empty it automatically.', 'positive_int_minus_one', ), 'ATTACHMENT_TRASH_RETENTION': ( 7, 'Number of days to keep attachments in trash after users (soft-)deleted ' 'them and before automatically hard-deleting them by the system', 'positive_int', ), 'AUTO_DELETE_ATTACHMENTS': ( False, 'Enable automatic deletion of attachments for users who have exceeded ' 'their storage limits.' ), 'EXPORT_RETENTION': ( 30, ( 'Number of minutes after which export tasks are cleaned up.\n' 'Cannot be less than `SYNCHRONOUS_EXPORT_CACHE_MAX_AGE`.' ), 'positive_int', ), 'OVER_LIMIT_ATTACHMENT_RETENTION': ( 90, 'Number of days to keep attachments after the user has exceeded their ' 'storage limits.' ), # Toggle for ZXCVBN 'ENABLE_PASSWORD_ENTROPY_METER': ( True, 'Display an entropy meter and password quality suggestions whenever users change their passwords.', ), 'ENABLE_PASSWORD_MINIMUM_LENGTH_VALIDATION': ( False, 'Enable minimum length validation', ), 'MINIMUM_PASSWORD_LENGTH': ( 10, 'Minimum length for all passwords.', int, ), 'ENABLE_PASSWORD_USER_ATTRIBUTE_SIMILARITY_VALIDATION': ( False, 'Enable user attribute similarity validation. ' 'See `PASSWORD_USER_ATTRIBUTES` below for customization.', ), 'PASSWORD_USER_ATTRIBUTES': ( ( 'username\n' 'full_name\n' 'email' ), 'List (one per line) all user attributes for similarity validation.\n' "Possible attributes are 'username', 'full_name', 'email', 'organization'." ), 'ENABLE_COMMON_PASSWORD_VALIDATION': ( False, 'Enable common password validation.\n' 'To customize the list, go to Configuration file section and add common password file.\n' 'Django default list is based on https://tinyurl.com/django3-2-common-passwords.', ), 'ENABLE_PASSWORD_CUSTOM_CHARACTER_RULES_VALIDATION': ( False, 'Enable custom character rules', ), 'PASSWORD_CUSTOM_CHARACTER_RULES': ( ( '[[:lower:]]\n' '[[:upper:]]\n' '\\d\n' '[\\W_]' ), 'List all custom character rules as regular expressions supported ' 'by `regex` python library.\n' 'One per line.', ), 'PASSWORD_CUSTOM_CHARACTER_RULES_REQUIRED_TO_PASS': ( 3, 'The minimum number of character rules to pass.', int, ), 'ENABLE_MOST_RECENT_PASSWORD_VALIDATION': ( False, 'Enable most recent password validation which will prevent the user from ' 'reusing the most recent password.', ), 'ENABLE_CUSTOM_PASSWORD_GUIDANCE_TEXT': ( False, 'Enable custom password guidance text to help users create their passwords.', ), 'CUSTOM_PASSWORD_GUIDANCE_TEXT': ( { 'default': t( 'The password must be at least 10 characters long and' ' contain 3 or more of the following: uppercase letters,' ' lowercase letters, numbers, and special characters. It' ' cannot be similar to your name, username, or email' ' address.' ), 'some-other-language': ( 'This will never appear because `some-other-language` is' ' not a valid language code, but this entry is here to show' ' you an example of adding another message in a different' ' language.' ), }, ( 'Guidance message presented when users create or modify a password. ' 'It should reflect the defined password rules.\n\n' 'Markdown syntax is supported.\n' 'The “default” message will be used if no translations are provided.' ' The “default” should be in English.\n' 'To add messages in other languages, follow the example of ' '“some-other-language“, but replace “some-other-language“ with a ' 'valid language code (e.g. “fr“ for French).' ), 'i18n_text_jsonfield_schema', ), 'MASS_EMAIL_ENQUEUED_RECORD_EXPIRY': ( 7, 'Number of days before enqueued mass email records are marked as failed.', 'positive_int', ), 'MASS_EMAIL_STALE_RECORD_RECHECK_HOURS': ( 12, 'Number of hours an enqueued mass email record can sit before it is ' 're-checked against its MassEmailConfig criteria at send time. ' 'Records still matching are sent normally; records that no longer ' 'match are marked stale.', 'positive_int', ), 'PROJECT_OWNERSHIP_RESUME_THRESHOLD': ( 10, 'Number of minutes asynchronous tasks can be idle before being ' 'restarted.\n' 'It is recommended to keep greater than 10 minutes.', 'positive_int', ), 'PROJECT_OWNERSHIP_STUCK_THRESHOLD': ( 12 * 60, ( 'Number of minutes asynchronous tasks can run before being ' 'flagged as failed.\n' 'Should be greater than `PROJECT_OWNERSHIP_RESUME_THRESHOLD`.' ), 'positive_int', ), 'PROJECT_OWNERSHIP_INVITE_EXPIRY': ( 14, 'Number of days before invites expire.', 'positive_int', ), 'PROJECT_OWNERSHIP_INVITE_HISTORY_RETENTION': ( 30, ( 'Number of days to keep invites history.\n' 'Failed invites are kept forever.' ), 'positive_int', ), 'PROJECT_OWNERSHIP_IN_APP_MESSAGES_EXPIRY': ( 7, 'The number of days after which in-app messages expire.', 'positive_int', ), 'PROJECT_OWNERSHIP_AUTO_ACCEPT_INVITES': ( False, 'Auto-accept invites by default and do not sent them by e-mail.' ), 'PROJECT_OWNERSHIP_ADMIN_EMAIL': ( '', ( 'Email addresses to which error reports are sent, one per line.\n' 'Leave empty to not send emails.' ), ), 'PROJECT_OWNERSHIP_ADMIN_EMAIL_SUBJECT': ( 'KoboToolbox Notifications: Project ownership transfer failure', 'Email subject to sent to admins on failure.', ), 'PROJECT_OWNERSHIP_ADMIN_EMAIL_BODY': ( ( 'Dear admins,\n\n' 'A transfer of project ownership has failed:\n' '##invite_url##' ), 'Email message to sent to admins on failure.', ), 'USE_TEAM_LABEL': ( True, 'Use the term "Team" instead of "Organization" when Stripe is not enabled', ), 'MASS_EMAIL_TEST_EMAILS': ( '', 'List (one per line) users who will be sent test emails when using the \n' '"test_users" query for MassEmailConfigs', ), 'ALLOW_SELF_ACCOUNT_DELETION': ( False, 'Allow users to delete their own account.', ), 'USER_REPORTS_PAGE_SIZE_LIMIT': ( 1000, 'Max page size for the user report endpoint', 'natural_int', ) } CONSTANCE_ADDITIONAL_FIELDS = { 'i18n_text_jsonfield_schema': [ 'kpi.fields.jsonschema_form_field.I18nTextJSONField', {'widget': 'django.forms.Textarea'}, ], 'long_metadata_fields_jsonschema': [ 'kpi.fields.jsonschema_form_field.UserMetadataFieldsListField', {'widget': 'django.forms.Textarea', 'widget_kwargs': {'attrs': {'rows': 45}}}, ], 'long_textfield': [ 'django.forms.fields.CharField', {'widget': 'django.forms.Textarea', 'widget_kwargs': {'attrs': {'rows': 30}}}, ], 'metadata_fields_jsonschema': [ 'kpi.fields.jsonschema_form_field.MetadataFieldsListField', {'widget': 'django.forms.Textarea'}, ], 'positive_int': ['django.forms.fields.IntegerField', {'min_value': 0}], 'positive_int_minus_one': ['django.forms.fields.IntegerField', {'min_value': -1}], 'natural_int': ['django.forms.fields.IntegerField', {'min_value': 1}], 'google_region_choice': [ 'django.forms.fields.ChoiceField', { 'choices': ( ('global', 'Global'), ('eu', 'Europe'), ), }, ], 'server_tag_choice': [ 'django.forms.fields.ChoiceField', { 'choices': [('', 'None')] + AUTH_SERVER_TAG_CHOICES, 'required': False, }, ], 'disabled': [ 'django.forms.fields.CharField', {'disabled': True, 'required': False}, ], 'disabled_textarea': [ 'django.forms.fields.CharField', {'widget': 'django.forms.Textarea', 'disabled': True, 'required': False}, ], } CONSTANCE_CONFIG_FIELDSETS = OrderedDict( { 'Registration': ( 'REGISTRATION_OPEN', 'REGISTRATION_ALLOWED_EMAIL_DOMAINS', 'REGISTRATION_DOMAIN_NOT_ALLOWED_ERROR_MESSAGE', 'REGISTRATION_BLACKLIST_EMAIL_DOMAINS', 'REGISTRATION_BLACKLIST_ERROR_MESSAGE', 'REGISTRATION_SSO_MANAGED_EMAIL_DOMAINS', 'EMAIL_CONFIRMATION_REQUESTS_PER_HOUR', ), 'Security': ( 'SSRF_ALLOWED_IP_ADDRESS', 'SSRF_DENIED_IP_ADDRESS', 'MFA_ISSUER_NAME', 'MFA_ENABLED', 'MFA_LOCALIZED_HELP_TEXT', 'SUPERUSER_AUTH_ENFORCEMENT', ), 'Password Validation': ( 'ENABLE_PASSWORD_ENTROPY_METER', 'ENABLE_PASSWORD_MINIMUM_LENGTH_VALIDATION', 'ENABLE_PASSWORD_USER_ATTRIBUTE_SIMILARITY_VALIDATION', 'ENABLE_COMMON_PASSWORD_VALIDATION', 'ENABLE_PASSWORD_CUSTOM_CHARACTER_RULES_VALIDATION', 'ENABLE_MOST_RECENT_PASSWORD_VALIDATION', 'ENABLE_CUSTOM_PASSWORD_GUIDANCE_TEXT', 'MINIMUM_PASSWORD_LENGTH', 'PASSWORD_USER_ATTRIBUTES', 'PASSWORD_CUSTOM_CHARACTER_RULES', 'PASSWORD_CUSTOM_CHARACTER_RULES_REQUIRED_TO_PASS', 'CUSTOM_PASSWORD_GUIDANCE_TEXT', ), 'Transfer project ownership': ( 'PROJECT_OWNERSHIP_RESUME_THRESHOLD', 'PROJECT_OWNERSHIP_STUCK_THRESHOLD', 'PROJECT_OWNERSHIP_INVITE_HISTORY_RETENTION', 'PROJECT_OWNERSHIP_INVITE_EXPIRY', 'PROJECT_OWNERSHIP_IN_APP_MESSAGES_EXPIRY', 'PROJECT_OWNERSHIP_ADMIN_EMAIL', 'PROJECT_OWNERSHIP_ADMIN_EMAIL_SUBJECT', 'PROJECT_OWNERSHIP_ADMIN_EMAIL_BODY', 'PROJECT_OWNERSHIP_AUTO_ACCEPT_INVITES', ), 'Natural language processing': ( 'ASR_MT_INVITEE_USERNAMES', 'ASR_MT_GOOGLE_PROJECT_ID', 'ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX', 'ASR_MT_GOOGLE_REGION', 'ASR_LANGUAGE_CODE_OVERRIDES', 'ASR_MT_GOOGLE_CREDENTIALS', 'ASR_MT_GOOGLE_REQUEST_TIMEOUT', 'AUTOMATIC_QA_REQUESTS_PER_SECOND', ), 'Mass emails': ( 'MASS_EMAIL_ENQUEUED_RECORD_EXPIRY', 'MASS_EMAIL_STALE_RECORD_RECHECK_HOURS', 'MASS_EMAIL_TEST_EMAILS', ), 'Metadata options': ( 'USER_METADATA_FIELDS', 'PROJECT_METADATA_FIELDS', 'SECTOR_CHOICES', 'OPERATIONAL_PURPOSE_CHOICES', ), 'Usage limits': ( 'USAGE_LIMIT_ENFORCEMENT', 'OVER_LIMIT_ATTACHMENT_RETENTION', 'AUTO_DELETE_ATTACHMENTS', ), 'Trash bin': ( 'ACCOUNT_TRASH_RETENTION', 'ATTACHMENT_TRASH_RETENTION', 'PROJECT_TRASH_RETENTION', 'ALLOW_SELF_ACCOUNT_DELETION', ), 'Regular maintenance settings': ( 'ASSET_SNAPSHOT_DAYS_RETENTION', 'IMPORT_TASK_DAYS_RETENTION', 'SUBMISSION_HISTORY_RETENTION', 'SYNCHRONOUS_EXPORT_CACHE_MAX_AGE', 'EXPORT_RETENTION', ), 'Rest Services': ( 'ALLOW_UNSECURED_HOOK_ENDPOINTS', 'HOOK_MAX_RETRIES', ), 'Organizations': ( 'USE_TEAM_LABEL', 'ORGANIZATION_INVITE_EXPIRY', ), 'Legal and support links': ( 'SHOW_KOBOTOOLBOX_LOGO', 'AUTH_SERVER_TAG', 'TERMS_OF_SERVICE_URL', 'LAST_TOS_UPDATE', 'PRIVACY_POLICY_URL', 'SOURCE_CODE_URL', 'SUPPORT_EMAIL', 'SUPPORT_URL', 'ACADEMY_URL', 'COMMUNITY_URL', ), 'General Options': ( 'EXPOSE_GIT_REV', 'FRONTEND_MIN_RETRY_TIME', 'FRONTEND_MAX_RETRY_TIME', 'USER_REPORTS_PAGE_SIZE_LIMIT', 'ENKETO_USE_INTERNAL_OPENROSA_URL', ), } ) # Tell django-constance to use a database model instead of Redis CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend' CONSTANCE_DATABASE_CACHE_BACKEND = 'constance' # Warn developers to use `pytest` instead of `./manage.py test` class DoNotUseRunner: def __init__(self, *args, **kwargs): raise NotImplementedError('Please run tests with `pytest` instead') TEST_RUNNER = __name__ + '.DoNotUseRunner' # ModelBackend does not interfere with object-level permissions: it always denies # object-specific requests (see # https://github.com/django/django/blob/1.7/django/contrib/auth/backends.py#L44 ). AUTHENTICATION_BACKENDS = ( 'kpi.backends.ModelBackend', 'kpi.backends.ObjectPermissionBackend', 'allauth.account.auth_backends.AuthenticationBackend', ) ROOT_URLCONF = 'kobo.urls' WSGI_APPLICATION = 'kobo.wsgi.application' # What User object should be mapped to AnonymousUser? ANONYMOUS_USER_ID = -1 # Permissions assigned to AnonymousUser are restricted to the following ALLOWED_ANONYMOUS_PERMISSIONS = ( 'kpi.view_asset', 'kpi.discover_asset', 'kpi.add_submissions', 'kpi.view_submissions', ) # run heavy migration scripts by default # NOTE: this should be set to False for major deployments. This can take a long time SKIP_HEAVY_MIGRATIONS = env.bool('SKIP_HEAVY_MIGRATIONS', False) # Database # https://docs.djangoproject.com/en/1.7/ref/settings/#databases DATABASES = { 'default': env.db_url( 'KPI_DATABASE_URL' if 'KPI_DATABASE_URL' in os.environ else 'DATABASE_URL', default='sqlite:///%s/db.sqlite3' % BASE_DIR ), } OPENROSA_DB_ALIAS = 'kobocat' if 'KC_DATABASE_URL' in os.environ: DATABASES[OPENROSA_DB_ALIAS] = env.db_url('KC_DATABASE_URL') DATABASE_ROUTERS = ['kpi.db_routers.DefaultDatabaseRouter'] # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ django.conf.locale.LANG_INFO.update(EXTRA_LANG_INFO) DJANGO_LANGUAGE_CODES = env.str( 'DJANGO_LANGUAGE_CODES', default=( 'am ' # Amharic 'ar ' # Arabic 'bn ' # Bengali 'cs ' # Czech 'de ' # German 'en ' # English 'es ' # Spanish 'fa ' # Persian/Farsi 'fr ' # French 'hi ' # Hindi 'hu ' # Hungarian 'id ' # Indonesian 'ja ' # Japanese 'km ' # Khmer 'ku ' # Kurdish 'ln ' # Lingala 'my ' # Burmese/Myanmar 'ny ' # Chewa/Chichewa/Nyanja 'ne ' # Nepali 'pl ' # Polish 'pt ' # Portuguese 'ru ' # Russian 'sw ' # Swahili 'th ' # Thai 'tr ' # Turkish 'uk ' # Ukrainian 'vi ' # Vietnamese 'yo ' # Yoruba 'zh-hans' # Chinese Simplified ) ) LANGUAGES = [ (lang_code, get_language_info(lang_code)['name_local']) for lang_code in DJANGO_LANGUAGE_CODES.split(' ') ] LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale'),) USE_I18N = True USE_TZ = True CAN_LOGIN_AS = lambda request, target_user: request.user.is_superuser # Default page size when no limit is specified in API requests. # Currently for submission lists only, but naming is future-proof for all endpoints DEFAULT_API_PAGE_SIZE = env.int('DEFAULT_API_PAGE_SIZE', 100) # Maximum page size for API responses. Currently applies to submission lists only, # but will likely be extended to all endpoints in the future MAX_API_PAGE_SIZE = env.int('MAX_API_PAGE_SIZE', 1000) # Capped API list counts (`LimitedCountPagination` and the MongoDB submission # list count): stop counting rows past this limit and report 'many' instead. # The high default keeps the feature inert until it is deliberately lowered # (DEV-2781). API_LIMITED_COUNT_MAX = env.int('API_LIMITED_COUNT_MAX', 100_000_000) # Timeout applied to the capped count query alone (env var in seconds, stored # in milliseconds): the Postgres statement_timeout, or the MongoDB maxTimeMS # for submission counts. On expiry the count is reported as 'unknown'. 0 # disables it, leaving only the connection-wide timeouts (DEV-1705). API_LIMITED_COUNT_TIMEOUT = env.int('API_LIMITED_COUNT_TIMEOUT', 0) * 1000 # uWSGI, NGINX, etc. allow only a limited amount of time to process a request. # Set this value to match their limits SYNCHRONOUS_REQUEST_TIME_LIMIT = 120 # seconds # REMOVE the oldest if a user exceeds this many exports for a particular form MAXIMUM_EXPORTS_PER_USER_PER_FORM = 10 MAX_RETRIES_FOR_IMPORT_EXPORT_TASK = 10 # Private media file configuration PRIVATE_STORAGE_ROOT = os.path.join(BASE_DIR, 'media') PRIVATE_STORAGE_AUTH_FUNCTION = \ 'kpi.utils.private_storage.superuser_or_username_matches_prefix' # django-markdownx, for in-app messages MARKDOWNX_UPLOAD_URLS_PATH = reverse_lazy('markdownx-uploader-image-upload') MARKDOWNX_UPLOAD_CONTENT_TYPES = [ 'image/jpeg', 'image/png', 'image/gif', 'image/webp', ] # Github-flavored Markdown from `py-gfm`, # ToDo Uncomment when it's compatible with Markdown 3.x # MARKDOWNX_MARKDOWN_EXTENSIONS = ['mdx_gfm'] # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.7/howto/static-files/ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/' + os.environ.get('KPI_MEDIA_URL', 'media').strip('/') + '/' # `PUBLIC_MEDIA_PATH` sets the `upload_to` attribute of explicitly-public # `FileField`s, e.g. in `ConfigurationFile`. The corresponding location on the # file system (usually `MEDIA_ROOT + PUBLIC_MEDIA_PATH`) should be exposed to # everyone via NGINX. For more information, see # https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.FileField.upload_to PUBLIC_MEDIA_PATH = '__public/' # Following the uWSGI mountpoint convention, this should have a leading slash # but no trailing slash KPI_PREFIX = env.str('KPI_PREFIX', 'False') if KPI_PREFIX.lower() == 'false': KPI_PREFIX = False else: KPI_PREFIX = '/' + KPI_PREFIX.strip('/') # KPI_PREFIX should be set in the environment when running in a subdirectory if KPI_PREFIX and KPI_PREFIX != '/': STATIC_URL = KPI_PREFIX + '/' + STATIC_URL.lstrip('/') MEDIA_URL = KPI_PREFIX + '/' + MEDIA_URL.lstrip('/') LOGIN_URL = KPI_PREFIX + '/' + global_settings.LOGIN_URL.lstrip('/') LOGIN_REDIRECT_URL = KPI_PREFIX + '/' + LOGIN_REDIRECT_URL.lstrip('/') STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'jsapp'), os.path.join(BASE_DIR, 'static'), ) if os.path.exists(os.path.join(BASE_DIR, 'dkobo', 'jsapp')): STATICFILES_DIRS = STATICFILES_DIRS + ( os.path.join(BASE_DIR, 'dkobo', 'jsapp'), os.path.join(BASE_DIR, 'dkobo', 'dkobo', 'static'), ) REST_FRAMEWORK = { 'URL_FIELD_NAME': 'url', 'DEFAULT_PAGINATION_CLASS': 'kpi.paginators.LimitedCountPagination', 'PAGE_SIZE': 100, 'DEFAULT_AUTHENTICATION_CLASSES': [ # SessionAuthentication and BasicAuthentication would be included by # default 'kpi.authentication.SessionAuthentication', 'kpi.authentication.BasicAuthentication', 'kpi.authentication.TokenAuthentication', 'kpi.authentication.OAuth2Authentication', ], 'DEFAULT_RENDERER_CLASSES': [ 'rest_framework.renderers.JSONRenderer', # "BasicHTMLRenderer" must always come after JSONRenderer 'kpi.renderers.BasicHTMLRenderer', ], 'DEFAULT_VERSIONING_CLASS': 'kpi.versioning.APIAutoVersioning', 'DEFAULT_THROTTLE_RATES': { # Changing the email address accepts `current_password` from clients # without a browser session; throttle it so it cannot be used to guess # passwords. Generous for the legitimate case, which is rare. 'email_change': env.str('EMAIL_CHANGE_THROTTLE_RATE', '10/hour'), }, # Cannot be placed in kpi.exceptions.py because of circular imports 'EXCEPTION_HANDLER': 'kpi.utils.drf_exceptions.custom_exception_handler', 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', 'DEFAULT_PARENT_LOOKUP_KWARG_NAME_PREFIX': 'uid_', } # Settings for the API documentation using drf-spectacular SPECTACULAR_SETTINGS = { 'TITLE': 'KoboToolbox Primary API', 'DESCRIPTION': ( 'This page documents all KoboToolbox API endpoints, except for those ' 'implementing the OpenRosa protocol, which are [documented separately](/api/openrosa/docs/).' # noqa '\n\n' 'The endpoints are grouped by area of intended use. Each category contains ' 'related endpoints, with detailed documentation on usage and configuration. ' 'Use this as a reference to quickly find the right endpoint for managing ' 'projects, forms, data, permissions, integrations, logs, and organizational ' 'resources.\n\n' '**General note**: All projects (whether deployed or draft), as well as all ' 'library content (questions, blocks, templates, and collections) in the ' 'user-facing application are represented in the API as "assets".' ), 'VERSION': '2.0.0', 'OAS_VERSION': '3.1.0', 'SERVE_INCLUDE_SCHEMA': False, 'POSTPROCESSING_HOOKS': [ 'drf_spectacular.hooks.postprocess_schema_enums', 'kpi.utils.spectacular_processing.merge_allauth_headless_schema', ], 'SWAGGER_UI_FAVICON_HREF': '/static/favicon.png', 'SWAGGER_UI_SETTINGS': { 'filter': True, 'docExpansion': None, # collapse all by default }, 'AUTHENTICATION_WHITELIST': [ 'kpi.authentication.BasicAuthentication', 'kpi.authentication.TokenAuthentication', 'kobo.apps.kobo_scim.authentication.ScimAuthentication', ], 'ENUM_NAME_OVERRIDES': { 'AuthServerTagEnum': 'kpi.constants.AUTH_SERVER_TAG_CHOICES', 'AuthThemeEnum': 'kpi.constants.AUTH_THEME_CHOICES', 'InviteStatusChoicesEnum': 'kobo.apps.organizations.models.OrganizationInviteStatusChoices.choices', # noqa 'InviteeRoleEnum': 'kpi.schema_extensions.v2.members.schema.ROLE_CHOICES_PAYLOAD_ENUM', # noqa 'MemberRoleEnum': 'kpi.schema_extensions.v2.members.schema.ROLE_CHOICES_ENUM', 'StripeProductType': 'kpi.schema_extensions.v2.stripe.schema.PRODUCT_TYPE_ENUM', 'StripePriceType': 'kpi.schema_extensions.v2.stripe.schema.PRICE_TYPE_ENUM', 'StripeIntervalEnum': 'kpi.schema_extensions.v2.stripe.schema.INTERVAL_ENUM', 'StripeUsageType': 'kpi.schema_extensions.v2.stripe.schema.USAGE_TYPE_ENUM', 'QualSimpleQuestionParamsTypeEnum': 'kpi.schema_extensions.v2.subsequences.schema.SIMPLE_QUESTION_TYPE_ENUM', # noqa 'QualSelectQuestionParamsTypeEnum': 'kpi.schema_extensions.v2.subsequences.schema.SELECT_QUESTION_TYPE_ENUM', # noqa 'AssetDeploymentStatusEnum': 'kpi.serializers.v2.asset.DEPLOYMENT_STATUS_ENUM', 'BulkActionResponseStatusEnum': 'kpi.schema_extensions.v2.subsequences.serializers.BULK_ACTION_STATUS_CHOICES', # noqa }, # We only want to blacklist BasicHTMLRenderer, but nothing like RENDERER_WHITELIST # exists 🤦 # List all the renderers that are used by documented API 'RENDERER_WHITELIST': [ 'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.StaticHTMLRenderer', 'kpi.renderers.MediaFileRenderer', 'kpi.renderers.MP3ConversionRenderer', 'kpi.renderers.OpenRosaRenderer', 'kpi.renderers.OpenRosaFormListRenderer', 'kpi.renderers.OpenRosaManifestRenderer', 'kpi.renderers.SSJsonRenderer', 'kpi.renderers.SubmissionGeoJsonRenderer', 'kpi.renderers.DoNothingRenderer', 'kpi.renderers.SubmissionXLSXRenderer', 'kpi.renderers.SubmissionCSVRenderer', 'kpi.renderers.SubmissionXMLRenderer', 'kpi.renderers.XMLRenderer', 'kpi.renderers.XFormRenderer', 'kpi.renderers.XlsRenderer', 'kobo.apps.openrosa.libs.renderers.renderers.XLSRenderer', 'kobo.apps.openrosa.libs.renderers.renderers.XLSXRenderer', 'kobo.apps.openrosa.libs.renderers.renderers.CSVRenderer', 'kobo.apps.openrosa.libs.renderers.renderers.RawXMLRenderer', 'kobo.apps.openrosa.libs.renderers.renderers.TemplateXMLRenderer', ], 'TAGS': [ { 'name': 'Manage projects and library content', 'description': ( 'Create, organize, and manage projects, assets ' '(projects/library content), and tags' ), }, { 'name': 'Form content', 'description': ( 'Export and preview assets (projects/library content) in different ' 'formats' ), }, { 'name': 'Survey data', 'description': 'View, edit, validate, export, and report collected data', }, { 'name': 'Survey data - Rest Services', 'description': 'Configure and manage webhooks for survey data integrations', }, { 'name': 'Manage permissions', 'description': ( 'Assign, clone, and bulk-manage project and asset ' '(projects/library content) permissions' ), }, { 'name': 'Logging', 'description': 'Project history logs, access logs, Rest Service hook logs', }, { 'name': 'Library collections', 'description': 'Subscribe to and manage shared library collections', }, { 'name': 'Server logs (superusers)', 'description': 'View server-wide logs', }, { 'name': 'User / team / organization / usage', 'description': 'Manage users, orgs, invites, roles, and usage tracking', }, { 'name': 'Other', 'description': 'Languages, available permissions, other', }, ], } SPECTACULAR_OPENROSA_TITLE = 'KoboToolbox OpenRosa API' SPECTACULAR_OPENROSA_DESCRIPTION = ( 'Welcome to the documentation for the KoboToolbox OpenRosa API. Data collection ' 'clients, including KoboCollect and web forms, use the API endpoints described ' 'here to retrieve surveys and upload submissions.\n\n' 'Our separate documentation of the primary KoboToolbox API endpoints, used to ' 'manage projects and data, can be found [here](/api/v2/docs/).' ) OPENROSA_REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': None, 'DEFAULT_VERSIONING_CLASS': None, # deprecated # # Use hyperlinked styles by default. # # Only used if the `serializer_class` attribute is not set on a view. # 'DEFAULT_MODEL_SERIALIZER_CLASS': ( # 'rest_framework.serializers.HyperlinkedModelSerializer' # ), # # Use Django's standard `django.contrib.auth` permissions, # # or allow read-only access for unauthenticated users. # 'DEFAULT_PERMISSION_CLASSES': [ # 'rest_framework.permissions.AllowAny', # ], 'DEFAULT_AUTHENTICATION_CLASSES': [ 'kpi.authentication.DigestAuthentication', 'kpi.authentication.OAuth2Authentication', 'kpi.authentication.TokenAuthentication', # HttpsOnlyBasicAuthentication must come before SessionAuthentication because # Django authentication is called before DRF authentication and users get authenticated with # Session if it comes first (which bypass BasicAuthentication and MFA validation) 'kobo.apps.openrosa.libs.authentication.HttpsOnlyBasicAuthentication', 'kpi.authentication.SessionAuthentication', ], 'DEFAULT_RENDERER_CLASSES': [ # Keep JSONRenderer at the top "in order to send JSON responses to # clients that do not specify an Accept header." See # http://www.django-rest-framework.org/api-guide/renderers/#ordering-of-renderer-classes 'rest_framework.renderers.JSONRenderer', 'rest_framework_jsonp.renderers.JSONPRenderer', 'kobo.apps.openrosa.libs.renderers.renderers.OpenRosaBasicHTMLRenderer', 'rest_framework_xml.renderers.XMLRenderer', 'rest_framework_csv.renderers.CSVRenderer', ], # FIXME Kobocat migration: Move to main REST_FRAMEWORK and change logic to handle kobocat view properly 'VIEW_NAME_FUNCTION': 'kobo.apps.openrosa.apps.api.tools.get_view_name', 'VIEW_DESCRIPTION_FUNCTION': 'kobo.apps.openrosa.apps.api.tools.get_view_description', } TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ # Default processors per # https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/#the-templates-settings 'django.contrib.auth.context_processors.auth', 'django.template.context_processors.debug', 'django.template.context_processors.i18n', 'django.template.context_processors.media', 'django.template.context_processors.static', 'django.template.context_processors.tz', 'django.template.context_processors.request', 'django.contrib.messages.context_processors.messages', # Additional processors 'kpi.context_processors.custom_password_guidance_text', 'kpi.context_processors.external_service_tokens', 'kpi.context_processors.email', 'kpi.context_processors.sitewide_messages', 'kpi.context_processors.config', 'kpi.context_processors.mfa', 'kpi.context_processors.django_settings', 'kpi.context_processors.kobocat', ], 'debug': os.environ.get('TEMPLATE_DEBUG', 'False') == 'True', }, }, ] DEFAULT_SUBMISSIONS_COUNT_NUMBER_OF_DAYS = 31 # Superusers submitting real survey data through the OpenRosa API tends to break # things (see DEV-32), so it is blocked by default. Self-hosters who really need # a superuser to submit can opt back into the risk by setting this to `True`; # consider yourself warned that bad things can happen. ALLOW_SUPERUSER_SUBMISSIONS = env.bool('ALLOW_SUPERUSER_SUBMISSIONS', False) GOOGLE_ANALYTICS_TOKEN = os.environ.get('GOOGLE_ANALYTICS_TOKEN') SENTRY_JS_DSN = None if SENTRY_JS_DSN_URL := env.url('SENTRY_JS_DSN', default=None): SENTRY_JS_DSN = SENTRY_JS_DSN_URL.geturl() # replace this with the pointer to the KoboCAT server, if it exists KOBOCAT_URL = os.environ.get('KOBOCAT_URL', 'https://change-me.invalid') # In case server must serve two KoboCAT domain names (e.g. during a # domain name transfer), `settings.KOBOCAT_OLD_URL` adds support for # the domain name. KOBOCAT_OLD_URL = os.environ.get('KOBOCAT_OLD_URL') # Internal URL does not use HTTPS KOBOCAT_INTERNAL_URL = os.environ.get( 'KOBOCAT_INTERNAL_URL', 'http://change-me.invalid' ) KOBOFORM_URL = os.environ.get('KOBOFORM_URL', 'https://change-me.invalid') if 'KOBOCAT_URL' in os.environ: DEFAULT_DEPLOYMENT_BACKEND = 'openrosa' else: DEFAULT_DEPLOYMENT_BACKEND = 'mock' """ Stripe configuration intended for kf.kobotoolbox.org only, tracks usage limit exceptions """ STRIPE_ENABLED = env.bool('STRIPE_ENABLED', False) BULK_ACTION_RATE_LIMITS = { 'automatic_gemini_transcription': { 'max_jobs_per_minute': 60, }, 'automatic_google_transcription': { 'max_jobs_per_minute': 120, }, 'automatic_google_translation': { 'max_jobs_per_minute': 300, }, } # Model called for automatic transcription with Gemini. Deliberately not in # Constance: changing the model must go through a release GEMINI_MODEL = 'gemini-3.5-transcribe' # Longest a transcription request to Gemini may take, in seconds. A request # blocks its Celery worker, and a bulk item is restarted as stuck after # `BULK_ACTION_STUCK_THRESHOLD`: keep it under that threshold GEMINI_REQUEST_TIMEOUT = env.int('GEMINI_REQUEST_TIMEOUT', 240) # These limits restrict the maximum number of requests per minute sent to each # NLP service across the entire platform. They are intentionally set lower than # the provider quotas to leave headroom for window skew, manual retries, and # services that share the same provider project outside this process. # Quota names are prefixed by provider API, so the dict stays flat. NLP_SERVICE_RATE_LIMITS = { 'gemini_transcribe': { 'max_requests': env.int( 'GEMINI_TRANSCRIBE_MAX_REQUESTS_PER_MINUTE', 60, ), 'period_seconds': 60, }, 'speech_v2_batch_recognize': { 'max_requests': env.int( 'GOOGLE_SPEECH_V2_BATCH_RECOGNIZE_MAX_REQUESTS_PER_MINUTE', 140, ), 'period_seconds': 60, }, 'translate_v3_translate_text': { 'max_requests': env.int( 'GOOGLE_TRANSLATE_V3_TRANSLATE_TEXT_MAX_REQUESTS_PER_MINUTE', 2000, ), 'period_seconds': 60, }, 'translate_v3_batch_translate_text': { 'max_requests': env.int( 'GOOGLE_TRANSLATE_V3_BATCH_TRANSLATE_TEXT_MAX_REQUESTS_PER_MINUTE', 3000, ), 'period_seconds': 60, }, } GOOGLE_SERVICE_QUOTA_RETRY_AFTER = env.int( 'GOOGLE_SERVICE_QUOTA_RETRY_AFTER', 100, ) BULK_ACTION_STATUS_POLL_INTERVAL = env.int( 'BULK_ACTION_STATUS_POLL_INTERVAL', 30, ) BULK_ACTION_STUCK_THRESHOLD = env.int( 'BULK_ACTION_STUCK_THRESHOLD', BULK_ACTION_STATUS_POLL_INTERVAL * 10, ) # The shortest quiet run that counts as a pause worth cutting at. Gaps shorter # than this are the spaces between words, where a cut would split a sentence. # Think of it as asking: "Is this silence long enough to count as a pause?" TRANSCRIPTION_SILENCE_MIN_DURATION = env.float( 'TRANSCRIPTION_SILENCE_MIN_DURATION', 0.5, ) # Loudness thresholds, in dBFS, tried in order until every cut has a pause to # land in. Recording levels differ enormously between files, so no single # threshold finds the pauses in all of them TRANSCRIPTION_SILENCE_THRESHOLDS_DB = (-30, -25, -20) # Longest a single ffmpeg run over a recording to be chunked may take, in the # Celery worker. The first run downloads, decodes and converts the whole # recording, so allow for a slow download. Every later run reads the local # copy: one more pause detection per further entry in # `TRANSCRIPTION_SILENCE_THRESHOLDS_DB` for a loud file, then one cut per # chunk, each a few seconds in practice. # # This bounds each run, not the preparation as a whole: that also includes # the uploads, each retried by the storage client for up to 120 s. When the # whole of it outlasts `CELERY_TASK_SOFT_TIME_LIMIT`, the poll ends the run as # failed rather than leaving it running TRANSCRIPTION_SILENCE_DETECT_TIMEOUT = env.int( 'TRANSCRIPTION_SILENCE_DETECT_TIMEOUT', 300, ) # Each chunk of a long recording is sent once, then again up to # `TRANSCRIPTION_CHUNK_MAX_RETRIES` times when it fails or is still not # transcribed `TRANSCRIPTION_CHUNK_TIMEOUT` seconds after it was sent. Chunks go # one after another, so in the worst case a run takes # chunks × (retries + 1) × timeout + preparation # which must stay within the 8 h its poll lasts (`GOOGLE_CACHE_TIMEOUT`). # Google transcribes a 55 min chunk in about 5 min (p90), so 900 s leaves room # for a slow day, and with 2 retries the worst case still covers 10 chunks, # about 9 h of audio TRANSCRIPTION_CHUNK_MAX_RETRIES = env.int('TRANSCRIPTION_CHUNK_MAX_RETRIES', 2) TRANSCRIPTION_CHUNK_TIMEOUT = env.int('TRANSCRIPTION_CHUNK_TIMEOUT', 900) DJSTRIPE_SUBSCRIBER_MODEL = 'organizations.Organization' DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK = dj_stripe_request_callback_method DJSTRIPE_FOREIGN_KEY_TO_FIELD = 'id' DJSTRIPE_USE_NATIVE_JSONFIELD = True STRIPE_LIVE_MODE = env.bool('STRIPE_LIVE_MODE', False) STRIPE_TEST_PUBLIC_KEY = env.str( 'STRIPE_TEST_PUBLIC_KEY', 'pk_test_qliDXQRyVGPWmsYR69tB1NPx00ndTrJfVM' ) STRIPE_LIVE_PUBLIC_KEY = 'pk_live_7JRQ5elvhnmz4YuWdlSRNmMj00lhvqZz8P' if STRIPE_ENABLED: INSTALLED_APPS += ('djstripe', 'kobo.apps.stripe') STRIPE_LIVE_SECRET_KEY = env.str('STRIPE_LIVE_SECRET_KEY', None) STRIPE_TEST_SECRET_KEY = env.str('STRIPE_TEST_SECRET_KEY', None) DJSTRIPE_WEBHOOK_SECRET = env.str('DJSTRIPE_WEBHOOK_SECRET', None) DJSTRIPE_WEBHOOK_VALIDATION = env.str('DJSTRIPE_WEBHOOK_VALIDATION', 'verify_signature') STRIPE_PUBLIC_KEY = STRIPE_LIVE_PUBLIC_KEY if STRIPE_LIVE_MODE else STRIPE_TEST_PUBLIC_KEY """Organizations settings""" # necessary to prevent calls to `/organizations/{ORG_ID}/service_usage/` (and any other # queries that may need to aggregate data for all organization users) from slowing down db ORGANIZATION_USER_LIMIT = env.str('ORGANIZATION_USER_LIMIT', 400) """ Enketo configuration """ ENKETO_URL = os.environ.get('ENKETO_URL') or os.environ.get( 'ENKETO_SERVER', 'https://change-me.invalid' ) ENKETO_URL = ENKETO_URL.rstrip('/') # Remove any trailing slashes ENKETO_VERSION = os.environ.get('ENKETO_VERSION', 'Legacy').lower() ENKETO_INTERNAL_URL = os.environ.get('ENKETO_INTERNAL_URL', ENKETO_URL) ENKETO_INTERNAL_URL = ENKETO_INTERNAL_URL.rstrip('/') # Remove any trailing slashes ENKETO_API_KEY = os.environ.get('ENKETO_API_KEY', 'enketorules') # http://apidocs.enketo.org/v2/ ENKETO_SURVEY_ENDPOINT = 'api/v2/survey/all' ENKETO_PREVIEW_ENDPOINT = 'api/v2/survey/preview/iframe' ENKETO_EDIT_INSTANCE_ENDPOINT = 'api/v2/instance' ENKETO_VIEW_INSTANCE_ENDPOINT = 'api/v2/instance/view' ENKETO_TIMEOUT = 5 # seconds # Content Security Policy (CSP) # CSP should "just work" by allowing any possible configuration # however CSP_EXTRA_DEFAULT_SRC is provided to allow for custom additions if env.bool('ENABLE_CSP', False): MIDDLEWARE.append('csp.middleware.CSPMiddleware') _csp_local_unsafe_allows = [ "'unsafe-eval'", 'http://localhost:3000', 'http://kf.kobo.local:3000', 'ws://kf.kobo.local:3000' ] _csp_default_src = env.list('CSP_EXTRA_DEFAULT_SRC', str, []) + [ "'self'", KOBOCAT_URL, ENKETO_URL, ] if env.str('FRONTEND_DEV_MODE', None) == 'host': _csp_default_src += _csp_local_unsafe_allows _csp_connect_src = [*_csp_default_src] _csp_script_src = [*_csp_default_src] _csp_style_src = [*_csp_default_src, "'unsafe-inline'"] _csp_img_src = [ *_csp_default_src, 'data:', 'https://*.openstreetmap.org', 'https://*.openstreetmap.fr', # Humanitarian OpenStreetMap Team 'https://*.opentopomap.org', 'https://*.arcgisonline.com', ] _csp_frame_src = [*_csp_default_src] if GOOGLE_ANALYTICS_TOKEN: # Taken from https://developers.google.com/tag-platform/tag-manager/csp#google_analytics_4_google_analytics _csp_script_src.append('https://*.googletagmanager.com') _csp_connect_src.extend( [ 'https://*.google-analytics.com', 'https://*.analytics.google.com', 'https://*.googletagmanager.com', ] ) _csp_img_src.extend( ['https://*.google-analytics.com', 'https://*.googletagmanager.com'] ) if SENTRY_JS_DSN_URL and SENTRY_JS_DSN_URL.scheme and SENTRY_JS_DSN_URL.hostname: _csp_sentry_url = f'{SENTRY_JS_DSN_URL.scheme}://{SENTRY_JS_DSN_URL.hostname}' if SENTRY_JS_DSN_URL.port: _csp_sentry_url += f':{SENTRY_JS_DSN_URL.port}' _csp_script_src.append(_csp_sentry_url) _csp_connect_src.append(_csp_sentry_url) if STRIPE_ENABLED: stripe_domain = 'https://js.stripe.com' _csp_script_src.append(stripe_domain) _csp_frame_src.append(stripe_domain) _csp_directives = { 'default-src': _csp_default_src, 'connect-src': _csp_connect_src, 'script-src': _csp_script_src, 'style-src': _csp_style_src, 'img-src': _csp_img_src, 'frame-src': _csp_frame_src, } _csp_report_uri = env.url('CSP_REPORT_URI', None) if _csp_report_uri: # Let environ validate uri, but set as string # _csp_directives is mutated in place here; the same dict is later # assigned (by reference) to whichever setting branch is taken below, # so report-uri ends up in the active policy regardless of REPORT_ONLY. _csp_directives['report-uri'] = [_csp_report_uri.geturl()] # CONTENT_SECURITY_POLICY (or its REPORT_ONLY counterpart) is always # defined at module load time, even when ENABLE_CSP=False. django-csp 4.0 # is inert without its middleware, so this is harmless in practice. # This mirrors the old behaviour (flat CSP_* settings were also always set). # Do not use the presence of these settings as a proxy for "CSP is enabled"; # check ENABLE_CSP or the middleware list instead. if env.bool('CSP_REPORT_ONLY', False): CONTENT_SECURITY_POLICY_REPORT_ONLY = {'DIRECTIVES': _csp_directives} else: CONTENT_SECURITY_POLICY = {'DIRECTIVES': _csp_directives} """ Celery configuration """ # Celery 4.0 New lowercase settings. # Uppercase settings can be used when using a PREFIX # http://docs.celeryproject.org/en/latest/userguide/configuration.html#new-lowercase-settings # http://docs.celeryproject.org/en/4.0/whatsnew-4.0.html#step-2-update-your-configuration-with-the-new-setting-names CELERY_TIMEZONE = 'UTC' # Use a throttled scheduler to prevent Beat from reloading its entire schedule # on every PeriodicTask change signal. On high-volume servers, one_off task # completions trigger hundreds of reload signals per minute, starving dispatch. # See kobo/apps/beat/schedulers.py for details. CELERY_BEAT_SCHEDULER = 'kobo.apps.beat.schedulers.ThrottledDatabaseScheduler' # helpful for certain debugging CELERY_TASK_ALWAYS_EAGER = env.bool('SKIP_CELERY', False) # Replace a worker after it completes 7 tasks by default. This allows the OS to # reclaim memory allocated during large tasks CELERY_WORKER_MAX_TASKS_PER_CHILD = int(os.environ.get( 'CELERYD_MAX_TASKS_PER_CHILD', 7)) # Default to a 30-minute soft time limit and a 35-minute hard time limit CELERY_TASK_TIME_LIMIT = int( os.environ.get('CELERYD_TASK_TIME_LIMIT', 2100) # seconds ) CELERY_TASK_SOFT_TIME_LIMIT = int( os.environ.get('CELERYD_TASK_SOFT_TIME_LIMIT', 1800) # seconds ) CELERY_BEAT_SCHEDULE = { # Schedule every day at midnight UTC. Can be customized in admin section 'send-hooks-failures-reports': { 'task': 'kobo.apps.hook.tasks.failures_reports', 'schedule': crontab(hour=0, minute=0), 'options': {'queue': 'kpi_low_priority_queue'}, }, # Schedule every 15 minutes 'trash-bin-task-restarter': { 'task': 'kobo.apps.trash_bin.tasks.task_restarter', 'schedule': crontab(minute='*/15'), # On `kpi_queue`, not `kpi_low_priority_queue`: this task orchestrates # the long deletions and transfers running there, and must not end up # stuck behind the very backlog it exists to clear 'options': {'queue': 'kpi_queue'}, }, 'perform-maintenance': { 'task': 'kpi.tasks.perform_maintenance', 'schedule': crontab(hour=20, minute=0), 'options': {'queue': 'kpi_low_priority_queue'}, }, 'log-stuck-exports-and-mark-failed': { 'task': 'kobo.apps.openrosa.apps.viewer.tasks.log_stuck_exports_and_mark_failed', 'schedule': timedelta(hours=6), 'options': {'queue': 'kobocat_queue'} }, 'delete-daily-xform-submissions-counter': { 'task': 'kobo.apps.openrosa.apps.logger.tasks.delete_daily_counters', 'schedule': crontab(hour=0, minute=0), 'options': {'queue': 'kobocat_queue'}, }, 'delete-expired-instance-history-records': { 'task': 'kobo.apps.openrosa.apps.logger.tasks.delete_expired_instance_history_records', # noqa 'schedule': crontab(hour=1, minute=0), 'options': {'queue': 'kobocat_queue'} }, # Schedule every 30 minutes 'organization-invite-mark-as-expired': { 'task': 'kobo.apps.organizations.tasks.mark_organization_invite_as_expired', 'schedule': crontab(minute='*/30'), 'options': {'queue': 'kpi_low_priority_queue'}, }, # Schedule every 30 minutes 'organization-ownership-transfer-task-restarter': { 'task': 'kobo.apps.organizations.tasks.restart_stuck_org_ownership_transfers', 'schedule': crontab(minute='*/30'), # On `kpi_queue`, not `kpi_low_priority_queue`: this task orchestrates # long-running transfers on that queue and must not get stuck behind them 'options': {'queue': 'kpi_queue'}, }, # Schedule every 10 minutes 'project-ownership-task-restarter': { 'task': 'kobo.apps.project_ownership.tasks.task_restarter', 'schedule': crontab(minute='*/30'), # On `kpi_queue`, not `kpi_low_priority_queue`: this task orchestrates # the long deletions and transfers running there, and must not end up # stuck behind the very backlog it exists to clear 'options': {'queue': 'kpi_queue'}, }, # Schedule every 30 minutes 'project-ownership-mark-as-failed': { 'task': 'kobo.apps.project_ownership.tasks.mark_as_failed', 'schedule': crontab(minute='*/30'), 'options': {'queue': 'kpi_low_priority_queue'} }, # Schedule every 30 minutes 'project-ownership-mark-as-expired': { 'task': 'kobo.apps.project_ownership.tasks.mark_as_expired', 'schedule': crontab(minute='*/30'), 'options': {'queue': 'kpi_low_priority_queue'} }, # Schedule every 5 minutes 'cleanup-anonymous-exports': { 'task': 'kpi.tasks.cleanup_anonymous_exports', 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_low_priority_queue'}, }, # Schedule every 5 minutes 'cleanup-synchronous-exports': { 'task': 'kpi.tasks.cleanup_synchronous_exports', 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_low_priority_queue'}, }, # Schedule every 5 minutes 'cleanup-project-view-exports': { 'task': 'kobo.apps.project_views.tasks.cleanup_project_view_exports', 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_low_priority_queue'}, }, # Schedule every 5 minutes 'cleanup-access-log-exports': { 'task': 'kobo.apps.audit_log.tasks.cleanup_access_log_exports', 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_low_priority_queue'}, }, # Schedule every 5 minutes 'cleanup-project-history-log-exports': { 'task': 'kobo.apps.audit_log.tasks.cleanup_project_history_log_exports', 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_low_priority_queue'}, }, # Ticks every 15 minutes, but `is_in_cooldown()` skips starting a new # cycle unless the last one completed more than # `USER_REPORTS_SNAPSHOT_MIN_INTERVAL_HOURS` ago - an in-progress run is # always resumed regardless, so a short tick lets it finish quickly # after an interruption instead of waiting up to that cooldown. 'refresh-user-report-snapshot': { 'task': 'kobo.apps.user_reports.tasks.refresh_user_report_snapshots', 'schedule': crontab(minute='*/15'), 'options': {'queue': 'kpi_long_running_tasks_queue'}, }, # Schedule every day at midnight UTC 'project-ownership-garbage-collector': { 'task': 'kobo.apps.project_ownership.tasks.garbage_collector', 'schedule': crontab(minute=0, hour=0), # On `kpi_queue`, not `kpi_low_priority_queue`: this task orchestrates # the long deletions and transfers running there, and must not end up # stuck behind the very backlog it exists to clear 'options': {'queue': 'kpi_queue'}, }, # Schedule every day at midnight UTC 'delete-expired-logs': { 'task': 'kobo.apps.audit_log.tasks.spawn_logs_cleaning_tasks', 'schedule': crontab(minute=0, hour=0), 'options': {'queue': 'kpi_low_priority_queue'} }, # Schedule every day at midnight UTC 'delete-expired-access-logs': { 'task': 'kobo.apps.audit_log.tasks.spawn_access_log_cleaning_tasks', 'schedule': crontab(minute=0, hour=0), 'options': {'queue': 'kpi_low_priority_queue'} }, # Schedule every 15 minutes 'long-running-migrations': { 'task': 'kobo.apps.long_running_migrations.tasks.execute_long_running_migrations', # noqa 'schedule': crontab(minute='*/15'), 'options': {'queue': 'kpi_long_running_tasks_queue'}, }, # Schedule every day at midnight UTC 'mass-email-record-mark-as-failed': { 'task': 'kobo.apps.mass_emails.tasks.mark_old_enqueued_mass_email_record_as_failed', # noqa 'schedule': crontab(minute=0, hour=0), 'options': {'queue': 'kpi_low_priority_queue'} }, 'mass-emails-send': { 'task': 'kobo.apps.mass_emails.tasks.send_emails', 'schedule': crontab(minute=1), 'options': {'queue': 'kpi_queue'}, }, 'mass-emails-enqueue-records': { 'task': 'kobo.apps.mass_emails.tasks.generate_mass_email_user_lists', 'schedule': crontab(minute=0), 'options': {'queue': 'kpi_queue'}, }, 'fix-stale-submissions-suspended-flag': { 'task': ( 'kobo.apps.openrosa.apps.logger.tasks.fix_stale_submissions_suspended_flag' ), 'schedule': crontab(minute='*/15'), 'description': ( 'Unlock accounts left suspended by a process which died before' ' releasing them' ), 'options': {'queue': 'kpi_low_priority_queue'}, }, 'recount-drifted-storage-counters': { 'task': ( 'kobo.apps.openrosa.apps.logger.tasks.recount_drifted_storage_counters' ), # A run lasts about as long as the soft time limit, and a single one # runs at a time: a short interval starts the next one soon after 'schedule': crontab(minute='*/15'), 'description': ( 'Recount the storage counters of projects which may have drifted, in' ' turns, until every one of them was checked, then start over' ), 'options': {'queue': 'kpi_long_running_tasks_queue'}, }, 'retry-stalled-submissions': { 'task': 'kobo.apps.hook.tasks.retry_stalled_pending_submissions', 'schedule': crontab(minute='*/30'), # Every 30 minutes 'options': {'queue': 'kpi_low_priority_queue'}, }, 'mark-zombie-submissions': { 'task': 'kobo.apps.hook.tasks.mark_zombie_processing_submissions', 'schedule': crontab(minute='*/30'), # Every 30 minutes 'options': {'queue': 'kpi_low_priority_queue'}, }, 'resume-stuck-subsequence-bulk-actions': { 'task': 'kobo.apps.subsequences.tasks.resume_stuck_bulk_actions', 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_low_priority_queue'}, }, 'enforce-managed-sso': { 'task': 'kobo.apps.accounts.tasks.managed_sso_sweep', 'schedule': crontab(hour=0, minute=45), 'options': {'queue': 'kpi_low_priority_queue'}, }, } if STRIPE_ENABLED: # Schedule to run once per celery timeout # with a five minute buffer minute_interval = (CELERY_TASK_TIME_LIMIT + (60 * 5)) // 60 CELERY_BEAT_SCHEDULE['update-exceeded-limit-counters'] = { 'task': 'kobo.apps.stripe.tasks.update_exceeded_limit_counters', 'schedule': crontab(minute='*/' + str(minute_interval)), 'options': {'queue': 'kpi_low_priority_queue'}, } # Small and frequent runs: each one queues at most # `AUTO_DELETE_ATTACHMENTS_USERS_PER_RUN` users, and each user gets at most # `AUTO_DELETE_ATTACHMENTS_MAX_PER_USER` attachments trashed per run CELERY_BEAT_SCHEDULE['attachment-cleanup-for-users-exceeding-limits'] = { 'task': 'kobo.apps.trash_bin.tasks.attachment.schedule_auto_attachment_cleanup_for_users', # noqa 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_low_priority_queue'}, } CELERY_BROKER_TRANSPORT_OPTIONS = { 'fanout_patterns': True, 'fanout_prefix': True, # http://docs.celeryproject.org/en/latest/getting-started/brokers/redis.html#redis-visibility-timeout # TODO figure out how to pass `Constance.HOOK_MAX_RETRIES` or `HookLog.get_remaining_seconds() # Otherwise hardcode `HOOK_MAX_RETRIES` in Settings 'visibility_timeout': 60 * (10**2), # Longest ETA for RestService (seconds) } CELERY_TASK_DEFAULT_QUEUE = 'kpi_queue' CELERY_BROKER_URL = os.environ.get( 'CELERY_BROKER_URL', os.environ.get('KPI_BROKER_URL', 'redis://change-me.invalid:6379/1'), ) if 'KPI_BROKER_URL' in os.environ: warnings.warn( 'KPI_BROKER_URL is renamed CELERY_BROKER_URL, update the environment variable.', DeprecationWarning, ) CELERY_RESULT_BACKEND = CELERY_BROKER_URL # Increase limits for long-running tasks # Notes: They are custom name, not part of `CELERY_*` namespace. CELERY_LONG_RUNNING_TASK_TIME_LIMIT = int( os.environ.get('CELERY_LONG_RUNNING_TASK_TIME_LIMIT', 4260) # seconds ) CELERY_LONG_RUNNING_TASK_SOFT_TIME_LIMIT = int( os.environ.get('CELERY_LONG_RUNNING_TASK_SOFT_TIME_LIMIT', 4200) # seconds ) # Dedicated, much longer limits for long-running migrations only. Unlike the # generic limits above (shared by many tasks), a single migration run may need # to churn through a whole table, so it is allowed up to ~24h in one pass. CELERY_LONG_RUNNING_MIGRATION_TASK_TIME_LIMIT = int( os.environ.get('CELERY_LONG_RUNNING_MIGRATION_TASK_TIME_LIMIT', 86460) # 24h + 1min ) CELERY_LONG_RUNNING_MIGRATION_TASK_SOFT_TIME_LIMIT = int( os.environ.get('CELERY_LONG_RUNNING_MIGRATION_TASK_SOFT_TIME_LIMIT', 86400) # 24h ) # Heartbeat that keeps a running migration's lock and `date_modified` fresh from # a background thread. The lock TTL is tied to this heartbeat, not to the task # time limit, so a hard-killed worker (e.g. a Kubernetes pod eviction that skips # the `finally` cleanup) releases the lock within a few heartbeats instead of # blocking re-execution for the whole 24h. The TTL is deliberately a few times # the interval to tolerate a briefly stalled worker without expiring the lock of # a task that is still alive. CELERY_LONG_RUNNING_MIGRATION_TASK_HEARTBEAT_INTERVAL = int( os.environ.get( 'CELERY_LONG_RUNNING_MIGRATION_TASK_HEARTBEAT_INTERVAL', 60 # 60 seconds ) ) CELERY_LONG_RUNNING_MIGRATION_TASK_HEARTBEAT_TTL = int( os.environ.get('CELERY_LONG_RUNNING_MIGRATION_TASK_HEARTBEAT_TTL', 300) # 300 sec ) # Minimum time to wait after a completed run before starting a brand-new # cycle. Does not delay resuming an in-progress run. USER_REPORTS_SNAPSHOT_MIN_INTERVAL_HOURS = int( os.environ.get('USER_REPORTS_SNAPSHOT_MIN_INTERVAL_HOURS', 4) # 4 hours ) CELERY_BEAT_RELOAD_INTERVAL = env.int('CELERY_BEAT_RELOAD_INTERVAL', 15) # 15 seconds """ Django allauth configuration """ # User.email should continue to be used instead of the EmailAddress model ACCOUNT_ADAPTER = 'kobo.apps.accounts.adapter.AccountAdapter' ACCOUNT_USERNAME_VALIDATORS = 'kobo.apps.accounts.validators.username_validators' ACCOUNT_SIGNUP_FIELDS = ['email*', 'username*', 'password1*', 'password2*'] # Adds our extra fields to every signup form, including the headless API's; # must point to a module that does not import `allauth.account.forms` ACCOUNT_SIGNUP_FORM_CLASS = 'kobo.apps.accounts.signup_fields.SignupExtraFieldsForm' ACCOUNT_EMAIL_UNKNOWN_ACCOUNTS = False ACCOUNT_EMAIL_VERIFICATION = env.str('ACCOUNT_EMAIL_VERIFICATION', 'mandatory') ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = env.int( 'ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS', 1 ) ACCOUNT_FORMS = { 'login': 'kobo.apps.accounts.forms.LoginForm', 'signup': 'kobo.apps.accounts.forms.SignupForm', 'reset_password': 'kobo.apps.accounts.forms.ResetPasswordForm', 'user_token': 'kobo.apps.accounts.forms.UserTokenForm', } ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTS = False # Accounts can share an email, so login stays username-only: allauth rejects # `email` in `ACCOUNT_LOGIN_METHODS` unless emails are unique ACCOUNT_UNIQUE_EMAIL = False ACCOUNT_RATE_LIMITS = { # Brute force. Per username ("key") and per IP. Also applies to wrong # MFA codes, per user. 10 matches the "5-10 failures, 15-minute lockout" # in the issue; 30 per IP tolerates a few people mistyping behind one # address while capping a spray from one IP at 120 guesses an hour. 'login_failed': env.str('ALLAUTH_LOGIN_FAILED_RATE_LIMIT', '30/15m/ip,10/15m/key'), # All login POSTs per IP, successful or not. Set loosely so a # training room behind one address is not blocked. '' turns it off. 'login': env.str('ALLAUTH_LOGIN_RATE_LIMIT', '40/m/ip'), # Account creation spam. Default. 'signup': env.str('ALLAUTH_SIGNUP_RATE_LIMIT', '20/m/ip'), # Reset emails per IP, and per address so one inbox cannot be flooded. # Defaults. 'reset_password': env.str('ALLAUTH_RESET_PASSWORD_RATE_LIMIT', '20/m/ip,5/m/key'), 'reset_password_from_key': env.str( 'ALLAUTH_RESET_PASSWORD_FROM_KEY_RATE_LIMIT', '20/m/ip' ), # Per logged-in user, so shared IPs are unaffected. Defaults. 'reauthenticate': env.str('ALLAUTH_REAUTHENTICATE_RATE_LIMIT', '10/m/user'), 'change_password': env.str('ALLAUTH_CHANGE_PASSWORD_RATE_LIMIT', '5/m/user'), 'manage_email': env.str('ALLAUTH_MANAGE_EMAIL_RATE_LIMIT', '10/m/user'), # Off: /api/v2/email-confirmations/ already has its own per-address # throttle (EMAIL_CONFIRMATION_REQUESTS_PER_HOUR). 'confirm_email': '', } ACCOUNT_SESSION_REMEMBER = True # How long a re-authentication remains valid before a sensitive action (e.g. # changing the email address) demands it again ACCOUNT_REAUTHENTICATION_TIMEOUT = env.int( 'ACCOUNT_REAUTHENTICATION_TIMEOUT', 300 # 5 minutes ) # Use X-Forwarded-For as the primary source of truth for client IPs ALLAUTH_TRUSTED_PROXY_COUNT = 1 SOCIALACCOUNT_ADAPTER = 'kobo.apps.accounts.adapter.SocialAccountAdapter' SOCIALACCOUNT_EMAIL_VERIFICATION = env.str('SOCIALACCOUNT_EMAIL_VERIFICATION', 'none') SOCIALACCOUNT_AUTO_SIGNUP = False SOCIALACCOUNT_FORMS = { 'signup': 'kobo.apps.accounts.forms.SocialSignupForm', } WEBPACK_LOADER = { 'DEFAULT': { 'BUNDLE_DIR_NAME': 'jsapp/compiled/', 'POLL_INTERVAL': 0.5, # seconds 'TIMEOUT': 5, # seconds } } """ Email configuration """ # This setting sets the prefix in the subject line of the account activation email # The default is the URL of the server. Set to blank to fit the email requirements ACCOUNT_EMAIL_SUBJECT_PREFIX = '' # Enable serving django-allauth Headless OpenAPI specs (we ingest these into # DRF-Spectacular) HEADLESS_SERVE_SPECIFICATION = True # Adds `has_validated_password` to the user payload on the session endpoint HEADLESS_ADAPTER = 'kobo.apps.accounts.adapter.HeadlessAdapter' EMAIL_BACKEND = os.environ.get( 'EMAIL_BACKEND', 'django.core.mail.backends.filebased.EmailBackend' ) if EMAIL_BACKEND == 'django.core.mail.backends.filebased.EmailBackend': EMAIL_FILE_PATH = os.environ.get( 'EMAIL_FILE_PATH', os.path.join(BASE_DIR, 'emails')) if not os.path.isdir(EMAIL_FILE_PATH): os.mkdir(EMAIL_FILE_PATH) if EMAIL_BACKEND == 'django.core.mail.backends.smtp.EmailBackend': EMAIL_BACKEND = 'kpi.utils.mailer.EmailBackend' if os.environ.get('EMAIL_HOST'): EMAIL_HOST = os.environ.get('EMAIL_HOST') if os.environ.get('EMAIL_HOST_USER'): EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER') EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD') if os.environ.get('EMAIL_PORT'): EMAIL_PORT = os.environ.get('EMAIL_PORT') if os.environ.get('EMAIL_USE_TLS'): EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS') # Size these to the SMTP provider actually configured for this deployment # (one provider at a time). # See kpi.utils.mailer.Mailer and kobo.apps.mass_emails.tasks.MassEmailSender. MAX_MASS_EMAILS_PER_DAY = env.int('MAX_MASS_EMAILS_PER_DAY', 10000) # This throttle applies only to mass email, not to transactional email # (e.g. forgot password). Set it well under the provider's real per-second # limit so there's still headroom left for transactional email, which # shares the same provider account but doesn't go through this throttle. MASS_EMAIL_THROTTLE_PER_SECOND = env.float('MASS_EMAIL_THROTTLE_PER_SECOND', 10) # Margin under the provider's SMTP idle timeout. MAILER_CONNECTION_IDLE_TIMEOUT = env.int('MAILER_CONNECTION_IDLE_TIMEOUT', 10) # change the interval between "daily" email sends for testing. this will set both # the frequency of the task and the expiry time of the cached email limits. should # only be True on small testing instances MASS_EMAILS_CONDENSE_SEND = env.bool('MASS_EMAILS_CONDENSE_SEND', False) if MASS_EMAILS_CONDENSE_SEND: CELERY_BEAT_SCHEDULE['mass-emails-send'] = { 'task': 'kobo.apps.mass_emails.tasks.send_emails', 'schedule': crontab(minute='1-59/5'), 'options': {'queue': 'kpi_queue'}, } CELERY_BEAT_SCHEDULE['mass-emails-enqueue-records'] = { 'task': 'kobo.apps.mass_emails.tasks.generate_mass_email_user_lists', 'schedule': crontab(minute='*/5'), 'options': {'queue': 'kpi_queue'}, } """ AWS configuration (email and storage) """ # Only set explicit credentials if provided via environment variables. # boto3 will otherwise fall back to ~/.aws/credentials, instance profiles, etc. if env.str('AWS_ACCESS_KEY_ID', False): AWS_ACCESS_KEY_ID = env.str('AWS_ACCESS_KEY_ID') AWS_SECRET_ACCESS_KEY = env.str('AWS_SECRET_ACCESS_KEY') AWS_BEDROCK_REGION_NAME = env.str('AWS_BEDROCK_REGION_NAME', None) AWS_BEDROCK_READ_TIMEOUT = env.int('AWS_BEDROCK_READ_TIMEOUT', 50) AWS_BEDROCK_CONNECT_TIMEOUT = env.int('AWS_BEDROCK_CONNECT_TIMEOUT', 5) AWS_SES_REGION_NAME = env.str('AWS_SES_REGION_NAME', None) AWS_SES_REGION_ENDPOINT = env.str('AWS_SES_REGION_ENDPOINT', None) AWS_S3_SIGNATURE_VERSION = env.str('AWS_S3_SIGNATURE_VERSION', 's3v4') # Only set the region if it is present in the environment. if region := env.str('AWS_S3_REGION_NAME', False): AWS_S3_REGION_NAME = region AWS_SES_CONFIGURATION_SET = env.str('AWS_SES_CONFIGURATION_SET', None) # Storage configuration STORAGES = global_settings.STORAGES default_file_storage = env.str( 'DEFAULT_FILE_STORAGE', env.str('KPI_DEFAULT_FILE_STORAGE', None) ) if 'KPI_DEFAULT_FILE_STORAGE' in os.environ: warnings.warn( 'KPI_DEFAULT_FILE_STORAGE is renamed DEFAULT_FILE_STORAGE, ' 'update the environment variable.', DeprecationWarning, ) # ToDo Find out why `private_storage.appconfig.PRIVATE_STORAGE_CLASS` # cannot be imported. Otherwise, some tests are failing. # from private_storage.appconfig import ( # PRIVATE_STORAGE_CLASS as DEFAULT_PRIVATE_STORAGE_CLASS # ) # PRIVATE_STORAGE_CLASS = DEFAULT_PRIVATE_STORAGE_CLASS PRIVATE_STORAGE_CLASS = 'private_storage.storage.files.PrivateFileSystemStorage' if default_file_storage: global_default_file_storage = STORAGES['default']['BACKEND'] default_file_storage = STORAGES['default']['BACKEND'] = default_file_storage if default_file_storage != global_default_file_storage: if default_file_storage.endswith('S3Boto3Storage'): # To use S3 storage, set this to `kobo.apps.storage_backends.s3boto3.S3Boto3Storage` # Force usage of custom S3 tellable Storage STORAGES['default']['BACKEND'] = ( 'kobo.apps.storage_backends.s3boto3.S3Boto3Storage' ) AWS_S3_FILE_OVERWRITE = False elif default_file_storage.endswith('AzureStorage'): PRIVATE_STORAGE_CLASS = ( 'kobo.apps.storage_backends.private_azure_storage.PrivateAzureStorage' ) PRIVATE_STORAGE_S3_REVERSE_PROXY = True # Yes S3 AZURE_ACCOUNT_NAME = env.str('AZURE_ACCOUNT_NAME') AZURE_ACCOUNT_KEY = env.str('AZURE_ACCOUNT_KEY') AZURE_CONTAINER = env.str('AZURE_CONTAINER') AZURE_URL_EXPIRATION_SECS = env.int( 'AZURE_URL_EXPIRATION_SECS', None ) aws_storage_bucket_name = env.str( 'AWS_STORAGE_BUCKET_NAME', env.str('KPI_AWS_STORAGE_BUCKET_NAME', None) ) if aws_storage_bucket_name: AWS_STORAGE_BUCKET_NAME = aws_storage_bucket_name AWS_DEFAULT_ACL = 'private' # django-private-storage needs its own S3 configuration PRIVATE_STORAGE_CLASS = ( 'private_storage.storage.s3boto3.PrivateS3BotoStorage' # NB.........There's intentionally no 3 here! ^ ) AWS_PRIVATE_STORAGE_BUCKET_NAME = AWS_STORAGE_BUCKET_NAME # Proxy S3 through our application instead of redirecting to bucket # URLs with query parameter authentication PRIVATE_STORAGE_S3_REVERSE_PROXY = True if 'KOBOCAT_DEFAULT_FILE_STORAGE' in os.environ: KOBOCAT_DEFAULT_FILE_STORAGE = os.environ.get('KOBOCAT_DEFAULT_FILE_STORAGE') if 'KOBOCAT_AWS_STORAGE_BUCKET_NAME' in os.environ: KOBOCAT_AWS_STORAGE_BUCKET_NAME = os.environ.get('KOBOCAT_AWS_STORAGE_BUCKET_NAME') STORAGES['local'] = { 'BACKEND': 'django.core.files.storage.FileSystemStorage', } else: KOBOCAT_DEFAULT_FILE_STORAGE = global_settings.STORAGES['default']['BACKEND'] KOBOCAT_MEDIA_ROOT = os.environ.get( 'KOBOCAT_MEDIA_ROOT', MEDIA_ROOT.replace('kpi', 'kobocat') ) STORAGES['import_export_celery'] = {'BACKEND': PRIVATE_STORAGE_CLASS} # Google Cloud Storage # Not fully supported as a generic storage backend GS_BUCKET_NAME = env.str('GS_BUCKET_NAME', None) """ Django error logging configuration """ LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s' + ' %(process)d %(thread)d %(message)s' }, 'simple': { 'format': '%(levelname)s %(message)s' }, }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose' } }, 'loggers': { 'console_logger': { 'handlers': ['console'], 'level': 'DEBUG', 'propagate': False, }, 'django.db.backends': { 'level': 'ERROR', 'handlers': ['console'], 'propagate': False }, } } ################################ # Sentry settings # ################################ sentry_dsn = env.str('SENTRY_DSN', None) if sentry_dsn: import sentry_sdk from sentry_sdk.integrations.celery import CeleryIntegration from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.logging import LoggingIntegration # All of this is already happening by default! sentry_logging = LoggingIntegration( level=logging.INFO, # Capture info and above as breadcrumbs event_level=logging.WARNING # Send warnings as events ) sentry_sdk.init( dsn=sentry_dsn, integrations=[ DjangoIntegration(), CeleryIntegration(), sentry_logging ], traces_sample_rate=env.float('SENTRY_TRACES_SAMPLE_RATE', 0.01), send_default_pii=True ) if ENABLE_METRICS := env.bool('ENABLE_METRICS', False): MIDDLEWARE.insert(0, 'django_prometheus.middleware.PrometheusBeforeMiddleware') MIDDLEWARE.append('django_prometheus.middleware.PrometheusAfterMiddleware') # Workaround https://github.com/korfuri/django-prometheus/issues/34 PROMETHEUS_EXPORT_MIGRATIONS = False # https://github.com/korfuri/django-prometheus/blob/master/documentation/exports.md#exporting-metrics-in-a-wsgi-application-with-multiple-processes-per-process if start_port := env.int('METRICS_START_PORT', None): PROMETHEUS_METRICS_EXPORT_PORT_RANGE = range( start_port, env.int('METRICS_END_PORT', start_port + 10) ) GIT_REV = get_git_rev(BASE_DIR) """ Since this project handles user creation, we must handle the model-level permission assignment that would've been done by KoBoCAT's user post_save signal handler. Here we record the content types of the models listed in KC's deprecated function set_api_permissions_for_user. TODO: This is being refactored and is pending to clean up """ KOBOCAT_DEFAULT_PERMISSION_CONTENT_TYPES = [ # Each tuple must be (app_label, model_name) ('main', 'userprofile'), ('logger', 'xform'), ('logger', 'note'), ] # A flag set by unit tests to bypass KoBoCAT user syncing TESTING = False """ Auxiliary database configuration """ if not (MONGO_DB_URL := env.str('MONGO_DB_URL', False)): # ToDo Remove all this block by the end of 2022. # Update kobo-install accordingly logging.warning( '`MONGO_DB_URL` is not found. ' '`KPI_MONGO_HOST`, `KPI_MONGO_PORT`, `KPI_MONGO_NAME`, ' '`KPI_MONGO_USER`, `KPI_MONGO_PASS` ' 'are deprecated and will not be supported anymore soon.' ) MONGO_DATABASE = { 'HOST': os.environ.get('KPI_MONGO_HOST', 'mongo'), 'PORT': int(os.environ.get('KPI_MONGO_PORT', 27017)), 'NAME': os.environ.get('KPI_MONGO_NAME', 'formhub'), 'USER': os.environ.get('KPI_MONGO_USER', ''), 'PASSWORD': os.environ.get('KPI_MONGO_PASS', '') } if MONGO_DATABASE.get('USER') and MONGO_DATABASE.get('PASSWORD'): MONGO_DB_URL = 'mongodb://{user}:{password}@{host}:{port}/{db_name}'.format( user=MONGO_DATABASE['USER'], password=quote_plus(MONGO_DATABASE['PASSWORD']), host=MONGO_DATABASE['HOST'], port=MONGO_DATABASE['PORT'], db_name=MONGO_DATABASE['NAME'], ) else: MONGO_DB_URL = 'mongodb://%(HOST)s:%(PORT)s/%(NAME)s' % MONGO_DATABASE mongo_db_name = MONGO_DATABASE['NAME'] else: # Attempt to get collection name from the connection string # fallback on MONGO_DB_NAME or 'formhub' if it is empty or None or unable to parse try: mongo_db_name = env.db_url('MONGO_DB_URL').get('NAME') or env.str('MONGO_DB_NAME', 'formhub') except ValueError: # db_url is unable to parse replica set strings mongo_db_name = env.str('MONGO_DB_NAME', 'formhub') mongo_client = MongoClient( MONGO_DB_URL, connect=False, journal=True, tz_aware=True ) MONGO_DB = mongo_client[mongo_db_name] # Maximum query duration (in milliseconds) for PostgreSQL (statement_timeout) # and MongoDB (maxTimeMS). Applied per environment: # - Web requests: DATABASE_QUERY_TIMEOUT, based on SYNCHRONOUS_REQUEST_TIME_LIMIT # - Celery workers: DATABASE_CELERY_QUERY_TIMEOUT, based on CELERY_TASK_TIME_LIMIT # ⚠️⚠️ # For MongoDB, use MongoHelper.get_max_time_ms() rather than referencing these # directly. # ⚠️⚠️ DATABASE_QUERY_TIMEOUT = ( env.int('DATABASE_QUERY_TIMEOUT', SYNCHRONOUS_REQUEST_TIME_LIMIT + 5) * 1000 ) # milliseconds DATABASE_CELERY_QUERY_TIMEOUT = (CELERY_TASK_TIME_LIMIT + 10) * 1000 # milliseconds SESSION_ENGINE = 'redis_sessions.session' # django-redis-session expects a dictionary with `url` redis_session_url = env.cache_url( 'REDIS_SESSION_URL', default='redis://change-me.invalid:6380/2' ) SESSION_REDIS = { 'url': redis_session_url['LOCATION'], 'prefix': env.str('REDIS_SESSION_PREFIX', 'session'), 'socket_timeout': env.int('REDIS_SESSION_SOCKET_TIMEOUT', 1), } CACHES = { # Set CACHE_URL to override 'default': env.cache_url(default='redis://change-me.invalid:6380/3'), 'enketo_redis_main': env.cache_url( 'ENKETO_REDIS_MAIN_URL', default='redis://change-me.invalid/0' ), # Isolated backend for Constance with a versioned key prefix to prevent # cache format collisions between old and new workers during rolling deploys. 'constance': { **env.cache_url(default='redis://change-me.invalid:6380/3'), 'KEY_PREFIX': 'constance_4x', }, } # How long to retain cached responses for kpi endpoints ENDPOINT_CACHE_DURATION = env.int('ENDPOINT_CACHE_DURATION', 60 * 15) # 15 minutes ENV = None # The maximum size in bytes that a request body may be before a # SuspiciousOperation (RequestDataTooBig) is raised DATA_UPLOAD_MAX_MEMORY_SIZE = 10485760 # The maximum size (in bytes) that an upload will be before it gets streamed # to the file system FILE_UPLOAD_MAX_MEMORY_SIZE = 10485760 # Maximum allowed size (in bytes) of the `choices` sheet in an XLSForm MAX_CHOICES_SIZE_BYTES = env.int('MAX_CHOICES_SIZE_BYTES', 2 * 1024 * 1024) # OpenRosa setting in bytes OPENROSA_DEFAULT_CONTENT_LENGTH = 10000000 # Expiration time in sec. after which paired data xml file must be regenerated PAIRED_DATA_EXPIRATION = 300 # seconds # Lock TTL for the async regeneration task; covers the worst-case generation # time and ensures the lock expires even if a K8s pod is killed mid-task. PAIRED_DATA_REGEN_LOCK_TIMEOUT = 600 # seconds CALCULATED_HASH_CACHE_EXPIRATION = 300 # seconds # add some mimetype add_type('application/wkt', '.wkt') add_type('application/geo+json', '.geojson') KOBOCAT_MEDIA_URL = f'{KOBOCAT_URL}/media/' MFA_FORMS = { 'authenticate': 'kobo.apps.accounts.mfa.forms.MfaAuthenticateForm', 'reauthenticate': 'kobo.apps.accounts.mfa.forms.MfaReauthenticateForm', } MFA_ADAPTER = 'kobo.apps.accounts.mfa.adapter.MfaAdapter' MFA_TOTP_DIGITS = env.int('MFA_CODE_LENGTH', 6) MFA_TOTP_PERIOD = env.int('MFA_CODE_VALIDITY_PERIOD', 30) MFA_RECOVERY_CODE_COUNT = 5 MFA_RECOVERY_CODE_DIGITS = 12 # Session Authentication is supported by default. MFA_SUPPORTED_AUTH_CLASSES = [ 'kpi.authentication.TokenAuthentication', 'kobo.apps.openrosa.libs.authentication.TokenAuthentication', ] MINIMUM_DEFAULT_SEARCH_CHARACTERS = 3 # Max to-many lookups in a single `q` search: each becomes its own EXISTS # subquery, so an unbounded count lets a search fan out. # See `kpi.utils.query_parser`. QUERY_PARSER_MAX_TO_MANY_FILTERS = 10 # Django 3.2 required settings DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'kpi.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'kpi.password_validation.MinimumLengthValidator', }, { 'NAME': 'kpi.password_validation.CommonPasswordValidator', }, { 'NAME': 'kpi.password_validation.CustomRulesValidator', }, { 'NAME': 'kpi.password_validation.MostRecentPasswordValidator', }, ] # Needed to avoid Constance to create permissions on KoboCAT database CONSTANCE_DBS = [ 'default' ] AUTH_USER_MODEL = 'kobo_auth.User' #################################### # KoboCAT settings # #################################### KOBOCAT_PUBLIC_HOSTNAME = ( f"{env.str('KOBOCAT_PUBLIC_SUBDOMAIN', 'kc')}" f".{env.str('PUBLIC_DOMAIN_NAME', 'domain.tld')}" ) KOBOFORM_INTERNAL_URL = env.url('KOBOFORM_INTERNAL_URL', KOBOFORM_URL).geturl() ENKETO_OFFLINE_SURVEYS = env.bool('ENKETO_OFFLINE_SURVEYS', True) ENKETO_ONLINE_SURVEY_ENDPOINT = 'api/v2/survey' ENKETO_OFFLINE_SURVEY_ENDPOINT = 'api/v2/survey/offline' OPENROSA_ENKETO_SURVEY_ENDPOINT = ( ENKETO_OFFLINE_SURVEY_ENDPOINT if ENKETO_OFFLINE_SURVEYS else ENKETO_ONLINE_SURVEY_ENDPOINT ) OPENROSA_APP_DIR = os.path.join(BASE_DIR, 'kobo', 'apps', 'openrosa') DEFAULT_SESSION_EXPIRY_TIME = 21600 # 6 hours CELERY_TASK_ROUTES = { 'kobo.apps.openrosa.*': 'kobocat_queue', } USE_THOUSAND_SEPARATOR = True DIGEST_NONCE_BACKEND = 'kobo.apps.openrosa.apps.django_digest_backends.cache.RedisCacheNonceStorage' # noqa KPI_HOOK_ENDPOINT_PATTERN = '/api/v2/assets/{asset_uid}/hook-signal/' # TODO Validate if `'PKCE_REQUIRED': False` is required in KPI OAUTH2_PROVIDER = { # this is the list of available scopes 'SCOPES': { 'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups' }, 'PKCE_REQUIRED': False, } REVERSION_MIDDLEWARE_SKIPPED_URL_PATTERNS = { r'/api/v1/users/(.*)': ['DELETE'] } DAILY_COUNTERS_MAX_DAYS = env.int('DAILY_COUNTERS_MAX_DAYS', 366) USE_POSTGRESQL = True # Added this because of https://github.com/onaio/kobo.apps.open_rosa_server/pull/2139 # Should bring support to ODK v1.17+ SUPPORT_BRIEFCASE_SUBMISSION_DATE = ( os.environ.get('SUPPORT_BRIEFCASE_SUBMISSION_DATE') != 'True' ) DEFAULT_VALIDATION_STATUSES = { 'validation_status_not_approved': 'Not Approved', 'validation_status_approved': 'Approved', 'validation_status_on_hold': 'On Hold', } THUMB_CONF = { 'large': 1280, 'medium': 640, 'small': 240, } SUPPORTED_MEDIA_UPLOAD_TYPES = [ 'image/jpeg', 'image/png', 'image/svg+xml', 'image/webp', 'video/3gpp', 'video/mp4', 'video/quicktime', 'video/ogg', 'video/webm', 'audio/aac', 'audio/aacp', 'audio/3gpp', 'audio/flac', 'audio/mp3', 'audio/mp4', 'audio/mpeg', 'audio/ogg', 'audio/wav', 'audio/x-wav', 'audio/webm', 'audio/x-m4a', 'text/csv', 'application/xml', 'application/zip', 'application/x-zip-compressed', 'application/geo+json', ] DIGEST_LOGIN_FACTORY = 'django_digest.NoEmailLoginFactory' # Admins will not be explicitly granted these permissions, (i.e., not referenced # in the ObjectPermission table), but the code will still conduct the permission # checks as if they were. ADMIN_ORG_INHERITED_PERMS = [PERM_DELETE_ASSET, PERM_MANAGE_ASSET] # Import/Export Celery IMPORT_EXPORT_CELERY_INIT_MODULE = 'kobo.celery' IMPORT_EXPORT_CELERY_MODELS = { 'OrganizationUser': { 'app_label': 'organization', 'model_name': 'OrganizationUser', }, } IMPORT_EXPORT_CELERY_STORAGE_ALIAS = 'import_export_celery' ORG_INVITATION_RESENT_RESET_AFTER = 15 * 60 # in seconds # Batch sizes DEFAULT_BATCH_SIZE = 1000 LOG_DELETION_BATCH_SIZE = 1000 USER_ASSET_ORG_TRANSFER_BATCH_SIZE = 1000 SUBMISSION_DELETION_BATCH_SIZE = 1000 LONG_RUNNING_MIGRATION_BATCH_SIZE = 2000 LONG_RUNNING_MIGRATION_SMALL_BATCH_SIZE = 100 VERSION_DELETION_BATCH_SIZE = 2000 S3_DELETE_BATCH_SIZE = 1000 AZURE_DELETE_BATCH_SIZE = 256 USAGE_QUERY_USER_ID_BATCH_SIZE = 20000 # Number of stuck project ownership tasks and transfers restarted at a time MAX_RESTARTED_TASKS = 100 MAX_RESTARTED_TRANSFERS = 20 # Number of trash bin deletions, due or stuck, that `task_restarter` sends per # run and per type. It runs every 15 minutes. MAX_RESTARTED_ACCOUNT_DELETIONS = env.int('MAX_RESTARTED_ACCOUNT_DELETIONS', 50) MAX_RESTARTED_PROJECT_DELETIONS = env.int('MAX_RESTARTED_PROJECT_DELETIONS', 100) MAX_RESTARTED_ATTACHMENT_DELETIONS = env.int('MAX_RESTARTED_ATTACHMENT_DELETIONS', 1000) # Number of attachments `auto_delete_excess_attachments` moves to trash per user # and per run. A user still over their limit gets the next ones on a later run AUTO_DELETE_ATTACHMENTS_MAX_PER_USER = env.int( 'AUTO_DELETE_ATTACHMENTS_MAX_PER_USER', 100 ) # Number of users `schedule_auto_attachment_cleanup_for_users` queues per run. # Users take turns, the next run continues with the following ones. AUTO_DELETE_ATTACHMENTS_USERS_PER_RUN = env.int( 'AUTO_DELETE_ATTACHMENTS_USERS_PER_RUN', 5 ) # Number of times a trash bin task that failed on a transient (infrastructure) # error is automatically restarted before it requires manual intervention TRASH_BIN_MAX_AUTO_RESTARTS = env.int('TRASH_BIN_MAX_AUTO_RESTARTS', 10) # How long a trash bin object stays locked while it is being deleted. Must be # greater than or equal to the Celery hard time limit of the task TRASH_BIN_DELETION_LOCK_TTL = CELERY_LONG_RUNNING_TASK_TIME_LIMIT + 60 * 5 # Number of transfer log records rendered inline on a transfer admin page PROJECT_OWNERSHIP_MAX_DISPLAYED_LOGS = 100 # Maximum timeout (in minutes) for hook processing HOOK_STALLED_PENDING_TIMEOUT = 120 HOOK_STALLED_RETRY_TIMEOUT = 1440 # Exponential backoff (in seconds) between automatic hook retries. # Retries wait 5, 10, 20, 20, ... minutes. HOOK_RETRY_BACKOFF = 300 HOOK_RETRY_BACKOFF_MAX = 1200 # Cache time-to-live (in seconds) for attachment XPaths ATTACHMENT_XPATHS_CACHE_TTL = 86400 # Same, for the XPaths of a single form version. A version never changes, so # its entry can live much longer than the one that merges all versions ATTACHMENT_XPATHS_VERSION_CACHE_TTL = 30 * 86400 # Cache time-to-live (in seconds) for the survey question types and default # language used by NLP actions SURVEY_METADATA_CACHE_TTL = 86400 # Configure the Referrer-Policy response header so OpenStreetMap tile servers # receive an acceptable referrer. See: # https://wiki.openstreetmap.org/wiki/Blocked_tiles#Referer_is_required # Can be overridden per environment via the SECURE_REFERRER_POLICY environment variable. SECURE_REFERRER_POLICY = env( 'SECURE_REFERRER_POLICY', default='strict-origin-when-cross-origin', ) AUTOQA_CLAUDESONNET_MODEL_AIP_ARN = env.str( 'AUTOQA_CLAUDESONNET_MODEL_AIP_ARN', default='us.anthropic.claude-sonnet-4-5-20250929-v1:0', ) AUTOQA_OSS120_MODEL_AIP_ARN = env.str( 'AUTOQA_OSS120_MODEL_AIP_ARN', default='openai.gpt-oss-120b-1:0' ) AUTOQA_GEMMA_MODEL_AIP_ARN = env.str( 'AUTOQA_GEMMA_MODEL_AIP_ARN', default='google.gemma-3-27b-it' ) # 24 months x 31 days/month = 744 default PROJECT_HISTORY_LOG_LIFESPAN = env.int('PROJECT_HISTORY_LOG_LIFESPAN', 744) ACCESS_LOG_LIFESPAN = env.int('ACCESS_LOG_LIFESPAN', 744) # Minimum gap between two ExtraUserDetail.last_project_activity writes for the same # user. Prevents write storms when a user submits many forms in a short window. LAST_PROJECT_ACTIVITY_THROTTLE_SECONDS = env.int( 'LAST_PROJECT_ACTIVITY_THROTTLE_SECONDS', 3600 # seconds ) # OpenAPI schema validation runs under the test settings only: the middleware # refuses to load anywhere else, whatever these say. See # kobo/apps/openapi_validator/README.md OPENAPI_VALIDATION = False OPENAPI_SCHEMA_PATH = 'static/openapi/schema_v2.json' # Do not change these variables here. Override them in `testing.py` OPENAPI_VALIDATION_STRICT = False OPENAPI_VALIDATION_BUILD_WHITELIST_LOG = False