<s> from corehq . apps . commtrack . models import StockState <EOL> from corehq . apps . locations . models import SQLLocation <EOL> from corehq . apps . users . models import WebUser <EOL> from custom . ewsghana . models import EWSExtension <EOL> from custom . ewsghana . reminders import STOCKOUT_REPORT <EOL> from custom . ewsghana . reminders . web_user_reminder import WebUserReminder <EOL> from custom . ewsghana . utils import has_notifications_enabled <EOL> class StockoutReminder ( WebUserReminder ) : <EOL> def get_users ( self ) : <EOL> return WebUser . by_domain ( self . domain ) <EOL> def recipients_filter ( self , user ) : <EOL> return has_notifications_enabled ( self . domain , user ) <EOL> def _get_stockouts ( self , case_id ) : <EOL> return StockState . objects . filter ( <EOL> case_id = case_id , stock_on_hand = <NUM_LIT:0> <EOL> ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> def get_message ( self , recipient ) : <EOL> web_user = recipient <EOL> try : <EOL> extension = EWSExtension . objects . get ( user_id = web_user . get_id , domain = self . domain ) <EOL> except EWSExtension . DoesNotExist : <EOL> return <EOL> if not extension . location_id : <EOL> return <EOL> sql_location = SQLLocation . objects . get ( location_id = extension . location_id , domain = self . domain ) <EOL> products_names = self . _get_stockouts ( sql_location . supply_point_id ) <EOL> if not products_names : <EOL> return <EOL> try : <EOL> last_report_date = StockState . objects . filter ( <EOL> case_id = sql_location . supply_point_id <EOL> ) . latest ( '<STR_LIT>' ) . last_modified_date . strftime ( '<STR_LIT>' ) <EOL> except StockState . DoesNotExist : <EOL> last_report_date = '<STR_LIT>' <EOL> return STOCKOUT_REPORT % { <EOL> '<STR_LIT:name>' : web_user . full_name , <EOL> '<STR_LIT>' : sql_location . name , <EOL> '<STR_LIT:date>' : last_report_date , <EOL> '<STR_LIT>' : '<STR_LIT:U+002CU+0020>' . join ( products_names ) <EOL> } </s>
