课程接口调试

Posted gukai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课程接口调试相关的知识,希望对你有一定的参考价值。

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

技术图片
  1 from  rest_framework.response import Response
  2 from rest_framework.views import APIView
  3 from app import models
  4 from utils.commonutils import LuffyResponse
  5 from utils.commonutils import CommonException
  6 
  7 from  django.core.exceptions import ObjectDoesNotExist
  8 from django_redis import get_redis_connection
  9 import json
 10 from utils.MyAuth import LoginAuth
 11 
 12 class ShoppingView(APIView):
 13     authentication_classes = [LoginAuth]
 14     #用户登录模块的装饰器
 15     conn = get_redis_connection()
 16     #使用redis的连接池
 17 
 18     def post(self,request):
 19         response=LuffyResponse()
 20         try:
 21             course_in_id = request.data.get(course_id)
 22             #post请求获取课程的id
 23             policy_in_id = request.data.get(policy_id)
 24             #post请求获取价格对应的id
 25             course = models.Course.objects.get(pk=course_in_id)
 26             #查询课程是否存在
 27             policy_list = course.price_policy.all()
 28             #获取所有课程的价格策略
 29             shopping_cart_dic_bytes=self.conn.get(shopping_card_%s % request.user.id)
 30             #json可以loadsbytes格式
 31             shopping_cart_dic= json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
 32 
 33             #循环价格策略
 34             policy_dict = {} #创建一个空字典
 35             for policy in policy_list:  #循环获取所有课程的价格策略
 36                 policy_dict[str(policy.pk)] = {
 37                     period: policy.valid_period,  #显示学习的时间
 38                     period_display: policy.get_valid_period_display(),    #显示学习时间对应的中文
 39                     price: policy.price   #获取对应的价格
 40                 }
 41             #检验价格策略是否是该课程的价格策略
 42             if policy_in_id not in policy_dict:
 43                 raise CommonException(价格策略不合法,你是爬虫)
 44 
 45             #构建购物车的字典
 46             shopping_cart_dic[course_in_id]= {
 47                 title:course.name,
 48                 img: course.cuorse_img,
 49                 default_policy: policy_in_id,
 50                 policy: policy_dict
 51             }
 52             #在redis内添加字符串
 53             self.conn.set(shopping_card_%s % request.user.id,json.dumps(shopping_cart_dic))
 54             response.msg = 加入购物车成功
 55 
 56         except CommonException as e:
 57             response.status = 102
 58             response.msg = e.msg
 59         except ObjectDoesNotExist as e:
 60             response.status = 101
 61             response.msg = 您要加入购物车的课程不存在
 62         except Exception as e:
 63             response.status = 105
 64             response.msg= str(e)
 65         return Response(response.get_dic)
 66 
 67     def get(self,request):
 68         response = LuffyResponse()
 69         shopping_cart_dic_bytes = self.conn.get(shopping_card_%s % request.user.id)
 70         # json可以loadsbytes格式
 71         shopping_cart_dic = json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
 72         response.data = shopping_cart_dic
 73         response.msg = 查询成功
 74 
 75         return  Response(response.get_dic)
 76 
 77     def delete(self,request):
 78         course_in_id = request.data.get(course_id)
 79         #取出course的id号
 80         response = LuffyResponse()
 81         shopping_cart_dic_bytes = self.conn.get(shopping_card_%s % request.user.id)
 82         # json可以loadsbytes格式
 83         shopping_cart_dic = json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
 84         shopping_cart_dic.pop(course_in_id)
 85         #删除课程的id号
 86 
 87         self.conn.set(shopping_card_%s % request.user.id,json.dumps(shopping_cart_dic))
 88         response.msg = 删除成功
 89         return  Response(response.get_dic)
 90 
 91 
 92     def put(self,request):
 93         course_in_id = request.data.get(course_id)
 94         policy_in_id = request.data.get(policy_id)
 95         response = LuffyResponse()
 96         try:
 97             shopping_cart_dic_bytes = self.conn.get(shopping_card_%s % request.user.id)
 98             shopping_cart_dic = json.loads(shopping_cart_dic_bytes) if shopping_cart_dic_bytes else {}
 99             policy = shopping_cart_dic[course_in_id][policy]
100             if policy_in_id not in policy:
101                 raise  CommonException(传入的价格策略非法, 你是爬虫)
102             shopping_cart_dic[course_in_id][default_policy]=policy_in_id
103             self.conn.set(shopping_card_%s % request.user.id,json.dumps(shopping_cart_dic))
104             response.msg = 修改成功
105         except CommonException as e:
106             response.status = 102
107             response.msg = e.msg
108         except Exception as e:
109             response.status = 105
110             response.msg = str(e)
111         return Response(response.get_dic)
drf框架

 

以上是关于课程接口调试的主要内容,如果未能解决你的问题,请参考以下文章

Android课程---Android Studio使用小技巧:提取方法代码片段

PHP代码-psysh调试代码片段工具

方便调试使用的代码片段

运行/调试你的PHP代码

片段中的 super.onCreateView

短信验证发送与返回值接收短信接口调试编程思路解析