def change(post_id): form = ChangeForm() post = Post.query.filter_by(id=post_id).first_or_404() if post.author.username != current_user.username: abort(404) if form.validate_on_submit(): t = Thread(target=del_image, args=(post, form, current_user.id)) t.start() temp_store = 0 if request.form.get('temp'): temp_store = 1 origin_id = str(post.cat_id) post.title = form.title.data post.content = form.content.data post.cat_id = form.cat.data post.last_modify = datetime.utcnow() post.temp_store = temp_store db.session.add(post) db.session.commit() Use_Redis.eval('article', post_id, '*', disable=flag) Use_Redis.eval('index', '*', disable=flag) Use_Redis.eval('cat', '0', '*', disable=flag) Use_Redis.eval('cat', origin_id, '*', disable=flag) Use_Redis.eval('cat', str(form.cat.data), '*', disable=flag) flash('你的修改已经保存.') return redirect(url_for('user', username=current_user.username)), 301 else: flash('修改异常!') app.logger.error(form.errors) return redirect(url_for('editpost', form=form, post_id=post.id)), 301
def delete(post_id): post = Post.query.filter_by(id=post_id).first_or_404() if post.author.username != current_user.username: abort(404) db.session.delete(post) db.session.commit() flash("删除成功!") Use_Redis.eval('index', '*', disable=flag) Use_Redis.eval('article', post_id, '*', disable=flag) Use_Redis.eval('cat', '0', '*', disable=flag) Use_Redis.eval('cat', str(post.cat_id), '*', disable=flag) Use_Redis.delete('total', 'post', disable=flag) return redirect(url_for('user', username=current_user.username))
def del_cat(): cat_id = request.form['cat'] if cat_id: cat = PostCat.query.filter_by(id=cat_id).first() if cat: try: db.session.delete(cat) db.session.commit() flash('成功删除') Use_Redis.eval('index', '*', disable=flag) Use_Redis.eval('cat', str(cat_id), '*', disable=flag) except: app.logger.warning("can't delete {0}".format(cat.name)) return redirect('control'), 301
def add_cat(): cat = AddCat() if cat.validate_on_submit(): query_cat = PostCat.query.filter_by(name=cat.name.data).first() if not query_cat: new_cat = PostCat(name=cat.name.data) try: db.session.add(new_cat) db.session.commit() flash('类别增加成功') Use_Redis.eval('index', '*', disable=flag) except: app.logger.error('database errror when add new cat') else: flash('类别已存在!') return redirect('control'), 301
def article_detail(username, post_id): html = Use_Redis.get('article', post_id, str(current_user.get_id() or '0'), disable=flag) if not html: post = Post.query.filter_by(id=post_id).first_or_404() html = render_template('detail.html', title=post.title, post=post, user=current_user) Use_Redis.set('article', post_id, str(current_user.get_id() or '0'), html, disable=flag) return html
def edit_profile(): form = EditProfileForm(current_user.username) if form.validate_on_submit(): current_user.username = form.username.data current_user.about_me = form.about_me.data try: db.session.commit() except: flash('服务存在异常!请稍后再试。') return redirect(url_for('edit_profile')) flash('修改成功。') Use_Redis.eval('index', '*', disable=flag) return redirect(url_for('user', username=current_user.username)) elif request.method == 'GET': form.username.data = current_user.username form.about_me.data = current_user.about_me return render_template('edit_profile.html', title='修改信息', form=form)
def search(): keys = request.args.get('s') if keys: html = Use_Redis.get('srh', keys, str(current_user.get_id() or '0'), disable=flag) else: html = None if not html: results = Post.query.whoosh_search(keys).all() html = render_template('search.html', results=results) Use_Redis.set('srh', keys, str(current_user.get_id() or '0'), html, disable=flag) return html
def user(username, page=1): if username is None: return redirect(url_for('login')) if username != current_user.username: abort(404) total = Use_Redis.get('total', 'post', disable=flag) if not total: total = Stats.query.filter_by(name='post_count').first().total Use_Redis.set('total', 'post', total, disable=flag) total = int(total) posts = Post.query.filter_by(user_id=current_user.id).order_by( db.desc(Post.time)).paginate(page, 8, False, total_in=total) if posts.pages < page and page > 1: abort(404) return render_template('user.html', user=current_user, posts=posts, page=page)
def index(page=1): html = Use_Redis.get('index', str(current_user.get_id() or '0'), disable=flag) if html: return html user = User.query.filter_by(role=1, username=app.config['DATABASE_ADMIN']).first() if user: total = Use_Redis.get('total', 'post', disable=flag) if not total: total = Stats.query.filter_by(name='post_count').first().total Use_Redis.set('total', 'post', total, disable=flag) total = int(total) posts = Post.query.filter_by(user_id=user.id).order_by( Post.time.desc()).paginate(page, app.config['POST_PER_PAGE'], False, total_in=total) news = user.about_me else: posts = None news = 'Testing' cats = PostCat.query.all() html = render_template('index.html', user=user, posts=posts, cats=cats, page=page, new=news) Use_Redis.set('index', str(current_user.get_id() or '0'), html, disable=flag) return html
def write(): form = PostForm() if form.validate_on_submit() and request.method == 'POST': temp_store = 0 if request.form.get('temp'): temp_store = 1 post = Post(title=form.title.data, content=form.content.data, user_id=current_user.id, cat_id=form.cat.data, temp_store=temp_store) db.session.add(post) db.session.commit() soup_post = BeautifulSoup(post.content, 'lxml') img_set = {img['src'] for img in soup_post.find_all('img')} if img_set: for img in img_set: f_fullname = os.path.basename(img) f_path = os.path.join(app.config['UPLOADED_PATH'], str(current_user.id), f_fullname) db_image = UploadImage.query.filter_by( user_id=current_user.id, image_path=f_path).first() if db_image: db_image.mark = 1 try: db.session.commit() except: flash('服务异常') return render_template('write.html', title='写作ing', form=form) Use_Redis.eval('index', '*', disable=flag) Use_Redis.eval('cat', str(form.cat.data), '*', disable=flag) Use_Redis.eval('cat', '0', '*', disable=flag) Use_Redis.eval('srh', '*', disable=flag) Use_Redis.delete('total', 'post', disable=flag) flash('提交成功!') if current_user.id == 1: return redirect(url_for('user', username=current_user.username)), 301 return render_template('write.html', title='写作ing', form=form)
def choose_cate(): if not request.is_xhr: abort(404) cat_id = request.args.get('cat_id', '0') page = request.args.get('page', '1') if len(cat_id) > 9 or len(page) > 9: abort(404) if int(cat_id) < 0 or int(page) < 1: abort(404) xml = Use_Redis.get('cat', cat_id, page, disable=flag) if xml: xml = pickle.loads(xml) return xml if int(cat_id) == 0: total = Use_Redis.get('total', 'post', disable=flag) if not total: total = Stats.query.filter_by(name='post_count').first().total Use_Redis.set('total', 'post', total, disable=flag) total = int(total) #user_id = 1 is admin'id posts = Post.query.filter_by(user_id=1).order_by(db.desc( Post.time)).paginate(int(page), app.config['POST_PER_PAGE'], False, total_in=total) xml = make_response(render_template('category.xml', posts=posts)) xml.headers['Content-Type'] = 'application/xml; charset=utf-8' Use_Redis.set('cat', cat_id, page, pickle.dumps(xml), disable=flag) else: #user_id=1 is admin's id posts = Post.query.filter_by(cat_id=int(cat_id), user_id=1).order_by( Post.time.desc()).paginate(int(page), app.config['POST_PER_PAGE'], False) xml = make_response(render_template('category.xml', posts=posts)) xml.headers['Content-Type'] = 'application/xml; charset=utf-8' Use_Redis.set('cat', cat_id, page, pickle.dumps(xml), disable=flag) return xml
def rep_cat(): origin_id = request.form.get('origin_id') if not origin_id: return redirect('control'), 301 form = RepCat() if form.validate_on_submit(): cat = PostCat.query.filter_by(id=int(origin_id)).first_or_404() cat.name = form.name.data try: db.session.commit() flash('ok') Use_Redis.eval('index', '*', disable=flag) Use_Redis.eval('total', 'post', disable=flag) Use_Redis.eval('cat', origin_id, '*', disable=flag) except: flash('DB error!') return redirect('control'), 301 for error in form.name.errors: flash(error) return redirect('control'), 301