<s> from datetime import datetime <EOL> import shake <EOL> from shake import url_for , get_csrf , to_unicode <EOL> from slugify import slugify <EOL> from sqlalchemy . orm import validates <EOL> from main import db <EOL> class BaseMixin ( object ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> deleted = db . Column ( db . Boolean , default = False , nullable = False ) <EOL> created_at = db . Column ( db . DateTime , default = datetime . utcnow , <EOL> nullable = False ) <EOL> modified_at = db . Column ( db . DateTime , default = datetime . utcnow , <EOL> onupdate = datetime . utcnow , nullable = False ) <EOL> @ classmethod <EOL> def by_id ( cls , item_id , deleted = False ) : <EOL> item = db . query ( cls ) . get ( item_id ) <EOL> if not item or ( item . deleted and not deleted ) : <EOL> raise shake . NotFound <EOL> return item <EOL> @ classmethod <EOL> def get_all ( cls , deleted = False ) : <EOL> query = db . query ( cls ) <EOL> if deleted is not None : <EOL> query = query . filter ( cls . deleted == deleted ) <EOL> return query <EOL> @ classmethod <EOL> def get_all_in ( cls , ids , deleted = False ) : <EOL> query = db . query ( cls ) . filter ( cls . id . in_ ( ids ) ) <EOL> if deleted is not None : <EOL> query = query . filter ( cls . deleted == deleted ) <EOL> return query <EOL> @ classmethod <EOL> def delete_all ( cls , ids ) : <EOL> ids = list ( ids ) <EOL> if not ids : <EOL> return <EOL> db . query ( cls ) . filter ( cls . id . in_ ( ids ) ) . delete ( synchronize_session = '<STR_LIT>' ) <EOL> def get_show_url ( self , external = False ) : <EOL> return url_for ( self . __tablename__ + '<STR_LIT>' , item_id = self . id , external = external ) <EOL> def get_edit_url ( self ) : <EOL> return url_for ( self . __tablename__ + '<STR_LIT>' , item_id = self . id ) <EOL> def get_delete_url ( self ) : <EOL> csfr = get_csrf ( ) <EOL> data = { <EOL> '<STR_LIT>' : self . id , <EOL> csfr . name : csfr . value , <EOL> } <EOL> return url_for ( self . __tablename__ + '<STR_LIT>' , ** data ) <EOL> def get_restore_url ( self ) : <EOL> csfr = get_csrf ( ) <EOL> data = { <EOL> '<STR_LIT>' : self . id , <EOL> csfr . name : csfr . value , <EOL> } <EOL> return url_for ( self . __tablename__ + '<STR_LIT>' , ** data ) <EOL> def delete ( self ) : <EOL> self . deleted = True <EOL> db . commit ( ) <EOL> def restore ( self ) : <EOL> self . deleted = False <EOL> db . commit ( ) <EOL> class SluggableMixin ( object ) : <EOL> name = db . Column ( db . Unicode ( <NUM_LIT> ) , default = u'<STR_LIT>' ) <EOL> slug = db . Column ( db . String ( <NUM_LIT> ) , default = '<STR_LIT>' ) <EOL> def __init__ ( self , name , * args , ** kwargs ) : <EOL> self . name = to_unicode ( name ) <EOL> db . Model . __init__ ( self , * args , ** kwargs ) <EOL> @ validates ( '<STR_LIT:name>' ) <EOL> def _set_slug ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . slug = slugify ( value ) <EOL> return value <EOL> @ classmethod <EOL> def by_slug ( cls , slug ) : <EOL> return db . query ( cls ) . filter ( cls . slug == slug ) . first ( ) </s>
