Skip to main content

snowland-djangohelper

version gitee download wheel status

Django 与 Django REST Framework(DRF)实用工具 / 扩展库。


目录


简介

snowland-djangohelper 是一套面向 Django / Django REST Framework 的辅助工具集,封装了常用的认证、加密、分页、统一响应、权限以及通用 Model 等功能,帮助开发者快速搭建规范、一致的 Django 后端服务。


特性

  • 支持 国密 SM3 系列密码哈希器(兼容 Django 密码框架)
  • 统一的 API 响应封装 APIResponse
  • 扩展的 DRF 分页与 ModelViewSet
  • 角色化权限控制(管理员 / 审核员 / 操作员 / 超级用户)
  • 通用 BaseModel(自动时间戳,new / update / one / all / filter 便捷方法)
  • 随机字符串 / 十六进制字符串生成工具
  • 项目源码打包工具(用于软著申请等场景)

安装

从 PyPI 安装

pip install snowland-djangohelper

如需使用 BCrypt-SM3 密码哈希器,可安装 bcrypt 可选依赖:

pip install snowland-djangohelper[bcrypt]

从源码安装

# 从 https://gitee.com/snowlandltd/snowland-djangohelper 下载代码(可选择 release 版本)
pip install .

注意:本项目需要 Python 3.6+ 与 Django 2.0+(兼容至 Django 5.1)。


模块说明

模块 说明
djangohelper.auth 基于国密 SM3 的密码哈希器(SM3 / PBKDF2-SM3 / BCrypt-SM3 等)。
djangohelper.contrib 通用业务应用,如 developerfriendlinksnowlandauth 等。
djangohelper.db 对 Django 数据库能力的扩展(连接管理、BaseModel 基础模型等)。
djangohelper.pagination 扩展的 DRF 分页(BasePageNumberPagination)。
djangohelper.permissions 角色化权限类(AdminPermission / AuditPermission / OperatorPermission / SuperuserPermission / AllPermission)。
djangohelper.requests HTTP 请求辅助(短信接口等)。
djangohelper.responses 统一的 API 响应封装 APIResponse
djangohelper.viewhelper 分页结果生成工具 generate_paginator_result
djangohelper.viewsets 扩展的 DRF ModelViewSetSelfModelViewSet)。
djangohelper.views 通用视图模板。
djangohelper.utils 工具函数:国密 crypto、正则辅助 regex_helper 等。
djangohelper.common 全局定义(错误码、允许的文件扩展名、JWT payload 示例等)。
djangohelper.number_tool 随机字符串 / 十六进制字符串生成。
djangohelper.project2lines 将项目源码打印为单文件(用于软著申请)。

快速上手

统一响应

from djangohelper.responses import APIResponse

def my_view(request):
    data = {"hello": "world"}
    return APIResponse(data=data, message="success")

响应结构:

{
  "successful": true,
  "code": 1,
  "message": "success",
  "data": {"hello": "world"}
}

国密密码哈希

# settings.py
PASSWORD_HASHERS = [
    'djangohelper.auth.hasher.SM3PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
]

扩展视图集

from djangohelper.viewsets import SelfModelViewSet

class MyViewSet(SelfModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MySerializer

通用模型

from djangohelper.db.models import BaseModel

class Article(BaseModel):
    title = models.CharField(max_length=200)

# 便捷方法
Article.new({"title": "Hello"})
Article.one(title="Hello")
Article.filter(title__contains="He")

依赖

运行时依赖:

  • django>=2.0.5
  • djangorestframework>=3.11.1
  • snowland-smx>=0.3.1(国密算法支持)
  • requests

可选依赖:

  • bcryptBCrypt-SM3 密码哈希器所需,对应 pyproject.tomlproject.optional-dependencies.bcrypt,通过 pip install snowland-djangohelper[bcrypt] 安装)

发布与文档依赖:

  • requirements-upload.txt: twine
  • requirements-doc.txt: astar-devopstool

参考资料

[1] https://github.com/astar-club/astar-devopstool-python


许可证

本项目基于 BSD License 发布。

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snowland_djangohelper-0.3.1.tar.gz (38.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snowland_djangohelper-0.3.1-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

Details for the file snowland_djangohelper-0.3.1.tar.gz.

File metadata

  • Download URL: snowland_djangohelper-0.3.1.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for snowland_djangohelper-0.3.1.tar.gz
Algorithm Hash digest
SHA256 d06b81fe4de2acb0b0b6a123992128130343035be9438cc6f5db06cd906e451b
MD5 d1bbd70ca63bbeddf77bae3e49c25b79
BLAKE2b-256 8d1a35a549c3518dcb02646791cc34b3d4c684e6189f576b2692a4aa9da40902

See more details on using hashes here.

File details

Details for the file snowland_djangohelper-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for snowland_djangohelper-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f06c87bbacb9659d9ce61045f233937cf13af70f62a8d1e1d956b252bd6a7cde
MD5 11d368764c26cb9ce5e046d15818f65d
BLAKE2b-256 d1876f2edc0418d36369643664eca74eaa58de8f530e05f44a775d15a7ff5e20

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 files

0.3

2 files

0.2

2 files

0.1.1

1 file

0.0.9

1 file

0.0.8

1 file

0.0.5

1 file

0.0.4

1 file

0.0.3

1 file

0.0.2

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page