def create_objs_rest_used_exta_arrts(name, extra_attrs, factory_params): """Create new objects via REST API according to object name (plural form) and list extra attributes. Return: [lib.entities.entity.*Entity, ...] """ if extra_attrs[0].type == objects.get_singular(objects.CUSTOM_ATTRIBUTES): if name == objects.ASSESSMENT_TEMPLATES: return factory.get_cls_rest_service(object_name=name)().create_objs( count=1, factory_params=factory_params, custom_attribute_definitions=CustomAttributeDefinitionsFactory. generate_ca_defenitions_for_asmt_tmpls( list_ca_definitions=extra_attrs[:len(_list_cas_types)]), audit=extra_attrs[len(_list_cas_types):][0].__dict__) else: cavs = [cav.__dict__ for cav in CustomAttributeDefinitionsFactory.generate_ca_values( cads=extra_attrs)] return factory.get_cls_rest_service(object_name=name)().create_objs( count=1, factory_params=factory_params, custom_attribute_definitions=[cad.__dict__ for cad in extra_attrs], custom_attribute_values=cavs) else: return ([factory.get_cls_rest_service(object_name=name)(). create_objs(count=1, factory_params=factory_params, **{parent_obj.type.lower(): parent_obj.__dict__})[0] for parent_obj in extra_attrs])
def _set_values_for_assessment(assessment, cads, only_checkbox, checkbox_value): """Set CA values for assessment""" checkbox_cad = Representation.filter_objs_by_attrs( objs=cads, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX) if only_checkbox: cavs = [CustomAttributeDefinitionsFactory.generate_ca_value( checkbox_cad, checkbox_value)] else: cavs = CustomAttributeDefinitionsFactory.generate_ca_values(cads) for cav in cavs: if cav.custom_attribute_id == checkbox_cad.id: cav.attribute_value = checkbox_value rest_service.AssessmentsService().update_obj( obj=assessment, custom_attribute_values=[cav.__dict__ for cav in cavs]) return cavs
def _set_values_for_assessment(assessment, cads, only_checkbox, checkbox_value): """Set CA values for assessment""" checkbox_cad = Representation.filter_objs_by_attrs( objs=cads, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX) if only_checkbox: cavs = [ CustomAttributeDefinitionsFactory.generate_ca_value( checkbox_cad, checkbox_value) ] else: cavs = CustomAttributeDefinitionsFactory.generate_ca_values(cads) for cav in cavs: if cav.custom_attribute_id == checkbox_cad.id: cav.attribute_value = checkbox_value rest_service.AssessmentsService().update_obj( obj=assessment, custom_attribute_values=[cav.__dict__ for cav in cavs]) return cavs
def update_rest_fixture(fixture, factory_params=None): """Extract arguments of 'update_rest_fixture' fixture from fixture name, update existing objects via REST API and return updated. """ parent_objs = None has_cas = False obj_name = fixture.replace("update_", "").replace("_rest", "") _objs_to_update = "new_{}_rest".format(obj_name) # e.g. need if: 'new_controls_rest' and 'update_control_rest' try: objs_to_update = get_fixture_from_dict_fixtures(fixture=_objs_to_update) except KeyError: _objs_to_update = "new_{}_rest".format(objects.get_plural(obj_name)) objs_to_update = get_fixture_from_dict_fixtures( fixture=_objs_to_update)[0] if objects.get_plural(obj_name) in objects.ALL_OBJS: obj_name = objects.get_plural(obj_name) if "_with_cas" in obj_name: has_cas = True obj_name = objects.get_plural(obj_name.replace("_with_cas", "")) parent_objs = get_fixture_from_dict_fixtures( fixture="new_{}_rest".format("cas_for_" + obj_name)) if objs_to_update: if has_cas and parent_objs: cavs = [cav.__dict__ for cav in CustomAttributeDefinitionsFactory.generate_ca_values( cads=parent_objs)] updated_objs = ( factory.get_cls_rest_service(object_name=obj_name)().update_objs( objs=objs_to_update, factory_params=factory_params, custom_attribute_definitions=[cad.__dict__ for cad in parent_objs], custom_attribute_values=cavs)) else: updated_objs = factory.get_cls_rest_service( object_name=obj_name)().update_objs(objs=objs_to_update, factory_params=factory_params) return updated_objs
def convert_obj_repr_from_rest_to_ui(obj): """Convert object's attributes from REST to UI like representation.""" def convert_attr_value_from_dict_to_unicode(attr_name, attr_value): """Convert attribute value from dictionary to unicode representation (get value by key from dictionary 'attr_value' where key determine according to 'attr_name'). """ if isinstance(attr_value, dict): converted_attr_value = attr_value if attr_name in [ "contact", "manager", "owners", "assignee", "creator", "verifier", "created_by", "modified_by", "Assignee", "Creator", "Verifier" ]: converted_attr_value = unicode(attr_value.get("email")) if attr_name in ["custom_attribute_definitions", "program", "audit", "objects_under_assessment"]: converted_attr_value = ( unicode(attr_value.get("title")) if attr_name != "custom_attribute_definitions" else {attr_value.get("id"): attr_value.get("title").upper()} ) if attr_name in ["custom_attribute_values"]: converted_attr_value = {attr_value.get("custom_attribute_id"): attr_value.get("attribute_value")} if obj_attr_name == "comments": converted_attr_value = { k: (parser.parse(v).replace(tzinfo=tz.tzutc()) if k == "created_at" and isinstance(v, unicode) else v) for k, v in attr_value.iteritems() if k in ["modified_by", "created_at", "description"]} return converted_attr_value origin_obj = copy.deepcopy(obj) for obj_attr_name in obj.__dict__.keys(): # 'Ex', u'Ex', 1, None to 'Ex', u'Ex', 1, None obj_attr_value = (obj.assignees.get(obj_attr_name.title()) if ( obj_attr_name in ["assignee", "creator", "verifier"] and "assignees" in obj.__dict__.keys()) else getattr(obj, obj_attr_name)) # REST like u'08-20-2017T04:30:45' to date=2017-08-20, # timetz=04:30:45+00:00 if (obj_attr_name in ["updated_at", "created_at"] and isinstance(obj_attr_value, unicode)): obj_attr_value = (parser.parse(obj_attr_value). replace(tzinfo=tz.tzutc())) if isinstance(obj_attr_value, dict) and obj_attr_value: # to "assignees" = {"Assignee": [], "Creator": [], "Verifier": []} if obj_attr_name == "assignees": obj_attr_value = { k: ([convert_attr_value_from_dict_to_unicode(k, _v) for _v in v] if isinstance(v, list) else convert_attr_value_from_dict_to_unicode(k, v)) for k, v in obj_attr_value.iteritems() if k in ["Assignee", "Creator", "Verifier"]} # "modified_by" {"type": "Person", "id": x} to u'*****@*****.**' if obj_attr_name == "modified_by": from lib.service import rest_service obj_attr_value = getattr(rest_service.ObjectsInfoService().get_obj( obj=Entity.convert_dict_to_obj_repr(obj_attr_value)), "email") # {'name': u'Ex1', 'type': u'Ex2', ...} to u'Ex1' else: obj_attr_value = convert_attr_value_from_dict_to_unicode( obj_attr_name, obj_attr_value) # [el1, el2, ...] or [{item1}, {item2}, ...] to [u'Ex1, u'Ex2', ...] if (isinstance(obj_attr_value, list) and all(isinstance(item, dict) for item in obj_attr_value)): obj_attr_value = [ convert_attr_value_from_dict_to_unicode(obj_attr_name, item) for item in obj_attr_value] setattr(obj, obj_attr_name, obj_attr_value) # merge "custom_attribute_definitions" and "custom_attribute_values" obj_cas_attrs_names = [ "custom_attributes", "custom_attribute_definitions", "custom_attribute_values"] if set(obj_cas_attrs_names).issubset(obj.__dict__.keys()): cas_def = obj.custom_attribute_definitions cas_val = obj.custom_attribute_values # form CAs values of CAs definitions exist but CAs values not, or CAs # definitions have different then CAs values lengths if (cas_def and (not cas_val or (isinstance(cas_def and cas_val, list)) and len(cas_def) != len(cas_val))): from lib.entities.entities_factory import ( CustomAttributeDefinitionsFactory) cas_val_dicts_keys = ([_.keys()[0] for _ in cas_val] if isinstance(cas_val, list) else [None]) _cas_val = [ {k: v} for k, v in CustomAttributeDefinitionsFactory.generate_ca_values( list_ca_def_objs=origin_obj.custom_attribute_definitions, is_none_values=True).iteritems() if k not in cas_val_dicts_keys] cas_val = _cas_val if not cas_val else cas_val + _cas_val cas_def_dict = ( dict([_def.iteritems().next() for _def in cas_def]) if (isinstance(cas_def, list) and all(isinstance(_def, dict) for _def in cas_def)) else {None: None}) cas_val_dict = ( dict([_val.iteritems().next() for _val in cas_val]) if (isinstance(cas_def, list) and all(isinstance(_def, dict) for _def in cas_def)) else {None: None}) cas = StringMethods.merge_dicts_by_same_key(cas_def_dict, cas_val_dict) setattr(obj, "custom_attributes", cas) return obj
def convert_repr_rest_to_ui(obj): """Convert object's attributes from REST to UI like representation.""" def convert_attr_val_repr_dict_to_unicode(attr_name, attr_value): """Convert attribute value from dictionary to unicode representation (get value by key from dictionary 'attr_value' where key determine according to 'attr_name'). """ if isinstance(attr_value, dict): converted_attr_value = attr_value if attr_name in [ "managers", "assignees", "creators", "verifiers", "created_by", "modified_by" ]: converted_attr_value = unicode(attr_value.get("email")) if attr_name in ["custom_attribute_definitions", "program", "audit", "mapped_objects"]: converted_attr_value = ( unicode(attr_value.get("title")) if attr_name != "custom_attribute_definitions" else {attr_value.get("id"): attr_value.get("title").upper()} ) if attr_name in ["custom_attribute_values"]: converted_attr_value = {attr_value.get("custom_attribute_id"): attr_value.get("attribute_value")} if obj_attr_name == "comments": converted_attr_value = { k: (parser.parse(v).replace(tzinfo=tz.tzutc()) if k == "created_at" and isinstance(v, unicode) else v) for k, v in attr_value.iteritems() if k in ["modified_by", "created_at", "description"]} return converted_attr_value origin_obj = copy.deepcopy(obj) for obj_attr_name in obj.__dict__.keys(): # 'Ex', u'Ex', 1, None to 'Ex', u'Ex', 1, None obj_attr_value = getattr(obj, obj_attr_name) # REST like u'08-20-2017T04:30:45' to date=2017-08-20, # timetz=04:30:45+00:00 if (obj_attr_name in ["updated_at", "created_at"] and isinstance(obj_attr_value, unicode)): obj_attr_value = (parser.parse(obj_attr_value). replace(tzinfo=tz.tzutc())) if isinstance(obj_attr_value, dict) and obj_attr_value: # "modified_by" {"type": "Person", "id": x} to u'*****@*****.**' # todo: deprecated? if obj_attr_name == "modified_by": from lib.service import rest_service obj_attr_value = getattr(rest_service.ObjectsInfoService().get_obj( obj=Representation.repr_dict_to_obj(obj_attr_value)), "email") # {'name': u'Ex1', 'type': u'Ex2', ...} to u'Ex1' else: obj_attr_value = convert_attr_val_repr_dict_to_unicode( obj_attr_name, obj_attr_value) # [el1, el2, ...] or [{item1}, {item2}, ...] to [u'Ex1, u'Ex2', ...] if (isinstance(obj_attr_value, list) and all(isinstance(item, dict) for item in obj_attr_value)): obj_attr_value = [ convert_attr_val_repr_dict_to_unicode(obj_attr_name, item) for item in obj_attr_value] setattr(obj, obj_attr_name, obj_attr_value) # merge "custom_attribute_definitions" and "custom_attribute_values" obj_cas_attrs_names = [ "custom_attributes", "custom_attribute_definitions", "custom_attribute_values"] if set(obj_cas_attrs_names).issubset(obj.__dict__.keys()): cas_def = obj.custom_attribute_definitions cas_val = obj.custom_attribute_values # form CAs values of CAs definitions exist but CAs values not, or CAs # definitions have different then CAs values lengths if (cas_def and (not cas_val or (isinstance(cas_def and cas_val, list)) and len(cas_def) != len(cas_val))): from lib.entities.entities_factory import ( CustomAttributeDefinitionsFactory) cas_val_dicts_keys = ([_.keys()[0] for _ in cas_val] if isinstance(cas_val, list) else [None]) _cas_val = [ {k: v} for k, v in CustomAttributeDefinitionsFactory.generate_ca_values( list_ca_def_objs=origin_obj.custom_attribute_definitions, is_none_values=True).iteritems() if k not in cas_val_dicts_keys] cas_val = _cas_val if not cas_val else cas_val + _cas_val cas_def_dict = ( dict([_def.iteritems().next() for _def in cas_def]) if (isinstance(cas_def, list) and all(isinstance(_def, dict) for _def in cas_def)) else None) cas_val_dict = ( dict([_val.iteritems().next() for _val in cas_val]) if (isinstance(cas_def, list) and all(isinstance(_def, dict) for _def in cas_def)) else None) cas = StringMethods.merge_dicts_by_same_key(cas_def_dict, cas_val_dict) if cas in [{None: None}, {}]: cas = None setattr(obj, "custom_attributes", cas) return obj