# Path: yapper/lib/decorators.py
# def profile(f):
#     """
#     Decorate a function with this to know its running time.
#     """
#     @wraps(f)
#     def decorated_function(*args, **kwargs):
#         strt = time.time()
#         L.i('Calling function %s @ %f' % (f.func_name, strt))
#         res = f(*args, **kwargs)
#         end = time.time()
#         L.i('Finished function %s @ %f' % (f.func_name, end))
#         L.i('Time taken : %f ms' % (end - strt))
#         return res
#     return decorated_function
# 
# Path: yapper/blueprints/user/decorators.py
# def permission_required(permission):
#     """
#     Checks for a specific permission the current_user has or not.
#     """
#     def decorator(f):
#         @wraps(f)
#         def decorated_function(*args, **kwargs):
#             if not current_user.can(permission):
#                 abort(403)
#             return f(*args, **kwargs)
#         return decorated_function
#     return decorator
# 
# Path: yapper/blueprints/blog/forms.py
# class PostForm(Form):
#     title = StringField(
#         u'Post Title',
#         validators=[
#             DataRequired(message=u'Empty title'),
#             Length(max=256, message=u'Maximum 256 characters allowed')
#         ]
#     )
#     description = TextAreaField(
#         u'Description',
#         validators=[
#             DataRequired(message=u'Provide a description/summary.'),
#             Length(max=300, message=u'Maximum 300 characters allowed')
#         ]
#     )
#     body = TextAreaField(
#         u'Content',
#         validators=[DataRequired(message=u'Empty Post')]
#     )
#     submit = SubmitField(u'Add Post')
# 
# Path: yapper/blueprints/blog/models.py
# class Post(BaseModel):
#     __tablename__ = 'posts'
#     author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
#     title = db.Column(db.String(256))
#     slug = db.Column(db.String(500), unique=True)
#     description = db.Column(db.Text, default='')
#     body = db.Column(db.Text, default='')
#     status = db.Column(db.Boolean, default=True)
#     body_html = db.Column(db.Text, default='')
#     tags = db.relationship(
#         'Tag', secondary=tags_posts,
#         backref=db.backref('posts', lazy='dynamic')
#     )
#     categories = db.relationship(
#         'Category', secondary=categories_posts,
#         backref=db.backref('posts', lazy='dynamic')
#     )
# 
#     def to_json(self):
#         json_data = {
#             'id': self.id,
#             'title': self.title,
#             'slug': self.slug,
#             'description': self.description,
#             'created_at': str(self.created_at),
#             'modified_at': str(self.modified_at),
#             'author': self.author.to_json(),
#             'permalink': url_for('blog.get_post_by_slug',
#                                  slug=self.slug, _external=True)
#         }
#         return json_data
# 
#     @classmethod
#     @profile
#     def get_by_slug(cls, slug):
#         L.d('Getting from Cache.')
#         obj = cache.get(cls.key(slug))
#         if obj:
#             L.d('Cache hit for - %s' % cls.key(slug))
#             return obj
#         L.d('Cache miss for - %s' % cls.key(slug))
#         obj = cls.query.filter_by(slug=slug).first()
#         if obj:
#             L.d('Saving in cache - %s' % cls.key(slug))
#             cache.set(cls.key(slug), obj)
#         else:
#             L.d('The object was None')
#         return obj
# 
#     @property
#     def content(self):
#         return self.body
# 
#     @content.setter
#     def content(self, body):
#         self.body = body
#         self.body_html = create_post_from_md(body)
# 
#     @property
#     def html(self):
#         return self.body_html
# 
#     @staticmethod
#     def on_change_body(target, value, oldvalue, initiator):
#         target.body_html = create_post_from_md(value)
# 
#     @staticmethod
#     def generate_fake(count=10):
#         from random import seed
#         import forgery_py
#         seed()
#         for i in range(count):
#             author_id = 1
#             title = forgery_py.lorem_ipsum.sentence()
#             p = Post(
#                 content=forgery_py.lorem_ipsum.paragraphs(),
#                 title=title,
#                 slug=slugify.slugify(title),
#                 description=forgery_py.lorem_ipsum.sentence(),
#                 author_id=author_id
#             )
#             db.session.add(p)
#         db.session.commit()
# 
# Path: yapper/blueprints/user/models.py
# class Permission:
#     WRITE_POSTS = 0x01
#     READ_ONLY = 0x02
#     ADMIN = 0xff
#     ANONYMOUS = 0x00
# 
#     @staticmethod
#     def user():
#         return (Permission.WRITE_POSTS, True)
# 
#     @staticmethod
#     def admin():
#         return (Permission.ADMIN, False)


from slugify import slugify
from flask import (
    current_app,
    redirect,
    url_for,
    render_template,
    flash,
    request,
    abort,
    Blueprint
)
from flask_login import login_required, current_user
from yapper.lib.decorators import profile
from ..user.decorators import permission_required
from .forms import PostForm
from .models import Post
from ..user.models import Permission
"""Blog post controller."""




BP_NM = 'blog'

blog = Blueprint(BP_NM, __name__)


@blog.route('/page/<int:page>')
@blog.route('/')
def index(page=1):
    """Show list of latest blog posts."""

