<s> from django . db import connections , models , router <EOL> from django . db . models . deletion import Collector <EOL> from django . utils import encoding <EOL> import bleach <EOL> import commonware . log <EOL> from olympia . amo . models import ModelBase , ManagerBase <EOL> from olympia . amo import urlresolvers <EOL> from . import utils <EOL> log = commonware . log . getLogger ( '<STR_LIT>' ) <EOL> class TranslationManager ( ManagerBase ) : <EOL> def remove_for ( self , obj , locale ) : <EOL> """<STR_LIT>""" <EOL> ids = [ getattr ( obj , f . attname ) for f in obj . _meta . translated_fields ] <EOL> qs = Translation . objects . filter ( id__in = filter ( None , ids ) , <EOL> locale = locale ) <EOL> qs . update ( localized_string = None , localized_string_clean = None ) <EOL> class Translation ( ModelBase ) : <EOL> """<STR_LIT>""" <EOL> autoid = models . AutoField ( primary_key = True ) <EOL> id = models . IntegerField ( ) <EOL> locale = models . CharField ( max_length = <NUM_LIT:10> ) <EOL> localized_string = models . TextField ( null = True ) <EOL> localized_string_clean = models . TextField ( null = True ) <EOL> objects = TranslationManager ( ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> unique_together = ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . localized_string and unicode ( self . localized_string ) or '<STR_LIT>' <EOL> def __nonzero__ ( self ) : <EOL> return ( bool ( self . localized_string ) and <EOL> bool ( self . localized_string . strip ( ) ) ) <EOL> def __eq__ ( self , other ) : <EOL> return self . __cmp__ ( other ) == <NUM_LIT:0> <EOL> def __cmp__ ( self , other ) : <EOL> if hasattr ( other , '<STR_LIT>' ) : <EOL> return cmp ( self . localized_string , other . localized_string ) <EOL> else : <EOL> return cmp ( self . localized_string , other ) <EOL> def clean ( self ) : <EOL> if self . localized_string : <EOL> self . localized_string = self . localized_string . strip ( ) <EOL> def save ( self , ** kwargs ) : <EOL> self . clean ( ) <EOL> return super ( Translation , self ) . save ( ** kwargs ) <EOL> def delete ( self , using = None ) : <EOL> cls = self . __class__ <EOL> using = using or router . db_for_write ( cls , instance = self ) <EOL> qs = cls . objects . filter ( id = self . id ) . exclude ( autoid = self . autoid ) <EOL> if qs . using ( using ) . exists ( ) : <EOL> assert self . _get_pk_val ( ) is not None <EOL> collector = Collector ( using = using ) <EOL> collector . collect ( [ self ] , collect_related = False ) <EOL> with connections [ using ] . constraint_checks_disabled ( ) : <EOL> collector . delete ( ) <EOL> else : <EOL> return super ( Translation , self ) . delete ( using = using ) <EOL> delete . alters_data = True <EOL> @ classmethod <EOL> def _cache_key ( cls , pk , db ) : <EOL> key_parts = ( '<STR_LIT:o>' , '<STR_LIT>' , pk , '<STR_LIT:default>' ) <EOL> return '<STR_LIT::>' . join ( map ( encoding . smart_unicode , key_parts ) ) <EOL> @ classmethod <EOL> def new ( cls , string , locale , id = None ) : <EOL> """<STR_LIT>""" <EOL> if id is None : <EOL> cursor = connections [ '<STR_LIT:default>' ] . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> if not cursor . rowcount > <NUM_LIT:0> : <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> cursor . execute ( '<STR_LIT>' ) <EOL> id = cursor . fetchone ( ) [ <NUM_LIT:0> ] <EOL> q = { '<STR_LIT:id>' : id , '<STR_LIT>' : locale } <EOL> try : <EOL> trans = cls . objects . get ( ** q ) <EOL> trans . localized_string = string <EOL> except cls . DoesNotExist : <EOL> trans = cls ( localized_string = string , ** q ) <EOL> return trans <EOL> class PurifiedTranslation ( Translation ) : <EOL> """<STR_LIT>""" <EOL> allowed_tags = [ <EOL> '<STR_LIT:a>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:b>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:code>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:i>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> allowed_attributes = { <EOL> '<STR_LIT:a>' : [ '<STR_LIT>' , '<STR_LIT:title>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:title>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:title>' ] , <EOL> } <EOL> class Meta : <EOL> proxy = True <EOL> def __unicode__ ( self ) : <EOL> if not self . localized_string_clean : <EOL> self . clean ( ) <EOL> return unicode ( self . localized_string_clean ) <EOL> def __html__ ( self ) : <EOL> return unicode ( self ) <EOL> def __truncate__ ( self , length , killwords , end ) : <EOL> return utils . truncate ( unicode ( self ) , length , killwords , end ) <EOL> def clean ( self ) : <EOL> from olympia . amo . utils import clean_nl <EOL> super ( PurifiedTranslation , self ) . clean ( ) <EOL> cleaned = self . clean_localized_string ( ) <EOL> self . localized_string_clean = clean_nl ( cleaned ) . strip ( ) <EOL> def clean_localized_string ( self ) : <EOL> linkified = urlresolvers . linkify_with_outgoing ( self . localized_string ) <EOL> return bleach . clean ( linkified , tags = self . allowed_tags , <EOL> attributes = self . allowed_attributes ) <EOL> class LinkifiedTranslation ( PurifiedTranslation ) : <EOL> """<STR_LIT>""" <EOL> allowed_tags = [ '<STR_LIT:a>' ] <EOL> class Meta : <EOL> proxy = True <EOL> class NoLinksMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def clean_localized_string ( self ) : <EOL> cleaned = super ( NoLinksMixin , self ) . clean_localized_string ( ) <EOL> emptied_links = bleach . linkify ( <EOL> cleaned , callbacks = [ lambda attrs , new : { '<STR_LIT>' : '<STR_LIT>' } ] ) <EOL> allowed_tags = self . allowed_tags [ : ] <EOL> allowed_tags . remove ( '<STR_LIT:a>' ) <EOL> return bleach . clean ( emptied_links , tags = allowed_tags , strip = True ) <EOL> class NoLinksTranslation ( NoLinksMixin , PurifiedTranslation ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> proxy = True <EOL> class NoLinksNoMarkupTranslation ( NoLinksMixin , LinkifiedTranslation ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> proxy = True <EOL> class TranslationSequence ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> id = models . IntegerField ( primary_key = True ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> def delete_translation ( obj , fieldname ) : <EOL> field = obj . _meta . get_field ( fieldname ) <EOL> trans_id = getattr ( obj , field . attname ) <EOL> obj . update ( ** { field . name : None } ) <EOL> if trans_id : <EOL> Translation . objects . filter ( id = trans_id ) . delete ( ) </s>
