<s> import operator <EOL> from collections import OrderedDict <EOL> from django . core . cache import cache <EOL> from django . db import models <EOL> from omblog . cache import get_key <EOL> from omblog import settings as o_settings <EOL> class TagManager ( models . Manager ) : <EOL> def tags_and_counts ( self ) : <EOL> """<STR_LIT>""" <EOL> if o_settings . CACHE_ENABLED : <EOL> key = get_key ( '<STR_LIT>' ) <EOL> cached = cache . get ( key ) <EOL> if cached : <EOL> return cached <EOL> tags = [ ] <EOL> tag_models = super ( TagManager , self ) . get_query_set ( ) . all ( ) <EOL> for tag in tag_models : <EOL> tags . append ( <EOL> ( tag , tag . post_set . filter ( status = <NUM_LIT:3> ) . count ( ) ) <EOL> ) <EOL> tags = sorted ( tags , key = operator . itemgetter ( <NUM_LIT:1> ) , reverse = True ) <EOL> if o_settings . CACHE_ENABLED : <EOL> cache . set ( key , tags , o_settings . CACHE_TIMEOUT ) <EOL> return tags <EOL> class PostManager ( models . Manager ) : <EOL> def dates ( self ) : <EOL> """<STR_LIT>""" <EOL> if o_settings . CACHE_ENABLED : <EOL> key = get_key ( '<STR_LIT>' ) <EOL> cached = cache . get ( key ) <EOL> if cached : <EOL> return cached <EOL> posts = self . published ( ) <EOL> dates = OrderedDict ( ) <EOL> for post in posts : <EOL> key = post . created . strftime ( '<STR_LIT>' ) <EOL> try : <EOL> dates [ key ] [ <NUM_LIT:1> ] = dates [ key ] [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> except KeyError : <EOL> dates [ key ] = [ post . created , <NUM_LIT:1> ] <EOL> if o_settings . CACHE_ENABLED : <EOL> cache . set ( key , dates , o_settings . CACHE_TIMEOUT ) <EOL> return dates <EOL> def published ( self ) : <EOL> """<STR_LIT>""" <EOL> if o_settings . CACHE_ENABLED : <EOL> key = get_key ( '<STR_LIT>' ) <EOL> cached = cache . get ( key ) <EOL> if cached : <EOL> return cached <EOL> posts = super ( PostManager , self ) . get_query_set ( ) . filter ( <EOL> status = self . model . PUBLISHED ) <EOL> if o_settings . CACHE_ENABLED : <EOL> cache . set ( key , posts , o_settings . CACHE_TIMEOUT ) <EOL> return posts <EOL> def hidden ( self ) : <EOL> """<STR_LIT>""" <EOL> if o_settings . CACHE_ENABLED : <EOL> key = get_key ( '<STR_LIT>' ) <EOL> cached = cache . get ( key ) <EOL> if cached : <EOL> return cached <EOL> posts = super ( PostManager , self ) . get_query_set ( ) . filter ( <EOL> status = self . model . HIDDEN ) <EOL> if o_settings . CACHE_ENABLED : <EOL> cache . set ( key , posts , o_settings . CACHE_TIMEOUT ) <EOL> return posts <EOL> def visible ( self , user ) : <EOL> """<STR_LIT>""" <EOL> posts = self . published ( ) <EOL> if o_settings . SHOW_HIDDEN_IF_LOGGED_IN and user . is_authenticated ( ) : <EOL> posts = posts | self . hidden ( ) <EOL> return posts </s>
