<s> import difflib <EOL> import logging <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from django . core . urlresolvers import reverse as django_reverse <EOL> from django . utils . translation import ugettext as _ , ugettext_lazy as _lazy <EOL> from bleach import clean <EOL> from tidings . events import InstanceEvent , Event , EventUnion <EOL> from tidings . utils import hash_to_unsigned <EOL> from wikimarkup . parser import ALLOWED_TAGS , ALLOWED_ATTRIBUTES <EOL> from kitsune . sumo import email_utils <EOL> from kitsune . sumo . templatetags . jinja_helpers import add_utm <EOL> from kitsune . sumo . urlresolvers import reverse <EOL> from kitsune . wiki . models import Document <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def get_diff_for ( doc , old_rev , new_rev ) : <EOL> fromfile = u'<STR_LIT>' % ( doc . locale , doc . title , old_rev . id ) <EOL> tofile = u'<STR_LIT>' % ( doc . locale , doc . title , new_rev . id ) <EOL> fromfile = fromfile . encode ( '<STR_LIT:utf8>' ) <EOL> tofile = tofile . encode ( '<STR_LIT:utf8>' ) <EOL> diff_parts = difflib . unified_diff ( <EOL> old_rev . content . splitlines ( <NUM_LIT:1> ) , <EOL> new_rev . content . splitlines ( <NUM_LIT:1> ) , <EOL> fromfile = fromfile , tofile = tofile ) <EOL> acc = u'<STR_LIT>' <EOL> for d in diff_parts : <EOL> if isinstance ( d , unicode ) : <EOL> acc = acc + d <EOL> else : <EOL> acc = acc + d . decode ( '<STR_LIT:utf8>' ) <EOL> return clean ( acc , ALLOWED_TAGS , ALLOWED_ATTRIBUTES ) <EOL> def context_dict ( revision , ready_for_l10n = False , revision_approved = False ) : <EOL> """<STR_LIT>""" <EOL> diff = '<STR_LIT>' <EOL> l10n = revision . document . revisions . filter ( is_ready_for_localization = True ) <EOL> approved = revision . document . revisions . filter ( is_approved = True ) <EOL> if ready_for_l10n and l10n . count ( ) > <NUM_LIT:1> : <EOL> old_rev = l10n . order_by ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> diff = get_diff_for ( revision . document , old_rev , revision ) <EOL> elif revision_approved and approved . count ( ) > <NUM_LIT:1> : <EOL> old_rev = approved . order_by ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> diff = get_diff_for ( revision . document , old_rev , revision ) <EOL> elif revision . document . current_revision is not None : <EOL> old_rev = revision . document . current_revision <EOL> diff = get_diff_for ( revision . document , old_rev , revision ) <EOL> return { <EOL> '<STR_LIT>' : revision . document . title , <EOL> '<STR_LIT>' : revision . creator , <EOL> '<STR_LIT:host>' : Site . objects . get_current ( ) . domain , <EOL> '<STR_LIT>' : diff , <EOL> '<STR_LIT>' : clean ( revision . summary , ALLOWED_TAGS , ALLOWED_ATTRIBUTES ) , <EOL> '<STR_LIT>' : clean ( revision . content , ALLOWED_TAGS , ALLOWED_ATTRIBUTES ) , <EOL> } <EOL> class EditDocumentEvent ( InstanceEvent ) : <EOL> """<STR_LIT>""" <EOL> event_type = '<STR_LIT>' <EOL> content_type = Document <EOL> def __init__ ( self , revision ) : <EOL> super ( EditDocumentEvent , self ) . __init__ ( revision . document ) <EOL> self . revision = revision <EOL> def _mails ( self , users_and_watches ) : <EOL> revision = self . revision <EOL> document = revision . document <EOL> log . debug ( '<STR_LIT>' % <EOL> document . id ) <EOL> subject = _lazy ( u'<STR_LIT>' ) <EOL> url = reverse ( '<STR_LIT>' , locale = document . locale , <EOL> args = [ document . slug ] ) <EOL> context = context_dict ( revision ) <EOL> context [ '<STR_LIT>' ] = add_utm ( url , '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = document . locale <EOL> context [ '<STR_LIT:title>' ] = document . title <EOL> context [ '<STR_LIT>' ] = revision . creator <EOL> context [ '<STR_LIT>' ] = revision . comment <EOL> return email_utils . emails_with_users_and_watches ( <EOL> subject = subject , <EOL> text_template = '<STR_LIT>' , <EOL> html_template = '<STR_LIT>' , <EOL> context_vars = context , <EOL> users_and_watches = users_and_watches , <EOL> default_locale = document . locale ) <EOL> class _RevisionConstructor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , revision ) : <EOL> super ( _RevisionConstructor , self ) . __init__ ( ) <EOL> self . revision = revision <EOL> class _BaseProductFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> def _filter_by_product ( self , all_watchers ) : <EOL> products = self . revision . document . get_products ( ) <EOL> product_hashes = [ hash_to_unsigned ( s . slug ) for s in products ] <EOL> watchers_and_watches = [ ] <EOL> for user , watches in all_watchers : <EOL> for watch in watches : <EOL> prods = watch . filters . filter ( <EOL> name = '<STR_LIT>' ) . values_list ( '<STR_LIT:value>' , flat = True ) <EOL> if len ( prods ) == <NUM_LIT:0> : <EOL> watchers_and_watches . append ( ( user , watches ) ) <EOL> break <EOL> for prod in prods : <EOL> if prod in product_hashes : <EOL> watchers_and_watches . append ( ( user , watches ) ) <EOL> break <EOL> return watchers_and_watches <EOL> class _ProductFilter ( _BaseProductFilter ) : <EOL> """<STR_LIT>""" <EOL> filters = set ( [ '<STR_LIT>' ] ) <EOL> def _users_watching ( self , ** kwargs ) : <EOL> users = list ( self . _users_watching_by_filter ( ** kwargs ) ) <EOL> return self . _filter_by_product ( users ) <EOL> class _LocaleAndProductFilter ( _BaseProductFilter ) : <EOL> """<STR_LIT>""" <EOL> filters = set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _users_watching ( self , ** kwargs ) : <EOL> locale = self . revision . document . locale <EOL> users = list ( self . _users_watching_by_filter ( locale = locale , ** kwargs ) ) <EOL> return self . _filter_by_product ( users ) <EOL> class ReviewableRevisionInLocaleEvent ( _RevisionConstructor , <EOL> _LocaleAndProductFilter , <EOL> Event ) : <EOL> """<STR_LIT>""" <EOL> event_type = '<STR_LIT>' <EOL> def _mails ( self , users_and_watches ) : <EOL> revision = self . revision <EOL> document = revision . document <EOL> log . debug ( '<STR_LIT>' % <EOL> revision . id ) <EOL> subject = _lazy ( u'<STR_LIT>' ) <EOL> url = reverse ( '<STR_LIT>' , <EOL> locale = document . locale , <EOL> args = [ document . slug , revision . id ] ) <EOL> context = context_dict ( revision ) <EOL> context [ '<STR_LIT>' ] = add_utm ( url , '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = document . locale <EOL> context [ '<STR_LIT:title>' ] = document . title <EOL> context [ '<STR_LIT>' ] = revision . creator <EOL> context [ '<STR_LIT>' ] = revision . comment <EOL> users = [ ] <EOL> for u , w in users_and_watches : <EOL> if document . allows ( u , '<STR_LIT>' ) : <EOL> users . append ( ( u , w ) ) <EOL> return email_utils . emails_with_users_and_watches ( <EOL> subject = subject , <EOL> text_template = '<STR_LIT>' , <EOL> html_template = '<STR_LIT>' , <EOL> context_vars = context , <EOL> users_and_watches = users , <EOL> default_locale = document . locale ) <EOL> class ReadyRevisionEvent ( _RevisionConstructor , _ProductFilter , Event ) : <EOL> """<STR_LIT>""" <EOL> event_type = '<STR_LIT>' <EOL> def _mails ( self , users_and_watches ) : <EOL> """<STR_LIT>""" <EOL> revision = self . revision <EOL> document = revision . document <EOL> log . debug ( '<STR_LIT>' % <EOL> revision . id ) <EOL> subject = _lazy ( u'<STR_LIT>' ) <EOL> url = django_reverse ( '<STR_LIT>' , args = [ document . slug ] ) <EOL> context = context_dict ( revision , ready_for_l10n = True ) <EOL> context [ '<STR_LIT>' ] = add_utm ( url , '<STR_LIT>' ) <EOL> context [ '<STR_LIT:title>' ] = document . title <EOL> return email_utils . emails_with_users_and_watches ( <EOL> subject = subject , <EOL> text_template = '<STR_LIT>' , <EOL> html_template = '<STR_LIT>' , <EOL> context_vars = context , <EOL> users_and_watches = users_and_watches , <EOL> default_locale = document . locale ) <EOL> class ApproveRevisionInLocaleEvent ( _RevisionConstructor , <EOL> _LocaleAndProductFilter , Event ) : <EOL> """<STR_LIT>""" <EOL> event_type = '<STR_LIT>' <EOL> class ApprovedOrReadyUnion ( EventUnion ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ApprovedOrReadyUnion , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _revision = self . events [ <NUM_LIT:0> ] . revision <EOL> def _mails ( self , users_and_watches ) : <EOL> """<STR_LIT>""" <EOL> revision = self . _revision <EOL> document = revision . document <EOL> is_ready = revision . is_ready_for_localization <EOL> log . debug ( '<STR_LIT>' % <EOL> revision . id ) <EOL> @ email_utils . safe_translation <EOL> def _make_mail ( locale , user , watches ) : <EOL> if ( is_ready and <EOL> ReadyRevisionEvent . event_type in <EOL> ( w . event_type for w in watches ) ) : <EOL> c = context_dict ( revision , ready_for_l10n = True ) <EOL> c [ '<STR_LIT>' ] = watches [ <NUM_LIT:0> ] <EOL> url = django_reverse ( <EOL> '<STR_LIT>' , args = [ document . slug ] ) <EOL> c [ '<STR_LIT>' ] = add_utm ( url , '<STR_LIT>' ) <EOL> subject = _ ( u'<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> text_template = '<STR_LIT>' <EOL> html_template = '<STR_LIT>' <EOL> else : <EOL> c = context_dict ( revision , revision_approved = True ) <EOL> approved_url = reverse ( '<STR_LIT>' , <EOL> locale = document . locale , <EOL> args = [ document . slug ] ) <EOL> c [ '<STR_LIT>' ] = add_utm ( approved_url , '<STR_LIT>' ) <EOL> c [ '<STR_LIT>' ] = watches [ <NUM_LIT:0> ] <EOL> c [ '<STR_LIT>' ] = revision . reviewer <EOL> subject = _ ( u'<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> text_template = '<STR_LIT>' <EOL> html_template = '<STR_LIT>' <EOL> subject = subject . format ( <EOL> title = document . title , <EOL> reviewer = revision . reviewer . username , <EOL> locale = document . locale ) <EOL> mail = email_utils . make_mail ( <EOL> subject = subject , <EOL> text_template = text_template , <EOL> html_template = html_template , <EOL> context_vars = c , <EOL> from_email = settings . TIDINGS_FROM_ADDRESS , <EOL> to_email = user . email ) <EOL> return mail <EOL> for user , watches in users_and_watches : <EOL> if hasattr ( user , '<STR_LIT>' ) : <EOL> locale = user . profile . locale <EOL> else : <EOL> locale = document . locale <EOL> yield _make_mail ( locale , user , watches ) </s>
