def get_unmapping_names(class_name): unmapping_rules = get_unmapping_rules().get(class_name) if unmapping_rules is not None: pretty_unmapping_rules = (title_from_camelcase(r) for r in unmapping_rules) unmapping_names = {"unmap:{}".format(n) for n in pretty_unmapping_rules} else: unmapping_names = None return unmapping_names
def get_mapping_names(class_name): """Return set of mapping names for sent model name.""" mapping_rules = get_mapping_rules().get(class_name) if mapping_rules is not None: pretty_mapping_rules = (title_from_camelcase(r) for r in mapping_rules) mapping_names = {"map:{}".format(n) for n in pretty_mapping_rules} else: mapping_names = None return mapping_names
def get_mapping_names(class_name): """Get mapping, unmapping and snapshot mapping column names.""" map_rules = rules.get_mapping_rules().get(class_name) or set() unmap_rules = rules.get_unmapping_rules().get(class_name) or set() unmap_sn_rules = rules.get_snapshot_mapping_rules().get(class_name) or set() format_rules = [("map:{}", map_rules), ("unmap:{}", unmap_rules), ("map:{} versions", unmap_sn_rules)] column_names = set() for format_, rule_set in format_rules: pretty_rules = (title_from_camelcase(r) for r in rule_set) column_names.update(format_.format(r) for r in pretty_rules) return column_names
def _generate_mapping_definition(cls, rules_set, prefix, display_name_tmpl): """Generate definition from template""" definitions = {} from ggrc.snapshotter.rules import Types read_only = Types.parents | Types.scoped read_only_text = "Read only column and will be ignored on import." for klass in rules_set: klass_name = title_from_camelcase(klass) key = "{}{}".format(prefix, klass_name) definitions[key.lower()] = { "display_name": display_name_tmpl.format(klass_name), "attr_name": klass.lower(), "mandatory": False, "unique": False, "description": read_only_text if klass in read_only else "", "type": cls.Type.MAPPING, } return definitions
def test_unmap_standard_objects(self, model1, model2): """Test deprecated unmapping between {0.__name__} and {1.__name__} """ response = self._get_import_csv_response(model1, model2) obj1 = model1.__name__ obj2 = title_from_camelcase(model2.__name__).title() # Check that mapping is not added self.assertEqual(len(response[1]['row_warnings']), 1) self._check_csv_response( [response[1]], { obj2: { "row_warnings": { errors.MAP_UNMAP_STANDARD_ERROR.format( line=7, object_type=obj1, ), }, }, })
def test_export_audit_mappings(self): """Test export of audit mapped objects""" snap_objects = [] mapped_slugs = defaultdict(list) with factories.single_commit(): audit = factories.AuditFactory(slug="Audit") # Create a group of mapped objects for current audit for _ in range(3): # All snapshotable objects should be mapped to Audit + Issue # and Assessment for type_ in Types.all.union(Types.scoped): if type_ == "Issue": obj = get_model_factory(type_)() factories.RelationshipFactory(source=audit, destination=obj) elif type_ in Types.scoped: obj = get_model_factory(type_)(audit=audit) factories.RelationshipFactory(source=audit, destination=obj) else: obj = get_model_factory(type_)() mapped_slugs[type_].append(obj.slug) snap_objects.append(obj) self._create_snapshots(audit, snap_objects) audit_data = self.export_parsed_csv([{ "object_name": "Audit", "filters": { "expression": {} }, "fields": "all", }])["Audit"][0] for type_, slugs in mapped_slugs.items(): if type_ in Types.all: format_ = "map:{} versions" else: format_ = "map:{}" mapping_name = format_.format(utils.title_from_camelcase(type_)) self.assertIn(mapping_name, audit_data) self.assertEqual(audit_data[mapping_name], "\n".join(sorted(slugs)))
def _generate_mapping_definition(cls, rules_set, prefix, display_name_tmpl, read_only_types=None): """Generate definition from template""" definitions = {} from ggrc.snapshotter.rules import Types read_only = read_only_types or Types.parents | Types.scoped read_only_text = "Read only column and will be ignored on import." for klass in rules_set: klass_name = title_from_camelcase(klass) key = "{}{}".format(prefix, klass_name) definitions[key.lower()] = { "display_name": display_name_tmpl.format(klass_name), "attr_name": klass.lower(), "mandatory": False, "unique": False, "description": read_only_text if klass in read_only else "", "type": cls.Type.MAPPING, } return definitions
def test_unmap_objects(self, model1, model2): """Test deprecated unmapping between {0.__name__} and {1.__name__} """ name1 = model1.__name__ name2 = model2.__name__ title1 = title_from_camelcase(name1) with factories.single_commit(): with mock.patch('ggrc.models.relationship.is_external_app_user', return_value=True): obj1 = factories.get_model_factory(name1)() obj2 = factories.get_model_factory(name2)() factories.RelationshipFactory(source=obj1, destination=obj2, is_external=True) slug1 = obj1.slug slug2 = obj2.slug data_block = [ OrderedDict([ ("object_type", name1), ("Code*", slug1), ]), OrderedDict([ ("object_type", name2), ("Code*", slug2), ("unmap:{}".format(title1), slug1), ]), ] response = self.import_data(*data_block) # Check that mapping is not added self.assertEqual(len(response[1]['row_warnings']), 1) self.assertIn( u'Line 7: You do not have the necessary permissions to unmap', response[1]['row_warnings'][0])
def title_singular(self): return getattr(self.model, 'readable_name_alias', utils.title_from_camelcase(self.model.__name__).title())
def title_singular(self): return utils.title_from_camelcase(self.model.__name__)