Esempio n. 1
0
    def convert_obj_repr_from_rest_to_ui(cls, obj):
        """Convert object's attributes values from REST like
    (dict or list of dict) representation to UI like with unicode.
    Examples:
    None to None, u'Ex' to u'Ex', [u'Ex1', u'Ex2', ...] to u'Ex1, Ex2',
    {'name': u'Ex', ...} to u'Ex',
    [{'name': u'Ex1', ...}, {'name': u'Ex2', ...}] to u'Ex1, Ex2'
    """

        # pylint: disable=too-many-locals
        # pylint: disable=undefined-loop-variable
        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", "assessor", "creator",
                        "verifier", "created_by", "modified_by", "Assessor",
                        "Creator", "Verifier"
                ]:
                    converted_attr_value = unicode(attr_value.get("name"))
                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:
                        (string_utils.convert_str_to_datetime(v) 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 ["assessor", "creator", "verifier"]
                and "assignees" in obj.__dict__.keys()) else getattr(
                    obj, obj_attr_name))
            # u'2017-06-07T16:50:16' and u'2017-06-07 16:50:16' to datetime
            if (obj_attr_name in ["updated_at", "created_at"]
                    and isinstance(obj_attr_value, unicode)):
                obj_attr_value = string_utils.convert_str_to_datetime(
                    obj_attr_value)
            if isinstance(obj_attr_value, dict) and obj_attr_value:
                # to "assignees" = {"Assessor": [], "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 ["Assessor", "Creator", "Verifier"]
                    }
                # {'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 if 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))):
                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 = string_utils.merge_dicts_by_same_key(cas_def_dict,
                                                       cas_val_dict)
            setattr(obj, "custom_attributes", cas)
        return obj
Esempio n. 2
0
 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", "assessor", "creator",
           "verifier", "created_by", "modified_by", "Assessor", "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 ["assessor", "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" = {"Assessor": [], "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 ["Assessor", "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 = string_utils.merge_dicts_by_same_key(cas_def_dict, cas_val_dict)
     setattr(obj, "custom_attributes", cas)
   return obj
Esempio n. 3
0
        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", "assessor",
                            "creator", "verifier", "created_by", "modified_by",
                            "Assessor", "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 ["assessor", "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" = {"Assessor": [], "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 ["Assessor", "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 = string_utils.merge_dicts_by_same_key(
                    cas_def_dict, cas_val_dict)
                setattr(obj, "custom_attributes", cas)
            return obj