def test_casedb_already_has_cases(self): case = CaseFactory().create_case() case_db = CaseDbCache(initial=[CommCareCase(_id='fake1'), CommCareCase(_id='fake2')]) form = XFormInstance.get(case.xform_ids[0]) def assert_exactly_one_case(sender, xform, cases, **kwargs): global case_count case_count = len(cases) cases_received.connect(assert_exactly_one_case) try: process_cases_with_casedb([form], case_db) self.assertEqual(1, case_count) finally: cases_received.disconnect(assert_exactly_one_case)
def test_casedb_already_has_cases(self): casedb_cache = FormProcessorInterface().casedb_cache case = CaseFactory().create_case() case_db = casedb_cache(initial=[CommCareCase(_id='fake1'), CommCareCase(_id='fake2')]) form = XFormInstance.get(case.xform_ids[0]) def assert_exactly_one_case(sender, xform, cases, **kwargs): global case_count case_count = len(cases) cases_received.connect(assert_exactly_one_case) try: process_cases_with_casedb([form], case_db) self.assertEqual(1, case_count) finally: cases_received.disconnect(assert_exactly_one_case)
def test_casedb_already_has_cases(self): casedb_cache = FormProcessorInterface().casedb_cache case = CaseFactory().create_case() case_db = casedb_cache(initial=[ CommCareCase(case_id='fake1'), CommCareCase(case_id='fake2'), ]) form = XFormInstance.objects.get_form(case.xform_ids[0]) received = [] def receive_cases(sender, xform, cases, **kwargs): received.extend(cases) cases_received.connect(receive_cases) try: process_cases_with_casedb([form], case_db) self.assertEqual(len(received), 1) finally: cases_received.disconnect(receive_cases)
registration_date = forms[index][1] status = "hidden" if index < len(forms) - 1 or immunized else "eligible" _create_mcct_status_row(form._id, status, form.domain, form.received_on.date(), registration_date, False, False, False) def handle_m4change_forms(sender, xform, cases, **kwargs): if hasattr(xform, "domain") and xform.domain in M4CHANGE_DOMAINS and hasattr(xform, "xmlns"): if xform.xmlns in ALL_M4CHANGE_FORMS: _handle_duplicate_form(xform, cases) if xform.xmlns in BOOKING_FORMS + BOOKED_DELIVERY_FORMS + UNBOOKED_DELIVERY_FORMS +\ IMMUNIZATION_FORMS + FOLLOW_UP_FORMS: _filter_forms(xform, cases) cases_received.connect(handle_m4change_forms) def handle_fixture_location_update(sender, doc, diff, backend, **kwargs): if doc.get('doc_type') == 'XFormInstance' and doc.get('domain') in M4CHANGE_DOMAINS: xform = XFormInstance.wrap(doc) if hasattr(xform, "xmlns") and xform.xmlns in ALL_M4CHANGE_FORMS: location_id = xform.form.get("location_id", None) if not location_id: return client = get_redis_client() redis_key = REDIS_FIXTURE_KEYS[xform.domain] redis_lock_key = REDIS_FIXTURE_LOCK_KEYS[xform.domain] lock = client.lock(redis_lock_key, timeout=5) if lock.acquire(blocking=True): try:
_id = SYSTEM_USERID def bihar_reassignment(sender, xform, cases, **kwargs): if hasattr(xform, 'domain') and xform.domain in BIHAR_DOMAINS and xform.metadata and xform.metadata.userID != SYSTEM_USERID: owner_ids = set(c.owner_id for c in cases) if len(owner_ids) != 1: logging.warning('form {form} had mismatched case owner ids'.format(form=xform._id)) else: [owner_id] = owner_ids if owner_id not in DEMO_OWNER_IDS: # don't attempt to reassign the cases included in this form cases_not_to_touch = set(c._id for c in cases) def bihar_exclude(case): return case._id in cases_not_to_touch or case.type in REASSIGN_BLACKLIST for case in cases: if case.type in ('cc_bihar_pregnancy', 'cc_bihar_newborn'): reassigned = assign_case( case, owner_id, BiharMockUser(), include_subcases=True, include_parent_cases=True, exclude_function=bihar_exclude, update={'reassignment_form_id': xform._id} ) # update the list of cases not to touch so we don't reassign the same # cases multiple times in the same form cases_not_to_touch = cases_not_to_touch | set(reassigned or []) cases_received.connect(bihar_reassignment)
from casexml.apps.case.signals import cases_received from custom.succeed.utils import SUCCEED_DOMAIN, update_patient_target_dates def update_patient_cases(sender, xform, cases, **kwargs): for case in cases: if case.domain == SUCCEED_DOMAIN and case.type == 'participant': update_patient_target_dates(case) cases_received.connect(update_patient_cases)
if requisition_cases and requisition_cases[0].requisition_status == RequisitionStatus.RECEIVED: requisition_receipt.send(sender=None, requisitions=requisition_cases) if requisition_cases and requisition_cases[0].requisition_status == RequisitionStatus.REQUESTED: requisition_modified.send(sender=None, cases=requisition_cases) def raise_supply_point_events(xform, cases): supply_points = [SupplyPointCase.wrap(c._doc) for c in cases if c.type == const.SUPPLY_POINT_CASE_TYPE] case_updates = get_case_updates(xform) for sp in supply_points: created = any(filter(lambda update: update.id == sp._id and update.creates_case(), case_updates)) supply_point_modified.send(sender=None, supply_point=sp, created=created) def supply_point_processing(sender, xform, cases, **kwargs): if is_supply_point_form(xform): attach_locations(xform, cases) raise_supply_point_events(xform, cases) cases_received.connect(supply_point_processing) def bootstrap_commtrack_settings_if_necessary_signal(sender, **kwargs): bootstrap_commtrack_settings_if_necessary(kwargs["domain"]) commcare_domain_post_save.connect(bootstrap_commtrack_settings_if_necessary_signal) successful_form_received.connect(process_stock_signal_catcher)
registration_date = forms[index][1] status = "hidden" if index < len(forms) - 1 or immunized else "eligible" _create_mcct_status_row(form._id, status, form.domain, form.received_on.date(), registration_date, False, False) def handle_m4change_forms(sender, xform, cases, **kwargs): if hasattr(xform, "domain") and xform.domain in M4CHANGE_DOMAINS and hasattr(xform, "xmlns"): if xform.xmlns in ALL_M4CHANGE_FORMS: _handle_duplicate_form(xform, cases) if xform.xmlns in BOOKING_FORMS + BOOKED_DELIVERY_FORMS + UNBOOKED_DELIVERY_FORMS +\ IMMUNIZATION_FORMS + FOLLOW_UP_FORMS: _filter_forms(xform, cases) cases_received.connect(handle_m4change_forms) def handle_fixture_update(sender, xform, cases, **kwargs): if hasattr(xform, "domain") and xform.domain == TEST_DOMAIN\ and hasattr(xform, "xmlns") and xform.xmlns in ALL_M4CHANGE_FORMS: db = FixtureReportResult.get_db() data_source = M4ChangeReportDataSource() date_range = get_last_month() location_id = get_user_by_id(xform.form['meta']['userID']).get_domain_membership(xform.domain).location_id results_for_last_month = FixtureReportResult.get_report_results_by_key(domain=xform.domain, location_id=location_id, start_date=date_range[0].strftime("%Y-%m-%d"), end_date=date_range[1].strftime("%Y-%m-%d")) db.delete_docs(results_for_last_month)
if next_action: # we could make this even more customizable by specifying it per requisition # but that would get even messier in terms of constructing the messages # so we'll just compose one message per status type now, and then send # it to everyone who should be notified. to_notify = filter( create_unique_filter(lambda u: u._id), itertools.chain(*(get_notification_recipients(next_action, r) for r in reqs)) ) msg = get_notification_message(next_action, reqs) for u in to_notify: phone = u.get_verified_number() if phone: send_sms_to_verified_number(phone, msg) def commtrack_processing(sender, xform, cases, **kwargs): if is_commtrack_form(xform): attach_locations(xform, cases) send_notifications(xform, cases) cases_received.connect(commtrack_processing) def bootstrap_commtrack_settings_if_necessary_signal(sender, **kwargs): bootstrap_commtrack_settings_if_necessary(kwargs['domain']) commcare_domain_post_save.connect(bootstrap_commtrack_settings_if_necessary_signal)
SupplyPointCase.wrap(c._doc) for c in cases if c.type == const.SUPPLY_POINT_CASE_TYPE ] case_updates = get_case_updates(xform) for sp in supply_points: created = any( filter( lambda update: update.id == sp._id and update.creates_case(), case_updates)) supply_point_modified.send(sender=None, supply_point=sp, created=created) def supply_point_processing(sender, xform, cases, **kwargs): if is_supply_point_form(xform): attach_locations(xform, cases) raise_supply_point_events(xform, cases) cases_received.connect(supply_point_processing) def bootstrap_commtrack_settings_if_necessary_signal(sender, **kwargs): bootstrap_commtrack_settings_if_necessary(kwargs['domain']) commcare_domain_post_save.connect( bootstrap_commtrack_settings_if_necessary_signal) successful_form_received.connect(process_stock_signal_catcher)
# since each state transition might trigger a different person to be notified for s, reqs in by_status.items(): next_action = req_config.get_next_action( RequisitionStatus.to_action_type(s)) if next_action: # we could make this even more customizable by specifying it per requisition # but that would get even messier in terms of constructing the messages # so we'll just compose one message per status type now, and then send # it to everyone who should be notified. to_notify = filter( create_unique_filter(lambda u: u._id), itertools.chain( *(get_notification_recipients(next_action, r) for r in reqs))) msg = get_notification_message(next_action, reqs) for u in to_notify: phone = u.get_verified_number() if phone: send_sms_to_verified_number(phone, msg) def commtrack_processing(sender, xform, cases, **kwargs): if is_commtrack_form(xform): attach_locations(xform, cases) send_notifications(xform, cases) cases_received.connect(commtrack_processing)