<s> """<STR_LIT>""" <EOL> import django <EOL> from django . core . cache import cache <EOL> from django . utils import six <EOL> from parler import appsettings <EOL> from parler . utils import get_language_settings <EOL> if six . PY3 : <EOL> long = int <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:6> ) : <EOL> DEFAULT_TIMEOUT = cache . default_timeout <EOL> else : <EOL> DEFAULT_TIMEOUT = <NUM_LIT:0> <EOL> class IsMissing ( object ) : <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> def __bool__ ( self ) : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> MISSING = IsMissing ( ) <EOL> def get_object_cache_keys ( instance ) : <EOL> """<STR_LIT>""" <EOL> if instance . pk is None or instance . _state . adding : <EOL> return [ ] <EOL> keys = [ ] <EOL> tr_models = instance . _parler_meta . get_all_models ( ) <EOL> for language in instance . get_available_languages ( ) : <EOL> for tr_model in tr_models : <EOL> keys . append ( get_translation_cache_key ( tr_model , instance . pk , language ) ) <EOL> return keys <EOL> def get_translation_cache_key ( translated_model , master_id , language_code ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( translated_model . _meta . app_label , translated_model . __name__ , master_id , language_code ) <EOL> def get_cached_translation ( instance , language_code = None , related_name = None , use_fallback = False ) : <EOL> """<STR_LIT>""" <EOL> if language_code is None : <EOL> language_code = instance . get_current_language ( ) <EOL> translated_model = instance . _parler_meta . get_model_by_related_name ( related_name ) <EOL> values = _get_cached_values ( instance , translated_model , language_code , use_fallback ) <EOL> if not values : <EOL> return None <EOL> try : <EOL> translation = translated_model ( ** values ) <EOL> except TypeError : <EOL> return None <EOL> translation . _state . adding = False <EOL> return translation <EOL> def get_cached_translated_field ( instance , field_name , language_code = None , use_fallback = False ) : <EOL> """<STR_LIT>""" <EOL> if language_code is None : <EOL> language_code = instance . get_current_language ( ) <EOL> if len ( field_name ) <= <NUM_LIT:5> and len ( language_code ) > <NUM_LIT:5> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> translated_model = instance . _parler_meta . get_model_by_field ( field_name ) <EOL> values = _get_cached_values ( instance , translated_model , language_code , use_fallback ) <EOL> if not values : <EOL> return None <EOL> return values . get ( field_name , None ) <EOL> def _get_cached_values ( instance , translated_model , language_code , use_fallback = False ) : <EOL> """<STR_LIT>""" <EOL> if not appsettings . PARLER_ENABLE_CACHING or not instance . pk or instance . _state . adding : <EOL> return None <EOL> key = get_translation_cache_key ( translated_model , instance . pk , language_code ) <EOL> values = cache . get ( key ) <EOL> if not values : <EOL> return None <EOL> if values . get ( '<STR_LIT>' , False ) : <EOL> instance . _translations_cache [ translated_model ] [ language_code ] = MISSING <EOL> if use_fallback : <EOL> lang_dict = get_language_settings ( language_code ) <EOL> for fallback_lang in lang_dict [ '<STR_LIT>' ] : <EOL> if fallback_lang != language_code : <EOL> return _get_cached_values ( <EOL> instance , translated_model , fallback_lang , <EOL> use_fallback = False <EOL> ) <EOL> return None <EOL> values [ '<STR_LIT>' ] = instance <EOL> values [ '<STR_LIT>' ] = language_code <EOL> return values <EOL> def _cache_translation ( translation , timeout = DEFAULT_TIMEOUT ) : <EOL> """<STR_LIT>""" <EOL> if not appsettings . PARLER_ENABLE_CACHING : <EOL> return <EOL> if translation . master_id is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> fields = translation . get_translated_fields ( ) <EOL> values = { '<STR_LIT:id>' : translation . id } <EOL> for name in fields : <EOL> values [ name ] = getattr ( translation , name ) <EOL> key = get_translation_cache_key ( translation . __class__ , translation . master_id , translation . language_code ) <EOL> cache . set ( key , values , timeout = timeout ) <EOL> def _cache_translation_needs_fallback ( instance , language_code , related_name , timeout = DEFAULT_TIMEOUT ) : <EOL> """<STR_LIT>""" <EOL> if not appsettings . PARLER_ENABLE_CACHING or not instance . pk or instance . _state . adding : <EOL> return <EOL> tr_model = instance . _parler_meta . get_model_by_related_name ( related_name ) <EOL> key = get_translation_cache_key ( tr_model , instance . pk , language_code ) <EOL> cache . set ( key , { '<STR_LIT>' : True } , timeout = timeout ) <EOL> def _delete_cached_translations ( shared_model ) : <EOL> cache . delete_many ( get_object_cache_keys ( shared_model ) ) <EOL> def _delete_cached_translation ( translation ) : <EOL> if not appsettings . PARLER_ENABLE_CACHING : <EOL> return <EOL> key = get_translation_cache_key ( translation . __class__ , translation . master_id , translation . language_code ) <EOL> cache . delete ( key ) </s>
