zoukankan      html  css  js  c++  java
  • 课程接口调试

    免费课程查看增加修改删除

    from  rest_framework.response import Response
    from rest_framework.views import APIView
    from app import models
    from utils.commonutils import LuffyResponse
    from utils.commonutils import CommonException
    
    from  django.core.exceptions import ObjectDoesNotExist
    from django_redis import get_redis_connection
    import json
    from utils.MyAuth import LoginAuth
    
    class ShoppingView(APIView):
        authentication_classes = [LoginAuth]
        #用户登录模块的装饰器
        conn = get_redis_connection()
        #使用redis的连接池
    
        def post(self,request):
            response=LuffyResponse()
            try:
                course_in_id = request.data.get('course_id')
                #post请求获取课程的id
                policy_in_id = request.data.get('policy_id')
                #post请求获取价格对应的id
                course = models.Course.objects.get(pk=course_in_id)
                #查询课程是否存在
                policy_list = course.price_policy.all()
                #获取所有课程的价格策略
    
    
                shopping_cart_dic_bytes=self.conn.get('shopping_card_%s' % request.user.id)
                #从redis内拿出key 为 shopping_card_1 样式的值
                shopping_cart_dic= json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
                # json可以loadsbytes格式 ,这样是三元表达式显示 如果有值就用jason 编译出来
    
                #循环价格策略
                policy_dict = {} #创建一个空字典
                for policy in policy_list:  #循环获取所有课程的价格策略
                    policy_dict[str(policy.pk)] = {
                        'period': policy.valid_period,  #显示学习的时间
                        'period_display': policy.get_valid_period_display(),    #显示学习时间对应的中文
                        'price': policy.price   #获取对应的价格
                    }
                #检验价格策略是否是该课程的价格策略,如果不在全部价格策略内就抛异常
                if policy_in_id not in policy_dict:
                    raise CommonException('价格策略不合法,你是爬虫')
    
                #构建购物车的字典
                shopping_cart_dic[course_in_id]= {
                    'title':course.name,        #课程名字
                    'img': course.course_img,   #课程图片
                    'default_policy': policy_in_id, #价格策略的id号
                    'policy': policy_dict       #价格策略的所有信息,时间 价格等等
                }
                #在redis内添加字符串,用json 来序列化信息
                self.conn.set('shopping_card_%s' % request.user.id,json.dumps(shopping_cart_dic))
                response.msg = '加入购物车成功'
    
            except CommonException as e:
                response.status = 102
                response.msg = e.msg
            except ObjectDoesNotExist as e:
                response.status = 101
                response.msg = '您要加入购物车的课程不存在'
            except Exception as e:
                response.status = 105
                response.msg= str(e)
            return Response(response.get_dic)
    
        def get(self,request):
            response = LuffyResponse()
            #从redis内获取值因为是bytes二进制形式,所以需要反序列化
            shopping_cart_dic_bytes = self.conn.get('shopping_card_%s' % request.user.id)
            # json可以loadsbytes格式
            shopping_cart_dic = json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
            response.data = shopping_cart_dic       #把格式化好的数据加入到data内,返回数据回去
            response.msg = '查询成功'
    
            return  Response(response.get_dic)
    
        def delete(self,request):
            course_in_id = request.data.get('course_id')
            #取出course的id号
            response = LuffyResponse()
            shopping_cart_dic_bytes = self.conn.get('shopping_card_%s' % request.user.id)
            # json可以loadsbytes格式
            shopping_cart_dic = json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
            shopping_cart_dic.pop(course_in_id)
            #删除课程的id号
    
            self.conn.set('shopping_card_%s' % request.user.id,json.dumps(shopping_cart_dic))
            #删除成功之后继续添加到redis内
            response.msg = '删除成功'
            return  Response(response.get_dic)
    
    
        def put(self,request):
            course_in_id = request.data.get('course_id')
            policy_in_id = request.data.get('policy_id')
            response = LuffyResponse()
            try:
                shopping_cart_dic_bytes = self.conn.get('shopping_card_%s' % request.user.id)
                shopping_cart_dic = json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
                policy = shopping_cart_dic[course_in_id]['policy']
                #获取这门课程的价格策略的全部信息 里面有时间,价钱等
                if policy_in_id not in policy:
                    raise  CommonException('传入的价格策略非法, 你是爬虫')
                shopping_cart_dic[course_in_id]['default_policy']=policy_in_id
                #修改价格策略的信息 因为default_policy 这个字段对应 用户输入的policy_in_id 这个字段的
                self.conn.set('shopping_card_%s' % request.user.id,json.dumps(shopping_cart_dic))
                response.msg = '修改成功'
            except CommonException as e:
                response.status = 102
                response.msg = e.msg
            except Exception as e:
                response.status = 105
                response.msg = str(e)
            return Response(response.get_dic)
    课程的增删改查接口
  • 相关阅读:
    eclipse快捷键
    iOS音频播放 (二):AudioSession 转
    HNU13377:Book Club(DFS)
    BAPC2014 K&&HUNNU11591:Key to Knowledge(中途相遇法)
    小米净水器与小区过滤价格水对照.xls
    NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(一)
    hibernate动态表名映射--仅仅有想不到,没有做不到
    【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节
    oracle-企业信息化
    线性查找算法
  • 原文地址:https://www.cnblogs.com/gukai/p/10679269.html
Copyright © 2011-2022 走看看