<s> from django . contrib . contenttypes . models import ContentType <EOL> from django . template . loader import render_to_string , get_template <EOL> from django . core . cache import cache <EOL> from django . db . models import get_model <EOL> from supertagging import settings <EOL> from supertagging . models import SuperTaggedItem <EOL> class MarkupHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , field ) : <EOL> self . field = field <EOL> self . model = model <EOL> self . content_type = ContentType . objects . get_for_model ( model ) <EOL> def __get__ ( self , instance , owner ) : <EOL> if not instance : <EOL> return <EOL> data = self . _get_cached_value ( instance ) <EOL> if data : <EOL> return data <EOL> try : <EOL> data = self . handle ( instance ) <EOL> except Exception , e : <EOL> data = getattr ( instance , self . field ) <EOL> if settings . ST_DEBUG : raise Exception ( e ) <EOL> cache . set ( self . _get_cache_key ( instance ) , data , settings . MARKUP_CONTENT_CACHE_TIMEOUT ) <EOL> return data <EOL> def _get_cache_key ( self , instance = None ) : <EOL> if instance : <EOL> return "<STR_LIT>" % ( self . content_type . pk , instance . pk , self . field ) <EOL> return None <EOL> def _get_cached_value ( self , instance = None ) : <EOL> if instance and self . _get_cache_key ( instance ) : <EOL> key = self . _get_cache_key ( instance ) <EOL> return cache . get ( key ) <EOL> return None <EOL> def handle ( self , instance = None ) : <EOL> if instance : <EOL> return markup_content ( instance , self . field ) <EOL> return "<STR_LIT>" <EOL> def invalidate_markup_cache ( obj , field ) : <EOL> if not obj : <EOL> return <EOL> ctype = ContentType . objects . get_for_model ( obj ) <EOL> key = "<STR_LIT>" % ( ctype . pk , obj . pk , field ) <EOL> cache . delete ( key ) <EOL> def get_handler_module ( module ) : <EOL> if not module : <EOL> return MarkupHandler <EOL> mod , f , i , fn = None , None , None , None <EOL> msplit = module . split ( "<STR_LIT:.>" ) <EOL> if len ( msplit ) == <NUM_LIT:1> : <EOL> mod = msplit [ <NUM_LIT:0> ] <EOL> else : <EOL> f = msplit [ : - <NUM_LIT:1> ] <EOL> i = msplit [ - <NUM_LIT:1> : ] <EOL> fn = "<STR_LIT:.>" . join ( f ) <EOL> if mod : <EOL> try : <EOL> return __import__ ( mod ) <EOL> except ImportError : <EOL> return MarkupHandler <EOL> else : <EOL> try : <EOL> mod = __import__ ( fn , fromlist = f ) <EOL> return getattr ( mod , i [ <NUM_LIT:0> ] ) <EOL> except : <EOL> return MarkupHandler <EOL> class FailedMarkupValidation ( Exception ) : <EOL> def __init__ ( self , value ) : <EOL> self . parameter = value <EOL> def __str__ ( self ) : <EOL> return repr ( self . parameter ) <EOL> def tag_instance_cmp ( x , y ) : <EOL> if isinstance ( x , dict ) and isinstance ( y , dict ) : <EOL> return cmp ( x [ '<STR_LIT>' ] , y [ '<STR_LIT>' ] ) <EOL> return cmp ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def markup_content ( obj , field , markup_template = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> ctype = ContentType . objects . get_for_model ( obj ) <EOL> items = SuperTaggedItem . objects . filter ( <EOL> content_type__pk = ctype . pk , object_id = obj . pk , <EOL> relevance__gte = settings . MIN_RELEVANCE_MARKUP ) . select_related ( ) <EOL> value = getattr ( obj , field , '<STR_LIT>' ) <EOL> full = [ ] <EOL> for item in items : <EOL> if not item . instances : <EOL> continue <EOL> i = item . instances <EOL> skip = False <EOL> for v in i : <EOL> if isinstance ( v , list ) : <EOL> skip = True <EOL> continue <EOL> if isinstance ( v , dict ) : <EOL> v [ '<STR_LIT>' ] = item . tag <EOL> if not skip : <EOL> full . extend ( i ) <EOL> full . sort ( tag_instance_cmp , reverse = True ) <EOL> for n , i in enumerate ( full ) : <EOL> if '<STR_LIT>' in i and '<STR_LIT>' in i and '<STR_LIT>' in i : <EOL> off , le , act_val = i [ '<STR_LIT>' ] , i [ '<STR_LIT>' ] , i [ '<STR_LIT>' ] <EOL> if act_val . lower ( ) in settings . MARKUP_EXCLUDES : <EOL> continue <EOL> else : <EOL> continue <EOL> if n != <NUM_LIT:0> : <EOL> if '<STR_LIT>' in full [ n - <NUM_LIT:1> ] : <EOL> prev_off = full [ n - <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> if ( ( off + <NUM_LIT:1> ) + le ) > prev_off : <EOL> continue <EOL> if not value [ off : ( off + le ) ] == act_val : <EOL> raise FailedMarkupValidation ( <EOL> "<STR_LIT>" % ( off , value [ off : ( off + le ) ] , act_val ) ) <EOL> break <EOL> tag = i [ '<STR_LIT>' ] <EOL> val = render_to_string ( markup_template , { '<STR_LIT>' : tag , '<STR_LIT>' : act_val } ) <EOL> pre , suf , repl = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> pre = value [ : off ] <EOL> suf = value [ ( off + le ) : ] <EOL> repl = value [ off : ( off + le ) ] <EOL> value = pre + val + suf <EOL> return value </s>
