def submit_problems(request, response, render): """上传问题""" # 获取问题类型 # todo emoji图形控制 xss攻击的防御 forms校验? submit_type = request.POST.get('submit_type', 'Bug') # BUG, Introduce type = request.POST.get('type', '') title = request.POST.get('title', '') content = request.POST.get('content', '') img_list = request.POST.get('img_list', '') if not all([submit_type, type, title, content]): return response_json(retcode='FAIL', msg="ErrorParams", description='错误的提交数据!') # if request.FILES: # bol, res = upload_files(request) # if not bol: # return response_json(retcode='FAIL', msg="ImageUploadFail", description=res) # img_url = ';'.join(res) if submit_type == 'Bug': Problems( user_id=request.user.id, type=submit_type, subtype=type, title=title, detail=content, img_list=img_list, ).save() return response_json(retcode='SUCC', msg="ProblemsSubmitSucc")
def userlogin(request, response, render): user_ip = request.META.get('REMOTE_ADDR', '') # redirect = request.POST.get('redirect', '/user/index') # todo 放在中间件中 # if ip_visit_limit(user_ip , 'login' , 10, 60): # return response_json(retcode='FAIL', msg="IP_LIMIT", description="您的访问过于频繁,请稍后重试") username = request.POST.get('username', '') pwd = request.POST.get('pwd', '') user = auth.authenticate(username=username, password=pwd) if not user: return response_json(retcode='FAIL', msg='WRONG_USER', description=u'手机号或密码错误!') auth.login(request, user) if request.POST.get('auto_login') == 'true': request.session.set_expiry(60 * 60 * 24 * 14) else: request.session.set_expiry(0) # 查看是否有通知, 给用户进行通知 # 1. 检查消息中心 这个在模板中通过过滤器获取到了 # render['notic_list'] = [] # # 2. 检查是否有新的通知 # sys_count = get_wait_message(request.user.id, 'all') # if sys_count: # render['notic_list'].append({ # 'title': '您的消息中心有%s条新的消息未查看' % sys_count, # 'desc': "", # }) return response_json(retcode='SUCC', msg='LOGIN_SUCC')
def send_captcha(request, response, render, username=""): """获取短信验证码""" type = request.GET['type'] captcha = "" if type == 'register': if User.objects.filter(username=username): return response_json('FAIL', description='该手机号已被注册', msg='HasRegistered') # 校验图形验证码 if request.session.get('checkcode', '-1') != request.GET.get( 'checkcode', '-2'): return response_json('FAIL', description='验证码错误', msg='ErrorCheckCode') del_session_key(request.session, 'checkcode') captcha = send_ali_sms(username, type) elif type == 'currency_pay': captcha = send_ali_sms(username, type) elif type == 'modify_pwd': captcha = send_ali_sms(username, type) else: return response_json('FAIL', description='错误的请求!', msg='UnlegalRequest') captcha = captcha if request.user.is_superuser == 1 else "" return response_json('SUCC', description='注册码已发送', msg='SendSucc', captcha=captcha)
def sign_api(request, response, render): today = render['timenow'].date() sign_log = CbgUserSign.objects.filter(user_id=request.user.id) if sign_log: sign_log = sign_log[0] if sign_log.sign_time == today: return response_json(retcode='FAIL', msg='SignRepeat', description='请不要重复签到!') # 计算连续多久签到了 if today - sign_log.sign_time == datetime.timedelta(days=1): sign_log.continue_days += 1 else: sign_log.continue_days = 1 sign_log.last_sign_time = sign_log.sign_time sign_log.sign_time = today sign_log.save() else: sign_log = CbgUserSign.objects.create(user=request.user, sign_time=today, last_sign_time=today, continue_days=1) # 签到奖励 # 1. 盒币奖励 profile = UserProfile.objects.select_for_update().get(user=request.user) prize_currency = 100 if sign_log.continue_days < 7 else 200 CbgRechargeRecord.objects.create(user=request.user, quantity=prize_currency, give=0, status='已支付', left_quantity=profile.currency, create_time=render['timenow'], pay_time=render['timenow'], alias='签到') profile.currency += prize_currency profile.save() # 抽奖次数奖励 lotter = CbgLottery1.objects.select_for_update().filter(user=request.user) if not lotter: CbgLottery1.objects.create(lottery_times=3 + 3, last_lottery_time=today, user=request.user) else: lotter = lotter[0] if lotter.last_lottery_time == today: lotter.lottery_times += 3 else: lotter.lottery_times = 3 + 3 lotter.last_lottery_time = today lotter.save() return response_json(retcode='SUCC', msg='SignSucc', description='签到成功!', continue_days=sign_log.continue_days, prize_currency=prize_currency / 100.0, prize_logger_times=3)
def userreg(request, response, render): username = request.POST.get('username', '') password = request.POST.get('pwd', '') captcha = request.POST.get('captcha', '') # nickname = request.POST.get('nickname', '') # 校验账号 if len(username) != 11 or not username.isdigit(): return response_json(retcode='FAIL', msg='UsernamaError', description='账号必须在6-12位之间!') # 校验密码 if not 6 <= len(password) < 16: return response_json(retcode='FAIL', msg='PwdError', description='密码必须在6-16位之间!') if len(captcha) != 4 or (captcha.encode() != settings.redis3.get( 'register_captcha_%s' % username)): return response_json(retcode='FAIL', msg='CatpchaError', description='验证码错误') # if checkcode != request.session.get('checkcode'): # return response_json(retcode='FAIL', msg='CHECKCODE_ERROR', description='验证码错误!') # if 'checkcode' in request.session: # del request.session['checkcode'] # 校验昵称 # 1.规范性 # nickname = re.sub(u"[^\u0000-\uffff]", u"", nickname) # msg = validate_nick_name(nickname) # if msg: # return response_json(retcode='FAIL', msg="NICKNAME_UNAVAILABLE", description=msg) # 2.唯一性 # if UserProfile.objects.filter(nickname=nickname).exists(): # return response_json(retcode='FAIL', msg="NICKNAME_REPEAT", description='该昵称已被占用,请更换其它昵称。') # 校验验证码 # if captcha != gpub.redis3.get("captcha_%s" % username): # return response_json(retcode='FAIL', msg='CAPTCHA', description='手机验证码错误!') # gpub.redis3.delete("captcha_%s" % username) # 用户是否已经注册 if User.objects.filter(username=username).exists(): return response_json(retcode='FAIL', msg="UserExist", description='该账号已被注册') # 创建用户 user = User.objects.create_user(username=username, email='', password=password) user.save() UserProfile.objects.create(nickname=username, user_id=user.id) # 删除验证码 settings.redis3.delete("register_captcha_%s" % username) return response_json(retcode='SUCC', msg='SUCCESS', description='注册成功!')
def turnplate_begin(request, response, render): """转盘开始抽奖""" today = render['timenow'].date() rd = random.randint(1, 100) # 随机取1-100 # code = 2 code = 7 # 谢谢参与 30% res_code, error_txt = assigin_coupon(request.user, random.randint(1,2), '', auto_expire=datetime.timedelta(days=3)) if res_code == 0: try: lottery = CbgLottery1.objects.select_for_update().get(user=request.user, last_lottery_time=today) lottery.lottery_times -= 1 lottery.save() except CbgLottery1.DoesNotExist: return response_json(retcode='FAIL', msg='UnLegalVist', description='非法访问') return response_json(retcode='SUCC', msg="TurplateSucc", code=code, left_times=lottery.lottery_times) else: return response_json(retcode='FAIL', msg=res_code, description=error_txt) if 1 <= rd <= 30: code = 2 # 10盒币 5% elif 31 <= rd <= 35: code = 0 # 5盒币 10 elif 36 <= rd <= 45: code = 3 # 1盒币 15% elif 46 <= rd <= 60: code = 8 # 10积分 5% elif 61 <= rd <= 65: code = 1 # 5积分 10% elif 66 <= rd <= 75: code = 9 # 1积分 15% elif 76 <= rd <= 90: code = 6 # 优惠券1 elif 91 <= rd <= 95: code = 4 # 优惠券2 elif 95 <= rd <= 100: code = 7 return response_json(retcode='SUCC', msg="TurplateSucc", code=code)
def modify_pwd_api(request, response, render): captcha = request.POST.get('captcha', '') new_pwd = request.POST.get('new_pwd', '') if not (6 <= len(new_pwd) <= 16): return response_json(retcode='FAIL', msg='UnLegalPwd', description='密码格式错误!') if len(captcha) != 4 or (captcha.encode() != settings.redis3.get( 'modify_pwd_captcha_%s' % request.user.username)): return response_json(retcode='FAIL', msg='CatpchaError', description='验证码错误!') settings.redis3.delete("modify_pwd_captcha_%s" % request.user.username) request.user.set_password(new_pwd) request.user.save() auth.logout(request) return response_json(retcode='SUCC', msg='ModifyPwdSuccess', description='密码修改成功!')
def get_service_coupon_api(request, response, render, service_id): """获取某个活动可用优惠券的接口""" today = render['timenow'].date() # 1. 获取用户拥有的所有优惠券 my_coupon_list = CbgCouponUserRelation.objects.filter( user_id=request.user.id, status=0, acquire_time__lte=today, expire_time__gte=today, usage_time__isnull=True) if not my_coupon_list: return response_json(retcode='SUCC', result='[]') # 2. 获取这些优惠券使用的service my_coupon_id_list = [str(_c.coupon_id) for _c in my_coupon_list] # 3. 筛选目标优惠券 sql = 'select cbgcoupon_id from cbg_coupon_service where cbgcoupon_id in (' + ','.join( my_coupon_id_list) + ') and cbgservice_id=%s' res, _ = run_custom_sql(sql, service_id) vaild_coupon_id_list = [_x[0] for _x in res] if not vaild_coupon_id_list: return response_json(retcode='SUCC', queryset='[]') vaild_my_coupon_list = [ _c for _c in my_coupon_list if _c.coupon_id in vaild_coupon_id_list ] vaild_my_coupon_dict = dict( (_x.coupon_id, _x) for _x in vaild_my_coupon_list) # 4. 获取目标优惠券的信息 coupon_list = CbgCoupon.objects.filter(id__in=vaild_coupon_id_list) # 5. 组装优惠券的信息 result = [] for coupon in coupon_list: my_rel = vaild_my_coupon_dict.get(coupon.id) if my_rel: r = obj_2_json(my_rel, support_json=True) r.update(obj_2_json(coupon, support_json=True) ) # 奖券的过期时间是按relation中来算 不安coupon来算 因为有的券的过期时间是从领取后计算的 r['id'] = my_rel.id result.append(r) return response_json(retcode='SUCC', result=result)
def use_convert_api(request, response, render): code = request.GET.get('code', '') if len(code) != 36: return response_json(retcode='FAIL', msg='ErrorConvertCode', description='错误的兑换码!') try: convert = CbgConvertCode.objects.select_for_update().get(convert_code=code) except: return response_json(retcode='FAIL', msg='ErrorConvertCode', description='错误的兑换码!') if convert.receive_total >= convert.total_limit: return response_json(retcode='FAIL', msg='ConvertQuantityLimit', description='该兑换码已被领完!') if CbgConvertCodeLog.objects.filter(user=request.user, convert=convert).count() >= convert.quantity_limit: return response_json(retcode='FAIL', msg='ConvertUserLimig', description='您已兑换过!') if render['timenow'] > convert.end_time: return response_json(retcode='FAIL', msg='ConvertDeadline', description='该兑换码已过期!') if render['timenow'] < convert.start_time: return response_json(retcode='FAIL', msg='ConvertFuture', description='该兑换码未到领取时间!') # 货币奖励 content ="" if convert.obj_type == 2: profile = UserProfile.objects.select_for_update().get(user_id=request.user.id) CbgRechargeRecord.objects.create(user=request.user, quantity=convert.obj_quantity, give=0, status='已支付', left_quantity=profile.currency, create_time=render['timenow'], pay_time=render['timenow'], alias='兑换码') profile.currency += convert.obj_quantity profile.save() CbgConvertCodeLog.objects.create(user=request.user, convert=convert) content = "恭喜您,成功兑换到了%s个货币" % (convert.obj_quantity / 100.0) # 发放优惠券 elif convert.obj_type == 1: b, error = assigin_coupon(request.user, convert.obj_id, '', datetime.timedelta(days=7)) if b != 0: return response_json(retcode='FAIL', msg='ConvertCouponFail', description=error) coupon = CbgCoupon.objects.get(id=convert.obj_id) content = "恭喜您,成功兑换到了【%s】" % coupon.coupon_name convert.receive_total += 1 convert.save() return response_json(retcode='SUCC', msg='ConvertSucc', content=content)
def get_coupon_api(request, response, render, coupon_id): """领取优惠券的api""" ret_code, err_txt = assigin_coupon(request.user, coupon_id, "") return response_json(retcode="SUCC" if ret_code == 0 else 'FAIL', description=err_txt)