def dbanner(): banner_id = request.form.get('banner_id') if not banner_id: return restful.params_error(message="请传入轮播图ID!") banner = BannerModel.query.get(banner_id) if not banner: return restful.params_error(message="没有这个轮播图!") db.session.delete(banner) db.session.commit() return restful.success()
def dpost(): post_id = request.form.get('post_id') if not post_id: return restful.params_error(message="请传入帖子ID!") post = PostModel.query.get(post_id) if not post: return restful.params_error(message="没有这篇帖子!") db.session.delete(post) db.session.commit() return restful.success()
def aboard(): form = AddBoardForm(request.form) if form.validate(): name = form.name.data board = BoardModel(name=name) db.session.add(board) db.session.commit() return restful.success() else: return restful.params_error(message=form.get_errors())
def dboard(): board_id = request.form.get("board_id") print(board_id) if not board_id: return restful.params_error(message="请传入板块ID!") board = BoardModel.query.get(board_id) if not board: return restful.params_error(message="没有这个板块!") db.session.delete(board) db.session.commit() return restful.success()
def uhpost(): post_id = request.form.get('post_id') if not post_id: return restful.params_error(message="请传入帖子ID!") post = PostModel.query.get(post_id) if not post: return restful.params_error(message="没有这篇帖子!") highlight = HighlightPostModel.query.filter_by(post_id=post_id).first() db.session.delete(highlight) db.session.commit() return restful.success()
def post(self): form = FrontSignupFrom(request.form) if form.validate(): telephone = form.telephone.data username = form.username.data password = form.password1.data user = FrontUserModel(telephone=telephone, username=username, password=password) db.session.add(user) db.session.commit() return restful.success() else: return restful.params_error(message=form.get_errors())
def hpost(): post_id = request.form.get('post_id') if not post_id: return restful.params_error(message="请传入帖子ID!") post = PostModel.query.get(post_id) if not post: return restful.params_error(message="没有这篇帖子!") highlight = HighlightPostModel() highlight.post = post db.session.add(highlight) db.session.commit() return restful.success()
def post(self): form = ResetEmailForm(request.form) if form.validate(): email = form.email.data email1 = CmsUserModel.query.filter_by(email=email).first() if email1: return restful.params_error(message="该邮箱已被注册!") g.cms_user.email = email db.session.commit() return restful.success() else: return restful.params_error(form.get_errors())
def abanner(): form = AddBannerForm(request.form) if form.validate(): name = form.name.data image_url = form.image_url.data link_url = form.link_url.data priority = form.priority.data banner = BannerModel(name=name, image_url=image_url, link_url=link_url, priority=priority) db.session.add(banner) db.session.commit() return restful.success() else: return restful.params_error(message=form.get_errors())
def post(self): form = ResetpwdForm(request.form) if form.validate(): newpwd = form.newpwd2.data cms_user = g.cms_user if cms_user: cms_user.password = newpwd db.session.commit() return restful.success() else: return restful.params_error(message="用户不存在!") else: return restful.params_error(message=form.get_errors())
def uboard(): form = UpdateBoardForm(request.form) if form.validate(): board_id = form.board_id.data name = form.name.data board = BoardModel.query.get(board_id) if not board: return restful.params_error(message="板块不存在!") board.name = name db.session.commit() return restful.success() else: return restful.params_error(message=form.get_errors())
def post(self): form = FrontSigninForm(request.form) if form.validate(): telephone = form.telephone.data password = form.password.data remember = form.remember.data user = FrontUserModel.query.filter_by(telephone=telephone).first() if user and user.check_pwd(password): session[config.FRONT_USER_ID] = user.id if remember: session.permanent = True return restful.success() else: return restful.params_error(message="手机号或者密码错误!") else: return restful.params_error(message=form.get_errors())
def sms_captcha(): __business_id = uuid.uuid1() form = SMSCaptchaForm(request.form) if form.validate(): telephone = form.telephone.data num = string.digits captcha = ''.join(random.sample(num, 4)) # params = u'{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"}' params = {"code":captcha,"product":"test"} if demo_sms_send.send_sms(__business_id, "18986205506", "泡泡茶壶", "SMS_122284746", params): memcaches.set(telephone, captcha) return restful.success() else: return restful.params_error(message="短信验证码发送失败!") else: return restful.params_error(message="参数错误!")
def add_comment(): form = AddCommentForm(request.form) if form.validate(): content = form.content.data post_id = form.post_id.data post = PostModel.query.get(post_id) if post: comment = CommentModel(content=content) comment.post = post comment.author = g.front_user db.session.add(comment) db.session.commit() return restful.success() else: return restful.params_error(message="没有这篇文章!") else: return restful.params_error(message=form.get_errors())
def post(post_id, type): if not post_id: post_id = request.form.get('post_id') if not type: type = request.form.get('type') if not post_id: return restful.params_error('此帖子ID不存在') post = PostModel.query.get(post_id) if not post: return restful.params_error('未找到此帖子') try: func = getattr(PostFuncView, type) except Exception as e: return restful.params_error('不支持此方法:%s' % type) func(post) apply = Apply.query.filter(Apply.post_id == post_id and Apply.type == type).first() db.session.delete(apply) db.session.commit() return restful.success(message='操作成功')
def ubanner(): form = UpdateBannerFrom(request.form) if form.validate(): banner_id = form.banner_id.data name = form.name.data image_url = form.image_url.data link_url = form.link_url.data priority = form.priority.data banner = BannerModel.query.get(banner_id) if banner: banner.name = name banner.image_url = image_url banner.link_url = link_url banner.priority = priority db.session.commit() return restful.success() else: return restful.params_error(message="没有这个轮播图!") else: return restful.params_error(message=form.get_errors())
def ubanner(): form = UpdateBannerForm(request.form) if form.validate(): banner_id = form.banner_id.data name = form.name.data image_url = form.image_url.data link_url = form.link_url.data priority = form.priority.data banner = BannerModel.query.get(banner_id) if banner: banner.name = name banner.image_url = image_url banner.link_url = link_url banner.priority = priority db.session.commit() return restful.success() else: return restful.params_error(message='没有这个轮播图!') else: return restful.params_error(message=form.get_error())
def email_captcha(): # /email_capthca/[email protected] email = request.args.get('email') if not email: return restful.params_error('请传递邮箱参数!') # source.extend(["0","1","2","3","4","5","6","7","8","9"]) source = list(string.ascii_letters) source.extend(map(lambda x: str(x), range(0, 10))) captcha = "".join(random.sample(source, 6)) # 给这个邮箱发送邮件 # message = Message('Python论坛邮箱验证码',recipients=[email],body='您的验证码是:%s'%captcha) # try: # mail.send(message) # except: # return restful.server_error() send_mail.delay('Python论坛邮箱验证码', [email], '您的验证码是:%s' % captcha) zlcache.set(email, captcha) return restful.success()
def crgroup(): if request.method == 'GET': return render_template('groups/create_group.html') else: form = CreateGroupForm(request.form) if form.validate(): user = g.front_user group_img = form.group_img.data group_name = form.group_name.data desc = form.desc.data group = GroupModel(name=group_name, create_user=user.id, desc=desc, avatar=group_img) group.point = 5 user.groups.append(group) db.session.add(group) db.session.commit() return restful.success() else: return restful.params_error(get_error(form))
def ucusers(): # return '功能暂未实现' form = UpdateCuserrForm(request.form) if form.validate(): cuser_id = form.cuser_id.data # 传递过来的用户id 11 role_id = form.role.data # 修改后的用户角色 4 cusers = CMSUser.query.filter_by(id=cuser_id).first() # 查找该用户 roles = CMSRole.query.filter_by(id=role_id).first() # 查找该用户对应的角色 if cusers: # 删除原先的用户-角色关系数据 for tmp in cusers.roles: cusers.roles.remove(tmp) db.session.commit() # 再重新添加一条用户-角色关系数据 cusers.roles.append(roles) db.session.commit() return restful.success() else: return restful.params_error(message='没有找到该用户~~~') else: return restful.params_error(form.get_error())
def email_captcha(): email = request.args.get('email') if not email: return restful.params_error('请输入邮箱') ''' 生成随机验证码,保存到数据库中,然后发送验证码,与用户提交的验证码对比 ''' captcha = str(uuid.uuid1())[:6] # 随机生成6位验证码 # user = g.cms_user # user.catptcha_code = captcha # db.session.commit() # 把验证码保存到数据库/存放在memcached中 # 给用户提交的邮箱发送邮件 # message = Message('Python论坛邮箱验证码', recipients=[email], body='您的验证码是:%s' % captcha) # # try: # mail.send(message) # 发送 # except: # return restful.server_error() send_mail.delay('Python论坛邮箱验证码', [email], '您的验证码是:%s' % captcha) mbcache.set(email, captcha) return restful.success()
def sms_captcha(): # 接口加密 # 1. telephone # 2. timestamp # 3. md5(timestamp+telephone+salt) form = SMSCaptchaForm(request.form) if form.validate(): telephone = form.telephone.data # 发送验证码 captcha = Captcha.gene_text(number=4) # if qcloud_sms.singleSender([telephone], [captcha]): # # 存储短信验证码到memcached中 # ycache.set(telephone, captcha) # return restful.success() # else: # return restful.params_error('短信验证码发送失败!') tasks.send_sms_captcha.delay([telephone], [captcha]) ycache.set(telephone, captcha) return restful.success() else: return restful.params_error(message="参数错误!")
def post(self): form = ResetpwdForm(request.form) if form.validate(): oldpwd = form.oldpwd.data newpwd = form.newpwd.data user = g.cms_user if user.check_password(oldpwd): user.password = newpwd db.session.commit() # 返回json数据 # return jsonify({ "code":200,"message":"" }) return restful.success() else: # return jsonify({ "code":400,"message":"旧密码错误" }) # return restful.params_error("旧密码错误") return restful.params_error(form.get_errors()) else: # message = form.get_errors() # return jsonify({ "code":400,"message":message}) return restful.params_error(form.get_errors())
def setting_count(): if request.method == 'GET': return render_template('front/setting_count.html') else: form = ResetPwdForm(request.form) if form.validate(): captcha = form.captcha.data oldpwd = form.oldpwd.data newpwd = form.newpwd.data user = g.front_user if user and user.check_pwd(oldpwd): if oldpwd == newpwd: return restful.params_error('新旧密码不能一致~') user.password = newpwd db.session.commit() mychache.delete(captcha) return restful.success() else: return restful.params_error('旧密码错误') else: return restful.params_error(message=get_error(form))
def post(self): # 验证登录 form = SigninForm(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() print(password) print(user.check_password(password)) print(user) if user and user.check_password(password): session[config.FRONT_USER_ID] = user.id if remember: session.permanent = True return restful.success() else: return restful.params_error(message='手机号码或密码错误!') else: return restful.params_error(message=form.get_error())
def register(request): form = RegisterForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') password = form.cleaned_data.get('password1') username = form.cleaned_data.get('username') print(telephone, username, password) #往数据库中存储 user = User.objects.create_user(telephone=telephone, username=username, password=password) #存储成功 自动完成登录 login(request, user) #返回成功的消息 return restful.success() else: #获取表单错误信息 errors = form.get_errors() print(errors) return restful.params_error(message=errors)
def email_captcha(): email = request.args.get('email') validate_res = validate_email(email) if not validate_res['flag']: return restful.params_error(message=validate_res['message']) source = list(string.ascii_lowercase) + list(string.digits) captcha = "".join(random.sample(source, 6)) try: send_email() my_redis.set(email, subject='CMS系统修改邮箱验证码', template='email/change_email', captcha=captcha, user=current_user, ex=300) print('生成的验证码', captcha) except Exception: return restful.server_error() return restful.success(message="邮件发送成功请注意查收!")
def publish_post(): if request.method == 'GET': boards = Board.query.all() return render_template('forum/publish_post.html', boards=boards) else: form = PubPostForm(request.form) if form.validate(): title = form.title.data content = form.content.data board_id = form.board_id.data board = Board.query.get(board_id) if not board: return restful.param_error(message='There is no this board') post = Post(title=title, content=content, board_id=board_id) post.author_id = g.user.id db.session.add(post) db.session.commit() return restful.success() else: return restful.param_error(message=form.get_error())
def ubanners(): form = AddBannerForm(request.form) if form.validate(): name = form.name.data image_url = form.image_url.data link_url = form.link_url.data priority = form.priority.data id = form.id.data banner = BannerModel.query.get(id) if banner: banner.name = name banner.image_url = image_url banner.link_url = link_url banner.priority = priority db.session.commit() return restful.success('轮播图信息修改成功!') else: return restful.server_error('没有这个轮播图') else: return restful.params_error(form.get_error())
def apost(): if request.method == 'GET': boards = BoardModel.query.all() return render_template('front/front_apost.html', boards=boards) else: form = AddPostForm(request.form) if form.validate(): title = form.title.data content = form.content.data board_id = form.board_id.data board = BoardModel.query.get(board_id) if not board: return restful.paramError(message='没有该板块!') post = PostModel(title=title, content=content) post.board = board post.author = g.front_user db.session.add(post) db.session.commit() return restful.success() return restful.paramError(message=form.get_error())
def sms_captcha(): form = SMSCaptchaForm(request.form) if form.validate(): telephone=form.telephone.data accountSid = "8a216da86f17653b016f3b4046b218ab" accountToken = "ac156972012a43dab1782f1f89995ac9" appId = "8a216da86f17653b016f3b40471818b2" rest = CCPRestSDK.REST(accountSid, accountToken, appId) captcha = Captcha.gene_text(number=4) result = rest.sendTemplateSMS(telephone, [captcha], "1") if result: #如果发送成功,存储到缓存里面(服务器里面) zlcache.set(telephone,captcha) return restful.success() else: return restful.params_error(message='短信验证码发送失败 ') else: return restful.params_error(message='参数错误')
def uproject(): # 1. 验证起 form = UpdateProjectForm(request.form) if form.validate(): projectID = form.projectID.data projectName = form.projectName.data projectType = form.projectType.data projectVersion = form.projectVersion.data # 2. 获取project_id的project project = EoProject.query.get(projectID) if project: project.projectType = projectType project.projectVersion = projectVersion project.projectName = projectName db.session.commit() return restful.success() else: return restful.params_error(message='没有这个项目!') else: return restful.params_error(message=form.get_error())
def sms_captcha(): form = SMSCaptchaForm(request.form) if form.validate(): telephone = form.telephone.data captcha = Captcha.gene_text(number=4) try: se = current_app.redis se.setex('sms_code:%s' % telephone, 60 * 5, captcha) except RedisError as e: print(e) return 'redis问题' # if send_sms(telephone, captcha): # return restful.success() # else: # return restful.params_error(message='短信验证码发送失败') send_sms_captcha(telephone, captcha) return restful.success() else: return restful.params_error(message='参数错误')
def apost(): if request.method == 'GET': boards = BoardModel.query.all() return render_template('front/front_apost.html', boards=boards) else: form = AddPostForm(request.form) if form.validate(): title = form.title.data content = form.content.data board_id = form.board_id.data board = BoardModel.query.get(board_id) if not board: return restful.params_error(message="没有这个板块!") post = PostModel(title=title, content=content) post.board = board post.author = g.front_user db.session.add(post) db.session.commit() return restful.success() else: return restful.params_error(message=form.get_errors())
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('发帖失败')
def stick_post(): post_id = request.form.get('post_id') to_do = request.form.get('to_do') if not post_id: return restful.params_error('参数错误,请稍后重试或联系管理员!') post = Post.query.get(post_id) if not post: return restful.params_error('帖子不存在!') if to_do == '1': sticky = StickyRecord() sticky.operator = g.cuser sticky.post = post db.session.add(sticky) elif to_do == '0': sticky = StickyRecord.query.filter_by(post_id=post_id).one() db.session.delete(sticky) else: return restful.params_error('参数错误,请稍后重试或联系管理员!') db.session.commit() return restful.success('操作成功!')
def acourse(): if request.method == 'GET': boards = Board.query.all() return render_template('cms/cms_acourse.html', boards=boards) else: form = AddCourseForm(request.form) if form.validate(): title = form.title.data content = form.content.data board_id = form.board_id.data board = Board.query.get(board_id) if not board: return restful.params_error(message='没有这个板块!') course = Course(title=title, content=content) course.board = board course.author = g.teacher db.session.add(course) db.session.commit() return restful.success() else: return restful.params_error(message=form.get_error())
def setting_profile(): if request.method == 'GET': return render_template('front/setting_profile.html') else: form = Setting_profile_Form(request.form) if form.validate(): qq = form.qq.data gender = form.gender.data desc = form.desc.data my_web = form.my_web.data real_name = form.real_name.data user = g.front_user user.qq = qq user.gender = gender user.my_web = my_web user.signature = desc user.realname = real_name db.session.commit() return restful.success(data=gender) else: return restful.params_error(message=get_error(form))
def upload_banner(): form = UpdataBannerForm(request.form) if form.validate(): banner_id = form.banner_id.data name = form.name.data img = form.img.data url = form.url.data prioirty = form.prioirty.data banner = BannerModel.query.get(banner_id) if banner_id: banner.name = name banner.img = img banner.url = url banner.prioirty = prioirty db.session.commit() return restful.success('编辑成功!') else: return restful.params_error('没有此banner,修改失败!') else: return restful.params_error(message=form.get_error())
def chat_send(): form = SendMsgModel(request.form) if form.validate(): reciver_id = form.reciver_id.data content = form.content.data msg = str({ 'msg': content, 'create_time': str(datetime.now()), 'reciver_id': reciver_id }) cache.lpush(g.front_user.id + '_chat', reciver_id + ':' + msg) cache.lpush( reciver_id + 'new_msg', str({ 'msg': content, 'create_time': str(datetime.now()), 'sender_id': g.front_user.id })) return restful.success() else: return restful.params_error(message=get_error(form))
def email_captcha(): # form = EmailCaptchaForm(request.form) # print(form.email.data) # if form.validate(): # email = form.email.data email = request.args.get('email') if not email and email == None: return restful.params_error(message="邮箱不能为空!") if email == g.cms_user.email: return restful.params_error(message="新邮箱不能与原邮箱一样!") source = list(string.ascii_letters+string.digits) captcha = ''.join(random.sample(source, 6)) # message = Message('zcbbs邮箱验证码', recipients=[email], body="您的邮箱验证码为:%s" % captcha) # try: # mail.send(message) # except: # return restful.server_error() # return restful.success() send_mail.delay('bbs论坛邮箱验证码', [email], '您的邮箱验证码为:%s' % captcha) memcaches.set(email, captcha) return restful.success()
def post(self): form = SigninForm(request.form) if form.validate(): telephone = form.telephone.data password = form.password.data remember = form.remember.data # user = FrontUser.query.filter(FrontUser.telephone == telephone).first() user = FrontUser.query.filter_by(telephone=telephone).first() if user and user.check_password(password): session['front_user_id'] = user.id if remember: # 持久化 session.permanent = True return restful.success() else: return restful.params_errors(message='手机号或者密码错误') else: return restful.params_errors(message=form.get_error())
def add_cuser(): if request.method == 'GET': roles = CMSRole.query.all() return render_template('cms/cms_addcmsuser.html', roles=roles) else: form = AddCMSUser(request.form) if form.validate(): email = form.email.data username = form.username.data password = form.password.data roles = request.values.getlist('roles[]') user = CMSUser(email=email, username=username, password=password) db.session.add(user) if roles: for item in roles: role = CMSRole.query.filter_by(id=int(item)).first() user.roles.append(role) db.session.commit() return restful.success() else: return restful.params_error(form.get_error())
def post(self): form = SigninForm(request.form) if form.validate(): telephone = form.telephone.data password = form.password.data remember = form.remember.data user = FrontUser.query.filter( FrontUser.telephone == telephone).first() if user and user.check_password(password): session[config.front_user_id] = user.id if remember: # 如果设置session.permanent = True # 那么过期时间是31天 session.permanent = True return restful.success() else: return restful.params_error(message='手机号或密码错误!') else: print(form.errors) return restful.params_error(message=form.get_error())
def croles(): if request.method == 'GET': roles = CMSRole.query.order_by(CMSRole.permissions.desc()).all() return render_template('cms/cms_roles.html', roles=roles) else: form = AddRoleForm(request.form) if form.validate(): name = form.name.data desc = form.desc.data permissions = request.values.getlist('permissions[]') if permissions: results = map(int, permissions) permissions = reduce(lambda x, y: x + y, results) role = CMSRole(name=name, desc=desc, permissions=permissions) db.session.add(role) db.session.commit() return restful.success() else: return restful.params_error(message='请至少选择一项权限!') else: return restful.params_error(form.get_error())
def apost(): if request.method == "GET": boards = BoardModel.query.all() return render_template("front/front_apost.html", boards=boards) else: form = AddPostForm(request.form) if form.validate(): title = form.title.data content = form.content.data boardId = form.boardId.data board = BoardModel.query.get(boardId) if not board: return restful.params_error(message="没有这个板块") post = PostModel(title=title, content=content) post.board = board post.author = current_user db.session.add(post) db.session.commit() return restful.success() else: return restful.params_error(form.get_error())
def post(self): form = SignupForm(request.form) # 收集表单信息 # 表单验证通过 if form.validate(): # 保存到数据库 telephone = form.telephone.data username = form.username.data password = form.password1.data # forms表单信息 # 前台用户模型数据添加到数据库 user = Front_User(telephone=telephone, username=username, password=password) db.session.add(user) db.session.commit() # 提交到数据库 # 表单验证通过,提交到数据库成功 return restful.success() else: return restful.params_error(message=form.get_error()) # 表单信息验证出错