# Path: yapper/lib/decorators.py
# def profile(f):
#     """
#     Decorate a function with this to know its running time.
#     """
#     @wraps(f)
#     def decorated_function(*args, **kwargs):
#         strt = time.time()
#         L.i('Calling function %s @ %f' % (f.func_name, strt))
#         res = f(*args, **kwargs)
#         end = time.time()
#         L.i('Finished function %s @ %f' % (f.func_name, end))
#         L.i('Time taken : %f ms' % (end - strt))
#         return res
#     return decorated_function
# 
# Path: yapper/blueprints/user/decorators.py
# def permission_required(permission):
#     """
#     Checks for a specific permission the current_user has or not.
#     """
#     def decorator(f):
#         @wraps(f)
#         def decorated_function(*args, **kwargs):
#             if not current_user.can(permission):
#                 abort(403)
#             return f(*args, **kwargs)
#         return decorated_function
#     return decorator
# 
# Path: yapper/blueprints/blog/forms.py
# class PostForm(Form):
#     title = StringField(
#         u'Post Title',
#         validators=[
#             DataRequired(message=u'Empty title'),
#             Length(max=256, message=u'Maximum 256 characters allowed')
#         ]
#     )
#     description = TextAreaField(
#         u'Description',
#         validators=[
#             DataRequired(message=u'Provide a description/summary.'),
#             Length(max=300, message=u'Maximum 300 characters allowed')
#         ]
#     )
#     body = TextAreaField(
#         u'Content',
#         validators=[DataRequired(message=u'Empty Post')]
#     )
#     submit = SubmitField(u'Add Post')
# 
# Path: yapper/blueprints/blog/models.py
# class Post(BaseModel):
#     __tablename__ = 'posts'
#     author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
#     title = db.Column(db.String(256))
#     slug = db.Column(db.String(500), unique=True)
#     description = db.Column(db.Text, default='')
#     body = db.Column(db.Text, default='')
#     status = db.Column(db.Boolean, default=True)
#     body_html = db.Column(db.Text, default='')
#     tags = db.relationship(
#         'Tag', secondary=tags_posts,
#         backref=db.backref('posts', lazy='dynamic')
#     )
#     categories = db.relationship(
#         'Category', secondary=categories_posts,
#         backref=db.backref('posts', lazy='dynamic')
#     )
# 
#     def to_json(self):
#         json_data = {
#             'id': self.id,
#             'title': self.title,
#             'slug': self.slug,
#             'description': self.description,
#             'created_at': str(self.created_at),
#             'modified_at': str(self.modified_at),
#             'author': self.author.to_json(),
#             'permalink': url_for('blog.get_post_by_slug',
#                                  slug=self.slug, _external=True)
#         }
#         return json_data
# 
#     @classmethod
#     @profile
#     def get_by_slug(cls, slug):
#         L.d('Getting from Cache.')
#         obj = cache.get(cls.key(slug))
#         if obj:
#             L.d('Cache hit for - %s' % cls.key(slug))
#             return obj
#         L.d('Cache miss for - %s' % cls.key(slug))
#         obj = cls.query.filter_by(slug=slug).first()
#         if obj:
#             L.d('Saving in cache - %s' % cls.key(slug))
#             cache.set(cls.key(slug), obj)
#         else:
#             L.d('The object was None')
#         return obj
# 
#     @property
#     def content(self):
#         return self.body
# 
#     @content.setter
#     def content(self, body):
#         self.body = body
#         self.body_html = create_post_from_md(body)
# 
#     @property
#     def html(self):
#         return self.body_html
# 
#     @staticmethod
#     def on_change_body(target, value, oldvalue, initiator):
#         target.body_html = create_post_from_md(value)
# 
#     @staticmethod
#     def generate_fake(count=10):
#         from random import seed
#         import forgery_py
#         seed()
#         for i in range(count):
#             author_id = 1
#             title = forgery_py.lorem_ipsum.sentence()
#             p = Post(
#                 content=forgery_py.lorem_ipsum.paragraphs(),
#                 title=title,
#                 slug=slugify.slugify(title),
#                 description=forgery_py.lorem_ipsum.sentence(),
#                 author_id=author_id
#             )
#             db.session.add(p)
#         db.session.commit()
# 
# Path: yapper/blueprints/user/models.py
# class Permission:
#     WRITE_POSTS = 0x01
#     READ_ONLY = 0x02
#     ADMIN = 0xff
#     ANONYMOUS = 0x00
# 
#     @staticmethod
#     def user():
#         return (Permission.WRITE_POSTS, True)
# 
#     @staticmethod
#     def admin():
#         return (Permission.ADMIN, False)


# Path: yapper/blueprints/blog/controllers.py
from slugify import slugify
from flask import (
    current_app,
    redirect,
    url_for,
    render_template,
    flash,
    request,
    abort,
    Blueprint
)
from flask_login import login_required, current_user
from yapper.lib.decorators import profile
from ..user.decorators import permission_required
from .forms import PostForm
from .models import Post
from ..user.models import Permission

"""Blog post controller."""




BP_NM = 'blog'

blog = Blueprint(BP_NM, __name__)


@blog.route('/page/<int:page>')
@blog.route('/')
def index(page=1):
    """Show list of latest blog posts."""

    pagination = Post.query.order_by(