Skip to main content

lminapi 基于django和django rest framework 组合常用APIView方法进行重新封装为LminApiView 继承LminApiView视图类后,结合django orm 更加方便快速的编写一般的增删改查api接口

获取方式

pip install lminapi

使用说明

class TestView(LminApiView):

    modelObj = UsersModels  # 查询数据库对象
    list_display = ("id", "name")
    get_list_filter = [] # 列表筛选字段  [{"field": , "rule": "field__in"}] "rule"根据django orm 的**kwargs查询方式条件来写
    post_files = {} # post上传文件 {"file_field": {"path": ["folder_name", "child_folder_name"]}}
    put_files = {} # post上传文件 {"file_field": {"path": ["folder_name", "child_folder_name"]}}
    unique_param = [] # 需要查重的字段 ["field"]
    delete_rule = {} # 删除时传入的字段规则 {"id": [Required,],}
    post_rule = {} # 新增时传入的字段规则  {"field": [Required, Length(32), InstanceOf(str)],}
    put_rule = {} # 编辑时传入的字段规则  {"field": [Required, Length(32), InstanceOf(str)],}
    put_where_params = [] # put请求修改时的条件参数  [{"field": , "rule": "field__in"}]
    post_inlist = [] # 新增时创建model对象需要的字段 ["field1","field2"]
    put_inlist = [] # 编辑时修改model对象需要的字段 ["field1","field2"]
    file_first_path = "/usr/myfolder/" # 文件保存的初始地址
    delete_where_params = [] # delete请求删除时的条件参数 [{"field": , "rule": "field"}]

# urls.py
re_path('^test$',views_api.TestView.as_view())
# 该视图类实现了 get post put delete请求

utils

  • comfunc.py
def paginationfunc(self, resList, pg, size):
    '''
    分页方法
    param resList: 需要进行分页的列表
    param pg: 页码
    param size: 每页条数
    return:
        count: 总条数
        num_pages: 总页数
        next_page: 下一页
        previous_page:上一页
        ret:当前页结果列表
    '''
    p = Paginator(resList, size)
    next_page = None
    previous_page = None
    page1 = p.page(pg)
    if page1.has_next():
        next_page = page1.next_page_number()
    if  page1.has_previous():
        previous_page = page1.previous_page_number()
    data = {"count":p.count,"num_pages":p.num_pages,"next_page":next_page,"previous_page":previous_page,
        "ret":page1.object_list}
    return data

def exam_old(modelObj, paramDict: dict, excludeDict: dict = None) -> bool:
    '''
    检查对象是否已存在 配合post新增时进行也有查重
    param modelObj: django models 对象
    param paramDict: 查重的字段以及值,{"field": field_value,}
    param excludeDict: 需要排除的条件,{"field": field_value,}
    return:
        返回bool值, 存在时返回true
    '''
    if excludeDict == None:
        old = modelObj.objects.filter(**paramDict).first()
    else:
        old = modelObj.objects.filter(**paramDict).exclude(**excludeDict).first()
    if not old:
        return False
    return True


def make_path(fpath: str):
    '''
    检查文件夹是否存在,不存在则创建
    return:
        True or err
    '''
    try:
        if not os.path.exists(fpath):
            os.makedirs(fpath)
        return True
    except Exception as e:
        return str(e)

def add_file(filePath, file):
    '''
    写入文件
    param filePath:文件写入地址
    param file: 文件对象
    return:
        True or err
    '''
    try:
        with open(filePath,'wb') as f:
            for fimg in file.chunks():
                f.write(fimg)
        return True
    except Exception as e:
        return str(e)

def file_save_addr(file, firstPath: str, pathList: list):
    '''
    对文件进行写入并返回路劲
    param file: 文件对象
    param firstPath: 初始文件夹路径
    param pathList: 文件写入的具体路径分级列表,["floder","child floder"]
    return:
        bool
        路径str
    '''
    filepath = firstPath
    filestr = ""
    for i in pathList:
        filepath = os.path.join(filepath, i)
        filestr = filestr + "/" + i
    make_path(filepath)
    lastfilePath = os.path.join(filepath, file.name)
    ok = add_file(filePath=lastfilePath, file=file)
    if ok != True:
        return ok, ""
    return True, filestr + "/" + file.name
  • params_to_dict.py
def get_params_dict_inlist(data, inList: list) -> dict:
    '''
    整理传入字段,紧包含在inList中的并传入值的字段
    '''
    try:
        adata = data.dict()
    except:
        adata = data
    paramDict = {}
    for i in inList:
        if i in adata.keys():
            if data[i] != "":
                paramDict[i] = adata[i]
            else:
                continue
        else:
            continue
    return paramDict
  • params_verify.py
  • ·该程序下是一些参数验证的方法以及视图参数验证装饰器

Metadata

Release files for lminapi 0.0.3

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

Source distribution (sdist)

Source distribution for lminapi 0.0.3
File Size Uploaded Provenance
lminapi-0.0.3.tar.gz 7.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for lminapi 0.0.3
File Interpreter ABI Platform
lminapi-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 19.4 kB

Release files / lminapi-0.0.3.tar.gz

Download URL lminapi-0.0.3.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2be6549b269d7b5a0ef1a5364f675de3ddd93d0e8eee83ea5e2dd76ccaa6d71f
BLAKE2b-256 checksum
How to use checksums
39a31ef557d68f36afa841d7f9700f767b5093e390e94a0174350e7bb469f050
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.5

Release files / lminapi-0.0.3-py3-none-any.whl

Download URL lminapi-0.0.3-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f43f3fd11eb8de51e78cfb97d993bd0ec62d68a5415cca137247bf412fe9f0d2
BLAKE2b-256 checksum
How to use checksums
047232879f64f9f167208c37d26268f6e5edc6f030f732f3f97c8d512d143d82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.5

Security for lminapi 0.0.3

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.

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files No provenance

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