def data_types(request, domain, data_type_id): if data_type_id: data_type = FixtureDataType.get(data_type_id) assert(data_type.doc_type == FixtureDataType._doc_type) assert(data_type.domain == domain) if request.method == 'GET': return json_response(strip_json(data_type)) elif request.method == 'PUT': new = FixtureDataType(domain=domain, **_to_kwargs(request)) for attr in 'tag', 'name', 'fields': setattr(data_type, attr, getattr(new, attr)) data_type.save() return json_response(strip_json(data_type)) elif request.method == 'DELETE': for item in FixtureDataItem.by_data_type(domain, data_type.get_id): item.delete() data_type.delete() return json_response({}) elif data_type_id is None: if request.method == 'POST': data_type = FixtureDataType(domain=domain, **_to_kwargs(request)) data_type.save() return json_response(strip_json(data_type)) elif request.method == 'GET': return json_response([strip_json(x) for x in FixtureDataType.by_domain(domain)]) return HttpResponseBadRequest()
def setUp(self): self.domain = 'qwerty' self.data_type = FixtureDataType(domain=self.domain, tag="contact", name="Contact", fields=['name', 'number']) self.data_type.save() self.data_item = FixtureDataItem(domain=self.domain, data_type_id=self.data_type.get_id, fields={ 'name': 'John', 'number': '+15555555555' }) self.data_item.save() self.user = CommCareUser.create(self.domain, 'rudolph', '***') self.fixture_ownership = FixtureOwnership( domain=self.domain, owner_id=self.user.get_id, owner_type='user', data_item_id=self.data_item.get_id) self.fixture_ownership.save()
def drilldown_map(self): diseases = [] disease_fixtures = FixtureDataItem.by_data_type( self.domain, FixtureDataType.by_domain_tag(self.domain, "diseases").one() ) for d in disease_fixtures: disease = dict( val="%(name)s:%(uid)s" % {'name': d.fields_without_attributes["disease_id"], 'uid': d.get_id}, text=d.fields_without_attributes["disease_name"] ) tests = [] test_fixtures = FixtureDataItem.by_field_value( self.domain, FixtureDataType.by_domain_tag(self.domain, "test").one(), "disease_id", d.fields_without_attributes["disease_id"] ) for t in test_fixtures: tests.append(dict( val="%(name)s:%(uid)s" % {'name': t.fields_without_attributes["test_name"], 'uid': t.get_id}, text=t.fields_without_attributes["visible_test_name"]) ) disease['next'] = tests diseases.append(disease) return diseases
def setUpClass(cls): super(TestLocationOwnership, cls).setUpClass() cls.tag = "big-mac-index" data_type = FixtureDataType( domain=cls.domain, tag=cls.tag, name="Big Mac Index", fields=[ FixtureTypeField(field_name="cost", properties=[]), FixtureTypeField(field_name="region", properties=[]), ], item_attributes=[], ) data_type.save() def make_data_item(location_name, cost): """Make a fixture data item and assign it to location_name""" data_item = FixtureDataItem( domain=cls.domain, data_type_id=data_type.get_id, fields={ "cost": FieldList( field_list=[FixtureItemField( field_value=cost, properties={}, )] ), "location_name": FieldList( field_list=[FixtureItemField( field_value=location_name, properties={}, )] ), }, item_attributes={}, ) data_item.save() FixtureOwnership( domain=cls.domain, owner_id=cls.locations[location_name].location_id, owner_type='location', data_item_id=data_item.get_id ).save() make_data_item('Suffolk', '8') make_data_item('Boston', '10') make_data_item('Somerville', '7') get_fixture_data_types.clear(cls.domain) cls.no_location_user = CommCareUser.create(cls.domain, 'no_location', '***', None, None) cls.suffolk_user = CommCareUser.create(cls.domain, 'guy-from-suffolk', '***', None, None) cls.suffolk_user.set_location(cls.locations['Suffolk']) cls.boston_user = CommCareUser.create(cls.domain, 'guy-from-boston', '***', None, None) cls.boston_user.set_location(cls.locations['Boston']) cls.middlesex_user = CommCareUser.create(cls.domain, 'guy-from-middlesex', '***', None, None) cls.middlesex_user.set_location(cls.locations['Middlesex'])
def setUpClass(cls): super(TestLocationOwnership, cls).setUpClass() cls.tag = "big-mac-index" data_type = FixtureDataType( domain=cls.domain, tag=cls.tag, name="Big Mac Index", fields=[ FixtureTypeField(field_name="cost", properties=[]), FixtureTypeField(field_name="region", properties=[]), ], item_attributes=[], ) data_type.save() def make_data_item(location_name, cost): """Make a fixture data item and assign it to location_name""" data_item = FixtureDataItem( domain=cls.domain, data_type_id=data_type.get_id, fields={ "cost": FieldList( field_list=[FixtureItemField( field_value=cost, properties={}, )] ), "location_name": FieldList( field_list=[FixtureItemField( field_value=location_name, properties={}, )] ), }, item_attributes={}, ) data_item.save() FixtureOwnership( domain=cls.domain, owner_id=cls.locations[location_name].location_id, owner_type='location', data_item_id=data_item.get_id ).save() make_data_item('Suffolk', '8') make_data_item('Boston', '10') make_data_item('Somerville', '7') get_fixture_data_types_in_domain.clear(cls.domain) cls.no_location_user = CommCareUser.create(cls.domain, 'no_location', '***') cls.suffolk_user = CommCareUser.create(cls.domain, 'guy-from-suffolk', '***') cls.suffolk_user.set_location(cls.locations['Suffolk']) cls.boston_user = CommCareUser.create(cls.domain, 'guy-from-boston', '***') cls.boston_user.set_location(cls.locations['Boston']) cls.middlesex_user = CommCareUser.create(cls.domain, 'guy-from-middlesex', '***') cls.middlesex_user.set_location(cls.locations['Middlesex'])
def setUpClass(cls): cls.domain = 'fixture-dbaccessors' cls.data_types = [ FixtureDataType(domain=cls.domain, tag='a'), FixtureDataType(domain=cls.domain, tag='b'), FixtureDataType(domain=cls.domain, tag='c'), FixtureDataType(domain='other-domain', tag='x'), ] FixtureDataType.get_db().bulk_save(cls.data_types)
def obj_create(self, bundle, request=None, **kwargs): if FixtureDataType.by_domain_tag(kwargs['domain'], bundle.data.get("tag")): raise BadRequest("A lookup table with name %s already exists" % bundle.data.get("tag")) bundle.obj = FixtureDataType(bundle.data) bundle.obj.domain = kwargs['domain'] bundle.obj.save() return bundle
def setUp(self): super(FixtureDataTest, self).setUp() self.domain = 'qwerty' self.tag = "district" delete_all_users() delete_all_fixture_data_types() self.data_type = FixtureDataType( domain=self.domain, tag=self.tag, name="Districts", fields=[ FixtureTypeField(field_name="state_name", properties=[]), FixtureTypeField(field_name="district_name", properties=["lang"]), FixtureTypeField(field_name="district_id", properties=[]), ], item_attributes=[], ) self.data_type.save() self.data_item = FixtureDataItem( domain=self.domain, data_type_id=self.data_type.get_id, fields={ "state_name": FieldList(field_list=[ FixtureItemField(field_value="Delhi_state", properties={}) ]), "district_name": FieldList(field_list=[ FixtureItemField(field_value="Delhi_in_HIN", properties={"lang": "hin"}), FixtureItemField(field_value="Delhi_in_ENG", properties={"lang": "eng"}) ]), "district_id": FieldList(field_list=[ FixtureItemField(field_value="Delhi_id", properties={}) ]) }, item_attributes={}, ) self.data_item.save() self.user = CommCareUser.create(self.domain, 'to_delete', '***', None, None) self.fixture_ownership = FixtureOwnership( domain=self.domain, owner_id=self.user.get_id, owner_type='user', data_item_id=self.data_item.get_id) self.fixture_ownership.save() get_fixture_data_types.clear(self.domain)
def setUp(self): super(TestLookupTableResource, self).setUp() self.data_type = FixtureDataType(domain=self.domain.name, tag="lookup_table", fields=[ FixtureTypeField( field_name="fixture_property", properties=["lang", "name"]) ], item_attributes=[]) self.data_type.save()
def delete_all_fixture_data_types(): from corehq.apps.fixtures.models import FixtureDataType results = FixtureDataType.get_db().view('fixtures/data_types_by_domain_tag', reduce=False).all() for result in results: try: fixture_data_type = FixtureDataType.get(result['id']) except Exception: pass else: fixture_data_type.delete()
def test_message_bank_doesnt_have_correct_properties(self): data_type = FixtureDataType(domain=self.domain_name, tag=self.fixture_name, fields=[], item_attributes=[]) data_type.save() self.addCleanup(data_type.delete) with create_test_case(self.domain_name, self.case_type, 'test-case') as case: self.assertRaises(AssertionError, ucla_message_bank_content, self._reminder(), self._handler(), case)
def test_message_bank_doesnt_have_correct_properties(self): data_type = FixtureDataType( domain=self.domain_name, tag=self.fixture_name, fields=[], item_attributes=[] ) data_type.save() self.addCleanup(data_type.delete) with create_test_case(self.domain_name, self.case_type, 'test-case') as case: self.assertRaises( AssertionError, general_health_message_bank_content, self._reminder(), self._handler(), case)
def fixture_type_context(): fixture_type = FixtureDataType( domain=DOMAIN, tag='dhis2_org_unit', fields=[FixtureTypeField(field_name='id', properties=[]), FixtureTypeField(field_name='name', properties=[])] ) fixture_type.save() try: yield fixture_type finally: fixture_type.delete()
def post(self, request): """View's dispatch method automatically calls this""" try: workbook = WorkbookJSONReader(request.file) except AttributeError: return HttpResponseBadRequest("Error processing your Excel (.xlsx) file") try: data_types = workbook.get_worksheet(title='types') except KeyError: return HttpResponseBadRequest("Workbook does not have a sheet called 'types'") for dt in data_types: print "DataType" print dt for di in workbook.get_worksheet(title=dt['tag']): print "DataItem" print di for dt in data_types: data_type = FixtureDataType( domain=self.domain, name=dt['name'], tag=dt['tag'], fields=dt['field'], ) data_type.save() data_items = workbook.get_worksheet(data_type.tag) for di in data_items: print di data_item = FixtureDataItem( domain=self.domain, data_type_id=data_type.get_id, fields=di['field'] ) data_item.save() for group_name in di.get('group', []): group = Group.by_name(self.domain, group_name) if group: data_item.add_group(group) else: messages.error(request, "Unknown group: %s" % group_name) for raw_username in di.get('user', []): username = normalize_username(raw_username, self.domain) user = CommCareUser.get_by_username(username) if user: data_item.add_user(user) else: messages.error(request, "Unknown user: %s" % raw_username) return HttpResponseRedirect(reverse('fixture_view', args=[self.domain]))
def _setup_fixture_type(self): self.data_type = FixtureDataType( domain=self.domain_name, tag=self.fixture_name, fields=[ FixtureTypeField(field_name="risk_profile", properties=[]), FixtureTypeField(field_name="sequence", properties=[]), FixtureTypeField(field_name="message", properties=[]), ], item_attributes=[]) self.data_type.save() self.addCleanup(self.data_type.delete) self._setup_data_item()
def make_data_type(self, name, is_global): data_type = FixtureDataType( domain=self.domain, tag="{}-index".format(name), is_global=is_global, name=name.title(), fields=[ FixtureTypeField(field_name="cost", properties=[]), ], item_attributes=[], ) data_type.save() self.addCleanup(data_type.delete) return data_type
def test_message_bank_doesnt_have_correct_properties_new(self): data_type = FixtureDataType( domain=self.domain_name, tag=self.fixture_name, fields=[], item_attributes=[] ) data_type.save() self.addCleanup(data_type.delete) with create_test_case(self.domain_name, self.case_type, 'test-case') as case: with self.assertRaises(AssertionError) as e: self._get_current_event_messages(self._schedule_instance(case)) self.assertIn('must have', str(e.exception))
def setUp(self): self.domain = 'dirty-fields' self.data_type = FixtureDataType( domain=self.domain, tag='dirty_fields', name="Dirty Fields", fields=[ FixtureTypeField(field_name="will/crash", properties=[]), FixtureTypeField(field_name="space cadet", properties=[]), FixtureTypeField(field_name="yes\\no", properties=[]), FixtureTypeField(field_name="<with>", properties=[]), FixtureTypeField(field_name="<crazy / combo><d", properties=[]) ], item_attributes=[], ) self.data_type.save() self.data_item = FixtureDataItem( domain=self.domain, data_type_id=self.data_type.get_id, fields={ "will/crash": FieldList(field_list=[ FixtureItemField(field_value="yep", properties={}) ]), "space cadet": FieldList(field_list=[ FixtureItemField(field_value="major tom", properties={}) ]), "yes\\no": FieldList(field_list=[ FixtureItemField(field_value="no, duh", properties={}) ]), "<with>": FieldList(field_list=[ FixtureItemField(field_value="so fail", properties={}) ]), "<crazy / combo><d": FieldList(field_list=[ FixtureItemField(field_value="just why", properties={}) ]), "xmlbad": FieldList(field_list=[ FixtureItemField(field_value="badxml", properties={}) ]) }, item_attributes={}, ) self.data_item.save()
def _create_fixture(domain, tag="table", should_save=True): data_type = FixtureDataType(domain=domain, tag=tag, fields=[ FixtureTypeField( field_name="fixture_property", properties=["test"]) ], item_attributes=[], is_global=True) if should_save: data_type.save() return data_type
def _create_data_type(domain, table_def, replace, transaction): """Processes a FixtureDataType from an _FixtureTableDefinition, which gets its definition from one row in the "types" sheet in the uploaded excel sheet Returns a tuple with - (unsaved) FixtureDataType instance - boolean flag to indiicate if the data type was deleted - a list of errors """ errors = [] new_data_type = FixtureDataType( domain=domain, is_global=table_def.is_global, tag=table_def.table_id, fields=table_def.fields, item_attributes=table_def.item_attributes ) try: existing_data_type = FixtureDataType.fixture_tag_exists(domain, new_data_type.tag) if existing_data_type: data_type = existing_data_type # support old usage with 'UID' elif table_def.uid: data_type = FixtureDataType.get(table_def.uid) else: data_type = new_data_type if replace and data_type != new_data_type: data_type.recursive_delete(transaction) data_type = new_data_type data_type.fields = table_def.fields data_type.item_attributes = table_def.item_attributes data_type.is_global = table_def.is_global if data_type.domain != domain: data_type = new_data_type errors.append( _("'%(UID)s' is not a valid UID. But the new type is created.") % {'UID': table_def.uid} ) if table_def.delete: data_type.recursive_delete(transaction) return data_type, True, errors except (ResourceNotFound, KeyError): data_type = new_data_type return data_type, False, errors
def __call__(self, restore_state): restore_user = restore_state.restore_user global_types = {} user_types = {} for data_type in FixtureDataType.by_domain(restore_user.domain): if data_type.is_global: global_types[data_type._id] = data_type else: user_types[data_type._id] = data_type items = [] user_items_count = 0 if global_types: global_items = self.get_global_items(global_types, restore_state) items.extend(global_items) if user_types: user_items, user_items_count = self.get_user_items_and_count(user_types, restore_user) items.extend(user_items) metrics_histogram( 'commcare.fixtures.item_lists.user', user_items_count, bucket_tag='items', buckets=[1, 100, 1000, 10000, 30000, 100000, 300000, 1000000], bucket_unit='', tags={ 'domain': restore_user.domain } ) return items
def project_info(request, domain, template="appstore/project_info.html"): dom = Domain.get(domain) if not can_view_app(request, dom): raise Http404() copies = dom.copies_of_parent() images = set() audio = set() return render( request, template, { "project": dom, "applications": dom.full_applications(include_builds=False), "fixtures": FixtureDataType.by_domain(dom.name), "copies": copies, "images": images, "audio": audio, "url_base": reverse('appstore'), 'display_import': True if getattr(request, "couch_user", "") and request.couch_user.get_domains() else False })
def update_types(patches, domain, data_type_id, data_tag, is_global, transaction): data_type = FixtureDataType.get(data_type_id) fields_patches = deepcopy(patches) assert(data_type.doc_type == FixtureDataType._doc_type) assert(data_type.domain == domain) old_fields = data_type.fields new_fixture_fields = [] setattr(data_type, "tag", data_tag) setattr(data_type, "is_global", is_global) for old_field in old_fields: patch = fields_patches.pop(old_field.field_name, {}) if not any(patch): new_fixture_fields.append(old_field) if "update" in patch: setattr(old_field, "field_name", patch["update"]) new_fixture_fields.append(old_field) if "remove" in patch: continue new_fields = fields_patches.keys() for new_field_name in new_fields: patch = fields_patches.pop(new_field_name) if "is_new" in patch: new_fixture_fields.append(FixtureTypeField( field_name=new_field_name, properties=[] )) setattr(data_type, "fields", new_fixture_fields) transaction.save(data_type) return data_type
def setupFixtureData(cls): cls.fixture_data = { 'schedule1': '7', 'schedule2': '14', 'schedule3': '21', } cls.data_type = FixtureDataType( domain=cls.domain, tag=DAILY_SCHEDULE_FIXTURE_NAME, name=DAILY_SCHEDULE_FIXTURE_NAME, fields=[ FixtureTypeField(field_name=SCHEDULE_ID_FIXTURE, properties=[]), FixtureTypeField(field_name="doses_per_week", properties=[]), ], item_attributes=[], ) cls.data_type.save() cls.data_items = [] for _id, value in cls.fixture_data.iteritems(): data_item = FixtureDataItem( domain=cls.domain, data_type_id=cls.data_type.get_id, fields={ SCHEDULE_ID_FIXTURE: FieldList( field_list=[FixtureItemField(field_value=_id, )]), "doses_per_week": FieldList( field_list=[FixtureItemField(field_value=value, )]) }, item_attributes={}, ) data_item.save() cls.data_items.append(data_item)
def item_lists(user, version=V2, last_sync=None): if isinstance(user, CommCareUser): pass elif hasattr(user, "_hq_user") and user._hq_user is not None: user = user._hq_user else: return [] items = FixtureDataItem.by_user(user) data_types = {} items_by_type = defaultdict(list) for item in items: if not data_types.has_key(item.data_type_id): try: data_types[item.data_type_id] = FixtureDataType.get(item.data_type_id) except (ResourceNotFound, AttributeError): continue items_by_type[item.data_type_id].append(item) item._data_type = data_types[item.data_type_id] fixtures = [] for data_type in data_types.values(): xFixture = ElementTree.Element('fixture', attrib={'id': 'item-list:%s' % data_type.tag, 'user_id': user.user_id}) xItemList = ElementTree.Element('%s_list' % data_type.tag) xFixture.append(xItemList) for item in items_by_type[data_type.get_id]: xItemList.append(item.to_xml()) fixtures.append(xFixture) return fixtures
def get_users_per_dctl(cls): dctls = dict() data_type = FixtureDataType.by_domain_tag(cls.domain, 'dctl').first() data_items = FixtureDataItem.by_data_type(cls.domain, data_type.get_id if data_type else None) for item in data_items: dctls[item.fields_without_attributes.get("id")] = item.get_users(wrap=False) return dctls
def setUp(self): self.domain = 'qwerty' self.data_type = FixtureDataType( domain=self.domain, tag="contact", name="Contact", fields=['name', 'number'] ) self.data_type.save() self.data_item = FixtureDataItem( domain=self.domain, data_type_id=self.data_type.get_id, fields={ 'name': 'John', 'number': '+15555555555' } ) self.data_item.save() self.user = CommCareUser.create(self.domain, 'rudolph', '***') self.fixture_ownership = FixtureOwnership( domain=self.domain, owner_id=self.user.get_id, owner_type='user', data_item_id=self.data_item.get_id ) self.fixture_ownership.save()
def item_lists_by_domain(domain): ret = list() for data_type in FixtureDataType.by_domain(domain): structure = { f.field_name: { 'name': f.field_name, 'no_option': True } for f in data_type.fields } for attr in data_type.item_attributes: structure['@' + attr] = {'name': attr, 'no_option': True} uri = 'jr://fixture/%s:%s' % (ItemListsProvider.id, data_type.tag) ret.append({ 'id': data_type.tag, 'uri': uri, 'path': "/{tag}_list/{tag}".format(tag=data_type.tag), 'name': data_type.tag, 'structure': structure, }) products = product_fixture_generator_json(domain) if products: ret.append(products) programs = program_fixture_generator_json(domain) if programs: ret.append(programs) return ret
def _update_types(patches, domain, data_type_id, data_tag, is_global, description, transaction): data_type = FixtureDataType.get(data_type_id) fields_patches = deepcopy(patches) old_fields = data_type.fields new_fixture_fields = [] data_type.tag = data_tag data_type.is_global = is_global data_type.description = description for old_field in old_fields: patch = fields_patches.pop(old_field.field_name, {}) if not any(patch): new_fixture_fields.append(old_field) if "update" in patch: setattr(old_field, "field_name", patch["update"]) new_fixture_fields.append(old_field) if "remove" in patch: continue new_fields = list(fields_patches.keys()) for new_field_name in new_fields: patch = fields_patches.pop(new_field_name) if "is_new" in patch: new_fixture_fields.append( FixtureTypeField(field_name=new_field_name, properties=[])) data_type.fields = new_fixture_fields transaction.save(data_type) return data_type
def item_lists_by_domain(domain): ret = list() for data_type in FixtureDataType.by_domain(domain): structure = { f.field_name: { 'name': f.field_name, 'no_option': True } for f in data_type.fields } for attr in data_type.item_attributes: structure['@' + attr] = { 'name': attr, 'no_option': True } ret.append({ 'sourceUri': 'jr://fixture/item-list:%s' % data_type.tag, 'defaultId': data_type.tag, 'initialQuery': "instance('{tag}')/{tag}_list/{tag}".format(tag=data_type.tag), 'name': data_type.tag, 'structure': structure, }) products = product_fixture_generator_json(domain) if products: ret.append(products) return ret
def _get_basic_module_view_context(app, module): case_property_builder = _setup_case_property_builder(app) fixture_columns = [ field.field_name for fixture in FixtureDataType.by_domain(app.domain) for field in fixture.fields ] case_type = module.case_type form_options = _case_list_form_options(app, module, case_type) # http://manage.dimagi.com/default.asp?178635 allow_with_parent_select = app.build_version >= '2.23' or not module.parent_select.active allow_case_list_form = _case_list_form_not_allowed_reason( module, AllowWithReason(allow_with_parent_select, AllowWithReason.PARENT_SELECT_ACTIVE) ) return { 'parent_modules': _get_parent_modules(app, module, case_property_builder, case_type), 'fixtures': _get_fixture_types(app.domain), 'fixture_columns': fixture_columns, 'details': _get_module_details_context(app, module, case_property_builder, case_type), 'case_list_form_options': form_options, 'case_list_form_not_allowed_reason': allow_case_list_form, 'valid_parent_modules': _get_valid_parent_modules(app, module), 'child_module_enabled': toggles.BASIC_CHILD_MODULE.enabled(app.domain) and module.doc_type != "ShadowModule" }
def update_types(patches, domain, data_type_id, data_tag, is_global, transaction): data_type = FixtureDataType.get(data_type_id) fields_patches = deepcopy(patches) assert (data_type.doc_type == FixtureDataType._doc_type) assert (data_type.domain == domain) old_fields = data_type.fields new_fixture_fields = [] setattr(data_type, "tag", data_tag) setattr(data_type, "is_global", is_global) for old_field in old_fields: patch = fields_patches.pop(old_field.field_name, {}) if not any(patch): new_fixture_fields.append(old_field) if "update" in patch: setattr(old_field, "field_name", patch["update"]) new_fixture_fields.append(old_field) if "remove" in patch: continue new_fields = fields_patches.keys() for new_field_name in new_fields: patch = fields_patches.pop(new_field_name) if "is_new" in patch: new_fixture_fields.append( FixtureTypeField(field_name=new_field_name, properties=[])) setattr(data_type, "fields", new_fixture_fields) transaction.save(data_type) return data_type
def test_types(self): test_fixtures = FixtureDataItem.by_data_type( self.domain, FixtureDataType.by_domain_tag(self.domain, "test").one()) return [ t.fields_without_attributes["test_name"] for t in test_fixtures ]
def data_types(self, index=None): if not self.fdts: self.fdts = [ FixtureDataType.by_domain_tag(self.domain, h["type"]).one() for h in self.hierarchy ] return self.fdts if index is None else self.fdts[index]
def obj_update(self, bundle, **kwargs): if 'tag' not in bundle.data: raise BadRequest("tag must be specified") try: bundle.obj = FixtureDataType.get(kwargs['pk']) except ResourceNotFound: raise NotFound('Lookup table not found') if bundle.obj.domain != kwargs['domain']: raise NotFound('Lookup table not found') if bundle.obj.tag != bundle.data['tag']: raise BadRequest("Lookup table tag cannot be changed") save = False if 'is_global' in bundle.data: save = True bundle.obj.is_global = bundle.data['is_global'] if 'fields' in bundle.data: save = True bundle.obj.fields = [ FixtureTypeField.wrap(field) for field in bundle.data['fields'] ] if 'item_attributes' in bundle.data: save = True bundle.obj.item_attributes = bundle.data['item_attributes'] if save: bundle.obj.save() return bundle
def convert_fdt(fdi): try: fdt = FixtureDataType.get(fdi.data_type_id) fdi.fixture_type = fdt.tag return fdi except ResourceNotFound: return fdi
def clear_fixture_quickcache(domain, data_types): """ Clears quickcache for fixtures.dbaccessors Args: :domain: The domain that has been updated :data_types: List of FixtureDataType objects with stale cache """ if not data_types: return type_ids = set() for data_type in data_types: type_ids.add(data_type.get_id) data_type.clear_caches() from corehq.apps.fixtures.dbaccessors import get_fixture_items_for_data_types get_fixture_items_for_data_types.clear(domain, type_ids) # We always call get_fixture_items_for_data_types with a list of all global # type ids when doing a restore (i.e. the cache key is a set of all global # type ids) So when updating just a subset of types, we still need to clear # the cache key that contains all types. global_type_ids = { dt.get_id for dt in FixtureDataType.by_domain(domain) if dt.is_global } get_fixture_items_for_data_types.clear(domain, global_type_ids)
def clear_fixture_quickcache(domain, data_types): """ Clears quickcache for fixtures.dbaccessors Args: :domain: The domain that has been updated :data_types: List of FixtureDataType objects with stale cache """ if not data_types: return type_ids = set() for data_type in data_types: type_ids.add(data_type.get_id) data_type.clear_caches() from corehq.apps.fixtures.dbaccessors import get_fixture_items_for_data_types get_fixture_items_for_data_types.clear(domain, type_ids) # We always call get_fixture_items_for_data_types with a list of all global # type ids when doing a restore (i.e. the cache key is a set of all global # type ids) So when updating just a subset of types, we still need to clear # the cache key that contains all types. global_type_ids = {dt.get_id for dt in FixtureDataType.by_domain(domain) if dt.is_global} get_fixture_items_for_data_types.clear(domain, global_type_ids)
def setUpClass(cls): super(TestFixtureOrdering, cls).setUpClass() cls.domain = "TestFixtureOrdering" cls.user = CommCareUser.create(cls.domain, 'george', '***') cls.data_type = FixtureDataType( domain=cls.domain, tag="houses-of-westeros", is_global=True, name="Great Houses of Westeros", fields=[ FixtureTypeField(field_name="name", properties=[]), FixtureTypeField(field_name="seat", properties=[]), FixtureTypeField(field_name="sigil", properties=[]), ], item_attributes=[], ) cls.data_type.save() cls.data_items = [ cls._make_data_item(4, "Tyrell", "Highgarden", "Rose"), cls._make_data_item(6, "Martell", "Sunspear", "Sun and Spear"), cls._make_data_item(3, "Lannister", "Casterly Rock", "Lion"), cls._make_data_item(1, "Targaryen", "Dragonstone", "Dragon"), cls._make_data_item(5, "Tully", "Riverrun", "Trout"), cls._make_data_item(2, "Stark", "Winterfell", "Direwolf"), cls._make_data_item(7, "Baratheon", "Storm's End", "Stag"), ]
def update_tables(request, domain, data_type_id, test_patch=None): """ receives a JSON-update patch like following { "_id":"0920fe1c6d4c846e17ee33e2177b36d6", "tag":"growth", "view_link":"/a/gsid/fixtures/view_lookup_tables/?table_id:0920fe1c6d4c846e17ee33e2177b36d6", "is_global":false, "fields":{"genderr":{"update":"gender"},"grade":{}} } """ if test_patch is None: test_patch = {} if data_type_id: try: data_type = FixtureDataType.get(data_type_id) except ResourceNotFound: raise Http404() assert(data_type.doc_type == FixtureDataType._doc_type) assert(data_type.domain == domain) if request.method == 'GET': return json_response(strip_json(data_type)) elif request.method == 'DELETE': with CouchTransaction() as transaction: data_type.recursive_delete(transaction) return json_response({}) elif not request.method == 'PUT': return HttpResponseBadRequest() if request.method == 'POST' or request.method == "PUT": fields_update = test_patch or _to_kwargs(request) fields_patches = fields_update["fields"] data_tag = fields_update["tag"] is_global = fields_update["is_global"] with CouchTransaction() as transaction: if data_type_id: data_type = update_types(fields_patches, domain, data_type_id, data_tag, is_global, transaction) update_items(fields_patches, domain, data_type_id, transaction) else: if FixtureDataType.fixture_tag_exists(domain, data_tag): return HttpResponseBadRequest("DuplicateFixture") else: data_type = create_types(fields_patches, domain, data_tag, is_global, transaction) return json_response(strip_json(data_type))
def update_tables(request, domain, data_type_id, test_patch={}): """ receives a JSON-update patch like following { "_id":"0920fe1c6d4c846e17ee33e2177b36d6", "tag":"growth", "view_link":"/a/gsid/fixtures/view_lookup_tables/?table_id:0920fe1c6d4c846e17ee33e2177b36d6", "is_global":false, "fields":{"genderr":{"update":"gender"},"grade":{}} } """ if data_type_id: try: data_type = FixtureDataType.get(data_type_id) except ResourceNotFound: raise Http404() assert (data_type.doc_type == FixtureDataType._doc_type) assert (data_type.domain == domain) if request.method == 'GET': return json_response(strip_json(data_type)) elif request.method == 'DELETE': with CouchTransaction() as transaction: data_type.recursive_delete(transaction) return json_response({}) elif not request.method == 'PUT': return HttpResponseBadRequest() if request.method == 'POST' or request.method == "PUT": fields_update = test_patch or _to_kwargs(request) fields_patches = fields_update["fields"] data_tag = fields_update["tag"] is_global = fields_update["is_global"] with CouchTransaction() as transaction: if data_type_id: data_type = update_types(fields_patches, domain, data_type_id, data_tag, is_global, transaction) update_items(fields_patches, domain, data_type_id, transaction) else: if FixtureDataType.fixture_tag_exists(domain, data_tag): return HttpResponseBadRequest("DuplicateFixture") else: data_type = create_types(fields_patches, domain, data_tag, is_global, transaction) return json_response(strip_json(data_type))
def setupFixtureData(cls): cls.fixture_data = [ { SCHEDULE_ID_FIXTURE: 'schedule1', 'doses_per_week': '7', 'dose_count_ip_new_patient': '56', 'dose_count_ip_recurring_patient': '84', 'dose_count_cp_new_patient': '112', 'dose_count_cp_recurring_patient': '140', 'dose_count_outcome_due_new_patient': '168', 'dose_count_outcome_due_recurring_patient': '168', }, { SCHEDULE_ID_FIXTURE: 'schedule2', 'doses_per_week': '14', 'dose_count_ip_new_patient': '24', 'dose_count_ip_recurring_patient': '36', 'dose_count_cp_new_patient': '54', 'dose_count_cp_recurring_patient': '66', 'dose_count_outcome_due_new_patient': '78', 'dose_count_outcome_due_recurring_patient': '78', }, { SCHEDULE_ID_FIXTURE: 'schedule3', 'doses_per_week': '21', 'dose_count_ip_new_patient': '24', 'dose_count_ip_recurring_patient': '36', 'dose_count_cp_new_patient': '54', 'dose_count_cp_recurring_patient': '66', 'dose_count_outcome_due_new_patient': '78', 'dose_count_outcome_due_recurring_patient': '78', }, ] cls.data_type = FixtureDataType( domain=cls.domain, tag=DAILY_SCHEDULE_FIXTURE_NAME, name=DAILY_SCHEDULE_FIXTURE_NAME, fields=[ FixtureTypeField(field_name=SCHEDULE_ID_FIXTURE, properties=[]), FixtureTypeField(field_name="doses_per_week", properties=[]), ], item_attributes=[], ) cls.data_type.save() cls.data_items = [] for row in cls.fixture_data: data_item = FixtureDataItem( domain=cls.domain, data_type_id=cls.data_type.get_id, fields={ column_name: FieldList( field_list=[FixtureItemField(field_value=value, )]) for column_name, value in six.iteritems(row) }, item_attributes={}, ) data_item.save() cls.data_items.append(data_item)
def setUp(self): self.domain = 'qwerty' self.tag = "district" self.data_type = FixtureDataType( domain=self.domain, tag=self.tag, name="Districts", fields=[ FixtureTypeField(field_name="state_name", properties=[]), FixtureTypeField(field_name="district_name", properties=["lang"]), FixtureTypeField(field_name="district_id", properties=[]) ]) self.data_type.save() self.data_item = FixtureDataItem( domain=self.domain, data_type_id=self.data_type.get_id, fields={ "state_name": FieldList(field_list=[ FixtureItemField(field_value="Delhi_state", properties={}) ]), "district_name": FieldList(field_list=[ FixtureItemField(field_value="Delhi_in_HIN", properties={"lang": "hin"}), FixtureItemField(field_value="Delhi_in_ENG", properties={"lang": "eng"}) ]), "district_id": FieldList(field_list=[ FixtureItemField(field_value="Delhi_id", properties={}) ]) }) self.data_item.save() self.user = CommCareUser.create(self.domain, 'to_delete', '***') self.fixture_ownership = FixtureOwnership( domain=self.domain, owner_id=self.user.get_id, owner_type='user', data_item_id=self.data_item.get_id) self.fixture_ownership.save()
def get_user_site_map(domain): user_site_map = defaultdict(list) data_type = FixtureDataType.by_domain_tag(domain, 'site').first() fixtures = FixtureDataItem.by_data_type(domain, data_type.get_id) for fixture in fixtures: for user in fixture.get_users(): user_site_map[user._id].append(fixture.fields['site_id']) return user_site_map
def obj_create(self, bundle, request=None, **kwargs): if 'data_type_id' not in bundle.data: raise BadRequest("data_type_id must be specified") data_type_id = bundle.data['data_type_id'] try: FixtureDataType.get(data_type_id) except ResourceNotFound: raise NotFound('Lookup table not found') number_items = len(FixtureDataItem.by_data_type(kwargs['domain'], data_type_id)) bundle.obj = FixtureDataItem(bundle.data) bundle.obj.domain = kwargs['domain'] bundle.obj.sort_key = number_items + 1 bundle.obj.save() return bundle
def diseases(self): disease_fixtures = FixtureDataItem.by_data_type( self.domain, FixtureDataType.by_domain_tag(self.domain, "diseases").one() ) return { "ids": [d.fields["disease_id"] for d in disease_fixtures], "names": [d.fields["disease_name"] for d in disease_fixtures], }
def province(self): case = self.care_case fixture_type = FixtureDataType.by_domain_tag("care-ihapc-live", "province").first() fixture_item = FixtureDataItem.by_field_value("care-ihapc-live", fixture_type, "id", case.province).first() return fixture_item._id
def get_user_site_map(domain): user_site_map = defaultdict(list) data_type = FixtureDataType.by_domain_tag(domain, 'site').first() fixtures = FixtureDataItem.by_data_type(domain, data_type.get_id) for fixture in fixtures: for user in fixture.get_users(): user_site_map[user._id].append(fixture.fields_without_attributes['site_id']) return user_site_map
def obj_delete(self, bundle, **kwargs): try: data_type = FixtureDataType.get(kwargs['pk']) except ResourceNotFound: raise NotFound('Lookup table not found') with CouchTransaction() as transaction: data_type.recursive_delete(transaction) return ImmediateHttpResponse(response=HttpAccepted())
def get_number_of_fixture_data_types_in_domain(domain): num_fixtures = FixtureDataType.get_db().view( 'domain/docs', startkey=[domain, 'FixtureDataType'], endkey=[domain, 'FixtureDataType', {}], reduce=True, group_level=2, ).first() return num_fixtures['value'] if num_fixtures is not None else 0
def get_fixture_data_types_in_domain(domain): return FixtureDataType.view( 'domain/docs', endkey=[domain, 'FixtureDataType'], startkey=[domain, 'FixtureDataType', {}], reduce=False, include_docs=True, descending=True, )
def download_item_lists(request, domain): data_types = FixtureDataType.by_domain(domain) data_type_schemas = [] max_fields = 0 max_groups = 0 max_users = 0 mmax_groups = 0 mmax_users = 0 data_tables = [] for data_type in data_types: type_schema = [data_type.name, data_type.tag] fields = [field for field in data_type.fields] type_id = data_type.get_id data_table_of_type = [] for item_row in FixtureDataItem.by_data_type(domain, type_id): group_len = len(item_row.get_groups()) max_groups = group_len if group_len>max_groups else max_groups user_len = len(item_row.get_users()) max_users = user_len if user_len>max_users else max_users for item_row in FixtureDataItem.by_data_type(domain, type_id): groups = [group.name for group in item_row.get_groups()] + ["" for x in range(0,max_groups-len(item_row.get_groups()))] users = [user.raw_username for user in item_row.get_users()] + ["" for x in range(0, max_users-len(item_row.get_users()))] data_row = tuple([str(_id_from_doc(item_row)),"N"]+ [item_row.fields[field] for field in fields]+ groups + users) data_table_of_type.append(data_row) type_schema.extend(fields) data_type_schemas.append(tuple(type_schema)) if max_fields<len(type_schema): max_fields = len(type_schema) data_tables.append((data_type.tag,tuple(data_table_of_type))) mmax_users = max_users if max_users>mmax_users else mmax_users mmax_groups = max_groups if max_groups>mmax_groups else mmax_groups max_users = 0 max_groups = 0 type_headers = ["name", "tag"] + ["field %d" % x for x in range(1, max_fields - 1)] type_headers = ("types", tuple(type_headers)) table_headers = [type_headers] for type_schema in data_type_schemas: item_header = (type_schema[1], tuple(["UID", DELETE_HEADER] + ["field: " + x for x in type_schema[2:]] + ["group %d" % x for x in range(1, mmax_groups + 1)] + ["user %d" % x for x in range(1, mmax_users + 1)])) table_headers.append(item_header) table_headers = tuple(table_headers) type_rows = ("types", tuple(data_type_schemas)) data_tables = tuple([type_rows]+data_tables) fd, path = tempfile.mkstemp() with os.fdopen(fd, 'w') as temp: export_raw((table_headers), (data_tables), temp) format = Format.XLS_2007 return export_response(open(path), format, "%s_fixtures" % domain)