鱼书项目模块化总结

鱼书项目模块化总结 项目总体思路

鱼书项目模块化总结

模型类

鱼书项目模块化总结

视图蓝本

鱼书项目模块化总结

表单验证数据API

鱼书项目模块化总结

flask上下文/ajax

鱼书项目模块化总结

1 异步发送邮件模块

邮件发送:

需要进行注册邮件发送或者功能模块需要发送邮件时可以采用:

from threading import Thread from app import mail from flask import current_app,render_template from flask_mail import Message '''开启异步线程''' def send_async_mail(app,msg): '''mail.send发送需要获取上下文,因此添加with''' with app.app_context(): try: mail.send(msg) except Exception as e: raise e def send_mail(to, subject, template,**kwargs): ''' 发送邮件 :param to: 收件人 :param subject: 标题 :param template: 渲染模板 :param kwargs: 关键字参数 :return: ''' msg = Message('[鱼书]'+ '' +subject, sender=current_app.config['MAIL_USERNAME'], recipients=[to]) msg.html = render_template(template,**kwargs) ''' current_app是代理对象,开启新的线程时,我们直接获取真实的app核心对象_get_current_object() ''' app = current_app._get_current_object() thr = Thread(target=send_async_mail,args=[app,msg]) thr.start() 2 枚举类标识状态

状态标识

状态的变更需要进行标识时(等待,成功,拒绝,撤销)

from enum import Enum class PendingStatus(Enum): ''' 交易状态: 枚举方法实现 ''' Waiting = 1 Success = 2 Reject = 3 Redraw = 4 @classmethod def pending_str(cls,status,key): key_map = { cls.Waiting:{ 'requester':'等待对方邮寄', 'gifter':'等待你邮寄' }, cls.Success: { 'requester': '对方已邮寄', 'gifter': '你已邮寄,交易完成' }, cls.Reject: { 'requester': '对方已拒绝', 'gifter': '你已经拒绝' }, cls.Redraw: { 'requester': '你已撤销', 'gifter': '对方已撤销' } } return key_map[status][key] 3 User模型的方法

模型定义的方法:

1 password以hash方式存储

2 用户信息生成token

3 password的私有化

from math import floor from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from app.libs.enums import PendingStatus from app.models.base import Base, db from sqlalchemy import Column, Integer, String, Boolean, Float from app import login_manager from app.models.drift import Drift from app.models.gift import Gift from app.models.wish import Wish from app.spider.yushu_book import YuShuBook from app.libs.helper import is_isbn_or_key from flask import current_app class User(Base, UserMixin): ''' 模型属性设置 , UserMixin 记录用户账号的状态 ''' id = Column(Integer, primary_key=True) nickname = Column(String(24), nullable=False) _password = Column('password', String(128), nullable=True) phone_number = Column(String(18), unique=True) email = Column(String(50), unique=True, nullable=False) confirmed = Column(Boolean, default=False) beans = Column(Float, default=0) send_counter = Column(Integer, default=0) receive_counter = Column(Integer, default=0) wx_open_id = Column(String(50)) wx_name = Column(String(32)) ''' 将password方法hash加密只读并将其变为属性访问 ''' @property def password(self): return self._password @password.setter def password(self, raw): self._password = generate_password_hash(raw) def check_password(self, raw): return check_password_hash(self._password, raw) def generate_token(self, expiration=600): s = Serializer(current_app.config['SECRET_KEY'],expiration) return s.dumps({'id':self.id}).decode('utf-8') @staticmethod def reset_password(token,new_password): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token.encode('utf-8')) except: return False uid = data.get('id') with db.auto_commit(): user = User.query.get(uid) if user: user.password = new_password return True @property def summary(self): return dict( nickname = self.nickname, beans = self.beans, email = self.email, send_receive = str(self.send_counter)+ 'http://www.likecs.com/' + str(self.receive_counter) ) @login_manager.user_loader def get_user(uid): ''' 继承UserMixin,进行用户的回调 :param uid: :return: ''' return User.query.get(int(uid))

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjsdx.html