def purchase(request): ''' 购买商品的逻辑 返回是否成功 ''' if request.method == "POST": user = request.user ss_user = user.ss_user goodId = request.POST.get('goodId') good = Goods.objects.get(pk=goodId) if user.balance < good.money: registerinfo = { 'title': '金额不足!', 'subtitle': '请去捐赠界面/联系站长充值', 'status': 'error', } else: # 验证成功进行提权操作 ss_user.enable = True ss_user.transfer_enable += good.transfer user.balance -= good.money if (user.level == good.level and user.level_expire_time > datetime.datetime.now()): user.level_expire_time += datetime.timedelta(days=good.days) else: user.level_expire_time = datetime.datetime.now() \ + datetime.timedelta(days=good.days) user.level = good.level user.save() ss_user.save() # 增加购买记录 record = PurchaseHistory( good=good, user=user, money=good.money, purchtime=timezone.now()) record.save() # 增加返利记录 inviter = User.objects.get(pk=user.invited_by) rebaterecord = RebateRecord( user_id=inviter.pk, money=good.money * Decimal(settings.INVITE_PERCENT)) inviter.balance += rebaterecord.money inviter.save() rebaterecord.save() registerinfo = { 'title': '购买成功', 'subtitle': '请在用户中心检查最新信息', 'status': 'success', } # 删除缓存 return JsonResponse(registerinfo) else: return HttpResponse('errors')
def purchase(request): ''' 购买商品的逻辑 返回是否成功 ''' if request.method == "POST": user = request.user ss_user = user.ss_user goodId = request.POST.get('goodId') good = Goods.objects.get(pk=goodId) if user.balance < good.money: registerinfo = { 'title': '金额不足!', 'subtitle': '请去捐赠界面/联系站长充值', 'status': 'error', } else: # 验证成功进行提权操作 ss_user.enable = True ss_user.transfer_enable += good.transfer user.balance -= good.money now = pendulum.now() days = pendulum.duration(days=good.days) if (user.level == good.level and user.level_expire_time > now): user.level_expire_time += days else: user.level_expire_time = now + days user.level = good.level user.save() ss_user.save() # 增加购买记录 record = PurchaseHistory(good=good, user=user, money=good.money, purchtime=timezone.now()) record.save() # 增加返利记录 inviter = User.objects.filter(pk=user.invited_by).first() if inviter: rebaterecord = RebateRecord(user_id=inviter.pk, money=good.money * Decimal(settings.INVITE_PERCENT)) inviter.balance += rebaterecord.money inviter.save() rebaterecord.save() registerinfo = { 'title': '购买成功', 'subtitle': '请在用户中心检查最新信息', 'status': 'success', } # 删除缓存 return JsonResponse(registerinfo) else: return HttpResponse('errors')
def get(self, request): user = request.user rebate_logs = RebateRecord.list_by_user_id_with_consumer_username(user.pk) bar_config = { "labels": ["z", "v", "x", "x", "z", "v", "x", "x", "z", "v"], "data": [1, 2, 3, 4, 1, 1, 1, 1, 1, 2], "data_title": "每日邀请注册人数", } context = {"rebate_logs": rebate_logs, "user": user, "bar_config": bar_config} return render(request, "sspanel/aff_status.html", context=context)