博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django2.1-mysql学习(六)
阅读量:3918 次
发布时间:2019-05-23

本文共 5343 字,大约阅读时间需要 17 分钟。

Django2.1-mysql学习(六)

直接把之后的一些功能都贴出来吧,都是一些简单的小功能,理解一个之后其他的也就都懂了

修改礼品信息

views.modify
method:PUT
"""modify修改一个礼品的信息,仓库管理员可操作,但不可修改其状态值以及打折、热度情况method:PUT"""def modify(request, employee_id):    employee = Employee.objects.get(pk=employee_id)    context = {        'error': 0    }    if (not employee) or ('EMPLOYEE_ID' not in request.session) or ('IS_LOGIN' not in request.session) \            or (request.session['EMPLOYEE_ID'] != employee_id):        context['error'] = 1        return HttpResponse(json.dumps(context), content_type="application/json")    if request.method == 'PUT':        modify = QueryDict(request.body)        id = modify.get('id')        present = Present.objects.filter(id=id)        if not present or present[0].pdepot != Depot.objects.get(manager=employee):            context['error'] = 2            return HttpResponse(json.dumps(context), content_type="application/json")        status = present[0].status        name = modify.get('name')        introduction = modify.get('introduction')        on_date = modify.get('on_date')        store_num = modify.get('store_num')        cost = modify.get('cost')        hot = present[0].hot        off = present[0].off        off_cost = present[0].off_cost        url = modify.get('url')        pdepot = present[0].pdepot        present.update(name=name, introduction=introduction, status=status, on_date=on_date,                       store_num=store_num, cost=cost, hot=hot, off=off, off_cost=off_cost,                       url=url, pdepot=pdepot)        conx = serializers.serialize("json", present)        return HttpResponse(conx, content_type="application/json")

删除礼品

views.delete
method:DELETE
"""delete删除一个礼品,仅仓库管理员可操作method:DELETE"""def delete(request, employee_id):    employee = Employee.objects.get(pk=employee_id)    context = {        'error': 0    }    if (not employee) or ('EMPLOYEE_ID' not in request.session) or ('IS_LOGIN' not in request.session) \            or (request.session['EMPLOYEE_ID'] != employee_id):        context['error'] = 1        return HttpResponse(json.dumps(context), content_type="application/json")    if request.method == 'DELETE':        delete = QueryDict(request.body)        key = delete.get('id')        present = Present.objects.filter(id=key)        if not present or present[0].pdepot != Depot.objects.get(manager=employee):            context['error'] = 2            return HttpResponse(json.dumps(context), content_type="application/json")        conx = serializers.serialize("json", present)        present.delete()        return HttpResponse(conx, content_type="application/json")

售卖礼品

views.sell
method:PUT , GET
"""sell修改一个礼品的状态值,即上架或下架该礼品,以及修改其打折、热度情况,仅普通员工可操作method:PUT"""def sell(request, employee_id):    employee = Employee.objects.get(pk=employee_id)    context = {        'error': 0    }    if (not employee) or ('EMPLOYEE_ID' not in request.session) or ('IS_LOGIN' not in request.session) \            or (request.session['EMPLOYEE_ID'] != employee_id):        context['error'] = 1        return HttpResponse(json.dumps(context), content_type="application/json")    # 修改礼品的状态、热度及折扣信息请求    if request.method == 'PUT':        sell = QueryDict(request.body)        key = sell.get('id')        present = Present.objects.filter(id=key)        if not present:            context['error'] = 2            return HttpResponse(json.dumps(context), content_type="application/json")        name = present[0].name        introduction = present[0].introduction        on_date = present[0].on_date        store_num = present[0].store_num        status = sell.get('status')        cost = present[0].cost        hot = sell.get('hot')        off = sell.get('off')        off_cost = sell.get('off_cost')        url = present[0].url        pdepot = present[0].pdepot        present.update(name=name, introduction=introduction, status=status, on_date=on_date,                       store_num=store_num, cost=cost, hot=hot, off=off, off_cost=off_cost,                       url=url, pdepot=pdepot)        conx = serializers.serialize("json", present)        return HttpResponse(conx, content_type="application/json")    # 返回礼品请求    if request.method == 'GET':        depots = QueryDict(request.body)        key1 = depots.get('depot_id')        key2 = depots.get('present_status')        # 返回某一个仓库礼品请求        if key1:            depot = Depot.objects.get(id=key1)            presents = Present.objects.filter(pdepot=depot)            conx = serializers.serialize("json", presents)            return HttpResponse(conx, content_type="application/json")        # 根据状态返回礼品请求        if key2:            # 返回上架礼品            if key2 == '1':                presents = Present.objects.filter(status=key2)                conx = serializers.serialize("json", presents)                return HttpResponse(conx, content_type="application/json")            # 返回待审核礼品            elif key2 == '0':                presents = Present.objects.filter(status=key2)                conx = serializers.serialize("json", presents)                return HttpResponse(conx, content_type="application/json")            # 返回未上架礼品            elif key2 == '2':                presents = Present.objects.filter(status=key2)                conx = serializers.serialize("json", presents)                return HttpResponse(conx, content_type="application/json")

源码参考:

转载地址:http://baprn.baihongyu.com/

你可能感兴趣的文章
Net5 已经来临,让我来送你一个成功
查看>>
Magicodes.IE 3.0重磅设计畅谈
查看>>
一个 Task 不够,又来一个 ValueTask ,真的学懵了!
查看>>
如何在ASP.NetCore增加文件上传大小
查看>>
BCVP第2期:项目已完成升级.NET5.0
查看>>
C# 9.0 正式发布了(C# 9.0 on the record)
查看>>
[C#.NET 拾遗补漏]12:死锁和活锁的发生及避免
查看>>
asp.net core web mvc之异常
查看>>
C# Span 源码解读和应用实践
查看>>
起点低,怎么破?
查看>>
聊聊单元测试
查看>>
推荐几款强大流行的BI系统
查看>>
.NET必知的EventCounters性能指标监视器
查看>>
快来参加学习.NET 挑战赛
查看>>
被冷落的运算符重载
查看>>
ASP.NET Core 中基于工厂的中间件激活
查看>>
跟我一起学Redis之Redis事务简单了解一下
查看>>
微软发布VS Code Jupyter插件!不止Python!多语言的Jupyter Notebook支持来了!
查看>>
64岁Python之父加入微软 | 谁说大龄程序员无出路
查看>>
说说 C# 9 新特性的实际运用
查看>>