<s> from datetime import datetime <EOL> from re import findall <EOL> from strop import maketrans <EOL> from custom . ilsgateway . slab . messages import REMINDER_TRANS , SOH_OVERSTOCKED <EOL> from custom . ilsgateway . tanzania . handlers . generic_stock_report_handler import GenericStockReportHandler <EOL> from custom . ilsgateway . tanzania . handlers . ils_stock_report_parser import Formatter <EOL> from custom . ilsgateway . models import SupplyPointStatusTypes , SupplyPointStatusValues , SupplyPointStatus , SLABConfig <EOL> from custom . ilsgateway . tanzania . reminders import SOH_HELP_MESSAGE , SOH_CONFIRM , SOH_BAD_FORMAT <EOL> from custom . ilsgateway . slab . utils import overstocked_products <EOL> def parse_report ( val ) : <EOL> """<STR_LIT>""" <EOL> def _cleanup ( s ) : <EOL> return unicode ( s ) . encode ( '<STR_LIT:utf-8>' ) <EOL> return [ <EOL> ( x [ <NUM_LIT:0> ] , int ( x [ <NUM_LIT:1> ] . translate ( maketrans ( "<STR_LIT>" , "<STR_LIT>" ) ) ) ) <EOL> for x in findall ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : <NUM_LIT:4> , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , _cleanup ( val ) ) <EOL> ] <EOL> class SohFormatter ( Formatter ) : <EOL> def format ( self , text ) : <EOL> split_text = text . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> keyword = split_text [ <NUM_LIT:0> ] . lower ( ) <EOL> content = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' . format ( code , amount ) for code , amount in parse_report ( split_text [ <NUM_LIT:1> ] ) ) <EOL> if keyword == '<STR_LIT>' : <EOL> text = '<STR_LIT>' + content <EOL> return text <EOL> class SOHHandler ( GenericStockReportHandler ) : <EOL> formatter = SohFormatter <EOL> def _is_pilot_location ( self ) : <EOL> try : <EOL> slab_config = SLABConfig . objects . get ( sql_location = self . sql_location ) <EOL> return slab_config . is_pilot <EOL> except SLABConfig . DoesNotExist : <EOL> return False <EOL> def get_message ( self , data ) : <EOL> if not self . _is_pilot_location ( ) : <EOL> return SOH_CONFIRM <EOL> else : <EOL> overstocked_msg = "<STR_LIT>" <EOL> products_msg = "<STR_LIT>" <EOL> for product_code , stock_on_hand , six_month_consumption in overstocked_products ( self . sql_location ) : <EOL> overstocked_msg += "<STR_LIT>" % ( product_code , stock_on_hand ) <EOL> products_msg += "<STR_LIT>" % ( product_code , six_month_consumption ) <EOL> if overstocked_msg and products_msg : <EOL> self . respond ( <EOL> SOH_OVERSTOCKED , overstocked_list = overstocked_msg . strip ( ) , products_list = products_msg . strip ( ) <EOL> ) <EOL> return REMINDER_TRANS <EOL> def on_success ( self ) : <EOL> SupplyPointStatus . objects . create ( location_id = self . location_id , <EOL> status_type = SupplyPointStatusTypes . SOH_FACILITY , <EOL> status_value = SupplyPointStatusValues . SUBMITTED , <EOL> status_date = datetime . utcnow ( ) ) <EOL> SupplyPointStatus . objects . create ( location_id = self . location_id , <EOL> status_type = SupplyPointStatusTypes . LOSS_ADJUSTMENT_FACILITY , <EOL> status_value = SupplyPointStatusValues . REMINDER_SENT , <EOL> status_date = datetime . utcnow ( ) ) <EOL> def on_error ( self , data ) : <EOL> self . respond ( SOH_BAD_FORMAT ) <EOL> def help ( self ) : <EOL> self . respond ( SOH_HELP_MESSAGE ) <EOL> return True </s>
