<s> import datetime <EOL> import random <EOL> from flask import current_app <EOL> from quokka . core import TEXT_FORMATS <EOL> from quokka . core . db import db <EOL> from quokka . modules . accounts . models import User <EOL> from quokka . utils . text import slugify <EOL> from quokka . utils import get_current_user_for_models <EOL> from quokka . utils . settings import get_setting_value <EOL> from quokka . core . admin . utils import _l <EOL> from quokka . core . models . custom_values import HasCustomValue <EOL> class ContentFormat ( object ) : <EOL> content_format = db . StringField ( <EOL> choices = TEXT_FORMATS , <EOL> default = get_setting_value ( '<STR_LIT>' , '<STR_LIT:html>' ) <EOL> ) <EOL> class Dated ( object ) : <EOL> available_at = db . DateTimeField ( default = datetime . datetime . now ) <EOL> available_until = db . DateTimeField ( required = False ) <EOL> created_at = db . DateTimeField ( default = datetime . datetime . now ) <EOL> updated_at = db . DateTimeField ( default = datetime . datetime . now ) <EOL> class Owned ( object ) : <EOL> created_by = db . ReferenceField ( User ) <EOL> last_updated_by = db . ReferenceField ( User ) <EOL> authors = db . ListField ( db . ReferenceField ( User ) ) <EOL> def get_authors ( self ) : <EOL> return set ( self . authors + [ self . created_by ] ) <EOL> @ property <EOL> def has_multiple_authors ( self ) : <EOL> return len ( self . get_authors ( ) ) > <NUM_LIT:1> <EOL> class Publishable ( Dated , Owned ) : <EOL> published = db . BooleanField ( default = False ) <EOL> @ property <EOL> def is_available ( self ) : <EOL> now = datetime . datetime . now ( ) <EOL> return ( <EOL> self . published and <EOL> self . available_at <= now and <EOL> ( self . available_until is None or <EOL> self . available_until >= now ) <EOL> ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> self . updated_at = datetime . datetime . now ( ) <EOL> user = get_current_user_for_models ( ) <EOL> if not self . id and not self . created_by : <EOL> self . created_by = user <EOL> self . last_updated_by = user <EOL> super ( Publishable , self ) . save ( * args , ** kwargs ) <EOL> class Slugged ( object ) : <EOL> slug = db . StringField ( max_length = <NUM_LIT:255> , required = True ) <EOL> def validate_slug ( self , title = None ) : <EOL> if self . slug : <EOL> self . slug = slugify ( self . slug ) <EOL> else : <EOL> self . slug = slugify ( title or self . title ) <EOL> class LongSlugged ( Slugged ) : <EOL> long_slug = db . StringField ( unique = True , required = True ) <EOL> mpath = db . StringField ( ) <EOL> def _create_mpath_long_slug ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> if self . parent and self . parent != self : <EOL> self . long_slug = "<STR_LIT:/>" . join ( <EOL> [ self . parent . long_slug , self . slug ] <EOL> ) <EOL> self . mpath = "<STR_LIT>" . join ( <EOL> [ self . parent . mpath , self . slug , '<STR_LIT:U+002C>' ] <EOL> ) <EOL> else : <EOL> self . long_slug = self . slug <EOL> self . mpath = "<STR_LIT>" % self . slug <EOL> else : <EOL> self . long_slug = "<STR_LIT:/>" . join ( <EOL> [ self . channel . long_slug , self . slug ] <EOL> ) <EOL> self . mpath = "<STR_LIT>" . join ( [ self . channel . mpath , self . slug , '<STR_LIT:U+002C>' ] ) <EOL> def validate_long_slug ( self ) : <EOL> self . _create_mpath_long_slug ( ) <EOL> filters = dict ( long_slug = self . long_slug ) <EOL> if self . id : <EOL> filters [ '<STR_LIT>' ] = self . id <EOL> exist = self . __class__ . objects ( ** filters ) <EOL> if exist . count ( ) : <EOL> if current_app . config . get ( '<STR_LIT>' , False ) : <EOL> self . slug = "<STR_LIT>" . format ( self . slug , random . getrandbits ( <NUM_LIT:32> ) ) <EOL> self . _create_mpath_long_slug ( ) <EOL> else : <EOL> raise db . ValidationError ( <EOL> _l ( "<STR_LIT>" , <EOL> slug = self . long_slug ) <EOL> ) <EOL> class Tagged ( object ) : <EOL> tags = db . ListField ( db . StringField ( max_length = <NUM_LIT:50> ) ) <EOL> class Ordered ( object ) : <EOL> order = db . IntField ( required = True , default = <NUM_LIT:1> ) <EOL> class TemplateType ( HasCustomValue ) : <EOL> title = db . StringField ( max_length = <NUM_LIT:255> , required = True ) <EOL> identifier = db . StringField ( max_length = <NUM_LIT:255> , required = True , unique = True ) <EOL> template_suffix = db . StringField ( max_length = <NUM_LIT:255> , required = True ) <EOL> theme_name = db . StringField ( max_length = <NUM_LIT:255> , required = False ) <EOL> def __unicode__ ( self ) : <EOL> return self . title </s>
