<s> from collections import namedtuple <EOL> from xml . etree import ElementTree <EOL> from casexml . apps . case . models import CommCareCase <EOL> from corehq import toggles , feature_previews <EOL> from corehq . apps . commtrack import const <EOL> from corehq . apps . commtrack . const import RequisitionActions <EOL> from corehq . apps . commtrack . models import CommtrackConfig , SupplyPointCase , CommtrackActionConfig , CommtrackRequisitionConfig <EOL> from corehq . apps . locations . models import Location <EOL> from corehq . apps . products . models import Product <EOL> from corehq . apps . programs . models import Program <EOL> import itertools <EOL> from datetime import date , timedelta <EOL> from calendar import monthrange <EOL> from corehq . apps . hqcase . utils import submit_case_blocks <EOL> from casexml . apps . case . mock import CaseBlock <EOL> from casexml . apps . case . xml import V2 <EOL> from django . utils . text import slugify <EOL> from unidecode import unidecode <EOL> from django . utils . translation import ugettext as _ <EOL> import re <EOL> from corehq . form_processor . interfaces . supply import SupplyInterface <EOL> CaseLocationTuple = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def all_sms_codes ( domain ) : <EOL> config = CommtrackConfig . for_domain ( domain ) <EOL> actions = dict ( ( action . keyword , action ) for action in config . actions ) <EOL> products = dict ( ( p . code , p ) for p in Product . by_domain ( domain ) ) <EOL> commands = { <EOL> config . multiaction_keyword : { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> } <EOL> sms_codes = zip ( ( '<STR_LIT:action>' , '<STR_LIT>' , '<STR_LIT>' ) , ( actions , products , commands ) ) <EOL> return dict ( itertools . chain ( * ( [ ( k . lower ( ) , ( type , v ) ) for k , v in codes . iteritems ( ) ] for type , codes in sms_codes ) ) ) <EOL> def get_supply_point_and_location ( domain , site_code ) : <EOL> location = Location . by_site_code ( domain , site_code ) <EOL> if location : <EOL> case = SupplyInterface ( domain ) . get_by_location ( location ) <EOL> else : <EOL> case = None <EOL> return CaseLocationTuple ( case = case , location = location ) <EOL> def make_program ( domain , name , code , default = False ) : <EOL> p = Program ( ) <EOL> p . domain = domain <EOL> p . name = name <EOL> p . code = code . lower ( ) <EOL> p . default = default <EOL> p . save ( ) <EOL> return p <EOL> def get_or_create_default_program ( domain ) : <EOL> program = Program . default_for_domain ( domain ) <EOL> if program : <EOL> return program <EOL> else : <EOL> return make_program ( <EOL> domain , <EOL> _ ( '<STR_LIT>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> default = True <EOL> ) <EOL> def _create_commtrack_config_if_needed ( domain ) : <EOL> if CommtrackConfig . for_domain ( domain ) : <EOL> return <EOL> CommtrackConfig ( <EOL> domain = domain , <EOL> multiaction_enabled = True , <EOL> multiaction_keyword = '<STR_LIT>' , <EOL> actions = [ <EOL> CommtrackActionConfig ( <EOL> action = '<STR_LIT>' , <EOL> keyword = '<STR_LIT:r>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> CommtrackActionConfig ( <EOL> action = '<STR_LIT>' , <EOL> keyword = '<STR_LIT:c>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> CommtrackActionConfig ( <EOL> action = '<STR_LIT>' , <EOL> subaction = '<STR_LIT>' , <EOL> keyword = '<STR_LIT:l>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> CommtrackActionConfig ( <EOL> action = '<STR_LIT>' , <EOL> keyword = '<STR_LIT>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> CommtrackActionConfig ( <EOL> action = '<STR_LIT>' , <EOL> keyword = '<STR_LIT>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> ] , <EOL> ) . save ( ) <EOL> def _enable_commtrack_previews ( domain ) : <EOL> for toggle_class in ( <EOL> toggles . COMMTRACK , <EOL> feature_previews . VELLUM_ADVANCED_ITEMSETS , <EOL> toggles . STOCK_TRANSACTION_EXPORT , <EOL> ) : <EOL> toggle_class . set ( domain , True , toggles . NAMESPACE_DOMAIN ) <EOL> def make_domain_commtrack ( domain_object ) : <EOL> domain_object . commtrack_enabled = True <EOL> domain_object . locations_enabled = True <EOL> domain_object . save ( ) <EOL> _create_commtrack_config_if_needed ( domain_object . name ) <EOL> get_or_create_default_program ( domain_object . name ) <EOL> _enable_commtrack_previews ( domain_object . name ) <EOL> def get_default_requisition_config ( ) : <EOL> return CommtrackRequisitionConfig ( <EOL> enabled = True , <EOL> actions = [ <EOL> CommtrackActionConfig ( <EOL> action = RequisitionActions . REQUEST , <EOL> keyword = '<STR_LIT>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> CommtrackActionConfig ( <EOL> action = RequisitionActions . FULFILL , <EOL> keyword = '<STR_LIT>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> CommtrackActionConfig ( <EOL> action = RequisitionActions . RECEIPTS , <EOL> keyword = '<STR_LIT>' , <EOL> caption = '<STR_LIT>' , <EOL> ) , <EOL> ] , <EOL> ) <EOL> def due_date_weekly ( dow , past_period = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> cur_weekday = date . today ( ) . isoweekday ( ) <EOL> days_till_due = ( dow - cur_weekday ) % <NUM_LIT:7> <EOL> return date . today ( ) + timedelta ( days = days_till_due - <NUM_LIT:7> * past_period ) <EOL> def due_date_monthly ( day , from_end = False , past_period = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if from_end : <EOL> assert False , '<STR_LIT>' <EOL> month_diff = - past_period <EOL> if date . today ( ) . day > day : <EOL> month_diff += <NUM_LIT:1> <EOL> month_seq = date . today ( ) . year * <NUM_LIT:12> + ( date . today ( ) . month - <NUM_LIT:1> ) <EOL> month_seq += month_diff <EOL> y = month_seq // <NUM_LIT:12> <EOL> m = month_seq % <NUM_LIT:12> + <NUM_LIT:1> <EOL> return date ( y , m , min ( day , monthrange ( y , m ) [ <NUM_LIT:1> ] ) ) <EOL> def submit_mapping_case_block ( user , index ) : <EOL> mapping = user . get_location_map_case ( ) <EOL> if mapping : <EOL> caseblock = CaseBlock ( <EOL> create = False , <EOL> case_id = mapping . case_id , <EOL> index = index <EOL> ) <EOL> else : <EOL> caseblock = CaseBlock ( <EOL> create = True , <EOL> case_type = const . USER_LOCATION_OWNER_MAP_TYPE , <EOL> case_id = location_map_case_id ( user ) , <EOL> owner_id = user . _id , <EOL> index = index , <EOL> case_name = const . USER_LOCATION_OWNER_MAP_TYPE . replace ( '<STR_LIT:->' , '<STR_LIT:U+0020>' ) , <EOL> user_id = const . COMMTRACK_USERNAME , <EOL> ) <EOL> submit_case_blocks ( <EOL> ElementTree . tostring ( <EOL> caseblock . as_xml ( ) <EOL> ) , <EOL> user . domain , <EOL> ) <EOL> def location_map_case_id ( user ) : <EOL> return '<STR_LIT>' + user . user_id <EOL> def get_commtrack_location_id ( user , domain ) : <EOL> if ( <EOL> user and <EOL> user . get_domain_membership ( domain . name ) and <EOL> user . get_domain_membership ( domain . name ) . location_id and <EOL> domain . commtrack_enabled <EOL> ) : <EOL> return user . get_domain_membership ( domain . name ) . location_id <EOL> else : <EOL> return None <EOL> def get_case_wrapper ( data ) : <EOL> return { <EOL> const . SUPPLY_POINT_CASE_TYPE : SupplyPointCase , <EOL> } . get ( data . get ( '<STR_LIT:type>' ) , CommCareCase ) <EOL> def unicode_slug ( text ) : <EOL> return slugify ( unicode ( unidecode ( text ) ) ) <EOL> def encode_if_needed ( val ) : <EOL> return val . encode ( "<STR_LIT:utf8>" ) if isinstance ( val , unicode ) else val <EOL> def _fetch_ending_numbers ( s ) : <EOL> matcher = re . compile ( "<STR_LIT>" ) <EOL> return matcher . search ( s ) . group ( ) <EOL> def generate_code ( object_name , existing_codes ) : <EOL> if not object_name : <EOL> object_name = '<STR_LIT>' <EOL> matcher = re . compile ( "<STR_LIT>" ) <EOL> name_slug = matcher . sub ( <EOL> '<STR_LIT:_>' , <EOL> unicode_slug ( object_name . lower ( ) ) <EOL> ) . strip ( '<STR_LIT:_>' ) <EOL> postfix = _fetch_ending_numbers ( object_name ) <EOL> while name_slug + postfix in existing_codes : <EOL> if postfix : <EOL> postfix = str ( int ( postfix ) + <NUM_LIT:1> ) <EOL> else : <EOL> postfix = '<STR_LIT:1>' <EOL> return name_slug + postfix </s>
