def vipCallback(request): void = request.GET.get("void", None) if not void: return Jsonify({"status":False, "error":"1101", "error_message":u"输入信息不足。"}) void = int(void) viporder = VIPOrder.objects.filter(void=void) if not viporder: return Jsonify({"status":False, "error":"1502", "error_message":u"订单不存在。"}) else: viporder = viporder[0] month = viporder.month _user = User.objects.filter(uid=viporder.user.uid) if not _user: return Jsonify({"status":False, "error":"1502", "error_message":u"该订单不属于任何用户。"}) else: _user = _user[0] if not _user.vip: _vip = VIP() _vip.save() _user.vip=_vip _user.save() viporder.state=1 viporder.save() newPackage = VIPPackage(level = viporder.level, days = viporder.month*31) _vip.addNewPackage(newPackage) return Jsonify({"status":True, "vip":_vip.toDict()})
def addNewPackage(month, typeid, vip=None, user=None): if type(user) == dict: user = User.objects.filter(uid=user['uid'])[0] vip = flushVip(vip) if vip: currentPackage = vip.headPackage if currentPackage.level == typeid: currentPackage.days = currentPackage.days + month*31 currentPackage.save() else: if currentPackage.level < typeid: newPackage = VIPPackage(start_date = None, days = month*31, level=typeid, nextPackage=None) newPackage.save() vip = addNewHeadPackage(vip, newPackage) else: fatherPackage = None while currentPackage.level > typeid: fatherPackage = currentPackage currentPackage = currentPackage.nextPackage if currentPackage.level == typeid: currentPackage.days = currentPackage.days + month * 31 currentPackage.save() else: newPackage = VIPPackage(start_date = None, days = month*31, level=typeid, nextPackage=currentPackage) newPackage.save() fatherPackage.nextPackage = newPackage fatherPackage.save() return vip else: vip = VIP() vip.save() newPackage = VIPPackage(start_date = timezone.now(), days = month*31, level=typeid) newPackage.save() vip.headPackage = newPackage vip.save() user.vip = vip user.save() return vip