class Work(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50)) foreword = db.Column(db.Text()) content = db.Column(db.Text()) intro = db.Column(db.Text()) create_time = db.Column(db.DateTime) author_id = db.Column(db.Integer, db.ForeignKey('author.id')) author = db.relationship('Author', backref=db.backref('works')) type_id = db.Column(db.Integer, db.ForeignKey('work_type.id')) type = db.relationship('WorkType', backref=db.backref('works')) def __repr__(self): return '<Work %s>' % self.title @property def clean_content(self): c = re.sub(r'<([^<]+)>', '', self.content) c = c.replace('%', '') c = c.replace('(一)', "") c = c.replace('(一)', "") return c @property def friendly_content(self): """ Add comment -> Split ci -> Generate paragraph """ c = re.sub(r'<([^<^b]+)>', r"<sup title='\1'></sup>", self.content) c = c.replace('%', " ") c = markdown2.markdown(c) return c
class Author(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True) abbr = db.Column(db.String(50), unique=True) intro = db.Column(db.Text()) birth_year = db.Column(db.String(20)) death_year = db.Column(db.String(20)) dynasty_id = db.Column(db.Integer, db.ForeignKey('dynasty.id')) dynasty = db.relationship('Dynasty', backref=db.backref('authors', lazy='dynamic')) def __repr__(self): return '<Author %s>' % self.name @property def random_quote(self): """ Get a random quote of the author """ if not hasattr(self, '_random_quote'): self._random_quote = AuthorQuote.query.filter( AuthorQuote.author_id == self.id).order_by( db.func.rand()).first() return self._random_quote
class AuthorQuote(db.Model): id = db.Column(db.Integer, primary_key=True) quote = db.Column(db.Text()) author_id = db.Column(db.Integer, db.ForeignKey('author.id')) author = db.relationship('Author', backref=db.backref('quotes')) work_id = db.Column(db.Integer, db.ForeignKey('work.id')) work = db.relationship('Work', backref=db.backref('quotes')) def __repr__(self): return '<AuthorQuote %s>' % self.quote
class Dynasty(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) abbr = db.Column(db.String(50), unique=True) intro = db.Column(db.Text()) start_year = db.Column(db.Integer) end_year = db.Column(db.Integer) def __repr__(self): return '<Dynasty %s>' % self.name @property def friendly_start_year(self): return "%s年" % str(self.start_year).replace('-', '前') @property def friendly_end_year(self): if self.end_year == 2012: return "至今" else: return "%s年" % str(self.end_year).replace('-', '前')