lf 前后端分离 (4) 价格策略
一.价格策略
价格策略就是通过前端发送要购买的课程以及价格策略来找出表关联的字段返回客户端
通过contenttype 属性 找到课程所有的价格策略
for prcie_policy in course_obj.price_policy.all():
进行判断获取的价格策略是否存在
if prcie_policy_id not in price_policy_dict:
raise CommonException(1002, "价格策略有误")
在创建与前端匹配的数据结构
1 import redis 2 import json 3 4 from rest_framework.views import APIView 5 from rest_framework.response import Response 6 7 from app01.models import * 8 from app01.utils.auth import LoginAuth 9 from app01.utils.response import BaseResponse 10 from app01.utils.exception import CommonException 11 12 from django.conf import settings 13 from django.core.exceptions import ObjectDoesNotExist 14 15 cache = redis.Redis(decode_responses=True) 16 17 class ShoppingCartView(APIView): 18 authentication_classes = [LoginAuth] 19 20 def post(self, request): 21 course_id = request.data.get("course_id") 22 prcie_policy_id = request.data.get("price_policy_id") 23 24 user_id = request.user.pk 25 res = BaseResponse() 26 27 try: 28 course_obj = Course.objects.get(pk=course_id) 29 price_policy_dict = {} 30 price_policy_list = [] 31 for prcie_policy in course_obj.price_policy.all(): 32 price_policy_dict[prcie_policy.pk] = { 33 "id": prcie_policy.pk, 34 "valid_period": prcie_policy.valid_period, 35 "valid_period_text": prcie_policy.get_valid_period_display(), 36 "price": prcie_policy.price, 37 "default": prcie_policy_id == prcie_policy.pk 38 } 39 print("price_policy_dict", price_policy_dict) 40 41 #这里需要进行价格策略判断 42 if prcie_policy_id not in price_policy_dict: 43 raise CommonException(1002, "价格策略有误") 44 pp = PricePolicy.objects.get(pk=prcie_policy_id) 45 for i in price_policy_dict: 46 price_policy_list.append(price_policy_dict[i]) 47 48 shoppingcar_key = settings.SHOPPING_CAR_KEY % (user_id, course_id) 49 shoppingcar_val = { 50 "id":course_id, 51 "name": course_obj.name, 52 "course_img": course_obj.course_img, 53 "relate_price_policy": price_policy_list, 54 "default_price_period": prcie_policy_id, 55 "default_price": pp.price, 56 # "valid_period": pp.valid_period, 57 # "valid_period_text": pp.get_valid_period_display(), 58 } 59 cache.set(shoppingcar_key, json.dumps(shoppingcar_val, ensure_ascii=False)) 60 print(cache.get(shoppingcar_key)) 61 62 res.data = "加入购物车成功" 63 64 except CommonException as e: 65 res.code = e.code 66 res.msg = e.msg 67 except ObjectDoesNotExist as e: 68 res.code = 1001 69 res.msg = "课程不存在" 70 71 return Response(res.dict) 72 73 def get(self, request): 74 75 76 77 shopping_car_list = [] 78 79 for key in cache.keys(pattern="shoppingcar_1_*"): 80 data = cache.get(key) 81 print(data) 82 shopping_car_list.append(json.loads(data)) 83 84 res = BaseResponse() 85 res.data = { 86 "total": len(shopping_car_list), 87 "shopping_car_list": shopping_car_list 88 } 89 90 91 92 return Response(res.dict)
关于baseresponse
通过定义静态方法,@property无需调用添加()
实例化对象res .dict发送字典形式数据
class BaseResponse(object): def __init__(self): self.code = 1000 self.data = None self.msg = "" @property def dict(self): return self.__dict__ if __name__ == '__main__': res = BaseResponse() res.data = 12312 print(res.dict)