Example #1
0
def ccs_record_case_has_future_edd(case, now):
    """
    NOTE: This criteria references today's date.
    Use this custom criteria with caution for SMS alerts.
    See `person_case_is_under_n_years_old` for explanation.
    """
    if case.type != 'ccs_record':
        return False

    try:
        edd = get_date(case.get_case_property('edd'))
    except Exception:
        return False

    return todays_date(now) < edd
Example #2
0
def immunization_is_due(tasks_case, anchor_date, immunization_product, all_immunization_products, ledger_values):
    """
    :param tasks_case: the CommCareCaseSQL with case type 'tasks'
    :param anchor_date: the output from get_immunization_anchor_date
    :param immunization_product: the SQLProduct representing the immunization
    :param all_immunization_products: the output from get_immunization_products
    :param ledger_values: the output from get_tasks_case_immunization_ledger_values
    :return: True if, based on the current date, the given immunization is due; False otherwise
    """
    _validate_tasks_case_and_immunization_product(tasks_case, immunization_product)

    # Check if the immunization has already happened
    product_id_to_ledger_value = get_map(ledger_values, 'entry_id')
    if immunization_product.product_id in product_id_to_ledger_value:
        return False

    # Check the product's schedule_flag vs the tasks_case schedule_flag, if applicable
    product_schedule_flag = immunization_product.product_data.get('schedule_flag', '').strip()
    if product_schedule_flag:
        tasks_case_schedule_flag = tasks_case.get_case_property('schedule_flag')
        if (
            not isinstance(tasks_case_schedule_flag, (six.text_type, bytes)) or
            product_schedule_flag not in tasks_case_schedule_flag
        ):
            return False

    # If all of the above checks pass, check that today's date falls within the immunization window
    today = todays_date(datetime.utcnow())

    start_date, end_date = calculate_immunization_window(
        tasks_case,
        anchor_date,
        immunization_product,
        all_immunization_products,
        ledger_values
    )

    return start_date is not None and today >= start_date and today <= end_date
Example #3
0
def person_case_is_under_n_years_old(case, now, n_years):
    """
    This custom criteria is fine to use with auto case update rules because
    those get run every day so the rule will be responsive to changes in today's date.
    For an auto case update rule, you actually wouldn't even need this custom
    criteria because you could just use one of the date criteria for the rule
    out of the box.

    But for rules that spawn conditional SMS alerts (i.e., those with
    workflow=WORKFLOW_SCHEDULING), we need to be careful about using criteria
    that reference today's date, because those don't run every day - they
    only run when the case changes. So those rules can't be responsive to changes in
    today's date, and that's why date criteria are not allowed in rules that
    spawn SMS alerts.

    For this criteria specifically, that means that you shouldn't use it to
    spawn an alert that repeats for a long period of time because the person
    might not be under N years of age for the entire duration of the schedule,
    or the schedule would stop erratically at the first case update that happens
    after the person's Nth birthday.

    But if you're just using this specific criteria to spawn a one-time alert
    that sends only when it first matches the case, it can be ok. And that only
    works because we're checking that today's date is less than some fixed value.
    If we were checking that today's date is greater than some fixed value, it
    likely wouldn't produce the desired behavior.
    """
    if case.type != 'person':
        return False

    try:
        dob = get_date(case.get_case_property('dob'))
    except Exception:
        return False

    return todays_date(now) < (dob + relativedelta(years=n_years))
Example #4
0
 def todays_date_as_str(self):
     return todays_date(datetime.utcnow()).strftime('%Y-%m-%d')
Example #5
0
 def test_todays_date(self):
     # Test the boundary between today and tomorrow IST, expressed in UTC timestamps
     self.assertEqual(todays_date(datetime(2018, 2, 22, 18, 29)),
                      date(2018, 2, 22))
     self.assertEqual(todays_date(datetime(2018, 2, 22, 18, 30)),
                      date(2018, 2, 23))
Example #6
0
    But if you're just using this specific criteria to spawn a one-time alert
    that sends only when it first matches the case, it can be ok. And that only
    works because we're checking that today's date is less than some fixed value.
    If we were checking that today's date is greater than some fixed value, it
    likely wouldn't produce the desired behavior.
    """
    if case.type != 'person':
        return False

    try:
        dob = get_date(case.get_case_property('dob'))
    except Exception:
        return False

    return todays_date(now) < (dob + relativedelta(years=n_years))


def ccs_record_case_has_future_edd(case, now):
    """
    NOTE: This criteria references today's date.
    Use this custom criteria with caution for SMS alerts.
    See `person_case_is_under_n_years_old` for explanation.
    """
    if case.type != 'ccs_record':
        return False

    try:
        edd = get_date(case.get_case_property('edd'))
    except Exception:
        return False
    But if you're just using this specific criteria to spawn a one-time alert
    that sends only when it first matches the case, it can be ok. And that only
    works because we're checking that today's date is less than some fixed value.
    If we were checking that today's date is greater than some fixed value, it
    likely wouldn't produce the desired behavior.
    """
    if case.type != 'person':
        return False

    try:
        dob = get_date(case.get_case_property('dob'))
    except:
        return False

    return todays_date(now) < (dob + relativedelta(years=6))


def check_user_location_type(usercase, location_type_code):
    user = get_user_from_usercase(usercase)
    if user and user.location:
        return user.location.location_type.code == location_type_code

    return False


def is_usercase_of_aww(case, now):
    return case.type == USERCASE_TYPE and check_user_location_type(case, AWC_LOCATION_TYPE_CODE)


def is_usercase_of_ls(case, now):