<s> from __future__ import absolute_import , print_function , unicode_literals <EOL> import django <EOL> from aldryn_apphooks_config . managers . parler import ( <EOL> AppHookConfigTranslatableManager , AppHookConfigTranslatableQueryset , <EOL> ) <EOL> from django . contrib . sites . models import Site <EOL> from django . db import models <EOL> from django . utils . timezone import now <EOL> try : <EOL> from collections import Counter <EOL> except ImportError : <EOL> from . compat import Counter <EOL> class TaggedFilterItem ( object ) : <EOL> def tagged ( self , other_model = None , queryset = None ) : <EOL> """<STR_LIT>""" <EOL> tags = self . _taglist ( other_model , queryset ) <EOL> return self . get_queryset ( ) . filter ( tags__in = tags ) . distinct ( ) <EOL> def _taglist ( self , other_model = None , queryset = None ) : <EOL> """<STR_LIT>""" <EOL> from taggit . models import TaggedItem <EOL> filter = None <EOL> if queryset is not None : <EOL> filter = set ( ) <EOL> for item in queryset . all ( ) : <EOL> filter . update ( item . tags . all ( ) ) <EOL> filter = set ( [ tag . id for tag in filter ] ) <EOL> elif other_model is not None : <EOL> filter = set ( TaggedItem . objects . filter ( <EOL> content_type__model = other_model . __name__ . lower ( ) <EOL> ) . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> tags = set ( TaggedItem . objects . filter ( <EOL> content_type__model = self . model . __name__ . lower ( ) <EOL> ) . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> if filter is not None : <EOL> tags = tags . intersection ( filter ) <EOL> return list ( tags ) <EOL> def tag_list ( self , other_model = None , queryset = None ) : <EOL> """<STR_LIT>""" <EOL> from taggit . models import Tag <EOL> return Tag . objects . filter ( id__in = self . _taglist ( other_model , queryset ) ) <EOL> def tag_list_slug ( self , other_model = None , queryset = None ) : <EOL> queryset = self . tag_list ( other_model , queryset ) <EOL> return queryset . values ( '<STR_LIT>' ) <EOL> def tag_cloud ( self , other_model = None , queryset = None , published = True ) : <EOL> from taggit . models import TaggedItem <EOL> tag_ids = self . _taglist ( other_model , queryset ) <EOL> kwargs = { } <EOL> if published : <EOL> kwargs = TaggedItem . bulk_lookup_kwargs ( self . model . objects . published ( ) ) <EOL> kwargs [ '<STR_LIT>' ] = tag_ids <EOL> counted_tags = dict ( TaggedItem . objects <EOL> . filter ( ** kwargs ) <EOL> . values ( '<STR_LIT>' ) <EOL> . annotate ( count = models . Count ( '<STR_LIT>' ) ) <EOL> . values_list ( '<STR_LIT>' , '<STR_LIT:count>' ) ) <EOL> tags = TaggedItem . tag_model ( ) . objects . filter ( pk__in = counted_tags . keys ( ) ) <EOL> for tag in tags : <EOL> tag . count = counted_tags [ tag . pk ] <EOL> return sorted ( tags , key = lambda x : - x . count ) <EOL> class GenericDateQuerySet ( AppHookConfigTranslatableQueryset ) : <EOL> start_date_field = '<STR_LIT>' <EOL> fallback_date_field = '<STR_LIT>' <EOL> end_date_field = '<STR_LIT>' <EOL> publish_field = '<STR_LIT>' <EOL> def on_site ( self ) : <EOL> return self . filter ( models . Q ( sites__isnull = True ) | <EOL> models . Q ( sites = Site . objects . get_current ( ) . pk ) ) <EOL> def published ( self ) : <EOL> queryset = self . published_future ( ) <EOL> if self . start_date_field : <EOL> return queryset . filter ( <EOL> ** { '<STR_LIT>' % self . start_date_field : now ( ) } ) <EOL> else : <EOL> return queryset <EOL> def published_future ( self ) : <EOL> queryset = self . on_site ( ) <EOL> if self . end_date_field : <EOL> qfilter = ( <EOL> models . Q ( ** { '<STR_LIT>' % self . end_date_field : now ( ) } ) | <EOL> models . Q ( ** { '<STR_LIT>' % self . end_date_field : True } ) <EOL> ) <EOL> queryset = queryset . filter ( qfilter ) <EOL> return queryset . filter ( ** { self . publish_field : True } ) <EOL> def archived ( self ) : <EOL> queryset = self . on_site ( ) <EOL> if self . end_date_field : <EOL> qfilter = ( <EOL> models . Q ( ** { '<STR_LIT>' % self . end_date_field : now ( ) } ) | <EOL> models . Q ( ** { '<STR_LIT>' % self . end_date_field : False } ) <EOL> ) <EOL> queryset = queryset . filter ( qfilter ) <EOL> return queryset . filter ( ** { self . publish_field : True } ) <EOL> def available ( self ) : <EOL> return self . on_site ( ) . filter ( ** { self . publish_field : True } ) <EOL> def filter_by_language ( self , language ) : <EOL> return self . active_translations ( language_code = language ) . on_site ( ) <EOL> class GenericDateTaggedManager ( TaggedFilterItem , AppHookConfigTranslatableManager ) : <EOL> use_for_related_fields = True <EOL> queryset_class = GenericDateQuerySet <EOL> def get_queryset ( self , * args , ** kwargs ) : <EOL> try : <EOL> return super ( GenericDateTaggedManager , self ) . get_queryset ( * args , ** kwargs ) <EOL> except AttributeError : <EOL> return super ( GenericDateTaggedManager , self ) . get_query_set ( * args , ** kwargs ) <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:8> ) : <EOL> get_query_set = get_queryset <EOL> def published ( self ) : <EOL> return self . get_queryset ( ) . published ( ) <EOL> def available ( self ) : <EOL> return self . get_queryset ( ) . available ( ) <EOL> def archived ( self ) : <EOL> return self . get_queryset ( ) . archived ( ) <EOL> def published_future ( self ) : <EOL> return self . get_queryset ( ) . published_future ( ) <EOL> def filter_by_language ( self , language ) : <EOL> return self . get_queryset ( ) . filter_by_language ( language ) <EOL> def get_months ( self , queryset = None ) : <EOL> """<STR_LIT>""" <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> queryset = queryset . on_site ( ) <EOL> dates_qs = queryset . values_list ( queryset . start_date_field , queryset . fallback_date_field ) <EOL> dates = [ ] <EOL> for blog_dates in dates_qs : <EOL> if blog_dates [ <NUM_LIT:0> ] : <EOL> current_date = blog_dates [ <NUM_LIT:0> ] <EOL> else : <EOL> current_date = blog_dates [ <NUM_LIT:1> ] <EOL> dates . append ( ( current_date . year , current_date . month , ) ) <EOL> date_counter = Counter ( dates ) <EOL> dates = set ( dates ) <EOL> dates = sorted ( dates , reverse = True ) <EOL> return [ { '<STR_LIT:date>' : now ( ) . replace ( year = year , month = month , day = <NUM_LIT:1> ) , <EOL> '<STR_LIT:count>' : date_counter [ year , month ] } for year , month in dates ] </s>
