<s> from django . utils . translation import ugettext_noop <EOL> import dateutil <EOL> from corehq . apps . app_manager . dbaccessors import get_latest_build_id <EOL> from corehq . apps . app_manager . models import ApplicationBase <EOL> from corehq . apps . domain . models import Domain <EOL> from datetime import timedelta <EOL> from pytz import timezone <EOL> EMPTY_FIELD = "<STR_LIT>" <EOL> SUCCEED_DOMAIN = '<STR_LIT>' <EOL> SUCCEED_CM_APPNAME = '<STR_LIT>' <EOL> SUCCEED_PM_APPNAME = '<STR_LIT>' <EOL> SUCCEED_CHW_APPNAME = '<STR_LIT>' <EOL> CONFIG = { <EOL> '<STR_LIT>' : [ <EOL> dict ( val = "<STR_LIT>" , text = ugettext_noop ( "<STR_LIT>" ) ) , <EOL> dict ( val = "<STR_LIT>" , text = ugettext_noop ( "<STR_LIT>" ) ) , <EOL> dict ( val = "<STR_LIT>" , text = ugettext_noop ( "<STR_LIT>" ) ) , <EOL> dict ( val = "<STR_LIT>" , text = ugettext_noop ( "<STR_LIT>" ) ) , <EOL> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def has_role ( user , roles ) : <EOL> return user . get_role ( ) is not None and user . get_role ( ) [ '<STR_LIT:name>' ] in roles <EOL> def is_succeed_admin ( user ) : <EOL> return has_role ( user , [ CONFIG [ '<STR_LIT>' ] , '<STR_LIT>' ] ) <EOL> def is_pi ( user ) : <EOL> return has_role ( user , [ CONFIG [ '<STR_LIT>' ] ] ) <EOL> def is_cm ( user ) : <EOL> return has_role ( user , [ CONFIG [ '<STR_LIT>' ] ] ) <EOL> def is_chw ( user ) : <EOL> return has_role ( user , [ CONFIG [ '<STR_LIT>' ] ] ) <EOL> def is_pm_or_pi ( user ) : <EOL> return has_role ( user , [ CONFIG [ '<STR_LIT>' ] , CONFIG [ '<STR_LIT>' ] ] ) <EOL> def has_any_role ( user ) : <EOL> return is_chw ( user ) or is_pm_or_pi ( user ) or is_cm ( user ) <EOL> def get_app_build ( app_dict ) : <EOL> domain = Domain . get_by_name ( app_dict [ '<STR_LIT>' ] ) <EOL> if domain . use_cloudcare_releases : <EOL> return ApplicationBase . get ( app_dict [ '<STR_LIT>' ] ) . get_latest_app ( ) [ '<STR_LIT>' ] <EOL> else : <EOL> return get_latest_build_id ( app_dict [ '<STR_LIT>' ] , app_dict [ '<STR_LIT>' ] ) <EOL> return None <EOL> def get_form_dict ( case , form_xmlns ) : <EOL> forms = case . get_forms ( ) <EOL> for form in forms : <EOL> form_dict = form . form <EOL> if form_xmlns == form_dict [ "<STR_LIT>" ] : <EOL> return form_dict <EOL> return None <EOL> def format_date ( date_string , OUTPUT_FORMAT , localize = None ) : <EOL> if date_string is None or date_string == '<STR_LIT>' or date_string == "<STR_LIT:U+0020>" or date_string == EMPTY_FIELD or isinstance ( date_string , ( int , float ) ) : <EOL> return '<STR_LIT>' <EOL> if isinstance ( date_string , basestring ) : <EOL> try : <EOL> date_string = dateutil . parser . parse ( date_string ) <EOL> except ( AttributeError , ValueError ) : <EOL> return '<STR_LIT>' <EOL> if localize : <EOL> tz = Domain . get_by_name ( SUCCEED_DOMAIN ) . get_default_timezone ( ) <EOL> if date_string . tzname ( ) is None : <EOL> date_string = timezone ( '<STR_LIT>' ) . localize ( date_string ) <EOL> date_string = date_string . astimezone ( tz ) <EOL> return date_string . strftime ( OUTPUT_FORMAT ) <EOL> def get_randomization_date ( case ) : <EOL> from custom . succeed . reports import INPUT_DATE_FORMAT <EOL> rand_date = case . get_case_property ( "<STR_LIT>" ) <EOL> if rand_date != None : <EOL> date = format_date ( rand_date , INPUT_DATE_FORMAT ) <EOL> return date <EOL> else : <EOL> return EMPTY_FIELD <EOL> def update_patient_target_dates ( case ) : <EOL> from custom . succeed . reports import VISIT_SCHEDULE <EOL> for visit_key , visit in enumerate ( VISIT_SCHEDULE ) : <EOL> try : <EOL> next_visit = VISIT_SCHEDULE [ visit_key + <NUM_LIT:1> ] <EOL> except IndexError : <EOL> next_visit = '<STR_LIT>' <EOL> if next_visit != '<STR_LIT>' and case . get_case_property ( "<STR_LIT>" ) is not None : <EOL> rand_date = dateutil . parser . parse ( get_randomization_date ( case ) ) <EOL> tg_date = rand_date . date ( ) + timedelta ( days = next_visit [ '<STR_LIT>' ] ) <EOL> setattr ( case , visit [ '<STR_LIT>' ] , tg_date . strftime ( "<STR_LIT>" ) ) <EOL> case . save ( ) </s>
