initial commit
This commit is contained in:
3
core/utils/__init__.py
Normal file
3
core/utils/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .cache import * # noqa
|
||||
from .console import * # noqa
|
||||
from .core import * # noqa
|
||||
28
core/utils/base_model.py
Normal file
28
core/utils/base_model.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import uuid
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.db import models
|
||||
|
||||
from django_core.models.base import AbstractBaseModel # type: ignore
|
||||
|
||||
|
||||
class UUIDPrimaryKeyBaseModel(AbstractBaseModel):
|
||||
id = models.UUIDField(
|
||||
verbose_name=_("ID"),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
)
|
||||
|
||||
created_at = models.DateTimeField(
|
||||
verbose_name=_("Created At"),
|
||||
auto_now_add=True
|
||||
)
|
||||
|
||||
updated_at = models.DateTimeField(
|
||||
verbose_name=_("Updated At"),
|
||||
auto_now=True
|
||||
)
|
||||
|
||||
class Meta: # type: ignore # noqa
|
||||
abstract = True
|
||||
18
core/utils/cache.py
Normal file
18
core/utils/cache.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import hashlib
|
||||
|
||||
from django.core.cache import cache
|
||||
|
||||
from config.env import env
|
||||
|
||||
|
||||
class Cache:
|
||||
def remember(self, func, key: str, timeout=None, *args, **kwargs):
|
||||
cache_enabled = env.bool("CACHE_ENABLED")
|
||||
key = hashlib.md5(key.encode("utf-8")).hexdigest()
|
||||
response = cache.get(key)
|
||||
if not cache_enabled:
|
||||
return func(*args, **kwargs)
|
||||
elif response is None:
|
||||
response = func(*args, **kwargs)
|
||||
cache.set(key, response, env.int("CACHE_TIME") if timeout is None else timeout)
|
||||
return response
|
||||
79
core/utils/console.py
Normal file
79
core/utils/console.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Union
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import management
|
||||
|
||||
|
||||
class Console(management.BaseCommand):
|
||||
"""
|
||||
Console logging class
|
||||
"""
|
||||
|
||||
def get_stdout(self):
|
||||
base_command = management.BaseCommand()
|
||||
return base_command.stdout
|
||||
|
||||
def get_style(self):
|
||||
base_command = management.BaseCommand()
|
||||
return base_command.style
|
||||
|
||||
def success(self, message):
|
||||
logging.debug(message)
|
||||
self.get_stdout().write(self.get_style().SUCCESS(message))
|
||||
|
||||
def error(self, message):
|
||||
logging.error(message)
|
||||
self.get_stdout().write(self.get_style().ERROR(message))
|
||||
|
||||
def log(self, message):
|
||||
self.get_stdout().write(
|
||||
self.get_style().ERROR(
|
||||
"\n{line}\n{message}\n{line}\n".format(
|
||||
message=message, line="=" * len(message)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class BaseMake(management.BaseCommand):
|
||||
path: str
|
||||
|
||||
def __init__(self, *args, **options):
|
||||
super().__init__(*args, **options)
|
||||
self.console = Console()
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument("name")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
name = options.get("name")
|
||||
if name is None:
|
||||
name = ""
|
||||
|
||||
stub = open(os.path.join(settings.BASE_DIR, f"resources/stub/{self.path}.stub"))
|
||||
data: Union[Any] = stub.read()
|
||||
stub.close()
|
||||
|
||||
stub = data.replace("{{name}}", name or "")
|
||||
|
||||
|
||||
core_http_path = os.path.join(settings.BASE_DIR, "core/http")
|
||||
if os.path.exists(
|
||||
os.path.join(core_http_path, f"{self.path}/{name.lower()}.py")
|
||||
): # noqa
|
||||
self.console.error(f"{self.name} already exists")
|
||||
return
|
||||
|
||||
if not os.path.exists(os.path.join(core_http_path, self.path)):
|
||||
os.makedirs(os.path.join(core_http_path, self.path))
|
||||
|
||||
file = open(
|
||||
os.path.join(core_http_path, f"{self.path}/{name.lower()}.py"),
|
||||
"w+",
|
||||
)
|
||||
file.write(stub) # type: ignore
|
||||
file.close()
|
||||
|
||||
self.console.success(f"{self.name} created")
|
||||
6
core/utils/core.py
Normal file
6
core/utils/core.py
Normal file
@@ -0,0 +1,6 @@
|
||||
class Helper:
|
||||
"""
|
||||
Helper class to handle index
|
||||
"""
|
||||
|
||||
pass
|
||||
33
core/utils/storage.py
Normal file
33
core/utils/storage.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from typing import Optional, Union
|
||||
|
||||
from config.env import env
|
||||
|
||||
|
||||
class Storage:
|
||||
|
||||
storages = ["AWS", "MINIO", "FILE", "STATIC"]
|
||||
|
||||
def __init__(self, storage: Union[str], storage_type: Union[str] = "default") -> None:
|
||||
self.storage = storage
|
||||
self.sorage_type = storage_type
|
||||
if storage not in self.storages:
|
||||
raise ValueError(f"Invalid storage type: {storage}")
|
||||
|
||||
def get_backend(self) -> Optional[str]:
|
||||
match self.storage:
|
||||
case "AWS" | "MINIO":
|
||||
return "storages.backends.s3boto3.S3Boto3Storage"
|
||||
case "FILE":
|
||||
return "django.core.files.storage.FileSystemStorage"
|
||||
case "STATIC":
|
||||
return "django.contrib.staticfiles.storage.StaticFilesStorage"
|
||||
|
||||
def get_options(self) -> Optional[dict[str, object]]:
|
||||
match self.storage:
|
||||
case "AWS" | "MINIO":
|
||||
if self.sorage_type == "default":
|
||||
return {"bucket_name": env.str("STORAGE_BUCKET_MEDIA")}
|
||||
elif self.sorage_type == "static":
|
||||
return {"bucket_name": env.str("STORAGE_BUCKET_STATIC")}
|
||||
case _:
|
||||
return {}
|
||||
Reference in New Issue
Block a user