<s> import logging <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from django . core import mail <EOL> from django . db . models import Max <EOL> from django . template import Context , loader <EOL> from oscar . apps . customer . notifications import services <EOL> from oscar . core . loading import get_class , get_model <EOL> ProductAlert = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Product = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Selector = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def send_alerts ( ) : <EOL> """<STR_LIT>""" <EOL> products = Product . objects . filter ( <EOL> productalert__status = ProductAlert . ACTIVE <EOL> ) . distinct ( ) <EOL> logger . info ( "<STR_LIT>" , products . count ( ) ) <EOL> for product in products : <EOL> send_product_alerts ( product ) <EOL> def send_alert_confirmation ( alert ) : <EOL> """<STR_LIT>""" <EOL> ctx = Context ( { <EOL> '<STR_LIT>' : alert , <EOL> '<STR_LIT>' : Site . objects . get_current ( ) , <EOL> } ) <EOL> subject_tpl = loader . get_template ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> body_tpl = loader . get_template ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> mail . send_mail ( <EOL> subject_tpl . render ( ctx ) . strip ( ) , <EOL> body_tpl . render ( ctx ) , <EOL> settings . OSCAR_FROM_EMAIL , <EOL> [ alert . email ] , <EOL> ) <EOL> def send_product_alerts ( product ) : <EOL> """<STR_LIT>""" <EOL> stockrecords = product . stockrecords . all ( ) <EOL> num_stockrecords = len ( stockrecords ) <EOL> if not num_stockrecords : <EOL> return <EOL> logger . info ( "<STR_LIT>" , product ) <EOL> alerts = ProductAlert . objects . filter ( <EOL> product_id__in = ( product . id , product . parent_id ) , <EOL> status = ProductAlert . ACTIVE , <EOL> ) <EOL> num_alerts = alerts . count ( ) <EOL> if num_stockrecords == <NUM_LIT:1> : <EOL> num_in_stock = stockrecords [ <NUM_LIT:0> ] . num_in_stock <EOL> hurry_mode = num_in_stock is not None and num_alerts < num_in_stock <EOL> else : <EOL> result = stockrecords . aggregate ( max_in_stock = Max ( '<STR_LIT>' ) ) <EOL> hurry_mode = result [ '<STR_LIT>' ] is not None and num_alerts < result [ '<STR_LIT>' ] <EOL> message_tpl = loader . get_template ( '<STR_LIT>' ) <EOL> email_subject_tpl = loader . get_template ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> email_body_tpl = loader . get_template ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> emails = [ ] <EOL> num_notifications = <NUM_LIT:0> <EOL> selector = Selector ( ) <EOL> for alert in alerts : <EOL> strategy = selector . strategy ( user = alert . user ) <EOL> data = strategy . fetch_for_product ( product ) <EOL> if not data . availability . is_available_to_buy : <EOL> continue <EOL> ctx = Context ( { <EOL> '<STR_LIT>' : alert , <EOL> '<STR_LIT>' : Site . objects . get_current ( ) , <EOL> '<STR_LIT>' : hurry_mode , <EOL> } ) <EOL> if alert . user : <EOL> num_notifications += <NUM_LIT:1> <EOL> services . notify_user ( alert . user , message_tpl . render ( ctx ) ) <EOL> emails . append ( <EOL> mail . EmailMessage ( <EOL> email_subject_tpl . render ( ctx ) . strip ( ) , <EOL> email_body_tpl . render ( ctx ) , <EOL> settings . OSCAR_FROM_EMAIL , <EOL> [ alert . get_email_address ( ) ] , <EOL> ) <EOL> ) <EOL> alert . close ( ) <EOL> if emails : <EOL> connection = mail . get_connection ( ) <EOL> connection . open ( ) <EOL> connection . send_messages ( emails ) <EOL> connection . close ( ) <EOL> logger . info ( "<STR_LIT>" , num_notifications , <EOL> len ( emails ) ) </s>
