1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
""" @Author : Young @Email : [email protected] @site : http://www.cnblogs.com/huang-yc/ @File : shopping_cart.py @version : 1.0 @Time : 2018/11/4 20:51 Description about this file:
""" from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet, ViewSetMixin from django_redis import get_redis_connection from utils.response_dict import BaseResponse from api.views.auth import YcAuth from api import models from utils.error import PricePolicyInvalid from django.core.exceptions import ObjectDoesNotExist from django.conf import settings import json
class ShoppingCart(APIView): authentication_classes = [YcAuth] conn = get_redis_connection('default')
def post(self, request, *args, **kwargs): """ 向购物车中添加商品 :param request: :param args: :param kwargs: :return: """ ret = BaseResponse() try: course_id = int(request.data.get('course_id')) policy_id = int(request.data.get('policy_id'))
course = models.Course.objects.get(id=course_id)
price_policy_list = course.price_policy.all() price_policy_dict = {} for item in price_policy_list: price_policy_dict[item.id] = { "period": item.valid_period, "period_display": item.get_valid_period_display(), "price": item.price } print(price_policy_dict) if policy_id not in price_policy_dict: raise PricePolicyInvalid('价格策略不合法') car_key = settings.SHOPPING_CART_KEY % (request.auth.user_id, course_id,) car_dict = { 'title': course.name, 'img': course.course_img, 'default_policy': policy_id, 'policy': json.dumps(price_policy_dict) } self.conn.hmset(car_key, car_dict) ret.data = '添加成功'
except PricePolicyInvalid as e: ret.data = 2001 ret.error = e.msg except ObjectDoesNotExist as e: ret.data = 2002 ret.error = '课程不存在'
except Exception as e: ret.code = 1001 ret.data = '获取购物车数据失败' return Response(ret.dict)
def delete(self, request, *args, **kwargs): ret = BaseResponse() try: course_id_list = request.data.get('courseids') key_list = [settings.SHOPPING_CART_KEY % (request.auth.user_id, course_id,) for course_id in course_id_list] self.conn.delete(*key_list) except Exception as e: ret.code = 1002 ret.error = "删除失败"
return Response(ret.dict)
def patch(self, request, *args, **kwargs): ret = BaseResponse() try: course_id = int(request.data.get('course_id')) policy_id = str(request.data.get('policy_id'))
key = settings.SHOPPING_CART_KEY % (request.auth.user_id, course_id)
if not self.conn.exists(key): ret.code = 1002 ret.error = "购物车中不存在此课程" return Response(ret.dict) policy_dict = json.loads(str(self.conn.hget(key, 'policy'), encoding='utf-8')) if policy_id not in policy_dict: ret.code = 1003 ret.error = "价格策略不合法" return Response(ret.dict)
self.conn.hset(key, 'default_policy', policy_id) ret.data = "修改成功"
except Exception as e: ret.code = 2003 ret.error = '修改数据失败'
return Response(ret.dict)
def get(self, request, *args, **kwargs): """ 查看购物车中所有的商品 :param request: :param args: :param kwargs: :return: """ ret = BaseResponse() try: key_match = settings.SHOPPING_CART_KEY % (request.auth.user_id, "*")
course_list = []
for key in self.conn.scan_iter(key_match, count=10): info = { "title": self.conn.hget(key, 'title').decode('utf-8'), "img": self.conn.hget(key, 'img').decode('utf-8'), "policy": json.loads(self.conn.hget(key, 'policy').decode('utf-8')), "default_policy": self.conn.hget(key, 'default_policy').decode('utf-8') } course_list.append(info) ret.data = course_list except Exception as e: ret.code = 1002 ret.error = "获取失败" return Response(ret.dict)
|