class Admin(db.Model): __tablename__ = 'Admin' adminName = db.Column(db.Unicode(20), primary_key=True) adminEmail = db.Column(db.Unicode(64), nullable=False) adminPhone = db.Column(db.CHAR(11), nullable=False) adminPass = db.Column(db.CHAR(40), nullable=False) adminType = db.Column(db.CHAR(6), nullable=False) lastLogin = db.Column(db.DateTime, nullable=False)
class CourseInfo(db.Model): __tablename__ = 'CourseInfo' courseId = db.Column(db.CHAR(8), primary_key=True) typeId = db.Column(db.Integer, db.ForeignKey('CourseType.typeId'), nullable=False) courseTitle = db.Column(db.Unicode(64), nullable=False) courseTeacher = db.Column(db.Unicode(20), nullable=False) coursePubTime = db.Column(db.DateTime, nullable=False) courseAbstract = db.Column(db.Text, nullable=False) courseImage = db.Column(db.Unicode(256), nullable=False)
class User(db.Model): __tablename__ = 'User' userName = db.Column(db.Unicode(20), primary_key=True) userEmail = db.Column(db.Unicode(64), nullable=False) userPhone = db.Column(db.CHAR(11), nullable=False) userPass = db.Column(db.Unicode(100), nullable=False) signUpDate = db.Column(db.DateTime, default=datetime.now(), nullable=False) lastLogin = db.Column(db.DateTime, nullable=False) studyDuration = db.Column(db.Float, nullable=False) frozenDuration = db.Column(db.Float, nullable=False) frozenReason = db.Column(db.Text) courseCount = db.relationship('CourseInfo', secondary=Enroll, backref=db.backref('stds', lazy='dynamic'), lazy='dynamic')
class CourseVideo(db.Model): __tablename__ = 'CourseVideo' MD5 = db.Column(db.CHAR(40), primary_key=True) chapId = db.Column(db.CHAR(15), db.ForeignKey('ChapterInfo.chapId'), nullable=False) courseId = db.Column(db.CHAR(8), db.ForeignKey('CourseInfo.courseId'), nullable=False) fileName = db.Column(db.Unicode(256), nullable=False) filePath = db.Column(db.Unicode(256), nullable=False) fileSize = db.Column(db.Integer, nullable=False) uploadTime = db.Column(db.DateTime, nullable=False) fileExt = db.Column(db.CHAR(4), nullable=False) isDoc = db.Column(db.Boolean, nullable=False)
class CourseType(db.Model): __tablename__ = 'CourseType' typeId = db.Column(db.Integer, primary_key=True) Cou_typeId = db.Column(db.Integer, db.ForeignKey('CourseType.typeId'), nullable=False) typeName = db.Column(db.Unicode(20), nullable=False)
class AdminLog(db.Model): __tablename__ = 'AdminLog' logId = db.Column(db.CHAR(40), primary_key=True) adminName = db.Column(db.Unicode(20), db.ForeignKey('Admin.adminName'), nullable=False) logTime = db.Column(db.DateTime, default=datetime.now(), nullable=False) logContent = db.Column(db.Text, nullable=False)
class ChapterInfo(db.Model): __tablename__ = 'ChapterInfo' chapId = db.Column(db.CHAR(15), primary_key=True) courseId = db.Column(db.CHAR(8), db.ForeignKey('CourseInfo.courseId'), primary_key=True, nullable=False) chapName = db.Column(db.Unicode(64), nullable=False) chapLayer = db.Column(db.SmallInteger, nullable=False)
class Comments(db.Model): __tablename__ = 'Comments' cmtId = db.Column(db.Integer, primary_key=True, autoincrement=True) rep_cmtId = db.Column(db.Integer, db.ForeignKey('Comments.cmtId'), nullable=True) userName = db.Column(db.Unicode(20), db.ForeignKey('User.userName'), nullable=False) chapId = db.Column(db.CHAR(15), db.ForeignKey('ChapterInfo.chapId'), nullable=True) courseId = db.Column('courseId', db.CHAR(8), db.ForeignKey('CourseInfo.courseId'), nullable=False) cmtContent = db.Column(db.Text, nullable=False) submitTime = db.Column(db.DateTime, nullable=False)
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(128), nullable=False, unique=True) username = db.Column(db.Unicode(128), nullable=False) password = db.Column(db.String(100), nullable=False) pw_hash = db.Column(db.String(128)) avatar_hash = db.Column(db.String(32)) desc = db.Column(db.UnicodeText, nullable=True, default=u"还没个人介绍呢") create_time = db.Column(db.DateTime, default=datetime.utcnow) last_active_time = db.Column(db.DateTime, default=datetime.utcnow) role_id = db.Column(db.Integer, db.ForeignKey('role.id')) role = db.relationship('Role', backref=db.backref('users')) confirmed = db.Column(db.Boolean, default=False) msg_unread = db.Column(db.Boolean, default=False) tags = db.relationship('Tag', secondary=table_user_to_tag, backref=db.backref('users', lazy='dynamic'), lazy='dynamic') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) if self.role is None: if self.email == APP_CONFIG['APP_ROOT']: self.role = Role.query.filter_by( type=USER_CODE['root']).first() else: self.role = Role.query.filter_by( type=USER_CODE['email_not_pass']).first() if self.email is not None and self.avatar_hash is None: self.avatar_hash = hashlib.md5( self.email.encode('utf-8')).hexdigest() launcher = Question(title=u"我的快速启动", desc=u"在此添加、删除、排序您的快速启动项", author=self, private=True) Theme.query.filter_by(name=APP_CONFIG['THEME_NAME_LAUNCHER'] ).first_or_404().questions.append(launcher) db.session.add(launcher) db.session.commit() def generate_confirmation_token(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'confirm': self.id}) def confirm(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return False if data.get('confirm') != self.id: return False self.confirmed = True db.session.commit() return True @property def password(self): raise AttributeError('unable') @password.setter def password(self, password): self.pw_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.pw_hash, password) @staticmethod def register_available(email): if User.query.filter_by(email=email).first() is not None: return False return True @staticmethod def get_user_by_email(email): return User.query.filter_by(email=email).first() @staticmethod def get_user_by_email_or_404(email): return User.query.filter_by(email=email).first_or_404() @staticmethod def get_user_by_id(id): return User.query.filter_by(id=id).first() @staticmethod def get_user_by_id_or_404(id): return User.query.filter_by(id=id).first_or_404() @staticmethod def delete_user_from_email_complete(email): user = User.get_user_by_email(email) if user is None: return False for item in Question.query.filter_by(author=user).all(): item.delete_answers() db.session.delete(item) db.session.delete(user) db.session.commit() return True def gravatar(self, size=100, default='identicon', rating='g'): if request.is_secure: url = 'https://secure.gravatar.com/avatar' else: url = 'http://www.gravatar.com/avatar' hash = hashlib.md5(self.email.encode('utf-8')).hexdigest() return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( url=url, hash=hash, size=size, default=default, rating=rating) @staticmethod def get_id_from_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return None return data.get('confirm') def get_follow_tag_question(self): query = db.session.query(Question) query = query.filter( Question.tags.any(Tag.users.any(User.id == self.id))) return query def hav_tag(self, name): if self.tags.filter_by(name=name).count() > 0: return True return False def add_tag(self, name): if self.hav_tag(name): return self.tags.append(Tag.get_or_create(name)) def remove_tag(self, name): if self.hav_tag(name): self.tags.remove(Tag.get_or_create(name)) def __repr__(self): return '<User %r %r>' % (self.username, self.email)
#encoding: utf-8 from exts import db from flask_sqlalchemy import SQLAlchemy from datetime import datetime Enroll = db.Table( 'Enroll', db.Column('userName', db.Unicode(20), db.ForeignKey('User.userName'), primary_key=True), db.Column('courseId', db.CHAR(8), db.ForeignKey('CourseInfo.courseId'), primary_key=True), ) class User(db.Model): __tablename__ = 'User' userName = db.Column(db.Unicode(20), primary_key=True) userEmail = db.Column(db.Unicode(64), nullable=False) userPhone = db.Column(db.CHAR(11), nullable=False) userPass = db.Column(db.Unicode(100), nullable=False) signUpDate = db.Column(db.DateTime, default=datetime.now(), nullable=False) lastLogin = db.Column(db.DateTime, nullable=False) studyDuration = db.Column(db.Float, nullable=False) frozenDuration = db.Column(db.Float, nullable=False) frozenReason = db.Column(db.Text) courseCount = db.relationship('CourseInfo',