<s> import re <EOL> import json <EOL> import inspect <EOL> import six <EOL> from funcy import memoize , compose , wraps , any <EOL> from funcy . py2 import mapcat <EOL> from . cross import md5hex <EOL> from django . db import models <EOL> from django . http import HttpRequest <EOL> from . conf import model_profile <EOL> NOT_SERIALIZED_FIELDS = ( <EOL> models . FileField , <EOL> models . TextField , <EOL> models . BinaryField , <EOL> ) <EOL> @ memoize <EOL> def non_proxy ( model ) : <EOL> while model . _meta . proxy : <EOL> model = next ( b for b in model . __bases__ <EOL> if issubclass ( b , models . Model ) and not b . _meta . abstract ) <EOL> return model <EOL> def model_family ( model ) : <EOL> """<STR_LIT>""" <EOL> def class_tree ( cls ) : <EOL> return [ cls ] + mapcat ( class_tree , cls . __subclasses__ ( ) ) <EOL> return class_tree ( non_proxy ( model ) ) <EOL> @ memoize <EOL> def family_has_profile ( cls ) : <EOL> return any ( model_profile , model_family ( cls ) ) <EOL> class MonkeyProxy ( object ) : <EOL> def __init__ ( self , cls ) : <EOL> monkey_bases = [ b . _no_monkey for b in cls . __bases__ if hasattr ( b , '<STR_LIT>' ) ] <EOL> for monkey_base in monkey_bases : <EOL> self . __dict__ . update ( monkey_base . __dict__ ) <EOL> def monkey_mix ( cls , mixin , methods = None ) : <EOL> """<STR_LIT>""" <EOL> assert '<STR_LIT>' not in cls . __dict__ , '<STR_LIT>' <EOL> cls . _no_monkey = MonkeyProxy ( cls ) <EOL> if methods is None : <EOL> isboundmethod = inspect . isfunction if six . PY3 else inspect . ismethod <EOL> methods = inspect . getmembers ( mixin , isboundmethod ) <EOL> else : <EOL> methods = [ ( m , getattr ( mixin , m ) ) for m in methods ] <EOL> for name , method in methods : <EOL> if hasattr ( cls , name ) : <EOL> setattr ( cls . _no_monkey , name , getattr ( cls , name ) ) <EOL> setattr ( cls , name , six . get_unbound_function ( method ) ) <EOL> @ memoize <EOL> def stamp_fields ( model ) : <EOL> """<STR_LIT>""" <EOL> stamp = str ( [ ( f . name , f . attname , f . db_column , f . __class__ ) for f in model . _meta . fields ] ) <EOL> return md5hex ( stamp ) <EOL> def obj_key ( obj ) : <EOL> if isinstance ( obj , models . Model ) : <EOL> return '<STR_LIT>' % ( obj . _meta . app_label , obj . _meta . model_name , obj . pk ) <EOL> else : <EOL> return str ( obj ) <EOL> def func_cache_key ( func , args , kwargs , extra = None ) : <EOL> """<STR_LIT>""" <EOL> factors = [ func . __module__ , func . __name__ , args , kwargs , extra ] <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> factors . append ( func . __code__ . co_firstlineno ) <EOL> return md5hex ( json . dumps ( factors , sort_keys = True , default = obj_key ) ) <EOL> def debug_cache_key ( func , args , kwargs , extra = None ) : <EOL> """<STR_LIT>""" <EOL> factors = [ func . __module__ , func . __name__ , args , kwargs , extra ] <EOL> return md5hex ( json . dumps ( factors , sort_keys = True , default = obj_key ) ) <EOL> def view_cache_key ( func , args , kwargs , extra = None ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( args [ <NUM_LIT:0> ] , '<STR_LIT>' ) : <EOL> uri = args [ <NUM_LIT:0> ] . build_absolute_uri ( ) <EOL> else : <EOL> uri = args [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' + func_cache_key ( func , args [ <NUM_LIT:1> : ] , kwargs , extra = ( uri , extra ) ) <EOL> def cached_view_fab ( _cached ) : <EOL> def force_render ( response ) : <EOL> if hasattr ( response , '<STR_LIT>' ) and callable ( response . render ) : <EOL> response . render ( ) <EOL> return response <EOL> def cached_view ( * dargs , ** dkwargs ) : <EOL> def decorator ( func ) : <EOL> dkwargs [ '<STR_LIT>' ] = view_cache_key <EOL> cached_func = _cached ( * dargs , ** dkwargs ) ( compose ( force_render , func ) ) <EOL> @ wraps ( func ) <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> assert isinstance ( request , HttpRequest ) , "<STR_LIT>" <EOL> if request . method not in ( '<STR_LIT:GET>' , '<STR_LIT>' ) : <EOL> return func ( request , * args , ** kwargs ) <EOL> return cached_func ( request , * args , ** kwargs ) <EOL> if hasattr ( cached_func , '<STR_LIT>' ) : <EOL> wrapper . invalidate = cached_func . invalidate <EOL> wrapper . key = cached_func . key <EOL> return wrapper <EOL> return decorator <EOL> return cached_view <EOL> from django . utils . safestring import mark_safe <EOL> NEWLINE_BETWEEN_TAGS = mark_safe ( '<STR_LIT>' ) <EOL> SPACE_BETWEEN_TAGS = mark_safe ( '<STR_LIT>' ) <EOL> def carefully_strip_whitespace ( text ) : <EOL> text = re . sub ( r'<STR_LIT>' , NEWLINE_BETWEEN_TAGS , text ) <EOL> text = re . sub ( r'<STR_LIT>' , SPACE_BETWEEN_TAGS , text ) <EOL> return text </s>
