<s> from . base import BaseBackend <EOL> from django . template . loader import render_to_string <EOL> from django . core import mail <EOL> from django . core . mail import EmailMultiAlternatives <EOL> from django . utils import translation <EOL> from . . models import NewsletterSubscriber <EOL> from . . settings import DEFAULT_FROM_EMAIL , PRE_PROCESSORS <EOL> from . . utils import load_class <EOL> from . . compat import update_fields <EOL> class SimpleBackend ( BaseBackend ) : <EOL> model = NewsletterSubscriber <EOL> def subscribe ( self , email , newsletter_list , lang = None , user = None ) : <EOL> return self . model . objects . create ( email = email , user = user , <EOL> newsletter_list = newsletter_list , lang = lang ) <EOL> def register ( self , email , newsletter_list , lang = None , user = None ) : <EOL> if not self . exists ( email , newsletter_list , lang = lang ) : <EOL> subscriber = self . subscribe ( email , newsletter_list , lang , user ) <EOL> else : <EOL> for subscriber in self . all ( email = email , newsletter_list = newsletter_list , lang = lang ) : <EOL> if subscriber . is_unsubscribed : <EOL> subscriber . subscribe ( ) <EOL> def unregister ( self , email , newsletter_list = None , user = None , lang = None ) : <EOL> qs = self . model . objects . filter ( email__iexact = email ) <EOL> if lang : <EOL> qs = qs . filter ( lang = lang ) <EOL> if not newsletter_list : <EOL> for subscriber in qs : <EOL> subscriber . unsubscribe ( commit = True ) <EOL> else : <EOL> if self . exists ( email , newsletter_list ) : <EOL> for subscriber in qs . filter ( newsletter_list = newsletter_list ) : <EOL> subscriber . unsubscribe ( commit = True ) <EOL> def exists ( self , email , newsletter_list = None , user = None , lang = None ) : <EOL> return self . all ( email , user = user , lang = lang , newsletter_list = newsletter_list ) . exists ( ) <EOL> def all ( self , email , user = None , lang = None , newsletter_list = None ) : <EOL> qs = self . model . objects . filter ( email__iexact = email ) . select_related ( '<STR_LIT>' ) <EOL> if user : <EOL> qs = qs . filter ( user = user ) <EOL> if lang : <EOL> qs = qs . filter ( lang = lang ) <EOL> if newsletter_list : <EOL> qs = qs . filter ( newsletter_list = newsletter_list ) <EOL> return qs <EOL> def subscribed ( self , email , newsletter_list = None , user = None , lang = None ) : <EOL> return ( self . all ( email , user = user , lang = lang , newsletter_list = newsletter_list ) <EOL> . filter ( is_unsubscribed = False ) <EOL> . exists ( ) ) <EOL> def send_mails ( self , newsletter , fail_silently = False ) : <EOL> qs = self . model . objects . filter ( newsletter_list = newsletter . newsletter_list ) . subscribed ( ) <EOL> if newsletter . languages : <EOL> subscribers = qs . has_langs ( newsletter . languages ) . prefetch_related ( '<STR_LIT:user>' ) <EOL> else : <EOL> subscribers = qs . prefetch_related ( '<STR_LIT:user>' ) <EOL> connection = mail . get_connection ( fail_silently = fail_silently ) <EOL> emails = [ ] <EOL> old_language = translation . get_language ( ) <EOL> for subscriber in subscribers : <EOL> translation . activate ( subscriber . lang ) <EOL> email = EmailMultiAlternatives ( newsletter . name , <EOL> render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT:object>' : newsletter , <EOL> '<STR_LIT>' : subscriber <EOL> } ) , <EOL> DEFAULT_FROM_EMAIL , <EOL> [ subscriber . email , ] , <EOL> connection = connection ) <EOL> html = render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT:object>' : newsletter , <EOL> '<STR_LIT>' : newsletter . items . all ( ) . prefetch_related ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : subscriber <EOL> } ) <EOL> for pre_processor in PRE_PROCESSORS : <EOL> html = load_class ( pre_processor ) ( html ) <EOL> email . attach_alternative ( html , '<STR_LIT>' ) <EOL> emails . append ( email ) <EOL> translation . activate ( old_language ) <EOL> results = connection . send_messages ( emails ) <EOL> newsletter . sent = True <EOL> update_fields ( newsletter , fields = ( '<STR_LIT>' , ) ) <EOL> return results </s>
