<s> from __future__ import unicode_literals <EOL> import warnings <EOL> from django . core . cache import cache <EOL> from django . db import models <EOL> from django . db . models . signals import post_delete <EOL> from django . dispatch import receiver <EOL> def set_cached_snippet ( key ) : <EOL> """<STR_LIT>""" <EOL> text_dict = { <EOL> trans . language : trans . text for trans in <EOL> SnippetTranslation . objects . filter ( snippet_id = key ) <EOL> } <EOL> text_dict . update ( { '<STR_LIT>' : Snippet . objects . get ( key = key ) . text } ) <EOL> cache . set ( '<STR_LIT>' . format ( key ) , text_dict ) <EOL> def get_cached_snippet ( key , language = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> snippet = cache . get ( '<STR_LIT>' . format ( key ) ) <EOL> if snippet == - <NUM_LIT:1> : <EOL> return None <EOL> if snippet is None : <EOL> try : <EOL> snippet = Snippet . objects . get ( key = key ) <EOL> except Snippet . DoesNotExist : <EOL> cache . set ( '<STR_LIT>' . format ( key ) , - <NUM_LIT:1> ) <EOL> snippet = { '<STR_LIT>' : None } <EOL> else : <EOL> set_cached_snippet ( key ) <EOL> snippet = cache . get ( '<STR_LIT>' . format ( key ) ) <EOL> return snippet . get ( language , snippet . get ( '<STR_LIT>' ) ) <EOL> class CachedManager ( models . Manager ) : <EOL> def get_from_cache ( self , key ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> snippet = cache . get ( '<STR_LIT>' . format ( key ) ) <EOL> if snippet == - <NUM_LIT:1> : <EOL> return None <EOL> if snippet is None : <EOL> try : <EOL> snippet = Snippet . objects . get ( key = key ) <EOL> except Snippet . DoesNotExist : <EOL> cache . set ( '<STR_LIT>' . format ( key ) , - <NUM_LIT:1> ) <EOL> else : <EOL> cache . set ( '<STR_LIT>' . format ( key ) , snippet ) <EOL> return snippet <EOL> class Snippet ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> key = models . CharField ( max_length = <NUM_LIT> , primary_key = True ) <EOL> text = models . TextField ( ) <EOL> objects = CachedManager ( ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT:key>' , ) <EOL> def __str__ ( self ) : <EOL> return self . key <EOL> def save ( self , * args , ** kwargs ) : <EOL> super ( Snippet , self ) . save ( * args , ** kwargs ) <EOL> set_cached_snippet ( self . key ) <EOL> return self <EOL> @ receiver ( post_delete , sender = Snippet ) <EOL> def delete_snippet ( instance , ** kwargs ) : <EOL> cache . delete ( '<STR_LIT>' . format ( instance . key ) ) <EOL> class SnippetTranslation ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> snippet = models . ForeignKey ( Snippet , related_name = "<STR_LIT>" ) <EOL> language = models . CharField ( max_length = <NUM_LIT:5> ) <EOL> text = models . TextField ( ) <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . snippet , self . language ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> super ( SnippetTranslation , self ) . save ( * args , ** kwargs ) <EOL> set_cached_snippet ( self . snippet_id ) <EOL> return self <EOL> @ receiver ( post_delete , sender = SnippetTranslation ) <EOL> def delete_snippet_translation ( instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> set_cached_snippet ( instance . snippet_id ) </s>
