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 -r requirements.txt
python setup.py 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
  • astartool

可选依赖:

  • bcryptBCrypt-SM3 密码哈希器所需,对应 setup.pyextras_require['bcrypt'],通过 pip install snowland-djangohelper[bcrypt] 安装)

发布与文档依赖:

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

参考资料

[1] https://github.com/ASTARCHEN/astartool

[2] 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.tar.gz (33.9 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-py3-none-any.whl (48.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for snowland_djangohelper-0.3.tar.gz
Algorithm Hash digest
SHA256 38937114b9707da670d1c4301b83baca9b25494146671b99302c258740c482f7
MD5 747c1ecb5ad9318a06a040ac3e173ff4
BLAKE2b-256 20c3fc4c1e593ee52209c46184502b9a528cd6000f9ccf4b775d49d3e91d4205

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for snowland_djangohelper-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c92673a847c5e0eef8bac606a34559e72417cf9cb01a83474f8fa0e0c13d1b0a
MD5 0bdddabd3c1ce697773592bdfbc80506
BLAKE2b-256 f54bdfd222edd28abf591e2f2981bc28c74e9e4fa2cf93938482351630137c57

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.1

2 files

This release

0.3 This release

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