<s> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db import models <EOL> from django . db . models . fields import NOT_PROVIDED <EOL> from django . forms . forms import pretty_name <EOL> from django . utils import six <EOL> from . import settings <EOL> from . import utils <EOL> LANGUAGE_CODE , LANGUAGE_NAME = <NUM_LIT:0> , <NUM_LIT:1> <EOL> SUPPORTED_FIELDS = ( <EOL> models . fields . CharField , <EOL> models . fields . TextField , <EOL> ) <EOL> def validate_meta ( meta ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( meta , ( dict , ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> required_keys = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for key in required_keys : <EOL> if key not in meta : <EOL> raise KeyError ( '<STR_LIT>' , key ) <EOL> if not isinstance ( meta [ '<STR_LIT>' ] , ( list , tuple ) ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> def default_value_getter ( field ) : <EOL> """<STR_LIT>""" <EOL> def default_value_func_getter ( self ) : <EOL> localized_field = utils . build_localized_field_name ( field , self . _linguist . active_language ) <EOL> value = getattr ( self , localized_field ) <EOL> if value : <EOL> return value <EOL> default_field = utils . build_localized_field_name ( field , self . default_language ) <EOL> return getattr ( self , default_field ) <EOL> return default_value_func_getter <EOL> def default_value_setter ( field ) : <EOL> """<STR_LIT>""" <EOL> def default_value_func_setter ( self , value ) : <EOL> localized_field = utils . build_localized_field_name ( field , self . _linguist . active_language ) <EOL> setattr ( self , localized_field , value ) <EOL> return default_value_func_setter <EOL> def field_factory ( base_class ) : <EOL> """<STR_LIT>""" <EOL> from . fields import TranslationField <EOL> class TranslationFieldField ( TranslationField , base_class ) : <EOL> pass <EOL> TranslationFieldField . __name__ = '<STR_LIT>' % base_class . __name__ <EOL> return TranslationFieldField <EOL> def create_translation_field ( translated_field , language ) : <EOL> """<STR_LIT>""" <EOL> cls_name = translated_field . __class__ . __name__ <EOL> if not isinstance ( translated_field , SUPPORTED_FIELDS ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % cls_name ) <EOL> translation_class = field_factory ( translated_field . __class__ ) <EOL> return translation_class ( translated_field = translated_field , <EOL> language = language ) <EOL> class ModelMeta ( models . base . ModelBase ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> from . fields import CacheDescriptor , DefaultLanguageDescriptor <EOL> from . mixins import ModelMixin <EOL> from . models import Translation <EOL> meta = None <EOL> default_language = utils . get_fallback_language ( ) <EOL> if '<STR_LIT:Meta>' not in attrs or not hasattr ( attrs [ '<STR_LIT:Meta>' ] , '<STR_LIT>' ) : <EOL> return super ( ModelMeta , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> validate_meta ( attrs [ '<STR_LIT:Meta>' ] . linguist ) <EOL> meta = attrs [ '<STR_LIT:Meta>' ] . linguist <EOL> delattr ( attrs [ '<STR_LIT:Meta>' ] , '<STR_LIT>' ) <EOL> all_fields = dict ( <EOL> ( attr_name , attr ) <EOL> for attr_name , attr in six . iteritems ( attrs ) <EOL> if isinstance ( attr , models . fields . Field ) ) <EOL> abstract_model_bases = [ <EOL> base <EOL> for base in bases <EOL> if hasattr ( base , '<STR_LIT>' ) and base . _meta . abstract <EOL> ] <EOL> for base in abstract_model_bases : <EOL> all_fields . update ( dict ( ( field . name , field ) for field in base . _meta . fields ) ) <EOL> original_fields = { } <EOL> for field in meta [ '<STR_LIT>' ] : <EOL> if field not in all_fields : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> dict ( field = field , name = name ) ) <EOL> original_fields [ field ] = all_fields [ field ] <EOL> if field in attrs : <EOL> del attrs [ field ] <EOL> bases = ( ModelMixin , ) + bases <EOL> new_class = super ( ModelMeta , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> setattr ( new_class , '<STR_LIT>' , CacheDescriptor ( meta = meta ) ) <EOL> setattr ( new_class , '<STR_LIT>' , DefaultLanguageDescriptor ( ) ) <EOL> decider = meta . get ( '<STR_LIT>' , Translation ) <EOL> if not hasattr ( decider , '<STR_LIT>' ) : <EOL> decider . linguist_models = [ ] <EOL> decider . linguist_models . append ( new_class ) <EOL> for field_name , field in six . iteritems ( original_fields ) : <EOL> field . name = field_name <EOL> field . model = new_class <EOL> if not field . verbose_name : <EOL> field . verbose_name = pretty_name ( field_name ) <EOL> for lang in settings . SUPPORTED_LANGUAGES : <EOL> lang_code = lang [ LANGUAGE_CODE ] <EOL> lang_attr = create_translation_field ( field , lang_code ) <EOL> lang_attr_name = utils . get_real_field_name ( field_name , lang_code ) <EOL> if lang_code != default_language : <EOL> if not lang_attr . null and lang_attr . default is NOT_PROVIDED : <EOL> lang_attr . null = True <EOL> if not lang_attr . blank : <EOL> lang_attr . blank = True <EOL> lang_attr . contribute_to_class ( new_class , lang_attr_name ) <EOL> setattr ( new_class , <EOL> field_name , <EOL> property ( default_value_getter ( field_name ) , default_value_setter ( field_name ) ) ) <EOL> new_class . _meta . linguist = meta <EOL> return new_class </s>
