<s> from datetime import datetime <EOL> import itertools <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import models <EOL> from django . db . models . signals import post_save <EOL> from django . dispatch import receiver <EOL> from django . template . defaultfilters import date as dj_date , linebreaks , removetags <EOL> from caching . base import CachingManager , CachingMixin <EOL> from sorl . thumbnail import ImageField <EOL> from source . code . models import Code <EOL> from source . people . models import Person , Organization <EOL> from source . tags . models import TechnologyTaggedItem , ConceptTaggedItem <EOL> from source . utils . caching import expire_page_cache <EOL> from taggit . managers import TaggableManager <EOL> class LiveArticleManager ( CachingManager ) : <EOL> def get_query_set ( self ) : <EOL> return super ( LiveArticleManager , self ) . get_query_set ( ) . filter ( is_live = True , pubdate__lte = datetime . now ( ) ) <EOL> class Article ( CachingMixin , models . Model ) : <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> modified = models . DateTimeField ( auto_now = True ) <EOL> is_live = models . BooleanField ( '<STR_LIT>' , default = True ) <EOL> show_in_lists = models . BooleanField ( '<STR_LIT>' , default = True ) <EOL> allow_comments = models . BooleanField ( '<STR_LIT>' , default = True ) <EOL> title = models . CharField ( max_length = <NUM_LIT> ) <EOL> slug = models . SlugField ( unique = True ) <EOL> pubdate = models . DateTimeField ( default = datetime . now ) <EOL> subhead = models . CharField ( max_length = <NUM_LIT> ) <EOL> authors = models . ManyToManyField ( Person , blank = True , null = True , related_name = '<STR_LIT>' ) <EOL> image = ImageField ( upload_to = '<STR_LIT>' , help_text = '<STR_LIT>' , blank = True , null = True ) <EOL> image_caption = models . TextField ( blank = True ) <EOL> image_credit = models . CharField ( max_length = <NUM_LIT> , blank = True , help_text = '<STR_LIT>' ) <EOL> body = models . TextField ( ) <EOL> summary = models . TextField ( ) <EOL> article_type = models . CharField ( max_length = <NUM_LIT:32> , blank = True ) <EOL> category = models . ForeignKey ( '<STR_LIT>' ) <EOL> people = models . ManyToManyField ( Person , blank = True , null = True ) <EOL> organizations = models . ManyToManyField ( Organization , blank = True , null = True ) <EOL> code = models . ManyToManyField ( Code , blank = True , null = True ) <EOL> tags = TaggableManager ( blank = True , help_text = '<STR_LIT>' ) <EOL> technology_tags = TaggableManager ( verbose_name = '<STR_LIT>' , help_text = '<STR_LIT>' , through = TechnologyTaggedItem , blank = True ) <EOL> concept_tags = TaggableManager ( verbose_name = '<STR_LIT>' , help_text = '<STR_LIT>' , through = ConceptTaggedItem , blank = True ) <EOL> objects = models . Manager ( ) <EOL> live_objects = LiveArticleManager ( ) <EOL> disable_auto_linebreaks = models . BooleanField ( default = False , help_text = '<STR_LIT>' ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT:title>' , ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT:%s>' % self . title <EOL> @ models . permalink <EOL> def get_absolute_url ( self ) : <EOL> return ( '<STR_LIT>' , ( ) , { <EOL> '<STR_LIT>' : self . section . slug , <EOL> '<STR_LIT>' : self . slug <EOL> } ) <EOL> @ property <EOL> def section ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . category : <EOL> return self . category . section <EOL> return None <EOL> @ property <EOL> def pretty_pubdate ( self ) : <EOL> '''<STR_LIT>''' <EOL> return dj_date ( self . pubdate , "<STR_LIT>" ) <EOL> @ property <EOL> def pretty_caption ( self ) : <EOL> '''<STR_LIT>''' <EOL> _caption = self . image_caption or '<STR_LIT>' <EOL> _credit = self . image_credit <EOL> if _credit : <EOL> _caption = '<STR_LIT>' % ( _caption , _credit ) <EOL> return _caption <EOL> @ property <EOL> def pretty_body_text ( self ) : <EOL> '''<STR_LIT>''' <EOL> _body = self . body <EOL> if not self . disable_auto_linebreaks : <EOL> _body = linebreaks ( _body ) <EOL> return _body <EOL> @ property <EOL> def safe_summary ( self ) : <EOL> '''<STR_LIT>''' <EOL> return removetags ( self . summary , '<STR_LIT:a>' ) <EOL> @ property <EOL> def merged_tag_list ( self ) : <EOL> '''<STR_LIT>''' <EOL> return [ item for item in itertools . chain ( self . technology_tags . all ( ) , self . concept_tags . all ( ) ) ] <EOL> def get_live_organization_set ( self ) : <EOL> return self . organizations . filter ( is_live = True ) <EOL> def get_live_people_set ( self ) : <EOL> return self . people . filter ( is_live = True ) <EOL> def get_live_author_set ( self ) : <EOL> return self . authors . filter ( is_live = True ) <EOL> def get_live_code_set ( self ) : <EOL> return self . code . filter ( is_live = True ) <EOL> def get_live_guide_set ( self ) : <EOL> return self . guidearticle_set . filter ( guide__is_live = True , guide__show_in_lists = True , guide__pubdate__lte = datetime . now ) <EOL> def get_live_author_bio_set ( self ) : <EOL> author_set = self . get_live_author_set ( ) . exclude ( description = '<STR_LIT>' ) <EOL> authors_to_exclude = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> author_set = author_set . exclude ( slug__in = authors_to_exclude ) <EOL> return author_set <EOL> IMAGE_PRESENTATION_CHOICES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> class ArticleBlock ( CachingMixin , models . Model ) : <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> modified = models . DateTimeField ( auto_now = True ) <EOL> article = models . ForeignKey ( Article ) <EOL> title = models . CharField ( max_length = <NUM_LIT> ) <EOL> slug = models . SlugField ( ) <EOL> order = models . PositiveIntegerField ( default = <NUM_LIT:1> ) <EOL> image = ImageField ( upload_to = '<STR_LIT>' , blank = True , null = True ) <EOL> image_presentation = models . CharField ( max_length = <NUM_LIT> , choices = IMAGE_PRESENTATION_CHOICES , blank = True ) <EOL> image_caption = models . TextField ( blank = True ) <EOL> image_credit = models . CharField ( max_length = <NUM_LIT> , blank = True , help_text = '<STR_LIT>' ) <EOL> body = models . TextField ( ) <EOL> objects = models . Manager ( ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:title>' , ) <EOL> verbose_name = '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . article . title , self . title ) <EOL> @ property <EOL> def pretty_caption ( self ) : <EOL> '''<STR_LIT>''' <EOL> _caption = self . image_caption or '<STR_LIT>' <EOL> _credit = self . image_credit <EOL> if _credit : <EOL> _caption = '<STR_LIT>' % ( _caption , _credit ) <EOL> return _caption <EOL> @ property <EOL> def pretty_body_text ( self ) : <EOL> '''<STR_LIT>''' <EOL> _body = self . body <EOL> if not self . article . disable_auto_linebreaks : <EOL> _body = linebreaks ( _body ) <EOL> return _body <EOL> @ receiver ( post_save , sender = Article ) <EOL> def clear_caches_for_article ( sender , instance , ** kwargs ) : <EOL> expire_page_cache ( instance . get_absolute_url ( ) ) <EOL> if instance . section . slug : <EOL> expire_page_cache ( reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : instance . section . slug } <EOL> ) ) <EOL> if instance . category : <EOL> expire_page_cache ( reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : instance . category . slug } <EOL> ) ) <EOL> for organization in instance . get_live_organization_set ( ) : <EOL> expire_page_cache ( organization . get_absolute_url ( ) ) <EOL> for person in instance . get_live_people_set ( ) : <EOL> expire_page_cache ( person . get_absolute_url ( ) ) <EOL> for author in instance . get_live_author_set ( ) : <EOL> expire_page_cache ( author . get_absolute_url ( ) ) <EOL> for code in instance . get_live_code_set ( ) : <EOL> expire_page_cache ( code . get_absolute_url ( ) ) <EOL> for guide in instance . get_live_guide_set ( ) : <EOL> expire_page_cache ( guide . get_absolute_url ( ) ) <EOL> for tag in instance . tags . all ( ) : <EOL> expire_page_cache ( reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : tag . slug } <EOL> ) ) <EOL> class Section ( CachingMixin , models . Model ) : <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> modified = models . DateTimeField ( auto_now = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT:64> ) <EOL> slug = models . SlugField ( ) <EOL> description = models . TextField ( blank = True , help_text = '<STR_LIT>' ) <EOL> gets_promo_items = models . BooleanField ( default = False , help_text = '<STR_LIT>' ) <EOL> special_template = models . CharField ( max_length = <NUM_LIT:255> , blank = True ) <EOL> objects = models . Manager ( ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT:name>' , ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT:%s>' % ( self . name ) <EOL> class Category ( CachingMixin , models . Model ) : <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> modified = models . DateTimeField ( auto_now = True ) <EOL> section = models . ForeignKey ( Section ) <EOL> name = models . CharField ( max_length = <NUM_LIT:64> ) <EOL> slug = models . SlugField ( ) <EOL> objects = models . Manager ( ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT:name>' , ) <EOL> verbose_name_plural = '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . section . name , self . name ) </s>
