def put(self, request): response = MyResponse() # 传入的数据{"course_id":"1","policy_id":"2"} course_id = request.data.get('course_id') policy_id = request.data.get('policy_id') shopping_cart_bytes = self.conn.get('shopping_cart_%s' % request.user.pk) shopping_cart_dic = json.loads( shopping_cart_bytes) if shopping_cart_bytes else {} try: # 判断课程是否在购物车字典中 if course_id not in shopping_cart_dic: # 说明可定不是正常 raise CommonException(102, '要修改的课程不存在') # 判断价格策略是否合法,是否在当前课程的所有价格策略中 if policy_id not in shopping_cart_dic[course_id]['policy']: raise CommonException(103, '要修改的价格策略不合法') # 修改默认价格策略 shopping_cart_dic[course_id]['default_policy'] = policy_id # 存入reids self.conn.set('shopping_cart_%s' % request.user.pk, json.dumps(shopping_cart_dic)) response.msg = '更新成功' except CommonException as e: response.status = e.status response.msg = e.msg except Exception as e: response.msg = str(e) # 项目上线,用下面这个 # response.msg = '您的操作有误' response.status = 109 return Response(response.get_data)
def post(self, request): response = MyResponse() # post 提交的数据,request.data中取 name = request.data.get('name') # 密码是明文 md5加密 pwd = request.data.get('pwd') user = models.UserInfo.objects.filter(username=name, password=pwd).first() if user: # 处理token # 生成token token = uuid.uuid4() import datetime models.Token.objects.update_or_create(user=user, defaults={'key': token,'created':datetime.datetime.now()}) # 用redis处理 # conn=get_redis_connection() # user_dic={'id':user.pk,'name':user.name} # conn.hmset(token,user_dic) # redis放置结束 # 把user对象放入缓存,key值是随机字符串asdfasdfasd cache.set(token,user,60*60*24) response.msg = '登录成功' response.token = token response.name = name else: response.msg = '用户名或密码错误' response.status = 101 return Response(response.get_data)
def delete(self, request): response = MyResponse() # 传列表形式 # "course_id_list":['1','2','3',100] course_id_list = request.data.get('course_id_list') # 获取购物车地址 shopping_cart_bytes = self.conn.get('shopping_cart_%s' % request.user.pk) shopping_cart_dic = json.loads( shopping_cart_bytes) if shopping_cart_bytes else {} try: for course_id in course_id_list: if course_id not in shopping_cart_dic: raise CommonException(102, '您传入的课程不合法') shopping_cart_dic.pop(course_id) self.conn.set('shopping_cart_%s' % request.user.pk, json.dumps(shopping_cart_dic)) response.msg = '删除成功' except CommonException as e: response.status = e.status response.msg = e.msg except Exception as e: response.msg = str(e) # 项目上线,用下面这个 # response.msg = '您的操作有误' response.status = 109 return Response(response.get_data)
def post(self, request): response = MyResponse() # 传入的参数 {"price":600",beli":100} price_in = request.data.get('price') beli = request.data.get('beli') try: # 1 从支付中心拿出字典,全局优惠券字典取出来 payment_dic = json.loads( self.conn.get('payment_%s' % request.user.pk)) global_coupon = json.loads( self.conn.get('global_coupon_%s' % request.user.pk)) # 定义一个用于存储所有课程价格的列表 price_list = [] # 2 循环结算中心字典 for course_id, course in payment_dic.items(): # 取出默认价格策略,取出默认价格,取出默认优惠券id default_policy_id = course['default_policy'] default_price = course['policy'][default_policy_id]['price'] default_coupon_id = course['default_coupon'] if default_coupon_id != 0: # 如果不等于0,表示使用了优惠券 coupon_dic = course['coupon'][str(default_coupon_id)] default_price = self.account(default_price, coupon_dic) # 注意要在if外面往里添加 price_list.append(default_price) # 3 取出使用的全局优惠券id和字典 default_coupon_id = global_coupon['default_coupon'] final_price = sum(price_list) if default_coupon_id != 0: # 使用了优惠券 global_coupon_dic = global_coupon['coupon'][str( default_coupon_id)] final_price = self.account(sum(price_list), global_coupon_dic) # 4 判断传入的贝利数是否合法 if beli > request.user.beli: raise CommonException(104, '传入的贝利数有问题') final_price = final_price - beli / 10 # 修改贝利数 request.user.beli = request.user.beli - beli request.user.save() if final_price < 0: final_price = 0 if final_price != price_in: raise CommonException(105, '您传入的价格不合法') if not final_price == 0: #构造阿里支付 pass response.url = '阿里支付的地址' except CommonException as e: response.msg = e.msg response.status = e.status except Exception as e: response.msg = str(e) # 项目上线,用下面这个 # response.msg = '您的操作有误' response.status = 109 return Response(response.get_data)
def get(self, request): response = MyResponse() response.payment = json.loads(self.conn.get('payment_%s' % request.user.pk)) response.global_coupon = json.loads(self.conn.get('global_coupon_%s' % request.user.pk)) response.msg = '查询成功' response.beli=request.user.beli return Response(response.get_data)
def get(self, request, pk): response = MyResponse() # pk 是课程的id,查询的是课程详情表****** try: course_detail = models.CourseDetail.objects.get(course_id=pk) coursedetailser = MySer.CourseDetailSer(instance=course_detail, many=False) response.data = coursedetailser.data except ObjectDoesNotExist as e: response.msg = '该课程不存在' response.status = 101 except Exception as e: response.msg = str(e) response.status = 105 return Response(response.get_data)
def get(self, request): response = MyResponse() course_list = models.Course.objects.all() course_ser = MySer.CourseSer(instance=course_list, many=True) response.msg = '查询成功' response.data = course_ser.data return Response(response.get_data)
def get(self, request): # 获取购物车接口 response = MyResponse() shopping_cart_bytes = self.conn.get('shopping_cart_%s' % request.user.pk) shopping_cart_dic = json.loads( shopping_cart_bytes) if shopping_cart_bytes else {} response.msg = '查询购物车成功' response.data = shopping_cart_dic return Response(response.get_data)
def post(self, request): response = MyResponse() # 传入的数据{"course_id":"1","policy_id":"1"} course_id = request.data.get('course_id') policy_id = request.data.get('policy_id') try: # 1 校验课程是否存在 course = models.Course.objects.get(pk=course_id) # 2 取出所有价格策略 policy_price_list = course.price_policy.all() # 3 取出redis中的购物车数据 # 购物车数据的key值 shopping_cart_用户id # 因为有了认证组件,所以request.user就是当前登录用户 shopping_cart_bytes = self.conn.get('shopping_cart_%s' % request.user.pk) shopping_cart_dic = json.loads( shopping_cart_bytes) if shopping_cart_bytes else {} # 跟上面一样 # if shopping_cart_bytes: # shopping_cart_dic=json.loads(shopping_cart_bytes) # else: # shopping_cart_dic={} # 4 用于存放所有价格策略的字典,循环所有的价格策略 policy = {} for policy_price in policy_price_list: policy[str(policy_price.pk)] = { 'period': policy_price.valid_period, # 取出价格周期的文字 'period_display': policy_price.get_valid_period_display(), 'price': policy_price.price } if policy_id not in policy: # 5 传入的价格策略不合法,不在该课程的所有价格策略中 raise CommonException(102, '价格策略不合法,你是爬虫') # 6 判断当前传入的课程id是否在购物车中,在则更新 if course_id in shopping_cart_dic: shopping_cart_dic[course_id]['default_policy'] = policy_id response.msg = '更新购物车成功' else: shopping_cart_dic[course_id] = { 'title': course.name, 'img': course.course_img, # 默认价格策略,是传入的价格策略id 'default_policy': policy_id, 'policy': policy } response.msg = '添加购物车成功' # 7 把字典转成字符串,存入redis self.conn.set('shopping_cart_%s' % request.user.pk, json.dumps(shopping_cart_dic)) except ObjectDoesNotExist as e: response.msg = '该课程不存在,你可能是爬虫' response.status = 101 except CommonException as e: response.msg = e.msg response.status = e.status except Exception as e: response.msg = str(e) # 项目上线,用下面这个 # response.msg = '您的操作有误' response.status = 109 return Response(response.get_data)
def post(self, request): response = MyResponse() # 传入的数据{"course_list":[{"course_id":"1","policy_id":"1"},{"course_id":"2","policy_id":"2"}]} course_list = request.data.get('course_list') try: # 1定义结算中心的字典,定义全局优惠券的字典 payment_dic = {} global_coupon_dic = { 'coupon': {}, 'default_coupon': 0 } # 2 取出购物车的数据 shopping_cart_bytes = self.conn.get('shopping_cart_%s' % request.user.pk) shopping_cart_dic = json.loads(shopping_cart_bytes) if shopping_cart_bytes else {} # 3 循环传入的course_list值 for course_id_dic in course_list: course_in_id = course_id_dic['course_id'] if course_in_id not in shopping_cart_dic: # 传入的数据不合法,要结算的课程不在购物车中 raise CommonException(102, '要结算的课程不在购物车中') # 4 构造出单个课程详情字典 course_detail = { 'coupon': {}, 'default_coupon':0 } course_detail.update(shopping_cart_dic[course_in_id]) # 5 把该课程详情添加到结算中心字典中 payment_dic[course_in_id] = course_detail # 6 查询出所有优惠券,包括全站优惠券和课程优惠券(当前用户,未使用,当前在有效期内) # 取出当前时间,有效优惠券时间:优惠券开始时间小于当前时间,优惠券结束时间,大于当前时间 ctime = datetime.datetime.now() coupon_list = models.CouponRecord.objects.filter(user=request.user, status=0, coupon__valid_begin_date__lte=ctime, coupon__valid_end_date__gte=ctime ) # 7 循环所有优惠券 for coupon in coupon_list: # 取出该优惠券类型,转str coupon_type = str(coupon.coupon.coupon_type) # 构造出单个优惠券详情的字典 coupon_detail = { 'coupon_display': coupon.coupon.get_coupon_type_display(), 'coupon_type': coupon_type } # 取出优惠券id,课程id coupon_id = str(coupon.pk) coupon_course_id = coupon.coupon.object_id # 8 根据优惠券类型,继续往单个优惠券字典中添加字段 if coupon_type == '0': coupon_detail['money_equivalent_value'] = coupon.coupon.money_equivalent_value elif coupon_type == '1': coupon_detail['money_equivalent_value'] = coupon.coupon.money_equivalent_value coupon_detail['minimum_consume'] = coupon.coupon.minimum_consume else: coupon_detail['off_percent'] = coupon.coupon.off_percent # 9 判断是全站优惠券还是课程优惠券 if not coupon_course_id: # 全局优惠券 global_coupon_dic['coupon'][coupon_id] = coupon_detail else: # 课程优惠券(大坑) # coupon_course_id 是当前优惠券对应的课程id,但是该课程不一定在结算中心中 if str(coupon_course_id) not in payment_dic: continue payment_dic[str(coupon_course_id)]['coupon'][coupon_id] = coupon_detail # 10 把结算中心字典,和全局优惠券字典放到redis中 self.conn.set('payment_%s' % request.user.pk, json.dumps(payment_dic)) self.conn.set('global_coupon_%s' % request.user.pk, json.dumps(global_coupon_dic)) response.msg = '加入结算中心成功' except ObjectDoesNotExist as e: response.msg = '该课程不存在,你可能是爬虫' response.status = 101 except CommonException as e: response.msg = e.msg response.status = e.status except Exception as e: response.msg = str(e) # 项目上线,用下面这个 # response.msg = '您的操作有误' response.status = 109 return Response(response.get_data)