<s> from __future__ import unicode_literals <EOL> from future . builtins import int , round <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db . models import Model , Count <EOL> from mezzanine import template <EOL> from mezzanine . conf import settings <EOL> from mezzanine . generic . models import AssignedKeyword , Keyword <EOL> register = template . Library ( ) <EOL> @ register . as_tag <EOL> def keywords_for ( * args ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( args [ <NUM_LIT:0> ] , Model ) : <EOL> obj = args [ <NUM_LIT:0> ] <EOL> if hasattr ( obj , "<STR_LIT>" ) : <EOL> obj = obj . get_content_model ( ) or obj <EOL> keywords_name = obj . get_keywordsfield_name ( ) <EOL> keywords_queryset = getattr ( obj , keywords_name ) . all ( ) <EOL> prefetched = getattr ( obj , "<STR_LIT>" , { } ) <EOL> if keywords_name not in prefetched : <EOL> keywords_queryset = keywords_queryset . select_related ( "<STR_LIT>" ) <EOL> return [ assigned . keyword for assigned in keywords_queryset ] <EOL> try : <EOL> app_label , model = args [ <NUM_LIT:0> ] . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> return [ ] <EOL> content_type = ContentType . objects . get ( app_label = app_label , model = model ) <EOL> assigned = AssignedKeyword . objects . filter ( content_type = content_type ) <EOL> keywords = Keyword . objects . filter ( assignments__in = assigned ) <EOL> keywords = keywords . annotate ( item_count = Count ( "<STR_LIT>" ) ) <EOL> if not keywords : <EOL> return [ ] <EOL> counts = [ keyword . item_count for keyword in keywords ] <EOL> min_count , max_count = min ( counts ) , max ( counts ) <EOL> factor = ( settings . TAG_CLOUD_SIZES - <NUM_LIT:1.> ) <EOL> if min_count != max_count : <EOL> factor /= ( max_count - min_count ) <EOL> for kywd in keywords : <EOL> kywd . weight = int ( round ( ( kywd . item_count - min_count ) * factor ) ) + <NUM_LIT:1> <EOL> return keywords </s>
