class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200)) short_body = db.Column(db.Text) body = db.Column(db.Text) time = db.Column(db.DateTime) u_id = db.Column(db.Integer, db.ForeignKey('users.id')) user = db.relationship('User', backref=db.backref('posts')) blog_image = db.Column(db.String(300)) t_id = db.Column(db.Integer, db.ForeignKey('tags.id')) tag = db.relationship('Tag', backref=db.backref('posts')) def __init__(self, title=None, short_body=None, body=None, time=None, user=None, blog_image=None, **kwargs): super(Post, self).__init__(**kwargs) self.title = title self.short_body = short_body self.body = body self.time = time self.user = user self.blog_image = blog_image def __repr__(self): return '<Post %r>' % (self.title)
class Comment(db.Model): __tablename__ = 'comment' id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.TEXT, nullable=False) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) create_time = db.Column(db.DateTime, default=datetime.now) question = db.relationship('Question', backref=db.backref('comments')) author = db.relationship('User', backref=db.backref('comments'))
class Command(db.Model): __tablename__ = 'command' id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.Text, nullable=False) create_time = db.Column(db.DateTime, default=datetime.now) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) #通过在backref里添加order_by的函数,是数据库的倒序输出 question = db.relationship('Questions', backref=db.backref('commands', order_by=id.desc())) author = db.relationship('User', backref=db.backref('commands'))
class Tag(db.Model, Utf8Set): """ 'My life' 'Beautiful Chengdu' 'Small City Tongliang' 'Hometown Zhuji' """ __tablename__ = 'tag' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(64), nullable=False) tagType = db.Column(db.String(64), default='destination') remark = db.Column(db.String(255), default=u'No introduction for tag') albums = db.relationship('Album', backref='tag', lazy='dynamic') articles = db.relationship('Article', backref='tag', lazy='dynamic')
class Rooms(db.Model): __tablename__ = 'rooms' id = db.Column(db.Integer, primary_key=True) room_type = db.Column(db.String(255)) status = db.Column(db.String(255)) price = db.Column(db.Integer) reservation = db.relationship('Reservation', backref="rooms", lazy=True) def __init__(self, room_type, status, price): self.room_type = room_type self.status = status self.price = price def store(self): db.session.add(self) return db.session.commit() def update(self, room_type, status, price): self.room_type = room_type self.status = status self.price = price return db.session.commit() def delete(self): db.session.delete(self) return db.session.commit()
class Bus(db.Model): __tablename__ = 'busses' id = db.Column(db.Integer, primary_key=True) model = db.Column(db.String(255)) plate_number = db.Column(db.String(255)) arrival = db.Column(db.String(20)) departure = db.Column(db.String(20)) type = db.Column(db.String(255)) seats = db.Column(db.Integer) registered_at = db.Column(db.DateTime) image = db.Column(db.String(255)) schedules = db.relationship('Schedule', backref="bus", lazy=True) repairs = db.relationship('Repair', backref="bus", lazy=True) def __init__(self, model, plate_number, arrival, departure, type, seats, registered_at, image): self.model = model self.plate_number = plate_number self.arrival = arrival self.departure = departure self.type = type self.seats = seats self.registered_at = registered_at self.image = image def store(self): db.session.add(self) return db.session.commit() def update(self, model, plate_number, arrival, departure, type, seats, registered_at, image): self.model = model self.plate_number = plate_number self.arrival = arrival self.departure = departure self.type = type self.seats = seats self.registered_at = registered_at self.image = image return db.session.commit() def delete(self): db.session.delete(self) return db.session.commit()
class Schedule(db.Model): __tablename__ = 'schedules' id = db.Column(db.Integer, primary_key=True) departured_at = db.Column(db.DateTime) arrived_at = db.Column(db.DateTime) driver_id = db.Column(db.Integer, db.ForeignKey('users.id')) bus_id = db.Column(db.Integer, db.ForeignKey('busses.id')) conductor_id = db.Column(db.Integer) origin = db.Column(db.String(20)) destination = db.Column(db.String(255)) status = db.Column(db.String(20)) amount = db.Column(db.Float) duration = db.Column(db.String(20)) reservations = db.relationship('Reservation', backref="schedule", lazy=True) def __init__(self, departured_at, arrived_at, driver_id, bus_id, conductor_id, origin, destination, status, amount, duration): self.departured_at = departured_at self.arrived_at = arrived_at self.driver_id = driver_id self.bus_id = bus_id self.conductor_id = conductor_id self.origin = origin self.destination = destination self.status = status self.amount = amount self.duration = duration def store(self): db.session.add(self) return db.session.commit() def update(self, departured_at, arrived_at, driver_id, bus_id, conductor_id, origin, destination, status, amount, duration): self.departured_at = departured_at self.arrived_at = arrived_at self.driver_id = driver_id self.bus_id = bus_id self.conductor_id = conductor_id self.origin = origin self.destination = destination self.status = status self.amount = amount self.duration = duration return db.session.commit() def delete(self): db.session.delete(self) return db.session.commit() def update1(self, status): self.status = status return db.session.commit()
class item_img(db.Model): id = db.Column(db.Integer, primary_key=True) itemid = db.Column(db.Integer, db.ForeignKey('item.id')) imgsrc = db.Column(db.Text) item = db.relationship('item', backref=db.backref('item_img', lazy='dynamic')) def __init__(self, imgsrc, item): #self.itemid = itemid self.imgsrc = imgsrc self.item = item
class Bookmark(db.Model): __tablename__ = 'Bookmark' bookmark_num = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.ForeignKey('User.id')) bookmark_name = db.Column(db.CHAR(50)) longitude = db.Column(db.Float, nullable=False) latitude = db.Column(db.Float, nullable=False) user = db.relationship('User')
class Diary(db.Model): __tablename__ = 'diarys' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200)) short_body = db.Column(db.String(300)) body = db.Column(db.Text) time = db.Column(db.DateTime) diary_image = db.Column(db.String(300)) u_id = db.Column(db.Integer, db.ForeignKey('users.id')) user = db.relationship('User', backref=db.backref('diarys'))
class Question(db.Model): __tablename__ = 'question' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(128), nullable=False) content = db.Column(db.TEXT, nullable=False) #now()会保存服务首次运行时间,now为内次运行时的时间 create_time = db.Column(db.DateTime, default=datetime.now) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref=db.backref('questions'))
class Author(db.Model, Utf8Set): """ Every author has the right to edition in principle, However this functionality is only for website manager apparently. What was lucky, submitting a comment is available for all users; """ __tablename__ = 'author' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(128), unique=True, nullable=False) password = db.Column(db.String(64), nullable=False) nickname = db.Column(db.String(64), nullable=False) isAdmin = db.Column(db.Boolean, default=False) portrait = db.Column(db.String(128)) description = db.Column(db.String(512), default=u'This Guy is lazy enough! oops') # one to many articles = db.relationship('Article', backref='author', lazy='dynamic') comments = db.relationship('Comment', backref='author', lazy='dynamic') albums = db.relationship('Album', backref='author', lazy='dynamic') pictures = db.relationship('Picture', backref='author', lazy='dynamic') def __init__(self, username): self.username = username def set_password(self, password): self.password = bcrypt.generate_password_hash(password) def check_password(self, password): return bcrypt.check_password_hash(self.password, password) @staticmethod def get_author_by_username_and_password(username, password): author = Author.query.filter(Author.username == username).first() if author: if author.check_password(password): return author else: return None else: return None
class Questions(db.Model): __tablename__ = 'question' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) #now()获取的是服务器第一次运行的时间 #now 是每次创立一个模型时候,都获取当前的时间,不要搞混了 create_time = db.Column(db.DateTime, default=datetime.now) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref=db.backref('questions'))
class Article(db.Model, Utf8Set): """ For article detail article info: id \ title \ text\ create_date \ update_date access info: privacy\status """ __tablename__ = 'article' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(64), nullable=False) cover = db.Column(db.String(255)) content = db.Column(db.Text, nullable=False) summary = db.Column(db.String(255), default=u'作者还未添加文字信息') # default value is draft and public status = db.Column(db.Boolean, default=False) privacy = db.Column(db.Boolean, default=False) createDate = db.Column(db.DateTime, default=datetime.datetime.now) updateDate = db.Column(db.DateTime, default=datetime.datetime.now) # one to one authorId = db.Column(db.Integer, db.ForeignKey('author.id')) # travel \ dairy, or blog articles are belong to dairy # 'My life' 'Beautiful Chengdu' 'Small City Tongliang' 'Hometown Zhuji' tagId = db.Column(db.Integer, db.ForeignKey('tag.id')) # one to many comments = db.relationship('Comment', backref='article', lazy='dynamic') def __init__(self, title, content, summary, privacy, tagId, authorId, cover=''): self.title = title self.content = content self.summary = summary self.privacy = privacy self.cover = cover self.tagId = tagId self.authorId = authorId
class User(db.Model, UserMixin): __tablename__ = 'User' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True, unique=True) email = db.Column(db.Text, nullable=False) name = db.Column(db.Text, nullable=False) password = db.Column(db.Text, nullable=False) age = db.Column(db.Integer) active = db.Column(db.Boolean) role = db.Column(db.ForeignKey('Role.id')) Role = db.relationship('Role') def set_password(self, password): self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password)
class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) email = db.Column(db.String(200)) comment = db.Column(db.Text) p_id = db.Column(db.Integer, db.ForeignKey('posts.id')) post = db.relationship('Post', backref=db.backref('comments')) def __init__(self, name=None, email=None, comment=None, post=None, **kwargs): super(Comment, self).__init__(**kwargs) self.name = name self.email = email self.comment = comment self.post = post def __repr__(self): return '<Comments %r>' % (self.name)
class Album(db.Model, Utf8Set): """ All albums are public; Currently, Album refer to tag Dairy Emotion Capture Travel ChengDu Tongliang Zhuji """ __tablename__ = 'album' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(64), nullable=False) remark = db.Column(db.String(255), nullable=False) cover = db.Column(db.String(255)) privacy = db.Column(db.Boolean, default=False) iscarrousel = db.Column(db.Boolean, default=False) createDate = db.Column(db.DateTime, default=datetime.datetime.now) # one to one authorId = db.Column(db.Integer, db.ForeignKey('author.id')) tagId = db.Column(db.Integer, db.ForeignKey('tag.id')) # one to many pictures = db.relationship('Picture', backref='album', lazy='dynamic') def __init__(self, title, remark, privacy, authorId, tagId): self.title = title self.remark = remark self.privacy = privacy self.authorId = authorId self.tagId = tagId
class Article(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(200), unique=True, nullable=False) slug = db.Column(db.String(400), unique=True, nullable=False) created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) modified = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) user_id = db.Column(db.Integer, nullable=False) url_en = db.Column(db.String(200), unique=True, nullable=False) image_url = db.Column(db.String(100), nullable=True) text = db.Column(db.Text, nullable=False) template = db.Column(db.Text, nullable=False) like_count = db.Column(db.Integer, nullable=True) views = db.Column(db.Integer, nullable=True) __searchable__ = ["text", "title", "slug", "url_en"] __msearch_analyzer__ = ChineseAnalyzer() tags = db.relationship('Tag', secondary=article_tag, backref=db.backref('articles', lazy="dynamic"), lazy="dynamic") category = db.relationship('Category', secondary=article_category, backref=db.backref('articles', lazy="dynamic"), lazy="dynamic") def save(self): try: db.session.add(self) db.session.commit() current_app.logger.info('文章保存成功') return {'status': True} except Exception as error: current_app.logger.error('文章保存错误{}:'.format(error)) return {'status': False, "message": error} def updata(self): try: db.session.commit() current_app.logger.info('文章更新成功') return {'status': True} except Exception as error: current_app.logger.error('文章保存错误{}:'.format(error)) return {'status': False, "message": error} def delete(self): try: db.session.delete(self) db.session.commit() current_app.logger.info('文章删除成功') return {'status': True} except Exception as error: current_app.logger.error('文章删除错误{}:'.format(error)) return {'status': False, "message": error} def to_json(self): dict = self.__dict__ if "_sa_instance_state" in dict: del dict["_sa_instance_state"] return dict def paginate(self): print(self)
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) firstname = db.Column(db.String(255)) lastname = db.Column(db.String(255)) middlename = db.Column(db.String(255)) address = db.Column(db.String(225)) mobile = db.Column(db.Integer) role = db.Column(db.String(255)) email = db.Column(db.String(225), unique=True) password = db.Column(db.String(150)) created_at = db.Column(db.DateTime) updated_at = db.Column(db.DateTime) reservation = db.relationship('Reservation', backref="users", lazy=True) #reservation = db.relationship('Reservation', backref="user",lazy=True) #schedules = db.relationship('Schedule', backref="user",lazy=True) #repairs = db.relationship('Repair', backref="user",lazy=True) def __init__(self, firstname, lastname, middlename, address, mobile, email, role, password): self.firstname = firstname self.lastname = lastname self.middlename = middlename self.address = address self.mobile = mobile self.email = email self.role = role self.password = password #self.created_at = created_at #self.updated_at = updated_at def __repr__(self): return '<User %r>' % self.email def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return str(self.email) def __init__( self, firstname, lastname, middlename, address, mobile, email, role, password, ): self.firstname = firstname self.lastname = lastname self.middlename = middlename self.address = address self.mobile = mobile self.email = email self.role = role self.password = password #self.created_at = created_at #self.updated_at = updated_at def store(self): db.session.add(self) return db.session.commit() def update(self, firstname, lastname, middlename, address, mobile, email, role, password): self.firstname = firstname self.lastname = lastname self.middlename = middlename self.address = address self.mobile = mobile self.email = email self.role = role self.password = password return db.session.commit() def delete(self): db.session.delete(self) return db.session.commit()