<s> from django . conf import settings <EOL> from django . utils . translation import ugettext_lazy as _lazy <EOL> from django . core . exceptions import ValidationError <EOL> from rest_framework import fields <EOL> from olympia . amo . utils import to_language <EOL> class TranslationSerializerField ( fields . Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _lazy ( u'<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _lazy ( u'<STR_LIT>' ) <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . min_length = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( TranslationSerializerField , self ) . __init__ ( * args , ** kwargs ) <EOL> def fetch_all_translations ( self , obj , source , field ) : <EOL> translations = field . __class__ . objects . filter ( <EOL> id = field . id , localized_string__isnull = False ) <EOL> return dict ( ( to_language ( trans . locale ) , unicode ( trans ) ) <EOL> for trans in translations ) if translations else None <EOL> def fetch_single_translation ( self , obj , source , field , requested_language ) : <EOL> return unicode ( field ) if field else None <EOL> def get_attribute ( self , obj ) : <EOL> source = self . source or self . field_name <EOL> field = fields . get_attribute ( obj , source . split ( '<STR_LIT:.>' ) ) <EOL> if not field : <EOL> return None <EOL> requested_language = None <EOL> request = self . context . get ( '<STR_LIT>' , None ) <EOL> if request and request . method == '<STR_LIT:GET>' and '<STR_LIT>' in request . GET : <EOL> requested_language = request . GET [ '<STR_LIT>' ] <EOL> if requested_language : <EOL> return self . fetch_single_translation ( obj , source , field , <EOL> requested_language ) <EOL> else : <EOL> return self . fetch_all_translations ( obj , source , field ) <EOL> def to_representation ( self , val ) : <EOL> return val <EOL> def to_internal_value ( self , data ) : <EOL> if isinstance ( data , basestring ) : <EOL> self . validate ( data ) <EOL> return data . strip ( ) <EOL> elif isinstance ( data , dict ) : <EOL> self . validate ( data ) <EOL> for key , value in data . items ( ) : <EOL> data [ key ] = value and value . strip ( ) <EOL> return data <EOL> return unicode ( data ) <EOL> def validate ( self , value ) : <EOL> value_too_short = True <EOL> if isinstance ( value , basestring ) : <EOL> if len ( value . strip ( ) ) >= self . min_length : <EOL> value_too_short = False <EOL> else : <EOL> for locale , string in value . items ( ) : <EOL> if locale . lower ( ) not in settings . LANGUAGES : <EOL> raise ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] . format ( <EOL> lang_code = repr ( locale ) ) ) <EOL> if string and ( len ( string . strip ( ) ) >= self . min_length ) : <EOL> value_too_short = False <EOL> break <EOL> if self . min_length and value_too_short : <EOL> raise ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] . format ( num = self . min_length ) ) <EOL> class ESTranslationSerializerField ( TranslationSerializerField ) : <EOL> """<STR_LIT>""" <EOL> suffix = '<STR_LIT>' <EOL> _source = None <EOL> def get_source ( self ) : <EOL> if self . _source is None : <EOL> return None <EOL> return self . _source + self . suffix <EOL> def set_source ( self , val ) : <EOL> self . _source = val <EOL> source = property ( get_source , set_source ) <EOL> @ classmethod <EOL> def attach_translations ( cls , obj , data , source_name , target_name = None ) : <EOL> """<STR_LIT>""" <EOL> if target_name is None : <EOL> target_name = source_name <EOL> target_key = '<STR_LIT>' % ( target_name , cls . suffix ) <EOL> source_key = '<STR_LIT>' % ( source_name , cls . suffix ) <EOL> setattr ( obj , target_key , dict ( ( v . get ( '<STR_LIT>' , '<STR_LIT>' ) , v . get ( '<STR_LIT:string>' , '<STR_LIT>' ) ) <EOL> for v in data . get ( source_key , { } ) or { } ) ) <EOL> def fetch_all_translations ( self , obj , source , field ) : <EOL> return field or None <EOL> def fetch_single_translation ( self , obj , source , field , requested_language ) : <EOL> translations = self . fetch_all_translations ( obj , source , field ) or { } <EOL> return ( translations . get ( requested_language ) or <EOL> translations . get ( getattr ( obj , '<STR_LIT>' , None ) ) or <EOL> translations . get ( getattr ( obj , '<STR_LIT>' , None ) ) or <EOL> translations . get ( settings . LANGUAGE_CODE ) or None ) </s>
