def privacy_setting(): form = PrivacySettingForm() if form.validate_on_submit(): with db.auto_commit(): current_user.set_attrs(form.data) flash('设置成功.', 'success') return redirect(url_for('user.index', username=current_user.username)) form.public_collections.data = current_user.public_collections return render_template('user/settings/edit_privacy.html', form=form)
def new_category(): form = NewCategoryForm() if form.validate_on_submit(): with db.auto_commit(): category = Category(name=form.name.data) db.session.add(category) flash('新的分类创建成功.', 'success') return redirect(url_for('main.index')) return render_template('admin/new_category.html', form=form)
def register(): form = RegisterForm() if form.validate_on_submit(): with db.auto_commit(): user = User() user.set_attrs(form.data) db.session.add(user) flash('你已经成功注册,请登录吧!', 'success') return redirect(url_for('auth.login')) return render_template('auth/login.html', form=form)
def notification_setting(): form = NotificationSettingForm() if form.validate_on_submit(): with db.auto_commit(): current_user.set_attrs(form.data) flash('提示消息更新成功.', 'success') return redirect(url_for('user.index', username=current_user.username)) form.receive_comment_notification.data = current_user.receive_comment_notification form.receive_follow_notification.data = current_user.receive_follow_notification form.receive_collect_notification.data = current_user.receive_collect_notification return render_template('user/settings/edit_notification.html', form=form)
def change_password(): form = ChangePasswordForm() if form.validate_on_submit(): if current_user.validate_password(form.old_password.data): with db.auto_commit(): current_user.password = form.new_password.data flash('密码修改成功,请重新登录.', 'success') return redirect(url_for('auth.login')) else: flash('原始密码错误.', 'warning') return render_template('user/settings/change_password.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): with db.auto_commit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category, user=current_user) db.session.add(post) flash('文章成功创建', 'success') return redirect(url_for('main.show_post', post_id=post.id)) return render_template('main/new_post.html', form=form)
def edit_profile(): form = EditProfileForm() if form.validate_on_submit(): with db.auto_commit(): current_user.set_attrs(form.data) flash('成功修改资料', 'success') return redirect(url_for('main.index', username=current_user.username)) form.name.data = current_user.name form.username.data = current_user.username form.website.data = current_user.website form.location.data = current_user.location form.bio.data = current_user.bio return render_template('user/settings/edit_profile.html', form=form)
def edit_post(post_id): post = Post.query.get_or_404(post_id) form = PostForm() if form.validate_on_submit(): with db.auto_commit(): post.title = form.title.data post.body = form.body.data post.category = Category.query.get(form.category.data) flash('文章更新完毕', 'success') return redirect(url_for('main.show_post', post_id=post.id)) form.title.data = post.title form.body.data = post.body form.category.data = post.category_id return render_template('main/edit_post.html', form=form)
def new_comment(post_id): post = Post.query.get_or_404(post_id) page = request.args.get('page', 1, type=int) form = CommentForm() if form.validate_on_submit(): with db.auto_commit(): body = form.body.data user = current_user._get_current_object() comment = Comment(body=body, user=user, post=post) db.session.add(comment) flash('评论成功', 'success') push_comment_notification(post_id=post.id, receiver=post.user, page=page) return redirect(url_for('main.show_post', post_id=post_id))
def edit_profile_admin(user_id): user = User.query.get_or_404(user_id) form = EditProfileAdminForm(user=user) if form.validate_on_submit(): with db.auto_commit(): role = Role.query.get(form.role.data) if role.name == 'Locked': user.lock() user.role = role user.username = form.username.data user.name = form.name.data user.bio = form.bio.data user.website = form.website.data user.location = form.location.data flash('Profile updated.', 'success') return redirect_back() form.username.data = user.username form.role.data = user.role_id form.name.data = user.name form.bio.data = user.bio form.website.data = user.website form.location.data = user.location return render_template('admin/edit_profile.html', user=user, form=form)