def write_msg(request): msg = request.POST.get('msg') riven_id = request.POST.get('riven_id') gfw = DFAFilter() path = paths.get_path(f'sensitive_words.txt') gfw.parse(path) msg = gfw.filter(msg) wfuser = request.user if wfuser.is_authenticated: if wfuser.game_name is not None: try: RivenMsg.objects.create(content=msg, view_state=False, riven_id=riven_id, writer=wfuser) print('留言成功') return restful.ok('留言成功') except: return restful.server_error('发表留言失败,请稍后再试。') else: return restful.authority_error('需要通过审核游戏ID后才能留言。') else: return restful.authority_error('请登录后留言。')
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 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 add_self_children_msg(request): wfuser = request.user if wfuser.game_name is not None: try: msg = request.POST.get('msg') parent_msg_id = request.POST.get('parent_msg_id') print(msg) gfw = DFAFilter() path = paths.get_path(f'sensitive_words.txt') gfw.parse(path) msg = gfw.filter(msg) parent_msg = RivenMsg.objects.filter(id=parent_msg_id).first() riven_id = parent_msg.riven_id RivenMsg.objects.create(view_state=True, content=msg, riven_id=riven_id, parent_id=parent_msg_id, writer=wfuser) return restful.ok('回复留言成功') except: return restful.ok('回复留言失败') else: return restful.authority_error('需要通过审核游戏ID后才能回复留言。')
def new_msg(request): if request.user.is_authenticated: my_riven_id = Riven.objects.filter(seller=request.user) msgs = RivenMsg.objects.filter( view_state=False).values('riven_id').distinct() new_msg_riven = msgs.filter(riven__id__in=my_riven_id) new_msg_riven = list(new_msg_riven) data = new_msg_riven return restful.ok(data=data) else: return restful.authority_error(data=[])
def riven_collection(request): if request.user.is_authenticated: try: wfuser = request.user riven_id = request.POST.get('riven_id') riven = Riven.objects.filter(id=riven_id).first() riven.collector.add(wfuser) riven.save() return restful.ok('收藏成功') except: return restful.server_error('收藏失败,请稍后再试') else: return restful.authority_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 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 index(request): if request.method == 'GET': if request.user.is_authenticated: return render(request, 'riven/riven_mygoods.html') else: return redirect(reverse('rivenmarket:index')) else: if request.user.is_authenticated: try: wfuser = request.user rivens = Riven.objects.filter(seller_id=wfuser.uid, is_sell__in=[ SellState.IS_SELL.value, SellState.IS_AUDIT.value ]).order_by('-pub_time') riven_serializer = RivenSerializer(rivens, many=True) return restful.ok(data=riven_serializer.data) except: return restful.server_error('加载失败') else: return restful.authority_error('请先登录')