class Place(db.Model): __tablename__ = "places" __table_args__ = (CheckConstraint('rating >= 0 and rating <= 5', name="rating_0_5"), ) id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), unique=True) snippet = db.Column(db.Text, index=True) description = db.Column(db.Text, index=True) rating = db.Column(db.Integer) price = db.Column(db.String(64)) schedule = db.Column(db.Text) info = db.Column(db.Text) address = db.Column(db.String(128)) location = db.Column(db.String(64)) slug = db.Column(db.String(128), unique=True) comments = db.relationship(Comment, secondary=comments2places) images = db.relationship("PlaceImage", backref="place") timestamp = db.Column(db.DateTime, default=datetime.utcnow) def __repr__(self): return self.title def generate_slug(self, title): """ Generate slug by title """ self.slug = slugify_ru(title, to_lower=True)
class Permission(db.Model): """ 用户权限 """ __tablename__ = "article" id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), unique=True, nullable=False) content = db.Column(db.Text, nullable=False) column = db.Column(db.String(20), nullable=False, default='news') author = db.Column(db.String(50), default='admin') article_status = db.Column(db.Integer, nullable=False, default=0) article_type = db.Column(db.Integer, nullable=False, default=0) create_time = db.Column(db.DateTime, nullable=False) pub_time = db.Column(db.DateTime) def to_json(self): pub_time = self.pub_time.strftime( "%Y-%m-%d %H:%S:%M") if self.pub_time else '' return { 'id': self.id, 'title': self.title, 'content': self.content, 'column': self.column, 'author': self.author, 'create_time': self.create_time.strftime("%Y-%m-%d %H:%S:%M"), 'pub_time': pub_time } def __repr__(self): return f"<User '{self.title}'>"
class Comment(db.Model): __tablename__ = "comments" id = db.Column(db.Integer, primary_key=True) author_name = db.Column(db.String(64), nullable=False) author_email = db.Column(db.String(64), nullable=False) text = db.Column(db.Text, nullable=False) posted_time = db.Column(db.DateTime, default=datetime.utcnow()) def __repr__(self): return "{} | {} | {}".format(self.author_name, self.author_email, self.posted_time)
class Phrases(db.Model): id = db.Column(db.Integer, primary_key=True) created = db.Column(db.DateTime, default=datetime.datetime.now) phrase = db.Column(db.String(140), index=True) def __repr__(self): return str(self.phrase)
class PlaceImage(db.Model): __tablename__ = "place_images" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) path = db.Column(db.String(256), unique=True) thumbnail = db.Column(db.String(256), unique=True) place_id = db.Column(db.Integer, db.ForeignKey("places.id")) def __repr__(self): return "{} | {}".format(self.name, self.path) def add_thumbnail(self): print(self.path) self.thumbnail = re.sub(r'\.', '_thumb.', self.path) print(self.thumbnail)
class Document(db.Model): """ 企业动态 """ __tablename__ = "document" id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), unique=True, nullable=False) content_text = db.Column(db.Text, nullable=False) content_html = db.Column(db.Text, nullable=False) column_id = db.Column(db.Integer, db.ForeignKey('column.id')) author_id = db.Column(db.String(100), db.ForeignKey('user.uid')) status = db.Column(db.Integer, nullable=False, default=0) type = db.Column(db.Integer, nullable=False, default=0) create_time = db.Column(db.DateTime, nullable=False) pub_time = db.Column(db.DateTime) column = db.relationship('Column', backref=db.backref('document')) author = db.relationship('User', backref=db.backref('document')) @property def content(self): return self.content_text @content.setter def content(self, content_html): soup = BeautifulSoup(content_html, 'html.parser') self.content_text = soup.text def to_json(self): pub_time = self.pub_time.strftime("%Y-%m-%d %H:%S:%M") if self.pub_time else '' return { 'id': self.id, 'title': self.title, 'content': self.content_text, 'content_html': self.content_html, 'column': self.column.title, 'column_id': self.column_id, 'author': self.author, 'create_time': self.create_time.strftime("%Y-%m-%d %H:%S:%M"), 'pub_time': pub_time, 'status': self.status } def __repr__(self): return f"<Document '{self.title}'>"
class User(db.Model): """ 用户表 """ __tablename__ = "user" uid = db.Column(db.String(100), primary_key=True, default=uuid1().hex) username = db.Column(db.String(100), unique=True) email = db.Column(db.String(100), unique=True) password_hash = db.Column(db.String(100)) role = db.Column(db.String(100), default='user') registration_time = db.Column(db.DateTime, default=datetime.now()) status = db.Column(db.Integer, default=0, nullable=False) @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = flask_bcrypt.generate_password_hash( password).decode('utf-8') def check_password(self, password): return flask_bcrypt.check_password_hash(self.password_hash, password) def to_json(self): registration_time = self.registration_time.strftime( "%Y-%m-%d %H:%S:%M") if self.registration_time else '' return { 'uid': self.uid, 'username': self.username, 'email': self.email, 'role': self.role, 'registration_time': registration_time, 'status': self.status, 'avatar': '', } def __repr__(self): return f"<User '{self.username}'>"
class Tour(db.Model): __tablename__ = "tours" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), unique=True) price = db.Column(db.String(64)) slug = db.Column(db.String(256), unique=True) duration = db.Column(db.String(64)) min_people_number = db.Column(db.Integer) distance = db.Column(db.Integer) places = db.relationship("Place", secondary=places2tours, backref="places") comments = db.relationship(Comment, secondary=comments2tours) def __repr__(self): return self.title def generate_slug(self, title): """ Generate slug by title """ self.slug = slugify_ru(title, to_lower=True)
class Column(db.Model): """ 企业动态 """ __tablename__ = "column" id = db.Column(db.String(100), primary_key=True, nullable=False) title = db.Column(db.String(100), nullable=False) parent_id = db.Column(db.String(100), nullable=False, default='root') type = db.Column(db.String(20)) __table_args__ = ( UniqueConstraint('title', 'parent_id'), # 同一栏目下目录标题唯一 ) def to_json(self): return { 'id': self.id, 'title': self.title, 'parent_id': self.parent_id, 'type': self.type, } def __repr__(self): return f"<Column '{self.id}'>"
class User(UserMixin, db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String, unique=True) email = db.Column(db.String, unique=True) password_hash = db.Column(db.String(128), unique=True) confirmed = db.Column(db.Boolean, default=False) groups = relationship("UserGroup", secondary=user2group, backref=backref('users', order_by=id)) def __repr__(self): return "<{}>".format(self.username) @property def password(self): raise AttributeError("Password is not readable") @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) 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 current_app.logger.info("User {} confirmed".format(self.username)) db.session.add(self) db.session.commit() return True
class UpperWords(db.Model): word = db.Column(db.String(40), index=True, primary_key=True, unique=True) def __repr__(self): return self.word