<s> """<STR_LIT>""" <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db import models <EOL> from django . db . models import signals <EOL> from django . db . models . base import ModelBase <EOL> from django . utils . translation import get_language <EOL> from multilingual . languages import * <EOL> from multilingual . exceptions import TranslationDoesNotExist <EOL> from multilingual . fields import TranslationForeignKey <EOL> from multilingual import manager <EOL> from multilingual . utils import GLL <EOL> from new import instancemethod <EOL> def translation_save_translated_fields ( instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( instance , '<STR_LIT>' ) : <EOL> return <EOL> for l_id , translation in instance . _translation_cache . iteritems ( ) : <EOL> translation . master_id = instance . _get_pk_val ( ) <EOL> translation . save ( ) <EOL> def fill_translation_cache ( instance ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( instance , '<STR_LIT>' ) : <EOL> return <EOL> instance . _translation_cache = { } <EOL> if not instance . pk : <EOL> return <EOL> for language_code in get_language_code_list ( ) : <EOL> field_alias = get_translated_field_alias ( '<STR_LIT:code>' , language_code ) <EOL> if getattr ( instance , field_alias , None ) is not None : <EOL> field_names = [ f . attname for f in instance . _meta . translation_model . _meta . fields ] <EOL> field_data = { } <EOL> for fname in field_names : <EOL> field_data [ fname ] = getattr ( instance , <EOL> get_translated_field_alias ( fname , language_code ) ) <EOL> translation = instance . _meta . translation_model ( ** field_data ) <EOL> instance . _translation_cache [ language_code ] = translation <EOL> if len ( instance . _translation_cache . keys ( ) ) == <NUM_LIT:0> : <EOL> for translation in instance . translations . all ( ) : <EOL> instance . _translation_cache [ translation . language_code ] = translation <EOL> class TranslatedFieldProxy ( property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field_name , alias , field , language_code = None , <EOL> fallback = False ) : <EOL> self . field_name = field_name <EOL> self . field = field <EOL> self . admin_order_field = alias <EOL> self . _language_code = language_code <EOL> self . fallback = fallback <EOL> @ property <EOL> def language_code ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _language_code : <EOL> return self . _language_code <EOL> return get_language ( ) <EOL> def __get__ ( self , obj , objtype = None ) : <EOL> if obj is None : <EOL> return self <EOL> return getattr ( obj , '<STR_LIT>' + self . field_name ) ( self . language_code , <EOL> self . fallback ) <EOL> def __set__ ( self , obj , value ) : <EOL> language_code = self . language_code <EOL> return getattr ( obj , '<STR_LIT>' + self . field_name ) ( value , self . language_code ) <EOL> short_description = property ( lambda self : self . field . short_description ) <EOL> def getter_generator ( field_name , short_description ) : <EOL> """<STR_LIT>""" <EOL> def get_translation_field ( cls , language_code = None , fallback = False ) : <EOL> try : <EOL> return cls . get_translation ( language_code , <EOL> fallback = fallback , <EOL> field = field_name ) <EOL> except TranslationDoesNotExist : <EOL> return None <EOL> get_translation_field . short_description = short_description <EOL> return get_translation_field <EOL> def setter_generator ( field_name ) : <EOL> """<STR_LIT>""" <EOL> def set_translation_field ( cls , value , language_code = None ) : <EOL> setattr ( cls . get_translation ( language_code , True ) , <EOL> field_name , value ) <EOL> set_translation_field . short_description = "<STR_LIT>" + field_name <EOL> return set_translation_field <EOL> def get_translation ( cls , language_code , create_if_necessary = False , <EOL> fallback = False , field = None ) : <EOL> """<STR_LIT>""" <EOL> cls . fill_translation_cache ( ) <EOL> if language_code is None : <EOL> language_code = getattr ( cls , '<STR_LIT>' , None ) <EOL> if language_code is None : <EOL> language_code = get_default_language ( ) <EOL> force = False <EOL> if GLL . is_active : <EOL> language_code = GLL . language_code <EOL> force = True <EOL> if language_code in cls . _translation_cache : <EOL> transobj = cls . _translation_cache . get ( language_code , None ) <EOL> if field is None : <EOL> return transobj <EOL> value = getattr ( transobj , field ) <EOL> if value or force or ( not fallback ) : <EOL> return value <EOL> if create_if_necessary : <EOL> new_translation = cls . _meta . translation_model ( master = cls , <EOL> language_code = language_code ) <EOL> cls . _translation_cache [ language_code ] = new_translation <EOL> return new_translation <EOL> elif ( not force ) and fallback : <EOL> for fb_lang_code in get_fallbacks ( language_code ) : <EOL> transobj = cls . _translation_cache . get ( fb_lang_code , None ) <EOL> if transobj : <EOL> if field is None : <EOL> return transobj <EOL> else : <EOL> value = getattr ( transobj , field ) <EOL> if value : <EOL> return value <EOL> raise TranslationDoesNotExist ( language_code ) <EOL> class TranslationModel ( object ) : <EOL> """<STR_LIT>""" <EOL> def contribute_to_class ( cls , main_cls , name ) : <EOL> """<STR_LIT>""" <EOL> signals . class_prepared . connect ( cls . finish_multilingual_class , <EOL> sender = main_cls , weak = False ) <EOL> signals . post_save . connect ( translation_save_translated_fields , <EOL> sender = main_cls ) <EOL> contribute_to_class = classmethod ( contribute_to_class ) <EOL> def create_translation_attrs ( cls , main_cls ) : <EOL> """<STR_LIT>""" <EOL> translated_fields = { } <EOL> for fname , field in cls . __dict__ . items ( ) : <EOL> if isinstance ( field , models . fields . Field ) : <EOL> translated_fields [ fname ] = ( field , None ) <EOL> getter = getter_generator ( fname , getattr ( field , '<STR_LIT>' , fname ) ) <EOL> setattr ( main_cls , '<STR_LIT>' + fname , getter ) <EOL> setter = setter_generator ( fname ) <EOL> setattr ( main_cls , '<STR_LIT>' + fname , setter ) <EOL> setattr ( main_cls , fname , <EOL> TranslatedFieldProxy ( fname , fname , field , fallback = True ) ) <EOL> setattr ( main_cls , fname + FALLBACK_FIELD_SUFFIX , <EOL> TranslatedFieldProxy ( fname , fname , field , fallback = True ) ) <EOL> for language_code in get_language_code_list ( ) : <EOL> fname_lng = fname + '<STR_LIT:_>' + language_code . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> translated_fields [ fname_lng ] = ( field , language_code ) <EOL> setattr ( main_cls , fname_lng , <EOL> TranslatedFieldProxy ( fname , fname_lng , field , <EOL> language_code ) ) <EOL> setattr ( main_cls , fname_lng + FALLBACK_FIELD_SUFFIX , <EOL> TranslatedFieldProxy ( fname , fname_lng , field , <EOL> language_code , fallback = True ) ) <EOL> fname_current = fname + '<STR_LIT>' <EOL> setattr ( main_cls , fname_current , <EOL> TranslatedFieldProxy ( fname , fname_current , field , None ) ) <EOL> setattr ( main_cls , fname_current + FALLBACK_FIELD_SUFFIX , <EOL> TranslatedFieldProxy ( fname , fname_current , field , None , fallback = True ) ) <EOL> return translated_fields <EOL> create_translation_attrs = classmethod ( create_translation_attrs ) <EOL> def get_unique_fields ( cls ) : <EOL> """<STR_LIT>""" <EOL> unique_fields = [ ] <EOL> for fname , field in cls . __dict__ . items ( ) : <EOL> if isinstance ( field , models . fields . Field ) : <EOL> if getattr ( field , '<STR_LIT>' , False ) : <EOL> try : <EOL> field . unique = False <EOL> except AttributeError : <EOL> field . _unique = False <EOL> unique_fields . append ( fname ) <EOL> return unique_fields <EOL> get_unique_fields = classmethod ( get_unique_fields ) <EOL> def finish_multilingual_class ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> main_cls = kwargs [ '<STR_LIT>' ] <EOL> translation_model_name = main_cls . __name__ + "<STR_LIT>" <EOL> unique = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> for f in cls . get_unique_fields ( ) : <EOL> unique . append ( ( '<STR_LIT>' , f ) ) <EOL> class TransMeta : <EOL> pass <EOL> try : <EOL> meta = cls . Meta <EOL> except AttributeError : <EOL> meta = TransMeta <EOL> meta . ordering = ( '<STR_LIT>' , ) <EOL> meta . unique_together = tuple ( unique ) <EOL> meta . app_label = main_cls . _meta . app_label <EOL> if not hasattr ( meta , '<STR_LIT>' ) : <EOL> meta . db_table = main_cls . _meta . db_table + '<STR_LIT>' <EOL> trans_attrs = cls . __dict__ . copy ( ) <EOL> trans_attrs [ '<STR_LIT:Meta>' ] = meta <EOL> trans_attrs [ '<STR_LIT>' ] = models . CharField ( max_length = <NUM_LIT:15> , blank = True , <EOL> choices = get_language_choices ( ) , <EOL> db_index = True ) <EOL> related_name = getattr ( meta , '<STR_LIT:related_name>' , '<STR_LIT>' ) <EOL> if hasattr ( meta , '<STR_LIT:related_name>' ) : <EOL> delattr ( meta , '<STR_LIT:related_name>' ) <EOL> edit_inline = True <EOL> trans_attrs [ '<STR_LIT>' ] = TranslationForeignKey ( main_cls , blank = False , null = False , <EOL> related_name = related_name , ) <EOL> trans_attrs [ '<STR_LIT>' ] = lambda self : ( "<STR_LIT>" <EOL> % ( translation_model_name , <EOL> self . language_code ) ) <EOL> trans_model = ModelBase ( translation_model_name , ( models . Model , ) , trans_attrs ) <EOL> trans_model . _meta . translated_fields = cls . create_translation_attrs ( main_cls ) <EOL> trans_model . _meta . related_name = related_name <EOL> _old_init_name_map = main_cls . _meta . __class__ . init_name_map <EOL> def init_name_map ( self ) : <EOL> cache = _old_init_name_map ( self ) <EOL> for name , field_and_lang_id in trans_model . _meta . translated_fields . items ( ) : <EOL> cache [ name ] = ( field_and_lang_id [ <NUM_LIT:0> ] , trans_model , True , False ) <EOL> return cache <EOL> main_cls . _meta . init_name_map = instancemethod ( init_name_map , <EOL> main_cls . _meta , <EOL> main_cls . _meta . __class__ ) <EOL> main_cls . _meta . translation_model = trans_model <EOL> main_cls . _meta . force_language = None <EOL> main_cls . Translation = trans_model <EOL> main_cls . get_translation = get_translation <EOL> main_cls . fill_translation_cache = fill_translation_cache <EOL> finish_multilingual_class = classmethod ( finish_multilingual_class ) <EOL> Translation = TranslationModel <EOL> def install_translation_library ( ) : <EOL> if getattr ( ModelBase , '<STR_LIT>' , False ) : <EOL> return <EOL> _old_new = ModelBase . __new__ <EOL> def multilingual_modelbase_new ( cls , name , bases , attrs ) : <EOL> if '<STR_LIT>' in attrs : <EOL> if not issubclass ( attrs [ '<STR_LIT>' ] , Translation ) : <EOL> raise ValueError , ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) % ( name , ) <EOL> if ( '<STR_LIT>' in attrs ) and ( not isinstance ( attrs [ '<STR_LIT>' ] , manager . Manager ) ) : <EOL> raise ValueError , ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) % ( name , ) <EOL> if not '<STR_LIT>' in attrs : <EOL> attrs [ '<STR_LIT>' ] = manager . Manager ( ) <EOL> return _old_new ( cls , name , bases , attrs ) <EOL> ModelBase . __new__ = staticmethod ( multilingual_modelbase_new ) <EOL> ModelBase . _multilingual_installed = True <EOL> install_translation_library ( ) </s>
