Skip to main content

TerminatorCore

Django的增强代码,不用手动添加路由,自带crud,代码生成器,简单的权限管理类,帮助缩减开发时间 仅限Django框架

1. 创建Django项目

2. pip install TerminatorCore

3. 配置

  • 在settings.py中引入TerminatorBaseCore

INSTALLED_APPS = [
    'TerminatorBaseCore',
]
  • 在urls.py中加入配置

load_custom_viewsets_from_directory方法directory参数为需要对外暴露的文件路径

'my_project/expose'意思为扫描该路径下的所有文件

from TerminatorBaseCore.route.load_custom_viewsets_from_directory import load_custom_viewsets_from_directory

urlpatterns = [
    *load_custom_viewsets_from_directory('my_project/expose'),
]
  • 使用基础代码生成器

from TerminatorBaseCore.utils.entity_export_util import generate_model_code

if __name__ == '__main__':
    generate_model_code('uploadfile_image', 'project_env')

这里generate_model_code有以下参数
table_name 数据库表名
project_name 配置项

生成器会在系统文档中生成model,expose,service文件,然后将文件复制到项目对应目录下即可

功能一

settings.py中加入,即可开启接口权限校验

PERMISSION_PATH = 'TerminatorBaseCore.service.authenticated_with_redis.AuthenticatedWithRedis'
from TerminatorBaseCore.utils.token_manger import TokenManager

# 通过TokenManager实现token的生成校验
# 用户登录成功后为其生成一个token
def user_login(request):
    user_id = 1
    email = "11"
    token = TokenManager().generate_token(user_id, email)
    
    # 为request赋予一个new_token属性,在返回结果后如果有这个属性会自动在响应头X-Token加入token,前端监听响应头中X-Token属性,有值则更新本地token
    request.new_token = token

    # token随请求头的Authorization传递

功能二

自定义异常处理方案

import time
import json
from TerminatorBaseCore.components.dynamic_call import ServiceExceptionAfterHandle, InfoExceptionAfterHandle, ExceptionAfterHandle
from TerminatorBaseCore.components.logger_util import api_log


class ServiceExceptionAfter(ServiceExceptionAfterHandle):
    def execute(self, *arg, **keyword):
        request = arg[0]
        error_msg = keyword.get("message", None)
        stack_info = keyword.get("stack_info", None)
        logging(request, error_msg, stack_info)


class InfoExceptionAfter(InfoExceptionAfterHandle):
    def execute(self, *arg, **keyword):
        request = arg[0]
        error_msg = keyword.get("message", None)
        logging(request, error_msg)


class ExceptionAfter(ExceptionAfterHandle):
    def execute(self, *arg, **keyword):
        request = arg[0]
        error_msg = keyword.get("message", None)
        stack_info = keyword.get("stack_info", None)
        logging(request, error_msg, stack_info, "error")


def logging(request, error_msg, stack_info=None, level="warning"):
    path = request.path
    headers = request.headers
    method = request.method

    log_data = {
        "method": method,
        "path_url": path,
        "error_msg": error_msg,
        "date_time": time.time(),
        "head": dict(headers)
    }

    if method == 'GET':
        query = request.GET
    else:
        query = request.POST

    if query:
        log_data.update({
            "query": json.dumps(query.dict())
        })

    if stack_info:
        log_data.update({
            "stack_info": stack_info
        })

    if level == "error":
        api_log.error(log_data)
    else:
        api_log.warning(log_data)

Metadata

Release files for TerminatorCore 0.1.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for TerminatorCore 0.1.19
File Size Uploaded Provenance Actions
terminatorcore-0.1.19.tar.gz 22.9 kB Without provenance Details for terminatorcore-0.1.19.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for TerminatorCore 0.1.19
File Interpreter ABI Platform Actions
terminatorcore-0.1.19-py3-none-any.whl Python 3 none any Details for terminatorcore-0.1.19-py3-none-any.whl

Total release size: 57.8 kB

Release files / terminatorcore-0.1.19.tar.gz

Download URL terminatorcore-0.1.19.tar.gz
Size 22.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b66c454fcaf4e592278ac5589c1e1d08debf0a87b302dc4d7d299a6d17da6b32
BLAKE2b-256 checksum
How to use checksums
a3e362e6f61e460eaaa2e494026ddae3b8a3879bad5039515a54a858ab01b3f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release files / terminatorcore-0.1.19-py3-none-any.whl

Download URL terminatorcore-0.1.19-py3-none-any.whl
Size 34.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9997e348cdc84d7cdc0e52fe2cf32eb1f9085827b962cb831571c4b93d5f0c9e
BLAKE2b-256 checksum
How to use checksums
c1d28f6a8b857af2cdbb5b131cc583219fb2ba036059735a6be04cde788805bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Security for TerminatorCore 0.1.19

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page