class Store(db.Model, BaseModel): name = db.Column(db.String(50), nullable=False) remark = db.Column(db.String(500)) members = db.relationship('Member', secondary=member_store, backref=db.backref( 'stores', lazy="dynamic", order_by="desc(Store.created_at)")) default = db.Column(db.Boolean, default=False) @property def url(self): return url_for('store', id=self.id) @property def absolute_url(self): return url_for('store', id=self.id, _external=True) @property def company_count(self): return self.companies.count() def get_user_by_mobile(self, mobile): return self.users.filter( or_(User.mobile == mobile, User.mobile2 == mobile)).first()
class Company(db.Model, BaseModel): name = db.Column(db.String(50), nullable=False) remark = db.Column(db.String(500)) web_site = db.Column(db.String(250)) store_id = db.Column(db.Integer, db.ForeignKey('store.id')) store = db.relationship('Store', backref=db.backref( 'companies', lazy="dynamic", order_by="desc(Company.created_at)")) @property def url(self): return url_for('company', id=self.id) @property def absolute_url(self): return url_for('company', id=self.id, _external=True) @staticmethod def get_by_user_id_and_company_id(user_id, company_id): company = Company.query.join(Store).join(member_store).filter( member_store.c.member_id == user_id, Company.id == company_id, ).first() return company
class Account(db.Model, SessionMixin): __tablename__ = "users" __table_args__ = { 'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8', 'mysql_collate': 'utf8_general_ci' } username = db.Column(db.String(100), primary_key=True, nullable=False) password = db.Column(db.String(100), nullable=False) role = db.Column(db.Integer, nullable=False) email = db.Column(db.String(100)) def __init__(self, **kwargs): if 'password' in kwargs: raw = kwargs.pop('password') self.password = self.create_password(raw) if 'username' in kwargs: self.username = kwargs.pop('username') for k, v in kwargs.items(): setattr(self, k, v) @property def is_admin(self): return self.role == 2 @property def is_stu(self): return self.role == 1 @property def is_teacher(self): return self.role == 3 @staticmethod def create_password(raw): passwd = '{old}{new}'.format(old=raw, new=db.app.config['PASSWORD_SECRET']) return security.generate_password_hash(passwd) def change_password(self, raw): self.passwd = self.create_password(raw) return self def check_password(self, raw): passwd = '{old}{new}'.format(old=raw, new=db.app.config['PASSWORD_SECRET']) return security.check_password_hash(self.password, passwd)
class User(db.Model): id = db.Column(db.Integer(), primary_key=True) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) def __init__(self, email, password): self.email = email self.active = True self.password = User.hashed_password(password) @staticmethod def hashed_password(password): return bcrypt.generate_password_hash(password) @staticmethod def get_user_with_email_and_password(email, password): user = User.query.filter_by(email=email).first() if user and bcrypt.check_password_hash(user.password, password): return user else: return None
class Member(db.Model, BaseModel): username = db.Column(db.String(50), unique=True) email = db.Column(db.String(128), unique=True) mobile = db.Column(db.String(11), unique=True) name = db.Column(db.String(50)) password = db.Column(db.String(128), nullable=False) @property def url(self): return url_for('member', id=self.id) @property def absolute_url(self): return url_for('member', id=self.id, _external=True) def __setattr__(self, name, value): # Hash password when set it. if name == 'password': value = generate_password_hash(value) super(Member, self).__setattr__(name, value) def check_password(self, password): return check_password_hash(self.password, password)
class Review(db.Model): rid = db.Column(db.Integer, primary_key=True, nullable=False) content = db.Column(db.String(255), nullable=False) # use review.activity aid = db.Column(db.Integer, db.ForeignKey('activity.aid'), nullable=False) # use review.reviewer uid = db.Column(db.Integer, db.ForeignKey('user.uid'), nullable=False) review_t = db.Column(db.DateTime, nullable=False, default=datetime.now) def __init__(self, content, aid, uid): self.content = content self.aid = int(aid) self.uid = int(uid) def dump(self): ''' Dump the object. ''' return { 'rid': self.rid, 'content': self.content, 'reviewer': self.reviewer.dump(), 'review_t': gettimestamp(self.review_t) }
class User(db.Model): ''' User model ''' uid = db.Column(db.Integer, primary_key=True, nullable=False) tel = db.Column(db.String(11), nullable=False, unique=True) pwd = db.Column(db.String(32), nullable=False) name = db.Column(db.String(32), nullable=False, default='Anonymous') big_avatar = db.Column(db.String(255), nullable=False, default=H_DEFAULT_AVATAR_BIG) small_avatar = db.Column(db.String(255), nullable=False, default=H_DEFAULT_AVATAR_SMALL) origin_avatar = db.Column(db.String(255), nullable=False, default=H_DEFAULT_AVATAR_ORIGIN) # sex # 1 indicates male while 0 indicates female. sex = db.Column(db.SmallInteger, nullable=False, default=1) age = db.Column(db.SmallInteger, nullable=False, default=0) loc = db.Column(db.String(255), nullable=True) reg_time = db.Column(db.DateTime, nullable=False, default=datetime.now()) last_login_t = db.Column(db.DateTime, nullable=False, default=datetime.now(), onupdate=datetime.now()) token = db.Column(db.String(16), nullable=False) h_activities = db.relationship('Activity', backref=db.backref('host', lazy='select'), lazy='select') p_activities = db.relationship('Activity', backref=db.backref('joins', lazy='select'), lazy='select', secondary=Participant) v_activities = db.relationship('Activity', backref=db.backref('voters', lazy='select'), lazy='select', secondary=Voteup) reviews = db.relationship('Review', backref=db.backref('reviewer', lazy='select'), lazy='select') def __init__(self, tel, pwd, token): self.tel = tel self.pwd = pwd self.token = token def render_avatar(self, type='small'): ''' Return a file object of avatar. ''' pic = { 'small': self.small_avatar, 'big': self.big_avatar, 'origin': self.origin_avatar } if type not in pic.keys(): raise ThrownError('Avatar type error') return open(pic[type], 'rb') def dump(self): return { 'uid': self.uid, 'tel': self.tel, 'name': self.name, 'big_avatar': self.big_avatar, 'small_avatar': self.small_avatar, 'sex': self.sex, 'age': self.age, 'loc': self.loc }
class Activity(db.Model): ''' Activity Model ''' aid = db.Column(db.Integer, primary_key=True, nullable=False) name = db.Column(db.String(32), nullable=False) # you can use Activity.host to visit the host of this activity uid = db.Column(db.Integer, db.ForeignKey('user.uid'), nullable=False) intro = db.Column(db.String(255), nullable=False) launch_t = db.Column(db.DateTime, nullable=False, default=datetime.now) update_t = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now) start_t = db.Column(db.DateTime, nullable=False) end_t = db.Column(db.DateTime, nullable=False) # 0 means to do; 1 means doing; 2 means done. status = db.Column(db.SmallInteger, nullable=False, default=0) limits = db.Column(db.Integer, nullable=False) participants = db.Column(db.Integer, nullable=False, default=0) voteups = db.Column(db.Integer, nullable=False, default=0) longitude = db.Column(DOUBLE, nullable=False) latitude = db.Column(DOUBLE, nullable=False) loc = db.Column(db.String(255), nullable=False) image = db.Column(db.String(255), nullable=False) reviews = db.relationship('Review', backref=db.backref('activity', lazy='select'), lazy='dynamic') def __init__(self, name, uid, start_t, end_t, limits, longitude, latitude, loc, intro='', image=''): self.name = name self.uid = int(uid) self.start_t = datetime.fromtimestamp(float(start_t)) self.end_t = datetime.fromtimestamp(float(end_t)) self.limits = int(limits) self.longitude = float(longitude) self.latitude = float(latitude) self.loc = loc self.intro = intro self.image = image def render_image(self): return open(self.image, 'rb') @property def last_time(self): ''' Return what time does this activity last for. ''' return self.end_t - self.start_t def convert(self): self.longitude = float(self.longitude) self.latitude = float(self.latitude) def distances(self, l, b): ''' Return distances from a specified location. ''' mlon_a = self.longitude * math.pi / 180 mlat_a = (90 - self.latitude) * math.pi / 180 mlon_b = l * math.pi / 180 mlat_b = (90 - b) * math.pi / 180 c = math.sin(mlat_a) * math.sin(mlat_b) * math.cos(mlon_a-mlon_b) + math.cos(mlat_a) * math.cos(mlat_b) d = 6371.004 * math.acos(c) * math.pi / 180 return d def dump(self): ''' Dump the object. ''' dd = int(math.ceil(time.time())) if dd - gettimestamp(self.start_t) > 0: if dd < gettimestamp(self.end_t) < 0: self.status = 1 else: self.status = 2 db.session.add(self) db.session.commit() print self.last_time self.convert() return { 'aid': self.aid, 'name': self.name, 'host': self.host.dump(), 'intro': self.intro, 'launch_t': gettimestamp(self.launch_t), 'start_t': gettimestamp(self.start_t), 'end_t': gettimestamp(self.end_t), 'last_time': self.last_time.total_seconds(), 'status': self.status, 'limits': self.limits, 'participants': self.participants, 'voteups': self.voteups, 'longitude': self.longitude, 'latitude': self.latitude, 'loc': self.loc, 'image': self.image }