# Path: yapper/lib/response.py
# def json_success(data=''):
#     return jsonify(type='success', data=data)
# 
# 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()


from flask.views import MethodView
from yapper.lib.response import json_success
from ..blog.models import Post



class PostAPI(MethodView):

    MODEL = Post

    def get(self, m_id=None):
        if m_id:
            item = self.MODEL.query.get_or_404(m_id)
            return json_success(data=item.to_json())
        items = self.MODEL.query.order_by(
            self.MODEL.created_at.desc()).limit(10)
        resp = []
        for item in items:
            resp.append(item.to_json())

# Path: yapper/lib/response.py
# def json_success(data=''):
#     return jsonify(type='success', data=data)
# 
# 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/api/blog.py
from flask.views import MethodView
from yapper.lib.response import json_success
from ..blog.models import Post




class PostAPI(MethodView):

    MODEL = Post

    def get(self, m_id=None):
        if m_id:
            item = self.MODEL.query.get_or_404(m_id)
            return json_success(data=item.to_json())
        items = self.MODEL.query.order_by(
            self.MODEL.created_at.desc()).limit(10)
        resp = []
        for item in items:
            resp.append(item.to_json())

        return json_success(data=resp)