init
This commit is contained in:
0
src/application/__init__.py
Normal file
0
src/application/__init__.py
Normal file
3
src/application/admin.py
Normal file
3
src/application/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
src/application/apps.py
Normal file
6
src/application/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MainConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'application'
|
||||
0
src/application/management/__init__.py
Normal file
0
src/application/management/__init__.py
Normal file
0
src/application/management/commands/__init__.py
Normal file
0
src/application/management/commands/__init__.py
Normal file
32
src/application/management/commands/update_admin.py
Normal file
32
src/application/management/commands/update_admin.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
||||
def add_arguments(self, parser):
|
||||
# Positional arguments
|
||||
parser.add_argument('username', type=str)
|
||||
parser.add_argument('password', type=str)
|
||||
parser.add_argument('--email', type=str, default='')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
user_model = get_user_model()
|
||||
message = None
|
||||
try:
|
||||
user = user_model.objects.get(username=options["username"])
|
||||
if not user.check_password(options["password"]):
|
||||
user.set_password(options["password"])
|
||||
user.save()
|
||||
message = "[INFO] Admin password has been updated"
|
||||
except user_model.DoesNotExist:
|
||||
user = user_model(
|
||||
username=options["username"],
|
||||
email=options["email"],
|
||||
is_superuser=True,
|
||||
is_staff=True
|
||||
)
|
||||
user.set_password(options["password"])
|
||||
user.save()
|
||||
message = "[INFO] Admin has been created"
|
||||
return message
|
||||
0
src/application/migrations/__init__.py
Normal file
0
src/application/migrations/__init__.py
Normal file
3
src/application/models.py
Normal file
3
src/application/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
src/application/routers/__init__.py
Normal file
3
src/application/routers/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
14
src/application/routers/api.py
Normal file
14
src/application/routers/api.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from starlette import status
|
||||
from starlette.responses import JSONResponse
|
||||
|
||||
from . import router
|
||||
|
||||
|
||||
@router.get("/example")
|
||||
async def example():
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_200_OK,
|
||||
content={
|
||||
"status": "OK"
|
||||
}
|
||||
)
|
||||
8
src/cache/__init__.py
vendored
Normal file
8
src/cache/__init__.py
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.conf import settings
|
||||
|
||||
from cache.provider import RedisOverride
|
||||
|
||||
redis = RedisOverride(
|
||||
host=settings.REDIS_HOST, port=settings.REDIS_PORT,
|
||||
password=settings.REDIS_PASSWORD
|
||||
)
|
||||
32
src/cache/provider.py
vendored
Normal file
32
src/cache/provider.py
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
import contextlib
|
||||
import json
|
||||
from typing import Union
|
||||
|
||||
from redis import Redis
|
||||
|
||||
|
||||
class RedisOverride:
|
||||
"""
|
||||
redis init
|
||||
"""
|
||||
|
||||
def __init__(self, host: str, port: int, password: str):
|
||||
self.redis = Redis(host=host, port=port, password=password)
|
||||
|
||||
def set(self, key: str, value: Union[str, dict, list, int]):
|
||||
if type(value) is not str:
|
||||
value = json.dumps(value)
|
||||
self.redis.set(key, value)
|
||||
|
||||
def get(self, key: str, default=None) -> Union[str, dict, int, list, None]:
|
||||
value = self.redis.get(key)
|
||||
if value is None:
|
||||
return default
|
||||
|
||||
value = value.decode("utf-8")
|
||||
with contextlib.suppress(Exception):
|
||||
value = json.loads(value)
|
||||
return value
|
||||
|
||||
def delete(self, key: str):
|
||||
self.redis.delete(key)
|
||||
0
src/core/__init__.py
Normal file
0
src/core/__init__.py
Normal file
31
src/core/asgi.py
Normal file
31
src/core/asgi.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
ASGI config for core 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/3.2/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
|
||||
application = get_asgi_application()
|
||||
|
||||
from application.routers.api import router
|
||||
|
||||
|
||||
fastapp = FastAPI()
|
||||
|
||||
fastapp.include_router(router)
|
||||
fastapp.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
100
src/core/settings.py
Normal file
100
src/core/settings.py
Normal file
@@ -0,0 +1,100 @@
|
||||
import os
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
SECRET_KEY = os.getenv("SECRET_KEY")
|
||||
|
||||
DEBUG = bool(int(os.getenv("DEBUG", 1)))
|
||||
|
||||
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "*").split(" ")
|
||||
|
||||
CSRF_TRUSTED_ORIGINS = os.getenv("CSRF_TRUSTED_ORIGINS", "http://* https://*").split(" ")
|
||||
|
||||
REDIS_HOST = os.getenv("REDIS_HOST")
|
||||
REDIS_PORT = int(os.getenv("REDIS_PORT"))
|
||||
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD")
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'application'
|
||||
]
|
||||
|
||||
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',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'core.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'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 = 'core.wsgi.application'
|
||||
ASGI_APPLICATION = "core.asgi.application"
|
||||
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.postgresql_psycopg2",
|
||||
"NAME": os.getenv("DB_NAME"),
|
||||
"USER": os.getenv("DB_USER"),
|
||||
"PASSWORD": os.getenv("DB_PASSWORD"),
|
||||
"HOST": os.getenv("DB_HOST"),
|
||||
"PORT": os.getenv("DB_PORT"),
|
||||
}
|
||||
}
|
||||
|
||||
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',
|
||||
},
|
||||
]
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
|
||||
21
src/core/urls.py
Normal file
21
src/core/urls.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""core URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/3.2/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
|
||||
|
||||
urlpatterns = [
|
||||
path('django/admin/', admin.site.urls),
|
||||
]
|
||||
16
src/core/wsgi.py
Normal file
16
src/core/wsgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for core 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/3.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
0
src/helpers/__init__.py
Normal file
0
src/helpers/__init__.py
Normal file
0
src/helpers/models/__init__.py
Normal file
0
src/helpers/models/__init__.py
Normal file
37
src/helpers/models/fields.py
Normal file
37
src/helpers/models/fields.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import json
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class JSONField(models.TextField):
|
||||
"""JSONField is a generic textfield that neatly serializes/unserializes
|
||||
JSON objects seamlessly"""
|
||||
|
||||
def to_python(self, value):
|
||||
"""Convert our string value to JSON after we load it from the DB"""
|
||||
|
||||
if value == "" or value is None:
|
||||
return None
|
||||
elif isinstance(value, str):
|
||||
value = json.loads(value)
|
||||
else:
|
||||
raise TypeError("Not valid value type in db")
|
||||
|
||||
return value
|
||||
|
||||
def from_db_value(self, value, expression, connection):
|
||||
return self.to_python(value)
|
||||
|
||||
def get_prep_value(self, value):
|
||||
"""Convert our JSON object to a string before we save"""
|
||||
|
||||
if isinstance(value, (dict, list)):
|
||||
value = json.dumps(value)
|
||||
elif value == "" or value is None:
|
||||
pass
|
||||
else:
|
||||
raise TypeError(f"Not valid value type. ValueType={type(value)}")
|
||||
return value
|
||||
|
||||
def value_from_object(self, obj):
|
||||
return json.dumps(super().value_from_object(obj))
|
||||
22
src/manage.py
Normal file
22
src/manage.py
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Run administrative tasks."""
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
5
src/requirements.txt
Normal file
5
src/requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
uvicorn==0.27.1
|
||||
fastapi==0.109.0
|
||||
Django==5.0.2
|
||||
psycopg2
|
||||
redis==4.6.0
|
||||
Reference in New Issue
Block a user