<s> from notifications . engine import NotificationEngine <EOL> from notifications import settings as notification_settings <EOL> class BackendError ( Exception ) : <EOL> pass <EOL> class NotificationTypeError ( Exception ) : <EOL> pass <EOL> class BaseNotification ( object ) : <EOL> name = None <EOL> help = '<STR_LIT>' <EOL> subject = None <EOL> text = None <EOL> send_at = None <EOL> allowed_backends = [ ] <EOL> _all_backends = NotificationEngine . _backends <EOL> @ classmethod <EOL> def get_name ( cls ) : <EOL> return cls . name or cls . __name__ <EOL> @ classmethod <EOL> def get_help ( cls ) : <EOL> return cls . help <EOL> def __init__ ( self , subject = None , text = None , request = None , user = None , ** kwargs ) : <EOL> if not self . allowed_backends : <EOL> self . allowed_backends = NotificationEngine . _backends . keys ( ) <EOL> self . subject = subject or self . subject <EOL> self . text = text or self . text <EOL> self . user = user <EOL> self . request = request <EOL> self . kwargs = kwargs <EOL> def is_registered ( self ) : <EOL> return self . __class__ . __name__ in NotificationEngine . _types <EOL> def send ( self ) : <EOL> if not self . is_registered ( ) : <EOL> if notification_settings . FAIL_SILENT : <EOL> return False <EOL> raise NotificationTypeError ( '<STR_LIT>' ) <EOL> users = self . get_recipients ( ) <EOL> if not hasattr ( users , '<STR_LIT>' ) : <EOL> users = [ users ] <EOL> if not users and not notification_settings . FAIL_SILENT : <EOL> raise NotificationTypeError ( '<STR_LIT>' ) <EOL> for user in users : <EOL> backends = self . _get_backends ( user ) <EOL> for backend_name , backend in backends . items ( ) : <EOL> backend . create ( ) <EOL> def get_recipients ( self ) : <EOL> user = self . user <EOL> if not user : <EOL> try : <EOL> user = self . request . user <EOL> except KeyError : <EOL> if notification_settings . FAIL_SILENT : <EOL> return False <EOL> raise BackendError ( '<STR_LIT>' ) <EOL> return user <EOL> def get_text ( self , backend , user ) : <EOL> return self . text <EOL> def get_subject ( self , backend , user ) : <EOL> return self . subject <EOL> def get_send_at ( self , backend , user ) : <EOL> return self . send_at <EOL> def _get_backends ( self , user ) : <EOL> """<STR_LIT>""" <EOL> from notifications . models import DisabledNotificationsTypeBackend <EOL> disabled_backends = [ ] <EOL> try : <EOL> disabled_backends = DisabledNotificationsTypeBackend . objects . get ( user = user , notification_type = self . __class__ . __name__ ) . get_backends ( ) <EOL> except DisabledNotificationsTypeBackend . DoesNotExist : <EOL> pass <EOL> backends = { } <EOL> for backend_name in self . allowed_backends : <EOL> if backend_name not in disabled_backends : <EOL> subject = self . get_subject ( backend_name , user ) <EOL> text = self . get_text ( backend_name , user ) <EOL> send_at = self . get_send_at ( backend_name , user ) <EOL> backend = self . _all_backends [ backend_name ] ( user = user , subject = subject , text = text , send_at = send_at ) <EOL> if backend . is_registered ( ) and backend . validate ( ) : <EOL> backends [ backend_name ] = backend <EOL> if not backends and not notification_settings . FAIL_SILENT : <EOL> raise BackendError ( '<STR_LIT>' ) <EOL> return backends <EOL> def __getattr__ ( self , name ) : <EOL> return self . kwargs [ name ] </s>
