def items_of_remap_keys(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ el = element.TransformationSetVisibleFields csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { el.TITLE: "title", el.ADMIN: "owners", el.CODE: "slug", el.REVIEW_STATE: "os_state", el.STATE: "status" } ui_remap_items = { el.MANAGER: "manager", el.VERIFIED: "verified", el.STATUS: "status", el.LAST_UPDATED: "updated_at", el.AUDIT_LEAD: "contact", el.CAS: "custom_attributes", el.MAPPED_OBJECTS: "objects_under_assessment", el.ASSIGNEES: "assessor", el.CREATORS: "creator", el.VERIFIERS: "verifier" } csv_remap_items = {csv.REVISION_DATE: "updated_at"} result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return string_utils.dict_keys_to_upper_case(result_remap_items)
def items_of_remap_keys(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ from lib.constants import element, files els = element.TransformationElements csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { els.TITLE: "title", els.ADMIN: "owners", els.CODE: "slug", els.REVIEW_STATE: "os_state", els.OBJECT_REVIEW: "os_state", els.STATE: "status" } ui_remap_items = { els.MANAGER: "manager", els.VERIFIED: "verified", els.STATUS: "status", els.LAST_UPDATED: "updated_at", els.AUDIT_CAPTAIN: "contact", els.CAS: "custom_attributes", els.MAPPED_OBJECTS: "objects_under_assessment", els.ASSIGNEES: "assessor", els.ASSIGNEES_: "assessor", els.CREATORS: "creator", els.CREATORS_: "creator", els.VERIFIERS: "verifier", els.VERIFIERS_: "verifier", element.AssessmentInfoWidget.COMMENTS_HEADER: "comments", els.PRIMARY_CONTACTS: "contact", els.CREATED_AT: "created_at", els.MODIFIED_BY: "modified_by", els.LAST_UPDATED_BY: "modified_by", els.UPDATED_AT: "updated_at" } csv_remap_items = { csv.REVISION_DATE: "updated_at" } result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return string_utils.dict_keys_to_upper_case(result_remap_items)
def items_of_remap_keys(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ from lib.constants import element, files els = element.TransformationElements csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { els.TITLE: "title", els.ADMIN: "owners", els.CODE: "slug", els.REVIEW_STATE: "os_state", els.OBJECT_REVIEW: "os_state", els.STATE: "status" } ui_remap_items = { els.MANAGER: "manager", els.VERIFIED: "verified", els.STATUS: "status", els.LAST_UPDATED: "updated_at", els.AUDIT_CAPTAIN: "contact", els.CAS: "custom_attributes", els.MAPPED_OBJECTS: "objects_under_assessment", els.ASSIGNEES: "assignee", els.CREATORS: "creator", els.VERIFIERS: "verifier", element.AssessmentInfoWidget.COMMENTS_HEADER: "comments", els.PRIMARY_CONTACTS: "contact", els.CREATED_AT: "created_at", els.MODIFIED_BY: "modified_by", els.LAST_UPDATED_BY: "modified_by", els.UPDATED_AT: "updated_at" } csv_remap_items = { csv.REVISION_DATE: "updated_at" } result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return string_utils.dict_keys_to_upper_case(result_remap_items)
def create_list_objs(self, entity_factory, list_scopes): """Create and return list of objects used entity factory and UI data (list of scopes UI text elements {"header": "item", ...} remapped to list of dicts {"attr": "value", ...}). Return list of created objects. """ list_factory_objs = [ entity_factory.create_empty() for _ in xrange(len(list_scopes)) ] list_scopes_with_upper_keys = [ string_utils.dict_keys_to_upper_case(scope) for scope in list_scopes ] list_scopes_to_convert = string_utils.exchange_dicts_items( transform_dict=Entity.items_of_remap_keys(), dicts=list_scopes_with_upper_keys, is_keys_not_values=True) # convert and represent values in scopes for scope in list_scopes_to_convert: # convert u'None', u'No person' to None type string_utils.update_dicts_values(scope, ["None", "No person"], None) for key, val in scope.iteritems(): if val: if key in ["mandatory", "verified"]: # convert u'false', u'true' like to Boolean scope[key] = string_utils.get_bool_value_from_arg(val) # convert datetime attributes' directly if key in ["updated_at", "created_at"]: scope[key] = string_utils.convert_str_to_datetime(val) # convert datetime attributes' in 'comments' if (key == "comments" and isinstance(val, list) and all( isinstance(comment, dict) for comment in val)): # u'(Creator) 07/06/2017 05:47:14 AM UTC' # to u'07/06/2017 05:47:14 AM UTC' scope[key] = [{ k: (string_utils.convert_str_to_datetime( re.sub(regex.TEXT_WITHIN_PARENTHESES, "", v)) if k == "created_at" else v) for k, v in comment.iteritems() } for comment in val] # convert multiple values to list of strings and split if need it if key in ["owners", "assessor", "creator", "verifier"]: # convert CSV like u'*****@*****.**' to u'Example User' val = val if val != url.DEFAULT_USER_EMAIL else roles.DEFAULT_USER # split multiple values if need 'Ex1, Ex2 F' to ['Ex1', 'Ex2 F'] scope[key] = val.split(", ") # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and (self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS) and "*" in val): scope[key] = val.replace("*", "") return [ Entity.update_objs_attrs_values_by_entered_data( obj_or_objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_scopes_to_convert, list_factory_objs) ]
def _create_list_objs(self, entity_factory, list_scopes): """Create and return list of objects used entity factory and UI data (list of scopes UI text elements {"header": "item", ...} remapped to list of dicts {"attr": "value", ...}). Return list of created objects. """ list_factory_objs = [ entity_factory.create_empty() for _ in xrange(len(list_scopes))] list_scopes_with_upper_keys = [ string_utils.dict_keys_to_upper_case(scope) for scope in list_scopes] list_scopes_to_convert = string_utils.exchange_dicts_items( transform_dict=Entity.items_of_remap_keys(), dicts=list_scopes_with_upper_keys, is_keys_not_values=True) # convert and represent values in scopes for scope in list_scopes_to_convert: # convert u'None', u'No person' to None type string_utils.update_dicts_values(scope, ["None", "No person"], None) for key, val in scope.iteritems(): if val: if key in ["mandatory", "verified"]: # convert u'false', u'true' like to Boolean scope[key] = string_utils.get_bool_value_from_arg(val) if key in ["updated_at", "created_at"]: # UI like u'08/20/2017' to date=2017-08-20, timetz=00:00:00 datetime_val = parser.parse(val) if str(datetime_val.time()) != "00:00:00": # UI like u'08/20/2017 07:30:45 AM +03:00' to date=2017-08-20, # timetz=04:30:45+00:00 if 'tzinfo', else: # CSV like u'08-20-2017 04:30:45' to date=2017-08-20, # timetz=04:30:45+00:00 datetime_val = ( datetime_val.astimezone(tz=tz.tzutc()) if datetime_val.tzinfo else datetime_val.replace(tzinfo=tz.tzutc())) scope[key] = datetime_val if (key == "comments" and isinstance(val, list) and all(isinstance(comment, dict) for comment in val)): # extract datetime from u'(Creator) 08/20/2017 07:30:45 AM +03:00' scope[key] = [ {k: (parser.parse(re.sub(regex.TEXT_W_PARENTHESES, string_utils.BLANK, v) ).astimezone(tz=tz.tzutc()) if k == "created_at" else v) for k, v in comment.iteritems()} for comment in val] # convert multiple values to list of strings and split if need it if (key in ["owners", "assessor", "creator", "verifier"] and not isinstance(val, list)): # split Tree View values if need 'Ex1, Ex2 F' to ['Ex1', 'Ex2 F'] # Info Widget values will be represent by internal methods scope[key] = val.split(", ") # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and (self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS) and string_utils.STAR in val): scope[key] = val.replace(string_utils.STAR, string_utils.BLANK) return [ Entity.update_objs_attrs_values_by_entered_data( obj_or_objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_scopes_to_convert, list_factory_objs)]
def _create_list_objs(self, entity_factory, list_scopes): """Create and return list of objects used entity factory and UI data (list of scopes UI text elements {"header": "item", ...} remapped to list of dicts {"attr": "value", ...}). Return list of created objects. """ list_factory_objs = [ entity_factory.create_empty() for _ in xrange(len(list_scopes))] list_scopes_with_upper_keys = [ string_utils.dict_keys_to_upper_case(scope) for scope in list_scopes] list_scopes_to_convert = string_utils.exchange_dicts_items( transform_dict=Entity.items_of_remap_keys(), dicts=list_scopes_with_upper_keys, is_keys_not_values=True) # convert and represent values in scopes for scope in list_scopes_to_convert: # convert u'None', u'No person' to None type string_utils.update_dicts_values(scope, ["None", "No person"], None) for key, val in scope.iteritems(): if val: if key in ["mandatory", "verified"]: # convert u'false', u'true' like to Boolean scope[key] = string_utils.get_bool_value_from_arg(val) if key in ["updated_at", "created_at"]: # UI like u'08/20/2017' to date=2017-08-20, timetz=00:00:00 datetime_val = parser.parse(val) if str(datetime_val.time()) != "00:00:00": # UI like u'08/20/2017 07:30:45 AM +03:00' to date=2017-08-20, # timetz=04:30:45+00:00 if 'tzinfo', else: # CSV like u'08-20-2017 04:30:45' to date=2017-08-20, # timetz=04:30:45+00:00 datetime_val = ( datetime_val.astimezone(tz=tz.tzutc()) if datetime_val.tzinfo else datetime_val.replace(tzinfo=tz.tzutc())) scope[key] = datetime_val if (key == "comments" and isinstance(val, list) and all(isinstance(comment, dict) for comment in val)): # extract datetime from u'(Creator) 08/20/2017 07:30:45 AM +03:00' scope[key] = [ {k: (parser.parse(re.sub(regex.TEXT_WITHIN_PARENTHESES, string_utils.BLANK, v) ).astimezone(tz=tz.tzutc()) if k == "created_at" else v) for k, v in comment.iteritems()} for comment in val] # convert multiple values to list of strings and split if need it if key in ["owners", "assessor", "creator", "verifier"]: # split multiple values if need 'Ex1, Ex2 F' to ['Ex1', 'Ex2 F'] scope[key] = val.split(", ") # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and (self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS) and string_utils.STAR in val): scope[key] = val.replace(string_utils.STAR, string_utils.BLANK) return [ Entity.update_objs_attrs_values_by_entered_data( obj_or_objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_scopes_to_convert, list_factory_objs)]
def create_list_objs(self, entity_factory, list_scopes): """Create and return list of objects used entity factory and UI data (list of scopes UI text elements {"header": "item", ...} remapped to list of dicts {"attr": "value", ...}). Return list of created objects. """ list_factory_objs = [ entity_factory.create_empty() for _ in xrange(len(list_scopes)) ] list_scopes_with_upper_keys = [ string_utils.dict_keys_to_upper_case(scope) for scope in list_scopes ] list_scopes_remapped = string_utils.remap_keys_for_list_dicts( dict_of_transform_keys=Entity.items_of_remap_keys(), list_dicts=list_scopes_with_upper_keys) # add extra 'owners' attribute name and value if 'creator' in scopes list_extra_scopes = [ dict(scope_remapped.items() + [("owners", scope_remapped.get("creator"))]) if "creator" in scope_remapped.keys() else scope_remapped for scope_remapped in list_scopes_remapped ] # convert and represent values in scopes for scope in list_extra_scopes: for key, val in scope.iteritems(): # convert u'false' and u'true' to boolean scope[key] = string_utils.get_bool_from_string(val) # convert multiple values to list of strings if key in ["owners", "assessor", "creator"]: # convert CSV like u'*****@*****.**' to u'Example User' val = val if val != url.DEFAULT_USER_EMAIL else roles.DEFAULT_USER scope[key] = string_utils.convert_to_list(val) # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS and "*" in val): scope[key] = val.replace("*", "") return [ EntitiesFactory.update_objs_attrs_values_by_entered_data( objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_extra_scopes, list_factory_objs) ]
def create_list_objs(self, entity_factory, list_scopes): """Create and return list of objects used entity factory and UI data (list of scopes UI text elements {"header": "item", ...} remapped to list of dicts {"attr": "value", ...}). Return list of created objects. """ list_factory_objs = [ entity_factory.create_empty() for _ in xrange(len(list_scopes))] list_scopes_with_upper_keys = [ string_utils.dict_keys_to_upper_case(scope) for scope in list_scopes] list_scopes_remapped = string_utils.remap_keys_for_list_dicts( dict_of_transform_keys=Entity.items_of_remap_keys(), list_dicts=list_scopes_with_upper_keys) # add extra 'owners' attribute name and value if 'creator' in scopes list_extra_scopes = [ dict(scope_remapped.items() + [("owners", scope_remapped.get("creator"))]) if "creator" in scope_remapped.keys() else scope_remapped for scope_remapped in list_scopes_remapped] # convert and represent values in scopes for scope in list_extra_scopes: for key, val in scope.iteritems(): # convert u'false' and u'true' to boolean scope[key] = string_utils.get_bool_from_string(val) # convert multiple values to list of strings if key in ["owners", "assessor", "creator"]: # convert CSV like u'*****@*****.**' to u'Example User' val = val if val != url.DEFAULT_USER_EMAIL else roles.DEFAULT_USER scope[key] = string_utils.convert_to_list(val) # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS and "*" in val): scope[key] = val.replace("*", "") return [ EntitiesFactory.update_objs_attrs_values_by_entered_data( objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_extra_scopes, list_factory_objs)]