def signinauth(request): form = signinauth_form(require_POST) if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") remember = form.cleaned_data.get("remember") user = authenticate(request, username=username, password=password) if user: if user.is_active: login(request, user) # 登录成功以后判断,是否为记住密码,记住密码的话 将session保留两周,不记住密码,到浏览器结束自动删除 if remember: request.session.set_expiry(None) else: request.session.set_expiry(0) # 最后返回json数据 return restful.ok(message="登录成功") # else:返回json数据,错误信息 else: return restful.blocked(message="账户被冻结") else: return restful.paramerror(message="用户名或密码错误") # 返回json数据 中 带有表单报错信息 else: return restful.paramerror(message=form.get_errors())
def login_view(request): form = 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') print(telephone) print(password) print(remember) user = authenticate(request, username=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() else: return restful.unauth(message="您的账号被冻结") else: return restful.paramerror(message="手机号或者密码错误") else: errors = form.get_errors() return restful.paramerror(message=errors)
def dpost(): post_id = request.form.get('post_id') if not post_id: return restful.paramerror(message='请传入帖子id') post = PostModel.query.get(post_id) if not post: return restful.paramerror('没有这篇帖子') db.session.delete(post) db.session.commit() return restful.success()
def dcomments(): comment_id = request.form.get('comment_id') if not comment_id: return restful.paramerror(message='请传入评论id') comment = CommonModel.query.get(comment_id) if not comment: return restful.paramerror('没有这篇评论') db.session.delete(comment) db.session.commit() return restful.success()
def uhpost(): post_id = request.form.get('post_id') if not post_id: return restful.paramerror(message='请传入帖子id') post = PostModel.query.get(post_id) if not post: return restful.paramerror('没有这篇帖子') highlight = HighlightPostModel.query.filter_by(post_id=post_id).first() db.session.delete(highlight) db.session.commit() return restful.success()
def edit_news_category(request): form = EditNewsCategoryForm(request.POST) if form.is_valid(): pk = form.cleaned_data.get('pk') name = form.cleaned_data.get('name') try: NewsCategory.objects.filter(pk=pk).update(name=name) return restful.success() except: return restful.paramerror(message='该分类不存在!') else: return restful.paramerror(message=form.get_error())
def hpost(): post_id = request.form.get('post_id') if not post_id: return restful.paramerror(message='请传入帖子id') post = PostModel.query.get(post_id) if not post: return restful.paramerror('没有这篇帖子') highlight = HighlightPostModel() highlight.post = post db.session.add(highlight) db.session.commit() return restful.success()
def dbanner(): form = DeleteBannerForm(request.form) if form.validate(): banner_id = form.banner_id.data banner = BannerModel.query.get(banner_id) if banner: db.session.delete(banner) db.session.commit() return restful.success() else: return restful.paramerror(message='没有这个轮播图') else: return restful.paramerror(form.get_errors())
def dboard(): form = DeleteboardForm(request.form) if form.validate(): board_id = form.board_id.data board = AddBoardModel.query.get(board_id) if board: db.session.delete(board) db.session.commit() return restful.success() else: return restful.paramerror(message='没有这个板块') else: return restful.paramerror(form.get_errors())
def post(self, request): form = PubCourseForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') category_id = form.cleaned_data.get('category_id') video_url = form.cleaned_data.get('video_url') cover_url = form.cleaned_data.get("cover_url") price = form.cleaned_data.get('price') duration = form.cleaned_data.get('duration') profile = form.cleaned_data.get('profile') teacher_id = form.cleaned_data.get('teacher_id') category = CourseCategory.objects.get(pk=category_id) teacher = Teacher.objects.get(pk=teacher_id) Course.objects.create(title=title, video_url=video_url, cover_url=cover_url, price=price, duration=duration, profile=profile, category=category, teacher=teacher) return restful.success() else: return restful.paramerror(message=form.get_errors())
def settings(): if request.method == 'GET': return render_template('front/front_settings.html') else: form = SettingForm(request.form) if form.validate(): username = form.username.data realname = form.realname.data email = form.email.data avatar = form.avatar.data signature = form.signature.data user_model = g.front_user user_model.username = username if realname: user_model.realname = realname if email: user_model.email = email if avatar: user_model.avatr = avatar if signature: user_model.signature = signature db.session.commit() return restful.success() else: return restful.paramerror(message=form.get_errors())
def post(self): form = SignupForm(request.form) if form.validate(): telephone = form.telephone.data tel = FrontUser.query.filter(FrontUser.telephone == telephone) if tel: return restful.paramerror(message='账号密码已存在') username = form.username.data password = form.password1.data user = FrontUser(telephone=telephone, username=username, password=password) db.session.add(user) db.session.commit() return restful.success() else: return restful.paramerror(form.get_errors())
def acomment(): 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 = CommonModel(content=content) comment.post = post comment.author = g.front_user db.session.add(comment) db.session.commit() return restful.success() else: return restful.paramerror(message='没有这个帖子') else: return restful.paramerror(form.get_errors())
def add_news_category(request): name = request.POST.get('name') exists = NewsCategory.objects.filter(name=name).exists() if not exists: NewsCategory.objects.create(name=name) return restful.success() else: return restful.paramerror(message='该分类已经存在!')
def post(self): form = ResetMailForm(request.form) if form.validate(): email = form.email.data g.cms_user.email = email db.session.commit() return restful.success() else: return restful.paramerror(form.get_errors())
def sms_captcha(): form = SMSCaptchaForm(request.form) if form.validate(): telephone = form.telephone.data text = Text_model captcha = Text_model[7:11] if telephonecode.send_sms(text=text, mobile=''): # todo zlcache.set(telephone, captcha) # return restful.success() return restful.paramerror(message='短信验证码系统维护中,验证码为1234') else: # return restful.paramerror(message='短信验证码发送失败') zlcache.set(telephone, captcha) # TODO # return restful.success() return restful.paramerror(message='短信验证码系统维护中,验证码为1234') else: return restful.paramerror(message='参数错误')
def aboard(): form = AddboardForm(request.form) if form.validate(): name = form.name.data board = AddBoardModel(name=name) db.session.add(board) db.session.commit() return restful.success() else: return restful.paramerror()
def post(self): form = ResetPwdForm(request.form) if form.validate(): newpwd = form.oldpwd.data oldpwd = form.oldpwd.data user = g.cms_user # cms_user是数据库表的名字 if user.check_password(oldpwd): user.password = newpwd db.session.commit() return restful.success() else: message = form.get_errors() if message == '': message = '旧密码错误' return restful.paramerror(message=message) else: message = form.get_errors() return restful.paramerror(message=message)
def profile(user_id=0): if not user_id: return abort(404) user = FrontUser.query.get(user_id) if user: current_user = user return render_template('front/front_profile.html', current_user=current_user) else: return restful.paramerror(message='此用户不存在')
def register_view(request): form = RegisterForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') username = form.cleaned_data.get('username') password = form.cleaned_data.get('password1') user = User.objects.create_user(telephone=telephone,username=username,password=password) login(request,user) return restful.success() else: return restful.paramerror(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.paramerror(message='没有这个轮播图') else: return restful.paramerror(form.get_errors())
def apost(): if request.method == "GET": boards_models = AddBoardModel.query.all() return render_template('front/front_apost.html', boards_models=boards_models) else: form = AddPostForm(request.form) if form.validate(): title = form.title.data content = form.content.data board_id = form.board_id.data board = AddBoardModel.query.get(board_id) if not board: return restful.paramerror(message='没有这个板块') post = PostModel(title=title, content=content) post.author = g.front_user post.board = board db.session.add(post) db.session.commit() return restful.success() else: return restful.paramerror(form.get_errors())
def public_comment(request): form = PublicCommentForm(request.POST) if form.is_valid(): news_id = form.cleaned_data.get('news_id') content = form.cleaned_data.get('content') news = News.objects.get(pk=news_id) comment = Comment.objects.create(content=content, news=news, author=request.user) serizlize = CommentSerizlizer(comment) return restful.result(data=serizlize.data) else: return restful.paramerror(message=form.get_errors())
def post(self,request): form = WriteNewsForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') desc = form.cleaned_data.get('desc') thumbnail = form.cleaned_data.get('thumbnail') content = form.cleaned_data.get('content') category_id = form.cleaned_data.get('category') category = NewsCategory.objects.get(pk=category_id) News.objects.create(title=title,desc=desc,thumbnail=thumbnail,content=content,category=category,author=request.user) return restful.success() else: return restful.paramerror(message=form.get_errors())
def post(self,request): form = EditNewsForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') desc = form.cleaned_data.get('desc') thumbnail = form.cleaned_data.get('thumbnail') content = form.cleaned_data.get('content') category_id = form.cleaned_data.get('category') pk = form.cleaned_data.get("pk") category = NewsCategory.objects.get(pk=category_id) News.objects.filter(pk=pk).update(title=title,desc=desc,thumbnail=thumbnail,content=content,category=category) return restful.success() else: return restful.paramerror(message=form.get_errors())
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() if user and user.check_password(password): session[config.FRONT_USER_ID] = user.id user.last_login_time = datetime.now() db.session.add(user) db.session.commit() if remember: # 如果设置session。过期时间是31天 ,如果需要自己制定时间,写在配置文件里面,会自动寻找,否则就是31天 session.permanent = True return restful.success() else: # message = form.get_errors() # return self.get(message=message) return restful.paramerror(message='手机号或密码错误') else: # message = form.get_errors() # return self.get(message=message) return restful.paramerror(message=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.paramerror(form.get_errors())
def email_captcha(): email = request.args.get('email') if not email: return restful.paramerror(message='请传递参数') source = list(string.ascii_letters) source.extend(map(lambda x: str(x), range(10))) # source.extend([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) captcha = "".join(random.sample(source, 6)) message = Message('flask论坛验证码发送', recipients=[email], body='验证码是{}'.format(captcha)) try: mail.send(message) except: return restful.servererror() zlcache.set(email, captcha) return restful.success()
def auth_wx_login(request): code = request.POST.get("code", None) a = request pass if code: url = settings.AHEN_URL.format(settings.WEAPPID, settings.WEAPPSEC, code) resp = json.loads(requests.get(url).text) openid = resp.get("openid") session_key = resp.get("session_key") unionid = resp.get("unionid") # account = Account.objects.filter(openid=openid).first() print(openid, session_key, unionid) # if account: # return restful.paramerror(msg='已经注册了') # else: # Account.objects.create_user(openid) # else: return restful.paramerror(msg='请传递code参数')