def login_view(request): form = forms.LoginForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') password = form.cleaned_data.get('password') remember = form.cleaned_data.get('remember') user = authenticate(request, telephone=telephone, password=password) if user: if user.is_active: login(request, user) if remember: request.session.set_expiry(None) else: request.session.set_expiry(0) return restful.success() # return JsonResponse({'code': 200, 'message': '', 'data': {}}) else: return restful.unauth_error(message='您的账号已被冻结!') # return JsonResponse({'code': 405, 'message': '您的账号已被冻结!', 'data':{}}) else: return restful.parms_error(message='手机号或密码错误!') # return JsonResponse({'code': 400, 'message': '手机号或密码错误!', 'data':{}}) else: return restful.parms_error(message=form.get_errors()) # return JsonResponse({'code': 400, 'message': '参数输入错误!', 'data': form.get_errors()})
def post(self): form = RestPassword(request.form) print(form.validate()) if form.validate(): oldpassword = form.oldpassword.data newpassword = form.newpassword.data print(oldpassword, newpassword) user = g.cms_user if user.password == oldpassword: user.password = newpassword db.session.commit() return restful.success('修改密码成功') else: return restful.parms_error('旧密码错误') else: return restful.parms_error(form.get_errors())
def un_good_post(): post_id = request.args.get('post_id') post = PostsModel.query.get(post_id) if post_id: highligth = HighLightPostModel.query.filter_by(post_id=post_id).first() db.session.delete(highligth) db.session.commit() return restful.success('取消加精成功') else: return restful.parms_error('取消加精失败')
def addboard(): form = AddBoard(request.form) if form.validate(): name = form.name.data # 添加模块名 board = BoardsModel(name=name) db.session.add(board) db.session.commit() return restful.success('添加成功') return restful.parms_error('添加失败')
def post(self): form = ResetEmail(request.form) print(form.validate()) if form.validate(): email = form.email.data g.cms_user.e_mail = email db.session.commit() return restful.success('chenggong') else: return restful.parms_error('cuowu')
def good_post(): post_id = request.args.get('post_id') post = PostsModel.query.get(post_id) print(post_id) if post_id: highligth = HighLightPostModel(post=post) db.session.add(highligth) db.session.commit() return restful.success('加精成功') else: return restful.parms_error('加精失败')
def delete_post(): if request.method == 'GET': posts = PostsModel.query.all() context = {'posts': posts} return render_template('cms/cms_posts.html', **context) else: post_id = request.form.get('post_id') post = PostsModel.query.get(post_id) if post: db.session.delete(post) db.session.commit() return restful.success('删除成功') else: return restful.parms_error('帖子不存在')
def addpost(): if request.method == 'GET': boards = BoardsModel.query.all() context = {'boards': boards} return render_template('front/front_post.html', **context) else: form = AddPostForm(request.form) if form.validate(): title = form.title.data board_id = form.board_id.data content = form.content.data # 1111 post = PostsModel(title=title, content=content) post.board_id = board_id post.author = g.front_user db.session.add(post) db.session.commit() return restful.success('发帖成功') else: return restful.parms_error('发帖失败')