sbt-idp/cope2n-api/fwd/settings.py

318 lines
10 KiB
Python
Raw Permalink Normal View History

2023-11-30 11:19:06 +00:00
"""
Django settings for fwd project.
Generated by 'django-admin startproject' using Django 4.1.3.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import os
from pathlib import Path
import environ
from django.urls import reverse_lazy
2024-07-05 06:32:00 +00:00
from fwd_api.middleware.logging_request_response_middleware import TraceIDLogFilter
from fwd_api.middleware.response_monitor import ResponseMonitorMiddleware
2023-11-30 11:19:06 +00:00
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
env = environ.Env(
2023-12-14 03:54:03 +00:00
DEBUG=(bool, False)
2023-11-30 11:19:06 +00:00
)
2023-12-14 03:54:03 +00:00
DEBUG = False
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
2023-11-30 11:19:06 +00:00
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=['*'] + ['107.120.{}.{}'.format(i, j) for i in range(256) for j in range(256)])
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
2023-12-14 03:54:03 +00:00
SECRET_KEY = env.str("SECRET_KEY", '')
CTEL_KEY = env.str("CTEL_KEY", '')
INTERNAL_SDS_KEY = env.str('SDS_SECRET_KEY', '')
DB_ENCRYPT_KEY = env.str('DB_INTERNAL_KEY', '')
2023-11-30 11:19:06 +00:00
BASE_URL = env.str("BASE_URL", "")
2023-12-14 03:54:03 +00:00
BASE_UI_URL = env.str("BASE_UI_URL", "")
2023-11-30 11:19:06 +00:00
AUTH_TOKEN_LIFE_TIME = env.int("AUTH_TOKEN_LIFE_TIME", 0)
IMAGE_TOKEN_LIFE_TIME = env.int("IMAGE_TOKEN_LIFE_TIME", 0)
ADMIN_USER_NAME = env.str("ADMIN_USER_NAME", "")
ADMIN_PASSWORD = env.str("ADMIN_PASSWORD", '')# SECURITY WARNING: don't run with debug turned on in production!
STANDARD_USER_NAME = env.str("STANDARD_USER_NAME", "")
STANDARD_PASSWORD = env.str("STANDARD_PASSWORD", '')# SECURITY WARNING: don't run with debug turned on in production!
2023-11-30 11:19:06 +00:00
# Application definition
S3_ENDPOINT = env.str("S3_ENDPOINT", "")
2023-12-14 03:54:03 +00:00
S3_ACCESS_KEY = env.str("S3_ACCESS_KEY", "")
S3_SECRET_KEY = env.str("S3_SECRET_KEY", "")
2023-11-30 11:19:06 +00:00
S3_BUCKET_NAME = env.str("S3_BUCKET_NAME", "ocr-data")
2023-12-13 11:43:10 +00:00
REDIS_HOST = env.str("REDIS_HOST", "result-cache")
REDIS_PORT = env.int("REDIS_PORT", 6379)
AWS_REGION = env.str("AWS_REGION", "")
AWS_QUEUE_URL = env.str("AWS_QUEUE_URL", "")
2023-12-13 11:43:10 +00:00
2023-11-30 11:19:06 +00:00
INSTALLED_APPS = [
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'fwd_api.apps.FwdApiConfig',
'django.contrib.admin',
'rest_framework',
'drf_spectacular',
'drf_spectacular_sidecar', # required for Django collectstatic discovery
'corsheaders',
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
'corsheaders.middleware.CorsMiddleware',
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.middleware.locale.LocaleMiddleware",
"fwd_api.middleware.logging_request_response_middleware.LoggingMiddleware",
'fwd_api.middleware.response_monitor.ResponseMonitorMiddleware',
2023-11-30 11:19:06 +00:00
]
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale')
]
ROOT_URLCONF = "fwd.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / 'templates']
,
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = "fwd.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
'default': {
2023-12-05 05:59:59 +00:00
'ENGINE': env.str("DB_ENGINE"),
'NAME': env.str("DB_SCHEMA"),
2023-11-30 11:19:06 +00:00
'USER': env.str("DB_USER", None),
'PASSWORD': env.str("DB_PASSWORD", None),
'HOST': env.str("DB_HOST", None),
'PORT': env.str("DB_PORT", None),
2024-03-14 03:11:20 +00:00
'CONN_MAX_AGE': 0,
2023-11-30 11:19:06 +00:00
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = "en-us"
USE_I18N = True
2023-12-13 09:01:31 +00:00
CELERY_ENABLE_UTC = False
2024-02-06 03:14:44 +00:00
CELERY_TIMEZONE = "Asia/Singapore"
TIME_ZONE = "Asia/Singapore"
2023-11-30 11:19:06 +00:00
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STORAGES = {
# ...
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
REST_FRAMEWORK = {
# YOUR SETTINGS
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
# 'DEFAULT_SCHEMA_CLASS': 'fwd_api.schema.AutoSchema',
"DEFAULT_AUTHENTICATION_CLASSES": (
"fwd_api.filter.AuthFilter.AuthFilter",
),
'EXCEPTION_HANDLER': 'fwd_api.exception.exceptions_handler.custom_exception_handler',
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework_xml.renderers.XMLRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
}
SPECTACULAR_SETTINGS = {
2023-12-15 05:43:19 +00:00
'TITLE': 'OCR Engine for Invoices',
'DESCRIPTION': 'AI Powered by Samsung SDS Vietnam',
2023-11-30 11:19:06 +00:00
'VERSION': '1.0.0',
'SERVE_INCLUDE_SCHEMA': True,
# OTHER SETTINGS
'SWAGGER_UI_DIST': 'SIDECAR', # shorthand to use the sidecar instead
'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
'REDOC_DIST': 'SIDECAR',
# OTHER SETTINGS
"PREPROCESSING_HOOKS": ["fwd_api.api_specs.hooks.remove_apis_from_list"],
# Custom Spectacular Settings
"EXCLUDE_PATH": [reverse_lazy("schema")],
"EXCLUDE_RELATIVE_PATH": ["/rsa", '/gen-token', '/app/'],
2023-12-15 05:43:19 +00:00
"TAGS_SORTER": "alpha"
2023-11-30 11:19:06 +00:00
}
FILE_UPLOAD_HANDLERS = [
'django.core.files.uploadhandler.TemporaryFileUploadHandler',
]
CORS_ORIGIN_ALLOW_ALL = True
MEDIA_ROOT = env.str("MEDIA_ROOT", default=r"/var/www/example.com/media/")
2024-06-26 07:58:24 +00:00
LOG_ROOT = env.str("LOG_ROOT", default="/app/log")
2023-11-30 11:19:06 +00:00
BROKER_URL = env.str("BROKER_URL", default="amqp://test:test@107.120.70.226:5672//")
CELERY_TASK_TRACK_STARTED = True
# CELERY_TASK_TIME_LIMIT = 30 * 60
CELERY_TASK_TIME_LIMIT = None
2023-11-30 11:19:06 +00:00
2023-12-26 11:44:03 +00:00
MAX_UPLOAD_SIZE_OF_A_FILE = 5 * 1024 * 1024 # 5 MB
2023-11-30 11:19:06 +00:00
MAX_UPLOAD_FILE_SIZE_OF_A_REQUEST = 100 * 1024 * 1024 # 100 MB
2024-05-02 07:15:42 +00:00
MAX_UPLOAD_FILES_IN_A_REQUEST = 2
2023-12-11 13:15:48 +00:00
MAX_PIXEL_IN_A_FILE = 5000
2023-12-12 05:54:34 +00:00
TARGET_MAX_IMAGE_SIZE = (2048, 2048)
2023-11-30 11:19:06 +00:00
SIZE_TO_COMPRESS = 2 * 1024 * 1024
MAX_NUMBER_OF_TEMPLATE = 3
MAX_PAGES_OF_PDF_FILE = 50
2023-12-05 05:59:59 +00:00
2024-02-05 05:56:51 +00:00
OVERVIEW_REFRESH_INTERVAL = 2
2024-02-06 03:14:44 +00:00
OVERVIEW_REPORT_ROOT = "overview"
OVERVIEW_REPORT_DURATION = ["30d", "7d"]
2024-02-22 20:58:10 +00:00
ACC_EXCLUDE_RESEASONS = ["Invalid Input", "Handwritten information", "handwritten", "invalid_image", "missing_information", "too_blurry_text", "too_small_text"]
2024-02-18 14:52:23 +00:00
2024-02-06 03:14:44 +00:00
SUBS = {
"SEAU": "AU",
"SESP": "SG",
"SME": "MY",
"SEPCO": "PH",
"TSE": "TH",
"SEIN": "ID",
2024-02-22 20:58:10 +00:00
"ALL": "all", # all_detail
2024-02-23 08:50:52 +00:00
"SEAO": "seao"
2024-02-06 03:14:44 +00:00
}
2024-02-05 05:56:51 +00:00
2024-06-20 08:02:10 +00:00
FIELDS_BY_SUB = {
"SG": {"imei": ["imei_number"],
"invoice": ["retailername", "purchase_date"]},
"default": {"imei": ["imei_number"],
"invoice": ["retailername", "invoice_no", "purchase_date"]},
}
2024-02-28 11:45:10 +00:00
BAD_THRESHOLD = 0.75
NEED_REVIEW = 1.0
2024-12-02 07:19:04 +00:00
DOC_TYPES = ["imei", "invoice"]
2024-02-23 08:50:52 +00:00
SUB_FOR_BILLING = ["all", "seao"]
FIELD = ["imei_number", "purchase_date", "retailername", "sold_to_party", "invoice_no"]
2024-02-23 08:50:52 +00:00
2023-12-05 05:59:59 +00:00
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
2024-06-26 07:58:24 +00:00
}
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
2024-07-05 06:32:00 +00:00
'filters': {
'trace_id': {
'()': TraceIDLogFilter,
},
},
2024-06-26 07:58:24 +00:00
'formatters': {
'verbose': {
2024-07-05 06:32:00 +00:00
'format': f'{{asctime}} - {{levelname}} - Trace_ID: {{trace_id}} - {{module}} - {{message}}',
2024-06-26 07:58:24 +00:00
'style': '{',
},
'simple': {
2024-07-05 06:32:00 +00:00
'format': f'{{asctime}} - {{levelname}} - Trace_ID: {{trace_id}} - {{message}}',
2024-06-26 07:58:24 +00:00
'style': '{',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose',
2024-07-05 13:14:47 +00:00
'filters': ['trace_id'],
2024-06-26 07:58:24 +00:00
},
'file': {
"class": 'logging.handlers.TimedRotatingFileHandler',
'filename': f'{LOG_ROOT}/sbt_idp_BE.log', # Specify the path and filename for the log file
"when": "midnight",
"interval": 1,
'backupCount': 10,
'formatter': 'verbose',
2024-07-05 13:14:47 +00:00
'filters': ['trace_id'],
2024-06-26 07:58:24 +00:00
},
},
'loggers': {
'django': {
'handlers': ['console', 'file'],
'level': 'INFO',
},
'': {
'handlers': ['console', 'file'],
'level': 'INFO',
}
},
2024-07-17 10:44:40 +00:00
}
REASON_SOLUTION_MAP = {"Invalid image": "Remove this image from the evaluation report",
"Missing information": "Remove this image from the evaluation report",
"Too blurry text": "Remove this image from the evaluation report",
"Too small text": "Remove this image from the evaluation report",
"Handwritten": "Remove this image from the evaluation report",
"Wrong feedback": "Update revised resutl and re-calculate accuracy",
"Ocr cannot extract": "Improve OCR",
}
IMEI_MAX_LENGHT = 14