def disfineforum(): post_id = request.form.get("post_id") if post_id: post = Post.query.get(post_id) if not post: return restful.args_error("没有这个帖子") else: plusfine = Plusfine.query.filter_by(post_id=post_id).first() db.session.delete(plusfine) db.session.commit() return restful.success() else: return restful.args_error("表单数据传输错误")
def uarea(): form = UpdateAreaForm(request.form) if form.validate(): name = form.name.data area_id = form.area_id.data area = Area.query.get(area_id) if area: area.name = name db.session.commit() return restful.success() else: return restful.args_error("没有这个版块") else: return restful.args_error(form.get_error())
def finefroum(): # 帖子加精的话会提交一个含有帖子id的表单 post_id = request.form.get("post_id") if post_id: post = Post.query.get(post_id) if not post: return restful.args_error("没有这个帖子") else: plusfine = Plusfine() plusfine.post = post db.session.add(post) db.session.commit() return restful.success() else: return restful.args_error("表单数据传输错误")
def post(self): form = LoginForm(request.form) if form.validate(): telephone = form.telephone.data password = form.password.data remember = form.remember.data user = FrontUser.query.filter_by(telephone=telephone).first() if user and user.check_password(password): # 将用户的id存储起来用作后面判定用户是否登录 session[config['development'].FRONTUSERID] = user.id if remember: session.permanent = True return restful.success() else: return restful.args_error("手机号或者密码错误!") else: return restful.args_error(message = form.get_error())
def sendmessage(): form = SendmessageForm(request.form) if form.validate(): source = list(map(lambda x: str(x), range(0, 10))) code = "".join(random.sample(source, 6)) phonenumber = form.telephone.data testphone = FrontUser.query.filter_by(telephone=phonenumber).first() if testphone: return restful.args_error("此手机号已注册过账号,请检查或者去登录!") if aliyunmessage.send_sms(phone_numbers=phonenumber, code=code): # 将验证码存储到memached中 mycache.set(phonenumber, code) return restful.success() else: return restful.args_error("短信验证码发送失败") else: return restful.args_error("参数错误")
def unlock(): telephone = request.form.get('telephone') user = FrontUser.query.filter_by(telephone=telephone).first() if user: user.locked = False db.session.commit() return restful.success() else: return restful.args_error("出现了小错误!")
def post(self): form = RegisterForm(request.form) if form.validate(): telephone = form.telephone.data username = form.username.data password = form.password1.data # 检验用户名 usertest = FrontUser.query.filter_by(username=username).first() if usertest: return restful.args_error(message="该用户名已被占用!") user = FrontUser(telephone = telephone, username = username, password = password) db.session.add(user) db.session.commit() import flask_whooshalchemyplus flask_whooshalchemyplus.index_one_model(FrontUser) return restful.success() else: return restful.args_error(message = form.get_error())
def delarea(): id = request.form.get('area_id') area = Area.query.get(id) if area: db.session.delete(area) db.session.commit() return restful.success() else: return restful.args_error("没有这个版块")
def delcarousel(): id = request.form.get('carousel_id') carousel = Carousel.query.get(id) if carousel: db.session.delete(carousel) db.session.commit() return restful.success() else: return restful.args_error("没有这个轮播图")
def post(self): form = ResetEmailForm(request.form) if form.validate(): email = form.email.data g.cms_user.email = email db.session.commit() return restful.success() else: message = form.get_error() return restful.args_error(message=message)
def delcomment(): com_id = request.form.get("com_id") comment = Comment.query.filter_by(id=com_id).first() post = Post.query.filter_by(id=comment.post_id).first() if comment: post.cnumber = post.cnumber - 1 db.session.delete(comment) db.session.commit() return restful.success() else: return restful.args_error("出了点小问题")
def post(self): form = ResetPwdForm(request.form) if form.validate(): oldpwd = form.oldpwd.data newpwd = form.newpwd.data # 因为在修改密码时,用户已经登录,所以Flask上下文全局对象g存储的有其相关信息 user = g.cms_user # 如果原密码正确 if user.check_password(oldpwd): user.password = newpwd db.session.commit() # 因为使用的是ajax所以需要返回json数据 # 定义的返回码200代表成功,无返回消息 # 返回码为400 代表失败, return restful.success() else: return restful.args_error("原密码错误") else: message = form.get_error() return restful.args_error(message=message)
def ucarousel(): form = UpdateCarouselForm(request.form) if form.validate(): name = form.name.data picture_url = form.pic_url.data next_url = form.next_url.data weight = form.weight.data carousel_id = form.carousel_id.data carousel = Carousel.query.get(carousel_id) if carousel: carousel.name = name carousel.picture_url = picture_url carousel.next_url = next_url carousel.weight = weight db.session.commit() return restful.success() else: return restful.args_error("没有这个轮播图") else: return restful.args_error(form.get_error())
def sendcaptcha(): user = g.cms_user email = request.args.get('email') #获取前台发来的email if not email: return restful.args_error("参数传递错误") if email == user.email: return restful.args_error("您的邮箱账号无改变") # 生成验证码 source = list(string.ascii_letters) source.extend(map(lambda x: str(x), range(0, 10))) captcha = "".join(random.sample(source, 6)) # 开始发送邮件 send_mail(to=email, subject="验证码", template='common/email/captcha', captcha=captcha, user=user) # 以键值对的形式存储email和验证码 mycache.set(email, captcha) return restful.success()
def addarea(): form = AddAreaForm(request.form) if form.validate(): name = form.name.data area = Area(name=name) db.session.add(area) import flask_whooshalchemyplus flask_whooshalchemyplus.index_one_model(Area) db.session.commit() return restful.success() else: return restful.args_error(form.get_error())
def delpost(): id = request.form.get('post_id') post = Post.query.get(id) if post: area = post.area area.number = area.number - 1 db.session.delete(post) db.session.commit() return restful.success() else: return restful.args_error("没有这个帖子")
def addcomment(): form = CommentForm(request.form) if form.validate(): content = form.content.data post_id = form.post_id.data post = Post.query.get(post_id) # 先判断帖子是否存在 if post: # 若帖子存在则判断用户是否被锁定 if g.front_user.locked: return restful.args_error("此账号已被封禁,若误封请联系管理员邮箱[email protected]") comment = Comment(content = content) comment.author = g.front_user comment.post = post post.cnumber = post.cnumber + 1 db.session.add(comment) db.session.commit() return restful.success() else: return restful.args_error("帖子不存在!") else: return restful.args_error(form.get_error())
def post(self): form = PostForm(request.form) if form.validate(): theme = form.theme.data content = form.content.data area_id = form.area_id.data # 根据版块id找到对应的版块 area = Area.query.filter_by(id = area_id).first() if not area: return restful.args_error("请输入已存在的版块!") if g.front_user.locked: return restful.args_error("此账号已被封禁,若误封请联系管理员邮箱[email protected]") post = Post(theme = theme, content = content) area.number = area.number + 1 post.area = area post.author = g.front_user db.session.add(post) import flask_whooshalchemyplus flask_whooshalchemyplus.index_one_model(Post) db.session.commit() return restful.success() else: return restful.args_error(message = form.get_error())
def addcarousel(): form = AddCarouselForm(request.form) if form.validate(): name = form.name.data picture_url = form.pic_url.data next_url = form.next_url.data weight = form.weight.data carousel = Carousel(name=name, picture_url=picture_url, next_url=next_url, weight=weight) db.session.add(carousel) db.session.commit() return restful.success() else: return restful.args_error(form.get_error())