from flask_blog import db, uploaded_images
from author.models import Author
from datetime import datetime

class Blog(db.Model):
	id=db.Column(db.Integer,primary_key=True)
	name=db.Column(db.String(80))
	admin=db.Column(db.Integer,db.ForeignKey('author.id'))
	
	posts=db.relationship('Post',backref='blog',lazy='dynamic')
	
	def __init__(self, name, admin):
		self.name=name
		self.admin=admin
		
	def __repr__(self):
		return '<Blog %r>' % self.name
		
		
class Post(db.Model):
	id=db.Column(db.Integer,primary_key=True)
	blog_id=db.Column(db.Integer,db.ForeignKey('blog.id'))
	author_id=db.Column(db.Integer,db.ForeignKey('author.id'))
	category_id=db.Column(db.Integer,db.ForeignKey('category.id'))
	title=db.Column(db.String(80))
	body=db.Column(db.Text)
	image=db.Column(db.String(255))
	slug=db.Column(db.String(255),unique=True)
	publish_date=db.Column(db.DateTime)
	live=db.Column(db.Boolean)
	
	@property
	def imgsrc(self):
		return uploaded_images.url(self.image)
		
	def __init__(self,blog,author,category,title,body,image=None,slug=None,publish_date=None,live=None):
		self.blog_id=blog.id
		self.author_id=author.id
		self.category_id=category.id
		self.title=title
		self.body=body
		self.image=image
		self.slug=slug
		if publish_date==None:
				self.publish_date=datetime.utcnow()
		else:
			self.publish_date=publish_date
		self.live=True
		
	def __repr__(self):
		return '<Post %r>' % self.title

class Category(db.Model):
	id=db.Column(db.Integer,primary_key=True)
	name=db.Column(db.String(80))
	
	posts_= db.relationship('Post',backref='category',lazy='dynamic')
	
	def __init__(self,name):
		self.name=name
		
	def __repr__(self):
		return self.name
