def update_mail_config(): from manager import app from app.function.config import get_config app.config.update( MAIL_SERVER=get_config("web_mail_smtp_host"), MAIL_PORT=get_config("web_mail_smtp_port"), MAIL_USERNAME=get_config("web_mail_smtp_user"), MAIL_PASSWORD=get_config("web_mail_smtp_pass") ) mail.init_app(app)
def send_reset_password(): update_mail_config() # 配置邮件模板 template = html.unescape(get_config("MAIL_VALIDATION_TEMPLATE")) template = render_template_string(template, user=current_user, token=current_user.generate_token()) msg = Message(subject='郝明宸的个人博客来信', sender=get_config("MAIL_SMTP_USER"), recipients=[current_user.email], html=template) thread = Thread(target=send_async_email, args=[app, msg]) thread.start() return json.dumps({ "code": 1, "message": "发送成功" })
def send_sms_email(): update_mail_config() recipient = request.get_data().decode(encoding='utf-8') if not recipient: recipient = current_user.email if not verify_email(recipient): return json.dumps({ "code": -1, "message": "请输入正确的邮箱格式" }) try: # 配置邮件模板 from manager import app from app.function.config import get_config code = yzm(6) session['sms_code'] = code html = render_template('common/email_sms.html', code=code) msg = Message(subject='郝明宸的个人博客来信', sender=get_config("web_mail_smtp_user"), recipients=[recipient], html=html) thread = Thread(target=send_async_email, args=[app, msg]) thread.start() return json.dumps({ "code": 1, "message": "发送成功" }) except Exception as e: return json.dumps({ "code": -1, "message": "发送失败" })
def __gen_random_font(cls): """ 采用随机字体 :return: """ from app.function.config import get_config fonts = ["consola.ttf", "consolab.ttf"] font = random.choice(fonts) return get_config("web_system_font_path") + font
def search(page=1): from app.function.config import get_config pagination = get_home_search_paginate( page=page, per_page=int(get_config("web_home_posts_pages")), query=request.args.get('query')) latest_posts = Post.get_latest_posts(5) return render_template('home/index.html', posts=pagination.items, latest_posts=latest_posts, pagination=pagination, current_index='index')
def draft_post(page=1): """ 草稿箱页面 :return: """ navigation = get_navigation_info(title="草稿箱", sub_title="修改未发布的文章", tag="draft_post") if request.method == 'GET': pagination = get_admin_posts_paginate(page, per_page=int(get_config("web_admin_post_per_page")), status=STATUS_DRAFT) if request.args.get('post_id') is not None: post = Post.query.filter_by(id=request.args.get('post_id')).first() post.delete() return redirect(url_for('admin.post')) return render_template("admin/post.html", navigation=navigation, posts=pagination.items, pagination=pagination, posts_total=len(pagination.items))
def category(category_sub_name, page=1): from app.function.config import get_config pagination = get_home_category_paginate( page=page, per_page=int(get_config("web_home_posts_pages")), category_sub_name=category_sub_name) latest_posts = Post.get_latest_posts(5) return render_template( 'home/index.html', current_category=category_sub_name, posts=pagination.items, latest_posts=latest_posts, pagination=pagination, )
def dustbin_post(page=1): """ 垃圾箱页面 在此页面删除的文章将会在数据库中真正的删除,无法找回 :return: """ navigation = get_navigation_info(title="垃圾箱", sub_title="被删除的文章", tag="dustbin_post") if request.method == 'GET': pagination = get_admin_posts_paginate(page, per_page=int(get_config("web_admin_post_per_page")), status=STATUS_DELETED) if request.args.get('post_id') is not None: post = Post.query.filter_by(id=request.args.get('post_id')).first() post.real_delete() return redirect(url_for('admin.post')) return render_template("admin/post.html", navigation=navigation, posts=pagination.items, pagination=pagination, posts_total=len(pagination.items))
def post_reply(page=1): navigation = get_navigation_info(title="文章回复列表", tag="post_reply") pagination = get_admin_post_replies_paginate( page, per_page=int(get_config("web_admin_comment_reply_per_page")), status=STATUS_PUBLISH) if request.method == 'GET': delete_id = request.args.get("delete_id") if delete_id: reply = PostReply.query.filter_by(id=delete_id).first() reply.update_status(status=STATUS_DELETED) return redirect(url_for('admin.post_reply')) return render_template("admin/post_reply.html", navigation=navigation, replies=pagination.items, pagination=pagination, replies_count=len(pagination.items))
def comment(page=1): navigation = get_navigation_info(title="评论列表", tag="comment") pagination = get_admin_comments_paginate( page, per_page=int(get_config("web_admin_comment_per_page")), status=STATUS_PUBLISH) if request.method == 'GET': delete_id = request.args.get("delete_id") if delete_id: comment = PostComment.query.filter_by(id=delete_id).first() comment.update_status(status=STATUS_DELETED) return redirect(url_for('admin.comment')) return render_template("admin/comment.html", navigation=navigation, comments=pagination.items, pagination=pagination, comments_count=len(pagination.items))
def water_mark(filepath, mark=u'@ 郝明宸的个人博客'): image = Image.open(filepath) from app.function.config import get_config ttf_path = os.path.join(get_config("web_system_font_path"), 'alibaba.ttf') font = ImageFont.truetype(ttf_path, size=18) layer = image.convert('RGBA') # 将图像转为RGBA图像 # 生成同等大小的图片 text_overlay = Image.new('RGBA', layer.size, (255, 255, 255, 0)) image_draw = ImageDraw.Draw(text_overlay) # 画图 # 获取文本大小 text_size_x, text_size_y = image_draw.textsize(mark, font=font) # 设置文本文字位置 text_xy = (layer.size[0] - text_size_x, layer.size[1] - text_size_y) image_draw.text(text_xy, mark, font=font, fill=(47, 79, 79, 100)) # 将新生成的图片覆盖到需要加水印的图片上 after = Image.alpha_composite(layer, text_overlay) after.save(filepath)
def user(gid=0, page=1): navigation = get_navigation_info(title="用户管理", sub_title="控制台", tag="user") pagination = get_admin_users_paginate( page, int(get_config("web_admin_user_per_page")), gid) groups = UserGroup.get_groups_name() if request.method == 'GET': if request.args.get('user_id') is not None: user = User.query.filter_by(id=request.args.get('user_id')).first() user.real_delete() return redirect(url_for('admin.user')) return render_template('admin/user.html', navigation=navigation, pagination=pagination, users=pagination.items, users_total=len(pagination.items), groups=groups, group_id=int(gid))
def alter_avatar(file, uid): from app.function.config import get_config # 获取安全的文件名,用于保存 name = secure_filename(file.filename) ext = os.path.splitext(name)[-1] import uuid filename = uuid.uuid4().hex + ext # 计算文件的MD5,防止重复保存 md5 = md5_file(file) # 文件的保存路径 path = get_config("web_system_upload_avatar_path") if not os.path.exists(path): os.mkdir(path) # 文件的保存路径 file.save(os.path.join(path, filename)) # 需要保存什么大小的头像 sizes = {"s" + filename: 80, "m" + filename: 160, "b" + filename: 250} # 对头像进行裁剪和缩放 return resize_avatar(os.path.join(path, filename), path, uid, sizes)
def index(page=1): from app.function.config import get_config pagination = get_home_index_paginate(page=page, per_page=int(get_config("web_home_posts_pages"))) latest_posts = Post.get_latest_posts(limit=3) return render_template('home/index.html', posts=pagination.items , latest_posts=latest_posts, pagination=pagination, current_index='index')