<s> import slugify <EOL> from flask import url_for <EOL> from vomitter import LOGGER as L <EOL> from yapper import db <EOL> from yapper . lib . models import BaseModel <EOL> from yapper . lib . cache import cache <EOL> from yapper . lib . decorators import profile <EOL> from yapper . utilities . md import create_post_from_md <EOL> TYPE_POST = <NUM_LIT:1> <EOL> TYPE_PAGE = <NUM_LIT:2> <EOL> tags_posts = db . Table ( <EOL> '<STR_LIT>' , <EOL> db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) , <EOL> db . Column ( '<STR_LIT>' , db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) , <EOL> db . Column ( '<STR_LIT>' , db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> ) <EOL> categories_posts = db . Table ( <EOL> '<STR_LIT>' , <EOL> db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) , <EOL> db . Column ( '<STR_LIT>' , db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) , <EOL> db . Column ( '<STR_LIT>' , db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> ) <EOL> class Tag ( BaseModel ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer ( ) , primary_key = True ) <EOL> name = db . Column ( db . String ( <NUM_LIT:100> ) , unique = True , index = True ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def to_json ( self ) : <EOL> json_data = { <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : str ( self . created_at ) } <EOL> return json_data <EOL> class Category ( BaseModel ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer ( ) , primary_key = True ) <EOL> name = db . Column ( db . String ( <NUM_LIT:100> ) , unique = True , index = True ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def to_json ( self ) : <EOL> json_data = { <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : str ( self . created_at ) } <EOL> return json_data <EOL> class Post ( BaseModel ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> author_id = db . Column ( db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> title = db . Column ( db . String ( <NUM_LIT> ) ) <EOL> slug = db . Column ( db . String ( <NUM_LIT> ) , unique = True ) <EOL> description = db . Column ( db . Text , default = '<STR_LIT>' ) <EOL> body = db . Column ( db . Text , default = '<STR_LIT>' ) <EOL> status = db . Column ( db . Boolean , default = True ) <EOL> body_html = db . Column ( db . Text , default = '<STR_LIT>' ) <EOL> tags = db . relationship ( <EOL> '<STR_LIT>' , secondary = tags_posts , <EOL> backref = db . backref ( '<STR_LIT>' , lazy = '<STR_LIT>' ) <EOL> ) <EOL> categories = db . relationship ( <EOL> '<STR_LIT>' , secondary = categories_posts , <EOL> backref = db . backref ( '<STR_LIT>' , lazy = '<STR_LIT>' ) <EOL> ) <EOL> def to_json ( self ) : <EOL> json_data = { <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT:title>' : self . title , <EOL> '<STR_LIT>' : self . slug , <EOL> '<STR_LIT:description>' : self . description , <EOL> '<STR_LIT>' : str ( self . created_at ) , <EOL> '<STR_LIT>' : str ( self . modified_at ) , <EOL> '<STR_LIT>' : self . author . to_json ( ) , <EOL> '<STR_LIT>' : url_for ( '<STR_LIT>' , <EOL> slug = self . slug , _external = True ) <EOL> } <EOL> return json_data <EOL> @ classmethod <EOL> @ profile <EOL> def get_by_slug ( cls , slug ) : <EOL> L . d ( '<STR_LIT>' ) <EOL> obj = cache . get ( cls . key ( slug ) ) <EOL> if obj : <EOL> L . d ( '<STR_LIT>' % cls . key ( slug ) ) <EOL> return obj <EOL> L . d ( '<STR_LIT>' % cls . key ( slug ) ) <EOL> obj = cls . query . filter_by ( slug = slug ) . first ( ) <EOL> if obj : <EOL> L . d ( '<STR_LIT>' % cls . key ( slug ) ) <EOL> cache . set ( cls . key ( slug ) , obj ) <EOL> else : <EOL> L . d ( '<STR_LIT>' ) <EOL> return obj <EOL> @ property <EOL> def content ( self ) : <EOL> return self . body <EOL> @ content . setter <EOL> def content ( self , body ) : <EOL> self . body = body <EOL> self . body_html = create_post_from_md ( body ) <EOL> @ property <EOL> def html ( self ) : <EOL> return self . body_html <EOL> @ staticmethod <EOL> def on_change_body ( target , value , oldvalue , initiator ) : <EOL> target . body_html = create_post_from_md ( value ) <EOL> @ staticmethod <EOL> def generate_fake ( count = <NUM_LIT:10> ) : <EOL> from random import seed <EOL> import forgery_py <EOL> seed ( ) <EOL> for i in range ( count ) : <EOL> author_id = <NUM_LIT:1> <EOL> title = forgery_py . lorem_ipsum . sentence ( ) <EOL> p = Post ( <EOL> content = forgery_py . lorem_ipsum . paragraphs ( ) , <EOL> title = title , <EOL> slug = slugify . slugify ( title ) , <EOL> description = forgery_py . lorem_ipsum . sentence ( ) , <EOL> author_id = author_id <EOL> ) <EOL> db . session . add ( p ) <EOL> db . session . commit ( ) <EOL> db . event . listen ( Post . body , '<STR_LIT>' , Post . on_change_body ) </s>
