def post(self, request): """接收post登录请求""" form = SigninForm(request.POST) if form.is_valid(): # 表单验证成功,提取数据 email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') remember = form.cleaned_data.get('remember') # 用户登录验证 user = authenticate(request, username=email, password=password) if user: # 用户账号密码正确 if user.is_active: # 用户可登录 login(request, user) if remember: # 设置登录保存时间为默认两周 request.session.set_expiry(None) else: # 设置登录保存至浏览器关闭 request.session.set_expiry(0) pprint(request.GET) http_next = request.GET.get('next', '/') print(http_next) return restful.ok(data={'next': http_next}) else: return restful.authority_error('用户被冻结') else: return restful.parameters_error('账号或密码错误!') else: errors = form.get_errors() return restful.parameters_error(errors)
def post(self, request): """接受Post注册请求""" pprint(request.POST) form = SignupForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') username = form.cleaned_data.get('username') img_captcha = form.cleaned_data.get('img_captcha') ip = request.META.get('REMOTE_ADDR') cache_img_captcha = cache.get(ip) if cache_img_captcha.lower() != img_captcha.lower(): return restful.parameters_error('图形验证码不正确') else: try: user = WfUser.objects.create_user(email=email, username=username, password=password) login(request, user) request.session.set_expiry(0) http_next = request.GET.get('next', '/') return restful.ok(data={'next': http_next}) except: return restful.authority_error('该邮箱已被注册') else: errors = form.get_errors() return restful.parameters_error(errors)
def change_name(request): user = request.user name_re = re.compile(r'^[a-zA-Z][_a-zA-Z0-9]{2,19}$') new_name = request.POST.get('username', None) if user.is_authenticated: try: if new_name and name_re.search(new_name): user.username = new_name print(user) user.save() return restful.ok() else: return restful.parameters_error('请传递正确的用户名。') except: return restful.parameters_error('请传递正确的用户名。') else: return restful.authority_error('请登录后再修改用户名。')
def email_captcha(request): email = request.POST.get('email') print(email) if email: captcha, result = EmailCaptcha.gene_captcha(email) print('邮箱验证码:{}'.format(captcha)) if result: cache.set(email, captcha, 5 * 60) return restful.ok('验证码发送成功,请注意查收邮件') else: return restful.server_error('服务器发生问题,验证码发送失败!') else: return restful.parameters_error('邮箱不存在!')
def audit_riven(request): if request.user.is_authenticated: if request.user.game_name is not None: try: wfuser = request.user riven_name = request.POST.get('riven_name') properties = json.loads(request.POST.get('properties')) price = request.POST.get('price') file = request.FILES.get('file') name = file.name name_re = re.compile('(.png|.jpg)$', flags=re.IGNORECASE) img_type = name_re.search(name).group() if img_type: img_name = shortuuid.uuid() if os.path.exists( paths.get_path(f'media/wfusers/{wfuser.uid}/rivens' )) is False: os.makedirs( paths.get_path( f'media/wfusers/{wfuser.uid}/rivens')) img_path = paths.get_path( f'media/wfusers/{wfuser.uid}/rivens/{img_name}{img_type}' ) with open(img_path, 'wb') as imgfp: for chunk in file.chunks(): imgfp.write(chunk) Riven.objects.create( riven_name=riven_name, properties=properties, price=price, is_sell=SellState.IS_AUDIT.value, thumbnail= f'/media/wfusers/{wfuser.uid}/rivens/{img_name}{img_type}', seller=wfuser) return restful.ok(message='提交审核成功') else: return restful.parameters_error( '请上传正确的格式的图片,后台只接受PNG与JPG图片。') except: return restful.server_error('上架紫卡失败,请稍后再试。') else: return restful.server_error('需要通过游戏ID审核才能发布紫卡。') else: return restful.authority_error('需要登录才能发布紫卡。')
def filter_hotmodboxs(request, weapon_name): try: count = int(HOTMODBOXS_PAGE_COUNT) page = int(request.GET.get('page', 1)) if int(request.GET.get('page', 1)) > 0 else 1 start = (page - 1) * count end = start + count name = re.sub(r'_', r' ', weapon_name.strip()) modboxs = QueryHotModBoxs.filter_hot_modboxs_list( 'Weapon', name, slice={ 'start': start, 'end': end } ) if modboxs: return restful.ok(data=modboxs) else: return restful.parameters_error('已展示所有配卡!') except: return restful.parameters_error(message='page参数传递错误!')
def personal_detail(request): if request.method == 'GET': if request.user.is_authenticated: return render(request, 'wfusers/personal.html') else: return redirect(reverse('app_wfusers:signin')) elif request.method == 'POST': wfuser = request.user if wfuser.is_authenticated: data = { 'username': request.user.username, 'email': request.user.email, 'game_id': request.user.game_name, } return restful.ok(data=data) else: return restful.parameters_error(message='请登陆后再查看个人信息。')
def share_modboxs(request, warframe_name): warframe = request.POST.get('warframe', None) if warframe: warframe = json.loads(warframe) modboxs = request.POST.get('modboxs', None) if modboxs: modboxs = json.loads(modboxs) try: ModBox.objects.create( modboxs=modboxs, wfuser=request.user, warframe_id=warframe.get('name', None), type='Warframe' ) return restful.ok(data=modboxs) except: return restful.parameters_error('参数传递错误!')
def upload_file(request): try: file = request.FILES.get('file') name = file.name name_re = re.compile('(.png|.jpg)$', flags=re.IGNORECASE) img_type = name_re.search(name).group() if img_type: img = bytes() for chunk in file.chunks(): img += chunk riven_data = riven_property_ocr(img) return restful.ok(data=riven_data) else: return restful.parameters_error('请上传正确的格式的图片,只接受PNG与JPG图片。') except: return restful.server_error('图片识别失败')
def share_modboxs(request, weapon_name): weapon = request.POST.get('weapon', None) if weapon: weapon = json.loads(weapon) modboxs = request.POST.get('modboxs', None) if modboxs: modboxs = json.loads(modboxs) weapon_id = weapon.get('id', None) weapon = Weapon.objects.get(id=weapon_id) try: ModBox.objects.create( modboxs=modboxs, wfuser=request.user, weapon=weapon, type='Weapon' ) return restful.ok(data=modboxs) except: return restful.parameters_error('参数传递错误!')
def post(self, request, weapon_name): en_to_zh = EnToZh() if re.search(r'(?i)original', request.POST.get('methond', '')): w_name = re.sub(r'_', r' ', weapon_name.strip()) weapon = Weapon.objects.filter(name__iexact=w_name).first() serializer = WeaponsSerializer(weapon) original = serializer.data # TODO 对属性值进行转换或翻译 original['noise_level'] = en_to_zh.query_zh(original['noise_level']) original['type'] = en_to_zh.query_zh(original['type']) original['name'] = en_to_zh.query_zh(original['name']) original['fire_rate'] = float(original['fire_rate']) original['reload_time'] = float(original['reload_time']) original['magazine_size'] = int(original['magazine_size']) try: original['max_ammo'] = int(original['max_ammo']) except: original['max_ammo'] = 'N/A' return restful.ok(data={ 'methond': 'original', 'original': original }) elif re.search(r'(?i)up', request.POST.get('methond', '')): weapon = request.POST.get('weapon', None) if weapon: weapon = json.loads(weapon) mods = request.POST.get('mods', None) if mods: mods = json.loads(mods) return_up = { 'fire_rate': weapon.get('fire_rate'), 'magazine_size': weapon.get('magazine_size'), 'max_ammo': weapon.get('max_ammo'), 'reload_time': weapon.get('reload_time'), 'pattern': weapon.get('pattern'), } if mods and weapon: up_dict = {} for num_key, mod in mods.items(): # 提取所有属性 if mod and mod.get('property', None): for key, value in mod.get('property', None).items(): (attr_up, num_up, type_up) = get_up_property(key, value) if type_up and attr_up: if attr_up in up_dict: old_num, old_type = up_dict[attr_up] if old_type == type_up: up_dict[attr_up] = (old_num + num_up, type_up) else: if old_num > num_up: up_dict[attr_up] = (old_num - num_up, old_num) elif old_num < num_up: up_dict[attr_up] = (num_up - old_num, old_num) else: del up_dict[attr_up] else: up_dict[attr_up] = (num_up, type_up) pattern_up_dict = {} for key, (num_up, type_up) in up_dict.items(): # 计算固定属性的提升 if key in BASE_PROPERTY: if type(return_up[key]) != str: if type_up in [UP_TYPE.get('PERCENTAGE_INCREASE')]: return_up[key] = return_up[key] * (1 + num_up) elif type_up in [UP_TYPE.get('PERCENTAGE_REDUCE')]: return_up[key] = return_up[key] * (1 - num_up) elif key in ['reload_time']: if type(return_up[key]) != str: if type_up in [UP_TYPE.get('PERCENTAGE_INCREASE')]: return_up[key] = return_up[key] * (1 - num_up) elif type_up in [UP_TYPE.get('PERCENTAGE_REDUCE')]: return_up[key] = return_up[key] * (1 + num_up) else: pattern_up_dict[key] = (num_up, type_up) for p_key, pattern in return_up['pattern'].items(): # TODO 计算不同模式的提升 new_pattern = copy.deepcopy(pattern) new_pattern_up_dict = copy.deepcopy(pattern_up_dict) # 1.提取原始元素属性 element_list = [] dict_keys.get_keys(element_list, new_pattern['total_damage'], ELEMENT_PROPERTY) for e_key in element_list: if e_key in new_pattern_up_dict: value = new_pattern['total_damage'][1].pop(e_key) value = value / new_pattern['total_damage'][0] if new_pattern_up_dict[e_key][1] in [UP_TYPE.get('PERCENTAGE_INCREASE')]: new_pattern_up_dict[e_key] = ( new_pattern_up_dict[e_key][0] + value, UP_TYPE.get('PERCENTAGE_INCREASE')) else: if value > new_pattern_up_dict[e_key][0]: new_pattern_up_dict[e_key] = ( value - new_pattern_up_dict[e_key][0], UP_TYPE.get('PERCENTAGE_INCREASE')) else: new_pattern_up_dict[e_key][0] = ( new_pattern_up_dict[e_key][0] - value, UP_TYPE.get('PERCENTAGE_REDUCE')) else: value = new_pattern['total_damage'][1].pop(e_key) value = value / new_pattern['total_damage'][0] new_pattern_up_dict[e_key] = (value, UP_TYPE.get('PERCENTAGE_INCREASE')) # 2.组合元素属性 element_list = [] dict_keys.get_keys(element_list, new_pattern_up_dict, ELEMENT_PROPERTY) for e_key_list in [element_list[0:2], element_list[2:4], element_list[4:6]]: if (len(e_key_list) == 2): new_e_key = combined_element(e_key_list) value_one, type_one = new_pattern_up_dict.pop(e_key_list[0]) value_two, type_two = new_pattern_up_dict.pop(e_key_list[1]) if type_one == type_two: new_pattern_up_dict[new_e_key] = (value_one + value_two, type_one) else: if value_one > value_two: new_pattern_up_dict[new_e_key] = (value_one - value_two, type_one) elif value_one < value_two: new_pattern_up_dict[new_e_key] = (value_two - value_one, type_two) # 3.计算模式提升 for up_key, (num_up, type_up) in new_pattern_up_dict.items(): if up_key in PHYSICAL_PROPERTY + ELEMENT_PROPERTY + COMBINED_PROPERTY: if type_up is UP_TYPE.get('PERCENTAGE_INCREASE'): value = new_pattern['total_damage'][1].get(up_key, 0) value = value + new_pattern['total_damage'][0] * num_up if value > 0: new_pattern['total_damage'][1][up_key] = value elif type_up is UP_TYPE.get('PERCENTAGE_REDUCE'): value = new_pattern['total_damage'][1].get(up_key, 0) value = value + new_pattern['total_damage'][0] * num_up if value > 0: new_pattern['total_damage'][1][up_key] = value elif up_key in new_pattern: if up_key is not 'total_damage': if type_up is UP_TYPE.get('PERCENTAGE_INCREASE'): new_pattern[up_key] = new_pattern[up_key] * (1 + num_up) elif type_up is UP_TYPE.get('PERCENTAGE_REDUCE'): new_pattern[up_key] = new_pattern[up_key] * (1 - num_up) # 4.计算基础提升 if 'total_damage' in new_pattern_up_dict: num_up, type_up = new_pattern_up_dict.get('total_damage') total_template = copy.deepcopy(new_pattern['total_damage'][1]) if type_up is UP_TYPE.get('PERCENTAGE_INCREASE'): for up_key in total_template.keys(): value = new_pattern['total_damage'][1].get(up_key, 0) value = value + new_pattern['total_damage'][0] * num_up if value > 0: new_pattern['total_damage'][1][up_key] = value else: del new_pattern['total_damage'][1][up_key] elif type_up is UP_TYPE.get('PERCENTAGE_REDUCE'): for up_key in total_template.keys(): value = new_pattern['total_damage'][1].get(up_key, 0) value = value - new_pattern['total_damage'][0] * num_up if value > 0: new_pattern['total_damage'][1][up_key] = value else: del new_pattern['total_damage'][1][up_key] return_up['pattern'][p_key] = new_pattern return restful.ok(data={ 'methond': 'up', 'up': return_up }) return restful.parameters_error(message='请传递正确的请求类型参数!') return restful.parameters_error(message='请传递正确的请求类型参数!')
def upload_file(request): try: file = request.FILES.get('file') name = file.name name_re = re.compile('(.png|.jpg)$', flags=re.IGNORECASE) if name_re.search(name): wfuser = request.user user_game, judge = WfUserGame.objects.get_or_create(wfuser=wfuser) if judge: img_name = f'{wfuser.username}+{wfuser.uid}{name_re.search(name).group()}' if os.path.exists(paths.get_path( f'media/wfusers/{wfuser.uid}')) is False: os.makedirs(paths.get_path(f'media/wfusers/{wfuser.uid}')) with open( paths.get_path( f'media/wfusers/{wfuser.uid}/{img_name}'), 'wb') as imgfp: for chunk in file.chunks(): imgfp.write(chunk) user_game.first_img = f'/media/wfusers/{wfuser.uid}/{img_name}' user_game.game_status = WfUserGame.AUDIT_STATUS user_game.save() add_img_thread = threading.Thread(target=add_wfuser_gamename, args=(wfuser, user_game)) add_img_thread.start() return restful.ok('后台以开始审核,请注意查看状态。') else: img_dict = user_game.other_imgs if img_dict: num = len(img_dict) + 1 img_name = f'{wfuser.username}+{wfuser.uid}_{num}{name_re.search(name).group()}' if os.path.exists( paths.get_path( f'media/wfusers/{wfuser.uid}')) is False: os.makedirs( paths.get_path(f'media/wfusers/{wfuser.uid}')) with open( paths.get_path( f'media/wfusers/{wfuser.uid}/{img_name}'), 'wb') as imgfp: for chunk in file.chunks(): imgfp.write(chunk) img_dict[ f'{num}'] = f'/media/wfusers/{wfuser.uid}/{img_name}' user_game.game_status = WfUserGame.MANUAL_STATUS user_game.save() wfuser.game_name = None wfuser.save() return restful.ok('后台已开始审核,请注意查看状态。') else: img_name = f'{wfuser.username}+{wfuser.uid}_1{name_re.search(name).group()}' if os.path.exists( paths.get_path( f'media/wfusers/{wfuser.uid}')) is False: os.makedirs( paths.get_path(f'media/wfusers/{wfuser.uid}')) with open( paths.get_path( f'media/wfusers/{wfuser.uid}/{img_name}'), 'wb') as imgfp: for chunk in file.chunks(): imgfp.write(chunk) img_dict['1'] = f'/media/wfusers/{wfuser.uid}/{img_name}' user_game.game_status = WfUserGame.MANUAL_STATUS user_game.save() wfuser.game_name = None wfuser.save() return restful.ok('后台已开始审核,请注意查看状态。') else: return restful.parameters_error('请上传正确的格式的图片,后台只接受PNG与JPG图片。') except: return restful.server_error('服务器处理请求失败,请稍后再试。')
def post(self, request, warframe_name): """获取战甲信息""" if request.POST.get('methond', None) == 'original': w_name = re.sub(r'(Prime|prime)', r' Prime', warframe_name) ws_query = Warframe.objects.filter(name=w_name.strip()) warframe = ws_query[0] warframe.health *= 3 warframe.shield *= 3 warframe.power = round(warframe.power * 1.5) warframe.sprint_speed = round(warframe.sprint_speed, 2) warframe.power_strength = round(warframe.power_strength, 2) warframe.power_duration = round(warframe.power_duration, 2) warframe.power_range = round(warframe.power_range, 2) warframe.power_efficiency = round(warframe.power_efficiency, 2) # 返回原始信息 serializer = WarframesSerializer(warframe) original = serializer.data return restful.ok(data={ 'methond': 'original', 'original': original }) elif request.POST.get('methond', None) == 'up': # 计算增强信息并返回 # 1.获取数据 warframe = request.POST.get('warframe', None) if warframe: warframe = json.loads(warframe) mods = request.POST.get('mods', None) if mods: mods = json.loads(mods) # 2.选取需要的属性 up = { 'health': round(warframe.get('health')), 'shield': round(warframe.get('shield')), 'armor': round(warframe.get('armor')), 'power': round(warframe.get('power')), 'sprint_speed': round(warframe.get('sprint_speed'), 2), 'power_strength': round(warframe.get('power_strength'), 2), 'power_duration': round(warframe.get('power_duration'), 2), 'power_range': round(warframe.get('power_range'), 2), 'power_efficiency': round(warframe.get('power_efficiency'), 2), } # 3.遍历数据进行计算 for key, mod in mods.items(): if mod and mod.get('property', None) and warframe: for key, value in mod.get('property', None).items(): (attr_up, num_up, type_up) = get_up_property(key, value) if attr_up and type_up: if type_up == UP_TYPE.get('PERCENTAGE_INCREASE'): up[attr_up] += warframe[attr_up] * num_up elif type_up == UP_TYPE.get('PERCENTAGE_REDUCE'): up[attr_up] -= warframe[attr_up] * num_up elif type_up == UP_TYPE.get('NUMERICAL_INCREASE'): up[attr_up] = up[attr_up] + num_up if up[attr_up] + num_up >= 0 else 0 elif type_up == UP_TYPE.get('NUMERICAL_REDUCE'): up[attr_up] = up[attr_up] - num_up if up[attr_up] - num_up >= 0 else 0 else: pass response_up = { 'health': round(up.get('health')), 'shield': round(up.get('shield')), 'armor': round(up.get('armor')), 'power': round(up.get('power')), 'sprint_speed': round(up.get('sprint_speed'), 2), 'power_strength': round(up.get('power_strength'), 2), 'power_duration': round(up.get('power_duration'), 2), 'power_range': round(up.get('power_range'), 2), 'power_efficiency': round(up.get('power_efficiency'), 2), } return restful.ok(data={ 'methond': 'up', 'up': response_up }) return restful.parameters_error(message='请传递正确的请求类型参数!')