def send_message(user_id): is_login() user = User.query.get_or_404(user_id) user.permissions.send_message.test(403) form = MessageForm() if form.validate_on_submit(): body = render_template("emails/send_message.html", user=user, subject=form.subject.data, message=form.message.data) subject = _("your friend %(name)s sent you a message", name=g.user.username) message = Message(subject=subject, body=body, recipients=[user.email]) mail.send(message) flash(_("Your message has been sent to %(name)s", name=user.username), "success") return redirect(url_for("user.posts", username=user.username)) return render_template("user/send_message.html", user=user, form=form)
def edit(post_id): is_login() post = Post.query.get_or_404(post_id) post.permissions.edit.test(403) form = PostForm(obj=post) if form.validate_on_submit(): form.populate_obj(post) db.session.commit() if g.user.id != post.author_id: body = render_template("emails/post_edited.html", post=post) message = Message(subject="Your post has been edited", body=body, recipients=[post.author.email]) mail.send(message) flash(_("The post has been updated"), "success") else: flash(_("Your post has been updated"), "success") return redirect(url_for("post.view", post_id=post_id)) return render_template("post/edit_post.html", post=post, form=form)
def delete(post_id): is_login() post = Post.query.get_or_404(post_id) post.permissions.delete.test(403) Comment.query.filter_by(post=post).delete() db.session.delete(post) db.session.commit() if g.user.id != post.author_id: body = render_template("emails/post_deleted.html", post=post) message = Message(subject="Your post has been deleted", body=body, recipients=[post.author.email]) mail.send(message) flash(_("The post has been deleted"), "success") else: flash(_("Your post has been deleted"), "success") return jsonify(success=True, redirect_url=url_for('frontend.index'))
def report_abuse(comment_id): is_login() comment = Comment.query.get_or_404(comment_id) form = CommentAbuseForm() if form.validate_on_submit(): admins = current_app.config['ADMINS'] if admins: body = render_template("emails/report_abuse.html", comment=comment, complaint=form.complaint.data) message = Message(subject="Report Abuse", body=body, sender=g.user.email, recipients=admins) mail.send(message) flash(_("Your report has been sent to the admins"), "success") return redirect(comment.url) return render_template("comment/report_abuse.html", comment=comment, form=form)
def forgot_password(): form = RecoverPasswordForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user: flash(u"临时登陆密码已发至"+form.email.data+u",请注意查收", "success") new_password = string.join( random.sample( ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '5', '6', '7', '8'], 6 ) ).replace( " ", "" ) user.password = new_password db.session.commit() body = render_template("emails/recover_password.html", new_password=new_password) message = Message(subject=u"42ic.com临时登陆密码", body=body, recipients=[user.email]) mail.send(message) return redirect(url_for("frontend.index")) else: flash(u"对不起,您的邮箱地址不存在", "error") return render_template("account/recover_password.html", form=form)