"""Collection of Models used in blogsite."""
from . import db


class Post(db.Model):
    """Model representing a blog post.

    Attributes
    ----------
    id : SQLAlchemy.Column
        Autogenerated primary key
    title : SQLAlchemy.Column
    body : SQLAlchemy.Column
    """

    # Columns
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(128))
    body = db.Column(db.String(4096))

    def __init__(self, title, body):
        """Constructor for Post.

        Parameters
        ----------
        title : String
            Title/Summary of post
        body : String
            Contents
        """
        self.title = title
        self.body = body

    def __repr__(self):
        """Representation."""
        return '<Post %r:%r>' % (self.id, self.title)

"""Collection of Models used in blogsite."""
from . import db
from datetime import datetime


class Post(db.Model):
    """Model representing a blog post.

    Attributes
    ----------
    id : SQLAlchemy.Column
        Autogenerated primary key
    title : SQLAlchemy.Column
    body : SQLAlchemy.Column
    pub_date : SQLAlchemy.Column
        Date and Time of Post creation
    category_id : SQLAlchemy.Column
        Foreign Key ID to Category
    category : SQLAlchemy.relationship
        Category object that relates to this Post
    """

    # Columns
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(128))
    body = db.Column(db.String(4096))
    pub_date = db.Column(db.DateTime)
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    category = db.relationship('Category',
                               backref=db.backref('posts', lazy='dynamic'))

    def __init__(self, title, body, category, pub_date=None):
        """Constructor for Post.

        Parameters
        ----------
        title : String
            Title/Summary of post
        body : String
            Contents
        category : Category
            Category object blog post is related to
        pub_date : DateTime
            Optional
        """
        self.title = title
        self.body = body
        self.category = category
        if pub_date is None:
            pub_date = datetime.utcnow()
        self.pub_date = pub_date

    def __repr__(self):
        """Representation."""
        return '<Post %r:%r>' % (self.id, self.title)


class Category(db.Model):
    """Model to represent a overall category.

    Attributes
    ----------
    id : SQLAlchemy.Column
    name : SQLAlchemy.Column
    """

    # Columns
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(128))

    def __init__(self, name):
        """Constructor for Category.

        Parameters
        ----------
        name : String
            Name of new category
        """
        self.name = name

    def __repr__(self):
        """Representation."""
        return '<Category %r>' % self.name

Add Category model and expanding Post model.
Add Category model and expanding Post model.

Post model now contains a publish date and a foreign key reference
to the Category model.

  """Collection of Models used in blogsite."""
  from . import db
+ from datetime import datetime
  
  
  class Post(db.Model):
      """Model representing a blog post.
  
      Attributes
      ----------
      id : SQLAlchemy.Column
          Autogenerated primary key
      title : SQLAlchemy.Column
      body : SQLAlchemy.Column
+     pub_date : SQLAlchemy.Column
+         Date and Time of Post creation
+     category_id : SQLAlchemy.Column
+         Foreign Key ID to Category
+     category : SQLAlchemy.relationship
+         Category object that relates to this Post
      """
  
      # Columns
      id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      title = db.Column(db.String(128))
      body = db.Column(db.String(4096))
+     pub_date = db.Column(db.DateTime)
+     category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
+     category = db.relationship('Category',
+                                backref=db.backref('posts', lazy='dynamic'))
  
-     def __init__(self, title, body):
+     def __init__(self, title, body, category, pub_date=None):
          """Constructor for Post.
  
          Parameters
          ----------
          title : String
              Title/Summary of post
          body : String
              Contents
+         category : Category
+             Category object blog post is related to
+         pub_date : DateTime
+             Optional
          """
          self.title = title
          self.body = body
+         self.category = category
+         if pub_date is None:
+             pub_date = datetime.utcnow()
+         self.pub_date = pub_date
  
      def __repr__(self):
          """Representation."""
          return '<Post %r:%r>' % (self.id, self.title)
  
+ 
+ class Category(db.Model):
+     """Model to represent a overall category.
+ 
+     Attributes
+     ----------
+     id : SQLAlchemy.Column
+     name : SQLAlchemy.Column
+     """
+ 
+     # Columns
+     id = db.Column(db.Integer, primary_key=True, autoincrement=True)
+     name = db.Column(db.String(128))
+ 
+     def __init__(self, name):
+         """Constructor for Category.
+ 
+         Parameters
+         ----------
+         name : String
+             Name of new category
+         """
+         self.name = name
+ 
+     def __repr__(self):
+         """Representation."""
+         return '<Category %r>' % self.name
+ 
Add Category model and expanding Post model.
## Code Before:
"""Collection of Models used in blogsite."""
from . import db


class Post(db.Model):
    """Model representing a blog post.

    Attributes
    ----------
    id : SQLAlchemy.Column
        Autogenerated primary key
    title : SQLAlchemy.Column
    body : SQLAlchemy.Column
    """

    # Columns
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(128))
    body = db.Column(db.String(4096))

    def __init__(self, title, body):
        """Constructor for Post.

        Parameters
        ----------
        title : String
            Title/Summary of post
        body : String
            Contents
        """
        self.title = title
        self.body = body

    def __repr__(self):
        """Representation."""
        return '<Post %r:%r>' % (self.id, self.title)

## Instruction:
Add Category model and expanding Post model.
## Code After:
"""Collection of Models used in blogsite."""
from . import db
from datetime import datetime


class Post(db.Model):
    """Model representing a blog post.

    Attributes
    ----------
    id : SQLAlchemy.Column
        Autogenerated primary key
    title : SQLAlchemy.Column
    body : SQLAlchemy.Column
    pub_date : SQLAlchemy.Column
        Date and Time of Post creation
    category_id : SQLAlchemy.Column
        Foreign Key ID to Category
    category : SQLAlchemy.relationship
        Category object that relates to this Post
    """

    # Columns
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(128))
    body = db.Column(db.String(4096))
    pub_date = db.Column(db.DateTime)
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    category = db.relationship('Category',
                               backref=db.backref('posts', lazy='dynamic'))

    def __init__(self, title, body, category, pub_date=None):
        """Constructor for Post.

        Parameters
        ----------
        title : String
            Title/Summary of post
        body : String
            Contents
        category : Category
            Category object blog post is related to
        pub_date : DateTime
            Optional
        """
        self.title = title
        self.body = body
        self.category = category
        if pub_date is None:
            pub_date = datetime.utcnow()
        self.pub_date = pub_date

    def __repr__(self):
        """Representation."""
        return '<Post %r:%r>' % (self.id, self.title)


class Category(db.Model):
    """Model to represent a overall category.

    Attributes
    ----------
    id : SQLAlchemy.Column
    name : SQLAlchemy.Column
    """

    # Columns
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(128))

    def __init__(self, name):
        """Constructor for Category.

        Parameters
        ----------
        name : String
            Name of new category
        """
        self.name = name

    def __repr__(self):
        """Representation."""
        return '<Category %r>' % self.name
