def edit_topic(topic_id): topic = Topic.query.get_or_404(topic_id) if current_user != topic.author and current_user != topic.group.admin and not current_user.can( 'MODERATE'): abort(403) #除了主题的author、主题所在组的管理员或者协管员,都不能编辑主题 form = PostForm() if form.validate_on_submit(): topic.name = form.title.data topic.body = form.body.data if form.publish.data: topic.saved = False db.session.commit() if form.notice.data and not current_user.is_noticing(topic): current_user.notice(topic) flash('主题已发表', 'success') return redirect(url_for('main.show_topic', topic_id=topic.id)) elif form.save.data: if topic.get_last_post(): flash('已有回帖,不允许保存。', 'warning') return redirect_back() else: topic.saved = True topic.top = False db.session.commit() flash('主题已保存', 'success') return redirect(url_for('user.draft_topic')) elif form.save1.data: topic.saved = True topic.top = False db.session.commit() flash('请上传附件', 'info') return redirect(url_for('.upload_topic', topic_id=topic.id)) form.title.data = topic.name form.body.data = topic.body return render_template('main/edit_topic.html', form=form, topic=topic)
def reply_post(replied_id): replied = Post.query.get_or_404(replied_id) if replied.topic.group.status_id == 1 and not current_user.can('MEMBER'): abort(403) form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data author = current_user._get_current_object() post = Post(body=body, title=title, author=author, topic_id=replied.topic_id, replied_id=replied_id) db.session.add(post) if form.publish.data: reads = Read.query.filter(Read.reader_id != current_user.id).all() if reads: for read in reads: db.session.delete(read) db.session.commit() if post.topic.receivers: for notice in post.topic.receivers: send_new_post_email(receiver=notice.receiver, topic=post.topic, user=current_user) if form.notice.data: current_user.notice(post.topic) if current_user != post.topic.author and post.topic.author.receive_post_notification: push_post_notification(post=post, receiver=post.topic.author) flash('帖子已发表', 'success') return redirect(url_for('main.show_post', post_id=post.id)) elif form.save.data: post.saved = True db.session.commit() flash('帖子已保存', 'success') return redirect(url_for('user.draft_post')) elif form.save1.data: post.saved = True db.session.commit() flash('请上传附件', 'info') return redirect(url_for('.upload_post', post_id=post.id)) form.title.data = 'Re:' + replied.title return render_template('main/reply_post.html', form=form, replied=replied)
def edit_post(post_id): post = Post.query.get_or_404(post_id) if post.deleted and not current_user.can('MODERATE'): abort(404) if current_user != post.author and current_user != post.topic.group.admin and not current_user.can( 'MODERATE'): abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data if form.publish.data: post.saved = False db.session.commit() if post.topic.receivers: for notice in post.topic.receivers: send_new_post_email(receiver=notice.receiver, topic=post.topic, user=current_user) if form.notice.data and not current_user.is_noticing(post.topic): current_user.notice(post.topic) if post.topic.author != current_user and post.topic.author.receive_post_notification: push_post_notification(post=post, receiver=post.topic.author) flash('帖子已发表', 'success') return redirect(url_for('main.show_post', post_id=post.id)) elif form.save.data: post.saved = True db.session.commit() flash('帖子已保存', 'success') return redirect(url_for('user.draft_post')) elif form.save1.data: post.saved = True db.session.commit() flash('请上传附件', 'info') return redirect(url_for('.upload_post', post_id=post.id)) form.title.data = post.title form.body.data = post.body return render_template('main/edit_post.html', form=form, post=post)
def new_post(topic_id): topic = Topic.query.get_or_404(topic_id) if topic.group.status_id == 1 and not current_user.can('MEMBER'): abort(403) if topic.group.status_id == 2 and not current_user.can('MEMBER'): abort(403) form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data post = Post(title=title, body=body, topic=topic, author=current_user._get_current_object()) db.session.add(post) if form.publish.data: db.session.commit() if topic.receivers: for notice in topic.receivers: send_new_post_email(receiver=notice.receiver, topic=topic, user=current_user) if form.notice.data: current_user.notice(topic) if current_user != topic.author and topic.author.receive_post_notification: push_post_notification(post=post, receiver=topic.author) flash('帖子已发表', 'success') return redirect(url_for('main.show_post', post_id=post.id)) elif form.save.data: post.saved = True db.session.commit() flash('帖子已保存', 'success') return redirect(url_for('user.draft_post')) elif form.save1.data: post.saved = True db.session.commit() flash('请上传附件', 'info') return redirect(url_for('.upload_post', post_id=post.id)) form.title.data = 'Re:' + topic.name return render_template('main/new_post.html', form=form, topic=topic)
def new_topic(group_id): if group_id == 6: abort(403) group = Group.query.get_or_404(group_id) if group.status_id == 1 and not current_user.can('MEMBER'): abort(403) if group.status_id == 2 and not current_user.can('MEMBER'): abort(403) form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data topic = Topic(name=title, body=body, group_id=group_id, author=current_user._get_current_object()) db.session.add(topic) if form.publish.data: current_user.read(topic) # 标记自己发表的文章为已读 db.session.commit() if form.notice.data: current_user.notice(topic) flash('主题已发表', 'success') return redirect(url_for('main.show_topic', topic_id=topic.id)) elif form.save.data: topic.saved = True db.session.commit() flash('主题已保存', 'success') return redirect(url_for('user.draft_topic')) elif form.save1.data: topic.saved = True db.session.commit() flash('请上传附件', 'info') return redirect(url_for('.upload_topic', topic_id=topic.id)) return render_template('main/new_topic.html', form=form, group=group)