mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-06-28 15:53:57 +00:00
Initial commit
This commit is contained in:
0
apibase/__init__.py
Normal file
0
apibase/__init__.py
Normal file
16
apibase/asgi.py
Normal file
16
apibase/asgi.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""
|
||||
ASGI config for apibase project.
|
||||
|
||||
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apibase.settings")
|
||||
|
||||
application = get_asgi_application()
|
355
apibase/settings.py
Normal file
355
apibase/settings.py
Normal file
@ -0,0 +1,355 @@
|
||||
"""
|
||||
Django settings for apibase project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 5.0.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.0/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/5.0/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import datetime
|
||||
from rest_framework.settings import api_settings
|
||||
from decouple import config
|
||||
from django.utils.log import DEFAULT_LOGGING
|
||||
import logging.config
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = config("SECRET_KEY")
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = config("DEBUG", cast=bool)
|
||||
|
||||
if not DEBUG:
|
||||
ALLOWED_HOSTS = str(config("DJANGO_ALLOWED_HOSTS", cast=str)).split(" ")
|
||||
if DEBUG:
|
||||
INTERNAL_IPS = [
|
||||
"127.0.0.1",
|
||||
]
|
||||
|
||||
# CORS
|
||||
# CORS_ALLOWED_ORIGINS = ["http://localhost:3000", "http://127.0.0.1:3000"]
|
||||
# CORS_ALLOWED_ORIGIN_REGEXES
|
||||
CORS_ALLOW_ALL_ORIGINS = True
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"django.contrib.admin",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"rest_framework",
|
||||
"django_rest_passwordreset",
|
||||
"djangopasswordlessknox",
|
||||
"django_extensions",
|
||||
"django_seed",
|
||||
"storages",
|
||||
"whitenoise.runserver_nostatic",
|
||||
"django.contrib.staticfiles",
|
||||
# applications
|
||||
"api",
|
||||
"billing",
|
||||
"devices",
|
||||
# Authentication
|
||||
"knox",
|
||||
# third party
|
||||
"django_filters",
|
||||
"corsheaders",
|
||||
]
|
||||
|
||||
if DEBUG:
|
||||
INSTALLED_APPS.append("debug_toolbar")
|
||||
INSTALLED_APPS.append("drf_spectacular")
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"whitenoise.middleware.WhiteNoiseMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"corsheaders.middleware.CorsMiddleware",
|
||||
"easyaudit.middleware.easyaudit.EasyAuditMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
]
|
||||
|
||||
if DEBUG:
|
||||
MIDDLEWARE.append(
|
||||
"debug_toolbar.middleware.DebugToolbarMiddleware",
|
||||
)
|
||||
|
||||
ROOT_URLCONF = "apibase.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 = "apibase.wsgi.application"
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
|
||||
|
||||
|
||||
if not DEBUG:
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.postgresql",
|
||||
"NAME": config("POSTGRES_DATABASE"),
|
||||
"USER": config("POSTGRES_USER"),
|
||||
"PASSWORD": config("POSTGRES_PASSWORD"),
|
||||
"HOST": config("POSTGRES_HOST"),
|
||||
"PORT": config("POSTGRES_PORT"),
|
||||
"OPTIONS": {
|
||||
"pool": True,
|
||||
},
|
||||
},
|
||||
}
|
||||
else:
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": BASE_DIR / "db.sqlite3",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if not DEBUG:
|
||||
CACHES = {
|
||||
"default": {
|
||||
"BACKEND": "django_redis.cache.RedisCache",
|
||||
"LOCATION": "redis://redis:6379/",
|
||||
"OPTIONS": {
|
||||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
|
||||
|
||||
# Uses email as username for login
|
||||
AUTH_USER_MODEL = "api.User"
|
||||
AUTHENTICATION_BACKENDS = ["api.backends.EmailBackend"]
|
||||
|
||||
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/5.0/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = "en-us"
|
||||
|
||||
TIME_ZONE = "UTC"
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/5.0/howto/static-files/
|
||||
|
||||
STATIC_URL = "/static/"
|
||||
STATIC_ROOT = BASE_DIR / "staticfiles"
|
||||
STATICFILES_DIRS = [
|
||||
# os.path.join(BASE_DIR, 'static'),
|
||||
]
|
||||
|
||||
|
||||
MEDIA_URL = "/media/"
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
|
||||
|
||||
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
"DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"],
|
||||
"DEFAULT_AUTHENTICATION_CLASSES": [
|
||||
"knox.auth.TokenAuthentication",
|
||||
# "api.authentication.TokenAuthentication",
|
||||
# "api.authentication.TokenAuthSupportCookie",
|
||||
# "rest_framework.authentication.SessionAuthentication",
|
||||
# "rest_framework_simplejwt.authentication.JWTAuthentication",
|
||||
],
|
||||
"DEFAULT_PERMISSION_CLASSES": [
|
||||
"rest_framework.permissions.IsAuthenticatedOrReadOnly"
|
||||
],
|
||||
"DEFAULT_PAGINATION_CLASS": "api.pagination.CustomPagination",
|
||||
"PAGE_SIZE": 10,
|
||||
"DEFAULT_THROTTLE_CLASSES": [
|
||||
"rest_framework.throttling.AnonRateThrottle",
|
||||
"rest_framework.throttling.UserRateThrottle",
|
||||
"rest_framework.throttling.ScopedRateThrottle",
|
||||
],
|
||||
"DEFAULT_THROTTLE_RATES": {
|
||||
"anon": "100/day",
|
||||
"user": "1000/min",
|
||||
"login": "1000/min",
|
||||
},
|
||||
"EXCEPTION_HANDLER": "api.exceptions.custom_exception_handler",
|
||||
"DEFAULT_RENDERER_CLASSES": ("rest_framework.renderers.JSONRenderer",),
|
||||
# "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema"
|
||||
}
|
||||
|
||||
|
||||
REST_KNOX = {
|
||||
"SECURE_HASH_ALGORITHM": "cryptography.hazmat.primitives.hashes.SHA512",
|
||||
"AUTH_TOKEN_CHARACTER_LENGTH": 64,
|
||||
"TOKEN_TTL": datetime.timedelta(minutes=30),
|
||||
"USER_SERIALIZER": "api.serializers.CustomUserSerializer",
|
||||
"TOKEN_LIMIT_PER_USER": 10,
|
||||
"AUTO_REFRESH": True,
|
||||
"EXPIRY_DATETIME_FORMAT": api_settings.DATETIME_FORMAT,
|
||||
}
|
||||
|
||||
|
||||
DJANGORESIZED_DEFAULT_SIZE = [500, 500]
|
||||
DJANGORESIZED_DEFAULT_QUALITY = 75
|
||||
DJANGORESIZED_DEFAULT_KEEP_META = True
|
||||
DJANGORESIZED_DEFAULT_FORCE_FORMAT = "JPEG"
|
||||
DJANGORESIZED_DEFAULT_FORMAT_EXTENSIONS = {"JPEG": ".jpg"}
|
||||
DJANGORESIZED_DEFAULT_NORMALIZE_ROTATION = True
|
||||
|
||||
|
||||
AWS_ACCESS_KEY_ID = config("AWS_ACCESS_KEY_ID")
|
||||
AWS_SECRET_ACCESS_KEY = config("AWS_ACCESS_SECRET")
|
||||
AWS_STORAGE_BUCKET_NAME = config("AWS_BUCKET")
|
||||
AWS_S3_FILE_OVERWRITE = False
|
||||
AWS_DEFAULT_ACL = None
|
||||
DEFAULT_FILE_STORAGE = "storages.backends.s3.S3Storage"
|
||||
STORAGES = {
|
||||
"default": {
|
||||
"BACKEND": "storages.backends.s3.S3Storage",
|
||||
},
|
||||
"staticfiles": {
|
||||
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
|
||||
},
|
||||
}
|
||||
|
||||
LOGGING_CONFIG = None
|
||||
|
||||
LOGLEVEL = os.environ.get("LOGLEVEL", "warning").upper()
|
||||
|
||||
logging.config.dictConfig(
|
||||
{
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"default": {
|
||||
"format": "%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
|
||||
},
|
||||
"request": {
|
||||
"format": "%(asctime)s %(levelname)s %(message)s",
|
||||
},
|
||||
"django.server": DEFAULT_LOGGING["formatters"]["django.server"],
|
||||
},
|
||||
"handlers": {
|
||||
"console": {
|
||||
"class": "logging.StreamHandler",
|
||||
"formatter": "default",
|
||||
},
|
||||
"request": {
|
||||
"class": "logging.StreamHandler",
|
||||
"formatter": "request",
|
||||
},
|
||||
"django.server": DEFAULT_LOGGING["handlers"]["django.server"],
|
||||
},
|
||||
"loggers": {
|
||||
"": {
|
||||
"level": "WARNING",
|
||||
"handlers": ["console"],
|
||||
},
|
||||
"app": {
|
||||
"level": LOGLEVEL,
|
||||
"handlers": ["console"],
|
||||
"propagate": False,
|
||||
},
|
||||
"django.request": {
|
||||
"level": "WARNING",
|
||||
"handlers": ["request"],
|
||||
"propagate": False,
|
||||
},
|
||||
"django.server": DEFAULT_LOGGING["loggers"]["django.server"],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if not DEBUG:
|
||||
SECURE_SSL_REDIRECT = True
|
||||
SESSION_COOKIE_SECURE = True
|
||||
CSRF_COOKIE_SECURE = True
|
||||
SECURE_HSTS_SECONDS = config("SECURE_HSTS_SECONDS", cast=int)
|
||||
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
|
||||
SECURE_HSTS_PRELOAD = True
|
||||
CSRF_TRUSTED_ORIGINS = [config("CSRF_ALLOWED_HOST")]
|
||||
CSRF_COOKIE_DOMAIN = config("CSRF_COOKIE_DOMAIN")
|
||||
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
|
||||
|
||||
|
||||
EMAIL_BACKEND = (
|
||||
"django.core.mail.backends.smtp.EmailBackend" # Replace with your preferred backend
|
||||
)
|
||||
EMAIL_HOST = config("EMAIL_HOSTNAME")
|
||||
EMAIL_PORT = config("EMAIL_PORT", cast=int)
|
||||
EMAIL_HOST_USER = config("EMAIL_USERNAME")
|
||||
EMAIL_HOST_PASSWORD = config("EMAIL_PASSWORD")
|
||||
# DEFAULT_FROM_EMAIL = "noreply@sarlink.net"
|
||||
EMAIL_USE_TLS = True
|
||||
|
||||
|
||||
PASSWORDLESS_AUTH = {
|
||||
# 'PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME': "password_reset_email.html",
|
||||
"PASSWORDLESS_AUTH_TYPES": ["EMAIL", "MOBILE"],
|
||||
"PASSWORDLESS_USER_MOBILE_FIELD_NAME": "mobile",
|
||||
"PASSWORDLESS_TEST_SUPPRESSION": False,
|
||||
"PASSWORDLESS_REGISTER_NEW_USERS": True,
|
||||
"PASSWORDLESS_EMAIL_NOREPLY_ADDRESS": "noreply@sarlink.net",
|
||||
}
|
57
apibase/urls.py
Normal file
57
apibase/urls.py
Normal file
@ -0,0 +1,57 @@
|
||||
"""
|
||||
URL configuration for apibase project.
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/5.0/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
from drf_spectacular.views import (
|
||||
SpectacularAPIView,
|
||||
SpectacularRedocView,
|
||||
SpectacularSwaggerView,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path("admin/", admin.site.urls),
|
||||
path(
|
||||
"api/password_reset/",
|
||||
include("django_rest_passwordreset.urls", namespace="password_reset"),
|
||||
),
|
||||
path("", include("djangopasswordlessknox.urls")),
|
||||
# Authentication
|
||||
path("api/auth/", include("api.urls")),
|
||||
# Devices
|
||||
path("api/devices/", include("devices.urls")),
|
||||
|
||||
]
|
||||
if settings.DEBUG:
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
urlpatterns += (path("api-auth/", include("rest_framework.urls")),)
|
||||
urlpatterns += (path("__debug__/", include("debug_toolbar.urls")),)
|
||||
urlpatterns += (path("api/schema/", SpectacularAPIView.as_view(), name="schema"),)
|
||||
urlpatterns += (
|
||||
path(
|
||||
"api/swagger/swagger-ui/",
|
||||
SpectacularSwaggerView.as_view(),
|
||||
name="swagger-ui",
|
||||
),
|
||||
)
|
||||
urlpatterns += (
|
||||
path(
|
||||
"api/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"
|
||||
),
|
||||
)
|
16
apibase/wsgi.py
Normal file
16
apibase/wsgi.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for apibase project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apibase.settings")
|
||||
|
||||
application = get_wsgi_application()
|
Reference in New Issue
Block a user