def see_myself(pid): posts = MPosts.query.get(pid) #查询出 博客id对应的博客数据 posts.state = not posts.state posts.save() flash('设置成功!') cache.clear() return redirect(url_for('center.blog_manage'))
def send_posts(): form = SendPosts() if form.validate_on_submit(): if current_user.is_authenticated: p = Posts(title=form.articletitle.data, article=form.article.data, user=current_user, timestamp=datetime.utcnow() + timedelta(hours=8)) print(p.timestamp) p.save() flash('发表成功') cache.clear() #因为数据库更新,页面也需要更新,所以需要清除所有缓存 return redirect(url_for('posts.send_posts')) else: print(1) flash('您还没登录') return redirect(url_for('user.login')) if not current_user.is_authenticated: print(2) flash('您还没登录 请登录后发表') c = Course.query.filter_by(pid=0) c1 = Course.query.filter(and_(Course.path.endswith(','), Course.pid != 0)) c2 = Course.query.filter( and_(not_(Course.path.endswith(',')), Course.pid != 0)) return render_template('posts/send_posts.html', form=form, course=c, course1=c1, course2=c2)
def logout(): u = User.query.filter_by(username=current_user.username).first() u.lastlogin_date = u.lastlogin_date_cache u.save() logout_user() cache.clear() return redirect(url_for('main.show', page=1))
def posts_detail(pid): form = Posts() data = MPosts.query.get(pid) comment = MPosts.query.filter(MPosts.path.contains(str(pid))).order_by( MPosts.path.concat(MPosts.id)) # 访问量 data.visit = data.visit + 1 data.save() cache.clear() return render_template('posts/posts_detail.html', data=data, form=form, comment=comment)
def login(): form = Login() if form.validate_on_submit(): u = User.query.filter_by(username=form.username.data).first() if not u.check_password(form.userpass.data): flash('请输入正确的密码') else: u.lastLogin = datetime.utcnow() u.save() flash('登陆成功!!!') cache.clear() login_user(u, remember=form.remember.data) return redirect(url_for('main.index')) return render_template('user/login.html',form=form)
def edit_blog(pid): form = Posts() posts = MPosts.query.get(pid) if form.validate_on_submit(): posts.title = request.form.get('title') posts.content = request.form.get('content') posts.timestamp = datetime.utcnow() posts.save() flash('博客更新成功') cache.clear() return redirect(url_for('center.blog_manage')) # 给表单添加默认值 form.title.data = posts.title form.content.data = posts.content return render_template('owncenter/edit_blog.html',form=form,posts=posts,pid=pid)
def discuss(posts): c = Course.query.filter_by(pid=0) c1 = Course.query.filter(and_(Course.path.endswith(','), Course.pid != 0)) c2 = Course.query.filter( and_(not_(Course.path.endswith(',')), Course.pid != 0)) ########################################################################## form = SendDiscuss() id = posts.split(' ')[1].split('>')[0] parent = Posts.query.filter_by(id=id).first() title = '回复' + parent.user.username + ':' grandparent = Posts.query.filter_by(id=parent.pid).first() if form.validate_on_submit(): if current_user.is_authenticated: p = Posts(pid=id, path=parent.path + id + ',', title=title, article=form.article.data, user=current_user, timestamp=datetime.utcnow() + timedelta(hours=8)) p.save() flash('发表成功') cache.clear() #因为数据库更新,页面也需要更新,所以需要清除所有缓存 if grandparent: return redirect( url_for('details.get_details', posts=grandparent, page=1)) else: return redirect( url_for('details.get_details', posts=parent, page=1)) else: flash('您还没登录') return redirect(url_for('user.login')) if not current_user.is_authenticated: flash('您还没登录 请登录后发表') return render_template('posts/discuss.html', form=form, course=c, course1=c1, course2=c2)
def login(): form = Login() if form.validate_on_submit(): u = User.query.filter_by(username=form.username.data).first() if not u: flash('当前用户不存在') elif not u.confirm: flash('你的账户未激活') elif u.check_password(form.userpass.data): flash('登录成功,欢迎'+u.username) login_user(u)#维护登录对象u cache.clear() u.lastlogin_date_cache = datetime.utcnow()+timedelta(hours=8) u.save() return redirect(url_for('main.index')) else: flash('请输入正确的密码') # return render_template('') ############################################# c = Course.query.filter_by(pid=0) c1 = Course.query.filter(and_(Course.path.endswith(','), Course.pid != 0)) c2 = Course.query.filter(and_(not_(Course.path.endswith(',')), Course.pid != 0)) return render_template('user/login.html',form=form,course=c,course1=c1,course2=c2)
def clear_cache(): cache.clear() return redirect(url_for('main.index'))
def logout(): flash('退出成功') logout_user() cache.clear() return redirect(url_for('main.index'))
def del_posts(pid): posts = MPosts.query.get(pid) #查询出 博客id对应的博客数据 posts.delete() flash('删除成功!') cache.clear() return redirect(url_for('center.blog_manage'))