<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from suggestions . models import Suggestion <EOL> from transifex . txcommon . log import logger <EOL> from transifex . resources . models import Translation , SourceEntity <EOL> from transifex . resources . formats . utils . string_utils import percent_diff <EOL> class SuggestionFormat ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource , language , user ) : <EOL> self . resource = resource <EOL> self . language = language <EOL> self . user = user <EOL> def _convert_to_suggestions ( self , source , dest , user = None , langs = None ) : <EOL> """<STR_LIT>""" <EOL> if langs : <EOL> translations = Translation . objects . filter ( source_entity = source , <EOL> language__in = langs , rule = <NUM_LIT:5> ) <EOL> else : <EOL> translations = Translation . objects . filter ( source_entity = source , rule = <NUM_LIT:5> ) <EOL> for t in translations : <EOL> if t . language == dest . resource . source_language : <EOL> continue <EOL> tr , created = Suggestion . objects . get_or_create ( <EOL> string = t . string , <EOL> source_entity = dest , <EOL> language = t . language <EOL> ) <EOL> if created and user : <EOL> tr . user = user <EOL> tr . save ( ) <EOL> return <EOL> def create_suggestions ( self , original , new ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def add_from_strings ( self , strings ) : <EOL> """<STR_LIT>""" <EOL> for j in strings : <EOL> try : <EOL> se = SourceEntity . objects . get ( <EOL> string = j . source_entity , context = j . context or "<STR_LIT:None>" , <EOL> resource = self . resource <EOL> ) <EOL> except SourceEntity . DoesNotExist : <EOL> logger . warning ( <EOL> "<STR_LIT>" % j . source_entity <EOL> ) <EOL> continue <EOL> Suggestion . objects . create ( <EOL> string = j . translation , source_entity = se , <EOL> language = self . language <EOL> ) <EOL> class KeySuggestionFormat ( SuggestionFormat ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ContentSuggestionFormat ( SuggestionFormat ) : <EOL> """<STR_LIT>""" <EOL> def create_suggestions ( self , original , new ) : <EOL> iterations = len ( original ) * len ( new ) <EOL> if iterations < settings . MAX_STRING_ITERATIONS : <EOL> for se in original : <EOL> for ne in new : <EOL> try : <EOL> old_trans = Translation . objects . get ( source_entity = se , <EOL> language = se . resource . source_language , rule = <NUM_LIT:5> ) <EOL> new_trans = Translation . objects . get ( source_entity = ne , <EOL> language = se . resource . source_language , rule = <NUM_LIT:5> ) <EOL> except Translation . DoesNotExist : <EOL> continue <EOL> if percent_diff ( old_trans . string , new_trans . string ) < settings . MAX_STRING_DISTANCE : <EOL> self . _convert_to_suggestions ( se , ne , self . user ) <EOL> break </s>
