# -*- coding：utf-8 -*-


from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()


class Blog(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    text = db.Column(db.Text)
    pub_date = db.Column(db.DateTime)
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    category = db.relationship('Category',
                               backref=db.backref('blog', lazy='dynamic'))

    def __init__(self, title, text, category_id, pub_date=None):
        self.title = title
        self.text = text
        if pub_date is None:
            pub_date = datetime.now()
        self.pub_date = pub_date
        self.category_id = category_id

    def __str__(self):
        return self.title


class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name
