def testSignal(self): global archive_counter, restore_counter archive_counter = 0 restore_counter = 0 def count_archive(**kwargs): global archive_counter archive_counter += 1 def count_unarchive(**kwargs): global restore_counter restore_counter += 1 xform_archived.connect(count_archive) xform_unarchived.connect(count_unarchive) form = XFormInstance(form={'foo': 'bar'}) form.save() self.assertEqual(0, archive_counter) self.assertEqual(0, restore_counter) form.archive() self.assertEqual(1, archive_counter) self.assertEqual(0, restore_counter) form.unarchive() self.assertEqual(1, archive_counter) self.assertEqual(1, restore_counter)
class StockReportDomainTest(TestCase): def _get_name_for_domain(self): return ''.join( random.choice(string.ascii_lowercase) for _ in range(DOMAIN_MAX_LENGTH)) def setUp(self): self.domain = self._get_name_for_domain() self.form = XFormInstance(domain=self.domain) self.form.save() self.new_stock_report = NewStockReport( self.form, datetime.now(), REPORT_TYPE_BALANCE, [], ) def tearDown(self): self.form.delete() StockReport.objects.all().delete() def test_stock_report(self): self.new_stock_report.create_models() filtered_stock_report = StockReport.objects.filter(domain=self.domain) self.assertEquals(filtered_stock_report.count(), 1) stock_report = filtered_stock_report.get() self.assertEquals(stock_report.form_id, self.form._id) self.assertEquals(stock_report.domain, self.domain)
def testArchive(self): form = XFormInstance(form={'foo': 'bar'}) form.save() self.assertEqual("XFormInstance", form.doc_type) self.assertEqual(0, len(form.history)) lower_bound = datetime.utcnow() - timedelta(seconds=1) form.archive(user='******') upper_bound = datetime.utcnow() + timedelta(seconds=1) form = fetch_and_wrap_form(form._id) self.assertEqual('XFormArchived', form.doc_type) self.assertTrue(isinstance(form, XFormArchived)) [archival] = form.history self.assertTrue(lower_bound <= archival.date <= upper_bound) self.assertEqual('archive', archival.operation) self.assertEqual('mr. librarian', archival.user) lower_bound = datetime.utcnow() - timedelta(seconds=1) form.unarchive(user='******') upper_bound = datetime.utcnow() + timedelta(seconds=1) form = fetch_and_wrap_form(form._id) self.assertEqual('XFormInstance', form.doc_type) self.assertTrue(isinstance(form, XFormInstance)) [archival, restoration] = form.history self.assertTrue(lower_bound <= restoration.date <= upper_bound) self.assertEqual('unarchive', restoration.operation) self.assertEqual('mr. researcher', restoration.user)
def growth_monitoring_forms_context(): forms_data = [ { 'child_first_name': 'Foo', 'dhis2_te_inst_id': '', # Not enrolled 'dhis2_processed': '' # Not processed }, { 'child_first_name': 'Bar', 'dhis2_te_inst_id': '123', # Enrolled 'dhis2_processed': '' # Not processed }, { 'child_first_name': 'Baz', 'dhis2_te_inst_id': '456', # Enrolled 'dhis2_processed': '' # Not processed } ] forms = [] for data in forms_data: form = XFormInstance(domain=DOMAIN, form=data) form.save() forms.append(form) yield forms
class StockReportDomainTest(TestCase): def _get_name_for_domain(self): return ''.join( random.choice(string.ascii_lowercase) for _ in range(DOMAIN_MAX_LENGTH) ) def setUp(self): self.domain = self._get_name_for_domain() self.form = XFormInstance(domain=self.domain) self.form.save() self.new_stock_report = NewStockReport( self.form, datetime.now(), REPORT_TYPE_BALANCE, [], ) def tearDown(self): self.form.delete() StockReport.objects.all().delete() def test_stock_report(self): self.new_stock_report.create_models() filtered_stock_report = StockReport.objects.filter(domain=self.domain) self.assertEquals(filtered_stock_report.count(), 1) stock_report = filtered_stock_report.get() self.assertEquals(stock_report.form_id, self.form._id) self.assertEquals(stock_report.domain, self.domain)
def _send_forms(self, forms): # list of form tuples [(xmlns, received_on)] to_ret = [] for xmlns, received_on in forms: backend_form = XFormInstance( xmlns=xmlns or 'fake-xmlns', domain=self.domain.name, received_on=received_on or datetime.utcnow(), edited_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': xmlns or 'fake-xmlns', 'meta': { 'userID': 'metadata-user-id' }, }, auth_context={ 'user_id': 'auth-user-id', 'domain': self.domain.name, 'authenticated': True, }, ) backend_form.save() to_ret.append(backend_form) self.addCleanup(backend_form.delete) send_to_elasticsearch( 'forms', transform_xform_for_elasticsearch(backend_form.to_json())) self.es.indices.refresh(XFORM_INDEX_INFO.index) return to_ret
def get_stock_transaction(domain, endpoint, facilities, checkpoint, date, limit=100, offset=0): # Faking xform try: xform = XFormInstance.get(docid='ilsgateway-xform') except ResourceNotFound: xform = XFormInstance(_id='ilsgateway-xform') xform.save() for facility in facilities: sync_stock_transaction(domain, endpoint, facility, xform, checkpoint, date, limit, offset) offset = 0
def sync_stock_transactions(domain, endpoint, facilities, checkpoint, date, limit=100, offset=0): # todo: should figure out whether there's a better thing to be doing than faking this global form try: xform = XFormInstance.get(docid='ilsgateway-xform') except ResourceNotFound: xform = XFormInstance(_id='ilsgateway-xform') xform.save() for facility in facilities: sync_stock_transactions_for_facility(domain, endpoint, facility, xform, checkpoint, date, limit, offset) offset = 0 # reset offset for each facility, is only set in the context of a checkpoint resume
def saving(request): xform = XFormInstance(_attachments={'form.xml': {'data': '-'}}) xform.save() case = CommCareCase() case.save() xform.initial_processing_complete = True xform.save() case.delete() xform.delete() return HttpResponse('Thanks for submitting', status=201)
def create_report(self, transactions=None, tag=None, date=None): form = XFormInstance(domain=self.domain) form.save() report = StockReportHelper( form, date or datetime.utcnow(), tag or REPORT_TYPE_BALANCE, transactions or [], ) return report, form
def create_report(self, transactions=None, tag=None, date=None): form = XFormInstance(domain=self.domain) form.save() report = NewStockReport( form, date or datetime.utcnow(), tag or REPORT_TYPE_BALANCE, transactions or [], ) return report, form
def setUpClass(cls): domain = prepare_domain(TEST_DOMAIN) p = Product(domain=domain.name, name='Jadelle', code='jd', unit='each') p.save() p2 = Product(domain=domain.name, name='Male Condom', code='mc', unit='each') p2.save() p3 = Product(domain=domain.name, name='Lofem', code='lf', unit='each') p3.save() p4 = Product(domain=domain.name, name='Ng', code='ng', unit='each') p4.save() p5 = Product(domain=domain.name, name='Micro-G', code='mg', unit='each') p5.save() loc = make_loc(code="garms", name="Test RMS", type="Regional Medical Store", domain=domain.name) test.bootstrap(TEST_BACKEND, to_console=True) cls.user1 = bootstrap_user(username='******', first_name='test1', last_name='test1', domain=domain.name, home_loc=loc) cls.user2 = bootstrap_user(username='******', domain=domain.name, home_loc=loc, first_name='test2', last_name='test2', phone_number='222222', user_data={'role': 'In Charge'}) try: XFormInstance.get(docid='test-xform') except ResourceNotFound: xform = XFormInstance(_id='test-xform') xform.save() sql_location = loc.sql_location sql_location.products = SQLProduct.objects.filter(product_id=p5.get_id) sql_location.save() config = CommtrackConfig.for_domain(domain.name) config.actions.append( CommtrackActionConfig(action='receipts', keyword='rec', caption='receipts')) config.consumption_config = ConsumptionConfig(min_transactions=0, min_window=0, optimal_window=60) config.save()
def test_get_docs_in_domain_by_class(self): commtrack_config = CommtrackConfig(domain=self.domain) group = Group(domain=self.domain) xform = XFormInstance(domain=self.domain) commtrack_config.save() group.save() xform.save() self.addCleanup(commtrack_config.delete) self.addCleanup(group.delete) self.addCleanup(xform.delete) [commtrack_config_2] = get_docs_in_domain_by_class(self.domain, CommtrackConfig) self.assertEqual(commtrack_config_2.to_json(), commtrack_config.to_json())
def test_get_doc_ids_in_domain_by_class(self): user_role = UserRole(domain=self.domain) group = Group(domain=self.domain) xform = XFormInstance(domain=self.domain) user_role.save() group.save() xform.save() self.addCleanup(user_role.delete) self.addCleanup(group.delete) self.addCleanup(xform.delete) [doc_id] = get_doc_ids_in_domain_by_class(self.domain, UserRole) self.assertEqual(doc_id, user_role.get_id)
def test_couch_filter(self): pillow = _make_couch_pillow(self.couch_db) pillow.couch_filter = 'couchforms/xforms' # save a random doc, then a form-looking thing self.couch_db.save_doc({'_id': uuid.uuid4().hex, 'property': 'property_value'}) form = XFormInstance(domain='test-domain') form.save() pillow.process_changes(since=self.update_seq, forever=False) change = self._extract_change_from_call_args(pillow.process_change.call_args) self.assertEqual(form._id, change['id']) self.assertEqual(form.domain, change['doc']['domain'])
def test_get_list(self): """ Any form in the appropriate domain should be in the list from the API. """ # The actual infrastructure involves saving to CouchDB, having PillowTop # read the changes and write it to ElasticSearch. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. # the pillow is set to offline mode - elasticsearch not needed to validate fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance( xmlns='fake-xmlns', domain=self.domain.name, received_on=datetime.utcnow(), edited_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': 'fake-xmlns', 'meta': { 'userID': 'metadata-user-id' }, }, auth_context={ 'user_id': 'auth-user-id', 'domain': self.domain.name, 'authenticated': True, }, ) backend_form.save() self.addCleanup(backend_form.delete) translated_doc = transform_xform_for_elasticsearch( backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) response = self._assert_auth_get_resource(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = json.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], json_format_datetime(backend_form.received_on)) self.assertEqual(api_form['metadata']['userID'], 'metadata-user-id') self.assertEqual(api_form['edited_by_user_id'], 'auth-user-id')
def test_get_list(self): """ Any form in the appropriate domain should be in the list from the API. """ # The actual infrastructure involves saving to CouchDB, having PillowTop # read the changes and write it to ElasticSearch. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. # the pillow is set to offline mode - elasticsearch not needed to validate fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance( xmlns='fake-xmlns', domain=self.domain.name, received_on=datetime.utcnow(), edited_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': 'fake-xmlns', 'meta': {'userID': 'metadata-user-id'}, }, auth_context={ 'user_id': 'auth-user-id', 'domain': self.domain.name, 'authenticated': True, }, ) backend_form.save() self.addCleanup(backend_form.delete) translated_doc = transform_xform_for_elasticsearch(backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) response = self._assert_auth_get_resource(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = json.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], json_format_datetime(backend_form.received_on)) self.assertEqual(api_form['metadata']['userID'], 'metadata-user-id') self.assertEqual(api_form['edited_by_user_id'], 'auth-user-id')
def testTransferToDomain(self): new_domain = 'my-new-domain' xform = XFormInstance( domain=self.domain, app_id="old-app-id", form=dict(meta=dict(userID=self.couch_user.user_id))) xform.save() self.assertEqual(self.couch_user.form_count, 1) self.couch_user.transfer_to_domain(new_domain, app_id="new-app-id") self.assertEqual(self.couch_user.form_count, 1) self.assertEqual(self.couch_user.domain, new_domain) self.assertEqual(self.couch_user.username, self.username.replace(self.domain, new_domain)) for form in self.couch_user.get_forms(): self.assertEqual(form.domain, new_domain) self.assertEqual(form.app_id, 'new-app-id')
def get_stock_transaction(domain, endpoint): # Faking xform try: xform = XFormInstance.get(docid='ilsgateway-xform') except ResourceNotFound: xform = XFormInstance(_id='ilsgateway-xform') xform.save() for facility in FACILITIES: has_next = True next_url = "" while has_next: meta, stocktransactions = endpoint.get_stocktransactions(next_url_params=next_url, filters=(dict(supply_point=facility, order_by='date'))) for stocktransaction in stocktransactions: case = SupplyPointCase.view('hqcase/by_domain_external_id', key=[domain, str(stocktransaction.supply_point_id)], reduce=False, include_docs=True, limit=1).first() product = Product.get_by_code(domain, stocktransaction.product_code) try: StockTransaction.objects.get(case_id=case._id, product_id=product._id, report__date=force_to_datetime(stocktransaction.date), stock_on_hand=Decimal(stocktransaction.ending_balance), type='stockonhand', report__domain=domain) except StockTransaction.DoesNotExist: r = StockReport.objects.create(form_id=xform._id, date=force_to_datetime(stocktransaction.date), type='balance', domain=domain) StockTransaction.objects.create(report=r, section_id='stock', case_id=case._id, product_id=product._id, type='stockonhand', stock_on_hand=Decimal(stocktransaction.ending_balance)) if not meta.get('next', False): has_next = False else: next_url = meta['next'].split('?')[1]
def test_get_list(self): """ Any form in the appropriate domain should be in the list from the API. """ # The actual infrastructure involves saving to CouchDB, having PillowTop # read the changes and write it to ElasticSearch. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. #the pillow is set to offline mode - elasticsearch not needed to validate pillow = XFormPillow(online=False) fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance(xmlns='fake-xmlns', domain=self.domain.name, received_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': 'fake-xmlns' }) backend_form.save() translated_doc = pillow.change_transform(backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) self.client.login(username=self.username, password=self.password) response = self.client.get(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = simplejson.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], backend_form.received_on.isoformat()) backend_form.delete()
def test_get_list(self): """ Any form in the appropriate domain should be in the list from the API. """ # The actual infrastructure involves saving to CouchDB, having PillowTop # read the changes and write it to ElasticSearch. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. #the pillow is set to offline mode - elasticsearch not needed to validate pillow = XFormPillow(online=False) fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance(xmlns = 'fake-xmlns', domain = self.domain.name, received_on = datetime.utcnow(), form = { '#type': 'fake-type', '@xmlns': 'fake-xmlns' }) backend_form.save() translated_doc = pillow.change_transform(backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) self.client.login(username=self.username, password=self.password) response = self.client.get(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = simplejson.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], backend_form.received_on.isoformat()) backend_form.delete()
def testTransferToDomain(self): new_domain = 'my-new-domain' xform = XFormInstance( domain=self.domain, app_id="old-app-id", form=dict( meta=dict( userID=self.couch_user.user_id ) ) ) xform.save() self.assertEqual(self.couch_user.form_count, 1) self.couch_user.transfer_to_domain(new_domain, app_id="new-app-id") self.assertEqual(self.couch_user.form_count, 1) self.assertEqual(self.couch_user.domain, new_domain) self.assertEqual(self.couch_user.username, self.username.replace(self.domain, new_domain)) for form in self.couch_user.get_forms(): self.assertEqual(form.domain, new_domain) self.assertEqual(form.app_id, 'new-app-id')
def test_couch_filter(self): pillow = _make_couch_pillow(self.couch_db) pillow.couch_filter = 'couchforms/xforms' # save a random doc, then a form-looking thing self.couch_db.save_doc({ '_id': uuid.uuid4().hex, 'property': 'property_value' }) form = XFormInstance(domain='test-domain') form.save() pillow.process_changes(since=self.update_seq) changes = self._extract_changes_from_call_args( pillow.process_change.call_args_list) change_ids = {change['id'] for change in changes} change_domains = { change['doc'].get('domain', None) for change in changes } self.assertIn(form._id, change_ids) self.assertIn(form.domain, change_domains)
def setUpClass(cls): domain = prepare_domain(TEST_DOMAIN) p = Product(domain=domain.name, name='Jadelle', code='jd', unit='each') p.save() p2 = Product(domain=domain.name, name='Male Condom', code='mc', unit='each') p2.save() p3 = Product(domain=domain.name, name='Lofem', code='lf', unit='each') p3.save() p4 = Product(domain=domain.name, name='Ng', code='ng', unit='each') p4.save() p5 = Product(domain=domain.name, name='Micro-G', code='mg', unit='each') p5.save() loc = make_loc(code="garms", name="Test RMS", type="Regional Medical Store", domain=domain.name) test.bootstrap(TEST_BACKEND, to_console=True) cls.user1 = bootstrap_user(username='******', first_name='test1', last_name='test1', domain=domain.name, home_loc=loc) cls.user2 = bootstrap_user(username='******', domain=domain.name, home_loc=loc, first_name='test2', last_name='test2', phone_number='222222', user_data={'role': 'In Charge'}) try: XFormInstance.get(docid='test-xform') except ResourceNotFound: xform = XFormInstance(_id='test-xform') xform.save() sql_location = loc.sql_location sql_location.products = SQLProduct.objects.filter(product_id=p5.get_id) sql_location.save() config = CommtrackConfig.for_domain(domain.name) config.actions.append( CommtrackActionConfig( action='receipts', keyword='rec', caption='receipts' ) ) config.consumption_config = ConsumptionConfig(min_transactions=0, min_window=0, optimal_window=60) config.save()
def setUpClass(cls): cls.backend, cls.sms_backend_mapping = setup_default_sms_test_backend() domain = prepare_domain(TEST_DOMAIN) p = Product(domain=domain.name, name="Jadelle", code="jd", unit="each") p.save() p2 = Product(domain=domain.name, name="Male Condom", code="mc", unit="each") p2.save() p3 = Product(domain=domain.name, name="Lofem", code="lf", unit="each") p3.save() p4 = Product(domain=domain.name, name="Ng", code="ng", unit="each") p4.save() p5 = Product(domain=domain.name, name="Micro-G", code="mg", unit="each") p5.save() Product(domain=domain.name, name="Ad", code="ad", unit="each").save() Product(domain=domain.name, name="Al", code="al", unit="each").save() Product(domain=domain.name, name="Qu", code="qu", unit="each").save() Product(domain=domain.name, name="Sp", code="sp", unit="each").save() Product(domain=domain.name, name="Rd", code="rd", unit="each").save() Product(domain=domain.name, name="Ov", code="ov", unit="each").save() Product(domain=domain.name, name="Ml", code="ml", unit="each").save() national = make_loc(code="country", name="Test national", type="country", domain=domain.name) region = make_loc(code="region", name="Test region", type="region", domain=domain.name, parent=national) loc = make_loc( code="garms", name="Test RMS", type="Regional Medical Store", domain=domain.name, parent=national ) loc.save() rms2 = make_loc( code="wrms", name="Test RMS 2", type="Regional Medical Store", domain=domain.name, parent=region ) rms2.save() cms = make_loc( code="cms", name="Central Medical Stores", type="Central Medical Store", domain=domain.name, parent=national ) cms.save() loc2 = make_loc(code="tf", name="Test Facility", type="CHPS Facility", domain=domain.name, parent=region) loc2.save() supply_point_id = loc.linked_supply_point().get_id supply_point_id2 = loc2.linked_supply_point().get_id cls.user1 = bootstrap_user( username="******", first_name="test1", last_name="test1", domain=domain.name, home_loc=loc ) cls.user2 = bootstrap_user( username="******", domain=domain.name, home_loc=loc2, first_name="test2", last_name="test2", phone_number="222222", user_data={"role": ["In Charge"]}, ) FacilityInCharge.objects.create(user_id=cls.user2.get_id, location=loc2.sql_location) cls.user3 = bootstrap_user( username="******", domain=domain.name, home_loc=loc2, first_name="test3", last_name="test3", phone_number="333333", ) cls.rms_user = bootstrap_user( username="******", domain=domain.name, home_loc=rms2, first_name="test4", last_name="test4", phone_number="44444", ) cls.cms_user = bootstrap_user( username="******", domain=domain.name, home_loc=cms, first_name="test5", last_name="test5", phone_number="55555", ) cls.region_user = bootstrap_user( username="******", domain=domain.name, home_loc=region, first_name="test6", last_name="test6", phone_number="66666", ) cls.without_location = bootstrap_user( username="******", domain=domain.name, first_name="test7", last_name="test7", phone_number="77777" ) try: XFormInstance.get(docid="test-xform") except ResourceNotFound: xform = XFormInstance(_id="test-xform") xform.save() sql_location = loc.sql_location sql_location.products = [] sql_location.save() sql_location = loc2.sql_location sql_location.products = [] sql_location.save() sql_location = rms2.sql_location sql_location.products = [] sql_location.save() sql_location = cms.sql_location sql_location.products = [] sql_location.save() config = CommtrackConfig.for_domain(domain.name) config.use_auto_consumption = False config.individual_consumption_defaults = True config.actions.append(CommtrackActionConfig(action="receipts", keyword="rec", caption="receipts")) config.consumption_config = ConsumptionConfig( use_supply_point_type_default_consumption=True, exclude_invalid_periods=True ) config.save() set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id, 8) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id, 5) set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p5.get_id, supply_point_id2, 10)
def setUpClass(cls): super(EWSScriptTest, cls).setUpClass() cls.backend, cls.sms_backend_mapping = setup_default_sms_test_backend() domain = prepare_domain(TEST_DOMAIN) p = Product(domain=domain.name, name='Jadelle', code='jd', unit='each') p.save() p2 = Product(domain=domain.name, name='Male Condom', code='mc', unit='each') p2.save() p3 = Product(domain=domain.name, name='Lofem', code='lf', unit='each') p3.save() p4 = Product(domain=domain.name, name='Ng', code='ng', unit='each') p4.save() p5 = Product(domain=domain.name, name='Micro-G', code='mg', unit='each') p5.save() Product(domain=domain.name, name='Ad', code='ad', unit='each').save() Product(domain=domain.name, name='Al', code='al', unit='each').save() Product(domain=domain.name, name='Qu', code='qu', unit='each').save() Product(domain=domain.name, name='Sp', code='sp', unit='each').save() Product(domain=domain.name, name='Rd', code='rd', unit='each').save() Product(domain=domain.name, name='Ov', code='ov', unit='each').save() Product(domain=domain.name, name='Ml', code='ml', unit='each').save() national = make_loc(code='country', name='Test national', type='country', domain=domain.name) region = make_loc(code='region', name='Test region', type='region', domain=domain.name, parent=national) loc = make_loc(code="garms", name="Test RMS", type="Regional Medical Store", domain=domain.name, parent=national) loc.save() rms2 = make_loc(code="wrms", name="Test RMS 2", type="Regional Medical Store", domain=domain.name, parent=region) rms2.save() cms = make_loc(code="cms", name="Central Medical Stores", type="Central Medical Store", domain=domain.name, parent=national) cms.save() loc2 = make_loc(code="tf", name="Test Facility", type="CHPS Facility", domain=domain.name, parent=region) loc2.save() supply_point_id = loc.linked_supply_point().get_id supply_point_id2 = loc2.linked_supply_point().get_id cls.user1 = bootstrap_user(username='******', first_name='test1', last_name='test1', domain=domain.name, home_loc=loc) cls.user2 = bootstrap_user(username='******', domain=domain.name, home_loc=loc2, first_name='test2', last_name='test2', phone_number='222222', user_data={'role': ['In Charge']}) FacilityInCharge.objects.create(user_id=cls.user2.get_id, location=loc2.sql_location) cls.user3 = bootstrap_user(username='******', domain=domain.name, home_loc=loc2, first_name='test3', last_name='test3', phone_number='333333') cls.rms_user = bootstrap_user(username='******', domain=domain.name, home_loc=rms2, first_name='test4', last_name='test4', phone_number='44444') cls.cms_user = bootstrap_user(username='******', domain=domain.name, home_loc=cms, first_name='test5', last_name='test5', phone_number='55555') cls.region_user = bootstrap_user(username='******', domain=domain.name, home_loc=region, first_name='test6', last_name='test6', phone_number='66666') cls.without_location = bootstrap_user(username='******', domain=domain.name, first_name='test7', last_name='test7', phone_number='77777') try: XFormInstance.get(docid='test-xform') except ResourceNotFound: xform = XFormInstance(_id='test-xform') xform.save() sql_location = loc.sql_location sql_location.products = [] sql_location.save() sql_location = loc2.sql_location sql_location.products = [] sql_location.save() sql_location = rms2.sql_location sql_location.products = [] sql_location.save() sql_location = cms.sql_location sql_location.products = [] sql_location.save() config = CommtrackConfig.for_domain(domain.name) config.use_auto_consumption = False config.individual_consumption_defaults = True config.actions.append( CommtrackActionConfig(action='receipts', keyword='rec', caption='receipts')) config.consumption_config = ConsumptionConfig( use_supply_point_type_default_consumption=True, exclude_invalid_periods=True) config.save() set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id, 8) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id, 5) set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p5.get_id, supply_point_id2, 10)
def set_archived_state(form, archive, user_id): operation = "archive" if archive else "unarchive" form.doc_type = "XFormArchived" if archive else "XFormInstance" form.history.append(XFormOperation(operation=operation, user=user_id)) # subclasses explicitly set the doc type so force regular save XFormInstance.save(form)
save and case_post_save.send(case.__class__, case=case) for ledger in ledgers: if ledger.ledger_reference in ledgers_updated: logger.info('Rebuilding ledger: %s', ledger.ledger_reference) if save: # only rebuild upated ledgers interface.ledger_processor.hard_rebuild_ledgers( **ledger.ledger_reference._asdict()) else: if save: with bulk_atomic_blobs([form] + cases): XFormInstance.save( form ) # use this save to that we don't overwrite the doc_type XFormInstance.get_db().bulk_save(cases) stock_result.commit() save and case_stock_result.stock_result.finalize() save and case_stock_result.case_result.commit_dirtiness_flags() return ReprocessingResult(form, cases, ledgers) def _log_changes(slug, cases, stock_updates, stock_deletes): if logger.isEnabledFor(logging.INFO): case_ids = [case.case_id for case in cases] logger.info( "%s changes:\n\tcases: %s\n\tstock changes%s\n\tstock deletes%s",
def setUpClass(cls): domain = prepare_domain(TEST_DOMAIN) cls.sms_backend_mapping, cls.backend = create_backend() p = Product(domain=domain.name, name='Jadelle', code='jd', unit='each') p.save() p2 = Product(domain=domain.name, name='Male Condom', code='mc', unit='each') p2.save() p3 = Product(domain=domain.name, name='Lofem', code='lf', unit='each') p3.save() p4 = Product(domain=domain.name, name='Ng', code='ng', unit='each') p4.save() p5 = Product(domain=domain.name, name='Micro-G', code='mg', unit='each') p5.save() Product(domain=domain.name, name='Ad', code='ad', unit='each').save() Product(domain=domain.name, name='Al', code='al', unit='each').save() Product(domain=domain.name, name='Qu', code='qu', unit='each').save() Product(domain=domain.name, name='Sp', code='sp', unit='each').save() Product(domain=domain.name, name='Rd', code='rd', unit='each').save() Product(domain=domain.name, name='Ov', code='ov', unit='each').save() Product(domain=domain.name, name='Ml', code='ml', unit='each').save() national = make_loc(code='country', name='Test national', type='country', domain=domain.name) region = make_loc(code='region', name='Test region', type='region', domain=domain.name, parent=national) loc = make_loc(code="garms", name="Test RMS", type="Regional Medical Store", domain=domain.name, parent=national) SupplyPointCase.create_from_location(TEST_DOMAIN, loc) loc.save() rms2 = make_loc(code="wrms", name="Test RMS 2", type="Regional Medical Store", domain=domain.name, parent=region) SupplyPointCase.create_from_location(TEST_DOMAIN, rms2) rms2.save() cms = make_loc(code="cms", name="Central Medical Stores", type="Central Medical Store", domain=domain.name, parent=national) SupplyPointCase.create_from_location(TEST_DOMAIN, cms) cms.save() loc2 = make_loc(code="tf", name="Test Facility", type="CHPS Facility", domain=domain.name, parent=region) SupplyPointCase.create_from_location(TEST_DOMAIN, loc2) loc2.save() supply_point_id = loc.linked_supply_point().get_id supply_point_id2 = loc2.linked_supply_point().get_id test.bootstrap(TEST_BACKEND, to_console=True) cls.user1 = bootstrap_user(username='******', first_name='test1', last_name='test1', domain=domain.name, home_loc=loc) cls.user2 = bootstrap_user(username='******', domain=domain.name, home_loc=loc2, first_name='test2', last_name='test2', phone_number='222222', user_data={'role': ['In Charge']}) cls.user3 = bootstrap_user(username='******', domain=domain.name, home_loc=loc2, first_name='test3', last_name='test3', phone_number='333333') cls.rms_user = bootstrap_user(username='******', domain=domain.name, home_loc=rms2, first_name='test4', last_name='test4', phone_number='44444') cls.cms_user = bootstrap_user(username='******', domain=domain.name, home_loc=cms, first_name='test5', last_name='test5', phone_number='55555') cls.region_user = bootstrap_user(username='******', domain=domain.name, home_loc=region, first_name='test6', last_name='test6', phone_number='66666') cls.without_location = bootstrap_user(username='******', domain=domain.name, first_name='test7', last_name='test7', phone_number='77777') try: XFormInstance.get(docid='test-xform') except ResourceNotFound: xform = XFormInstance(_id='test-xform') xform.save() sql_location = loc.sql_location sql_location.products = [] sql_location.save() sql_location = loc2.sql_location sql_location.products = [] sql_location.save() sql_location = rms2.sql_location sql_location.products = [] sql_location.save() sql_location = cms.sql_location sql_location.products = [] sql_location.save() config = CommtrackConfig.for_domain(domain.name) config.use_auto_consumption = False config.individual_consumption_defaults = True config.actions.append( CommtrackActionConfig( action='receipts', keyword='rec', caption='receipts' ) ) config.consumption_config = ConsumptionConfig( use_supply_point_type_default_consumption=True, exclude_invalid_periods=True ) config.save() set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id, 8) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id, 5) set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p5.get_id, supply_point_id2, 10)
def reprocess_xform_error(form): """ Attempt to re-process an error form. This was created specifically to address the issue of out of order forms and child cases (form creates child case before parent case has been created). See http://manage.dimagi.com/default.asp?250459 :param form_id: ID of the error form to process """ from corehq.form_processor.interfaces.processor import FormProcessorInterface from corehq.form_processor.submission_post import SubmissionPost from corehq.form_processor.utils import should_use_sql_backend from corehq.form_processor.backends.sql.dbaccessors import CaseAccessorSQL, FormAccessorSQL, LedgerAccessorSQL from corehq.blobs.mixin import bulk_atomic_blobs from couchforms.models import XFormInstance from casexml.apps.case.signals import case_post_save from corehq.form_processor.interfaces.processor import ProcessedForms from corehq.form_processor.backends.sql.processor import FormProcessorSQL if not form: raise Exception('Form with ID {} not found'.format(form.form_id)) if not form.is_error: raise Exception('Form was not an error form: {}={}'.format(form.form_id, form.doc_type)) # reset form state prior to processing if should_use_sql_backend(form.domain): form.state = XFormInstanceSQL.NORMAL else: form.doc_type = 'XFormInstance' form.initial_processing_complete = True form.problem = None cache = FormProcessorInterface(form.domain).casedb_cache( domain=form.domain, lock=True, deleted_ok=True, xforms=[form] ) with cache as casedb: case_stock_result = SubmissionPost.process_xforms_for_cases([form], casedb) if case_stock_result: stock_result = case_stock_result.stock_result if stock_result: assert stock_result.populated cases = case_stock_result.case_models if should_use_sql_backend(form.domain): for case in cases: CaseAccessorSQL.save_case(case) if stock_result: LedgerAccessorSQL.save_ledger_values(stock_result.models_to_save) FormAccessorSQL.update_form_problem_and_state(form) FormProcessorSQL._publish_changes( ProcessedForms(form, None), cases, stock_result ) else: with bulk_atomic_blobs([form] + cases): XFormInstance.save(form) # use this save to that we don't overwrite the doc_type XFormInstance.get_db().bulk_save(cases) if stock_result: stock_result.commit() case_stock_result.stock_result.finalize() case_stock_result.case_result.commit_dirtiness_flags() for case in cases: case_post_save.send(case.__class__, case=case) return form