gold eggs backend
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
This commit is contained in:
6
core/utils/__init__.py
Executable file
6
core/utils/__init__.py
Executable file
@@ -0,0 +1,6 @@
|
||||
from .cache import * # noqa
|
||||
from .console import * # noqa
|
||||
from .core import * # noqa
|
||||
from .dd import * # noqa
|
||||
from .exception import * # noqa
|
||||
from .factory import * # noqa
|
||||
17
core/utils/cache.py
Executable file
17
core/utils/cache.py
Executable file
@@ -0,0 +1,17 @@
|
||||
import hashlib
|
||||
from django.core.cache import cache
|
||||
|
||||
from common.env import env
|
||||
|
||||
|
||||
class Cache:
|
||||
def remember(self, func, key: str):
|
||||
cache_enabled = env("CACHE_ENABLED")
|
||||
key = hashlib.md5(key.encode("utf-8")).hexdigest()
|
||||
response = cache.get(key)
|
||||
|
||||
if (response is None) or cache_enabled:
|
||||
response = func()
|
||||
cache.set(key, response, env("CACHE_TIME"))
|
||||
|
||||
return response
|
||||
65
core/utils/console.py
Executable file
65
core/utils/console.py
Executable file
@@ -0,0 +1,65 @@
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import management
|
||||
|
||||
|
||||
class Console(management.BaseCommand):
|
||||
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):
|
||||
self.get_stdout().write(self.get_style().SUCCESS(message))
|
||||
|
||||
def error(self, message):
|
||||
self.get_stdout().write(self.get_style().ERROR(message))
|
||||
|
||||
def log(self, message):
|
||||
self.get_stdout().write(
|
||||
self.get_style().ERROR(
|
||||
"\n====================\n{}\n====================\n".format(
|
||||
message
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class BaseMake(management.BaseCommand):
|
||||
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")
|
||||
with open(
|
||||
os.path.join(settings.BASE_DIR, f"stub/{self.path}.stub")
|
||||
) as stub: # noqa
|
||||
data = stub.read()
|
||||
stub.close()
|
||||
stub = data.replace("{{name}}", name)
|
||||
|
||||
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))
|
||||
|
||||
with open(
|
||||
os.path.join(core_http_path, f"{self.path}/{name.lower()}.py"),
|
||||
"w+",
|
||||
) as file: # noqa
|
||||
file.write(stub)
|
||||
|
||||
self.console.success(f"{self.name} created")
|
||||
6
core/utils/core.py
Executable file
6
core/utils/core.py
Executable file
@@ -0,0 +1,6 @@
|
||||
class Helper:
|
||||
"""
|
||||
Helper class to handle index
|
||||
"""
|
||||
|
||||
pass
|
||||
12
core/utils/dd.py
Executable file
12
core/utils/dd.py
Executable file
@@ -0,0 +1,12 @@
|
||||
from core import exceptions
|
||||
|
||||
|
||||
def dd(
|
||||
*args, message: str | None = None, data: dict | list | None = None
|
||||
) -> None:
|
||||
"""
|
||||
Dump the given variables and then raise a SystemExit exception
|
||||
to stop execution of the script.
|
||||
"""
|
||||
|
||||
raise exceptions.BreakException(args, message=message, data=data)
|
||||
29
core/utils/exception.py
Executable file
29
core/utils/exception.py
Executable file
@@ -0,0 +1,29 @@
|
||||
from rest_framework import status
|
||||
|
||||
from core import exceptions
|
||||
|
||||
|
||||
class ResponseException:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message="",
|
||||
data=None,
|
||||
error_code=0,
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
exception=None,
|
||||
**kwargs
|
||||
):
|
||||
if isinstance(exception, exceptions.BreakException):
|
||||
raise exception
|
||||
|
||||
if data is None:
|
||||
data = []
|
||||
response = {
|
||||
"success": False,
|
||||
"message": message,
|
||||
"data": data,
|
||||
"error_code": error_code,
|
||||
**kwargs,
|
||||
}
|
||||
raise exceptions.MyApiException(response, status_code)
|
||||
7
core/utils/factory.py
Executable file
7
core/utils/factory.py
Executable file
@@ -0,0 +1,7 @@
|
||||
import abc
|
||||
import faker
|
||||
|
||||
|
||||
class BaseFaker(abc.ABC):
|
||||
def __init__(self):
|
||||
self.faker = faker.Faker()
|
||||
Reference in New Issue
Block a user