<s> """<STR_LIT>""" <EOL> from builtins import object <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from django . conf import settings <EOL> from django . utils . translation import ugettext as _ <EOL> from lino . api import rt <EOL> from lino . core . signals import receiver <EOL> from lino . core . signals import on_ui_created , on_ui_updated , pre_ui_delete <EOL> from lino . core . fields import fields_list <EOL> from lino . core . utils import resolve_model <EOL> from lino . core . utils import full_model_name as fmn <EOL> SUBSCRIPTIONS = [ ] <EOL> EMITTERS = [ ] <EOL> class Emitter ( object ) : <EOL> """<STR_LIT>""" <EOL> created_tpl = None <EOL> updated_tpl = None <EOL> deleted_tpl = None <EOL> created_subject = _ ( "<STR_LIT>" ) <EOL> updated_subject = _ ( "<STR_LIT>" ) <EOL> deleted_subject = _ ( "<STR_LIT>" ) <EOL> master_field = None <EOL> def __init__ ( self , model = None , watched_fields = None , <EOL> created_tpl = None , updated_tpl = None , deleted_tpl = None , <EOL> master_field = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if model : <EOL> self . model = model <EOL> if watched_fields : <EOL> self . watched_fields = watched_fields <EOL> self . model = resolve_model ( self . model , strict = True ) <EOL> self . watched_fields = fields_list ( self . model , self . watched_fields ) <EOL> if master_field : <EOL> self . master_field = master_field <EOL> for k in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> v = locals ( ) . get ( k ) <EOL> if v : <EOL> setattr ( self , k , v ) <EOL> v = getattr ( self , k ) <EOL> if v : <EOL> setattr ( self , k , rt . get_template ( v ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( fmn ( self . model ) ) <EOL> def register ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self not in EMITTERS <EOL> EMITTERS . append ( self ) <EOL> return self <EOL> def get_master ( self , obj ) : <EOL> if self . master_field is None : <EOL> return None <EOL> return getattr ( obj , self . master_field ) <EOL> def get_recipients ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return SUBSCRIPTIONS <EOL> def emit_created ( self , request , obj , ** context ) : <EOL> """<STR_LIT>""" <EOL> if self . created_tpl : <EOL> context . update ( obj = obj ) <EOL> context . update ( master = self . get_master ( obj ) ) <EOL> subject = self . created_subject . format ( ** context ) <EOL> recs = self . get_recipients ( ** context ) <EOL> self . sendmails ( request , subject , recs , self . created_tpl , ** context ) <EOL> def emit_updated ( self , request , cw , ** context ) : <EOL> """<STR_LIT>""" <EOL> if not self . updated_tpl : <EOL> return <EOL> updates = list ( cw . get_updates ( watched_fields = self . watched_fields ) ) <EOL> if len ( updates ) == <NUM_LIT:0> : <EOL> return <EOL> context . update ( obj = cw . watched ) <EOL> context . update ( master = self . get_master ( cw . watched ) ) <EOL> context . update ( old = cw . original_state ) <EOL> context . update ( updates = updates ) <EOL> subject = self . updated_subject . format ( ** context ) <EOL> recs = self . get_recipients ( ** context ) <EOL> self . sendmails ( request , subject , recs , self . updated_tpl , ** context ) <EOL> def emit_deleted ( self , request , obj , ** context ) : <EOL> """<STR_LIT>""" <EOL> if self . deleted_tpl : <EOL> context . update ( obj = obj ) <EOL> context . update ( master = self . get_master ( obj ) ) <EOL> subject = self . deleted_subject . format ( ** context ) <EOL> recs = self . get_recipients ( ** context ) <EOL> self . sendmails ( request , subject , recs , self . deleted_tpl , ** context ) <EOL> def sendmails ( self , request , subject , recs , template , ** context ) : <EOL> context . update ( request = request ) <EOL> body = template . render ( ** context ) <EOL> sender = request . user . email or settings . SERVER_EMAIL <EOL> rt . send_email ( subject , sender , body , recs ) <EOL> def subscribe ( addr ) : <EOL> """<STR_LIT>""" <EOL> SUBSCRIPTIONS . append ( addr ) <EOL> def register ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return Emitter ( * args , ** kwargs ) . register ( ) <EOL> def find_emitters ( obj ) : <EOL> """<STR_LIT>""" <EOL> for e in EMITTERS : <EOL> if isinstance ( obj , e . model ) : <EOL> yield e <EOL> @ receiver ( on_ui_created ) <EOL> def on_created ( sender = None , request = None , ** kw ) : <EOL> for s in find_emitters ( sender ) : <EOL> s . emit_created ( request , sender ) <EOL> @ receiver ( on_ui_updated ) <EOL> def on_updated ( sender = None , watcher = None , request = None , ** kw ) : <EOL> for s in find_emitters ( watcher . watched ) : <EOL> s . emit_updated ( request , watcher ) <EOL> @ receiver ( pre_ui_delete ) <EOL> def on_deleted ( sender = None , request = None , ** kw ) : <EOL> for s in find_emitters ( sender ) : <EOL> s . emit_deleted ( request , sender ) </s>
