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 update_obj_attrs_values(obj, is_replace_attrs_values, is_allow_none_values, **arguments): """Update object's attributes values.""" for obj_attr_name in arguments: if (obj_attr_name in Entity.get_attrs_names_for_entities(obj.__class__)): _obj_attr_value = arguments.get(obj_attr_name) condition = (True if is_allow_none_values else _obj_attr_value) if condition and not is_replace_values_of_dicts: # convert repr from objects to dicts exclude datetime objects obj_attr_value = ( cls.convert_objs_repr_to_dict(_obj_attr_value) if not isinstance(_obj_attr_value, datetime) else _obj_attr_value) if not is_replace_attrs_values: origin_obj_attr_value = getattr(obj, obj_attr_name) obj_attr_value = ( dict(origin_obj_attr_value.items() + obj_attr_value.items()) if obj_attr_name == "custom_attributes" else help_utils. convert_to_list(origin_obj_attr_value) + help_utils.convert_to_list(obj_attr_value)) setattr(obj, obj_attr_name, obj_attr_value) if obj_attr_name in [ "creator", "assessor", "verifier" ]: from lib.entities.entities_factory import ObjectPersonsFactory if not isinstance(obj.assignees, dict): obj.assignees = dict() obj.assignees[obj_attr_name.capitalize()] = ([ ObjectPersonsFactory().default().__dict__ ]) if is_replace_values_of_dicts and isinstance( _obj_attr_value, dict): obj_attr_value = string_utils.exchange_dicts_items( transform_dict=_obj_attr_value, dicts=help_utils.convert_to_list( getattr(obj, obj_attr_name)), is_keys_not_values=False) obj_attr_value = (obj_attr_value if isinstance( getattr(obj, obj_attr_name), list) else obj_attr_value[0]) setattr(obj, obj_attr_name, obj_attr_value) return obj
def update_obj_attrs_values(obj, is_replace_attrs_values, is_allow_none_values, **arguments): """Update object's attributes values.""" for obj_attr_name in arguments: if (obj_attr_name in Entity.get_attrs_names_for_entities(obj.__class__)): _obj_attr_value = arguments.get(obj_attr_name) condition = (True if is_allow_none_values else _obj_attr_value) if condition and not is_replace_values_of_dicts: # convert repr from objects to dicts exclude datetime objects obj_attr_value = ( cls.convert_objs_repr_to_dict(_obj_attr_value) if not isinstance(_obj_attr_value, datetime) else _obj_attr_value) if not is_replace_attrs_values: origin_obj_attr_value = getattr(obj, obj_attr_name) obj_attr_value = ( dict(origin_obj_attr_value.items() + obj_attr_value.items()) if obj_attr_name == "custom_attributes" else help_utils.convert_to_list(origin_obj_attr_value) + help_utils.convert_to_list(obj_attr_value)) setattr(obj, obj_attr_name, obj_attr_value) if obj_attr_name in ["creator", "assessor", "verifier"]: from lib.entities.entities_factory import ObjectPersonsFactory if not isinstance(obj.assignees, dict): obj.assignees = dict() obj.assignees[obj_attr_name.capitalize()] = ( [ObjectPersonsFactory().default().__dict__]) if is_replace_values_of_dicts and isinstance(_obj_attr_value, dict): obj_attr_value = string_utils.exchange_dicts_items( transform_dict=_obj_attr_value, dicts=help_utils.convert_to_list( getattr(obj, obj_attr_name)), is_keys_not_values=False) obj_attr_value = ( obj_attr_value if isinstance(getattr(obj, obj_attr_name), list) else obj_attr_value[0]) setattr(obj, obj_attr_name, obj_attr_value) return obj
def update_obj_attrs_values(obj, is_replace_attrs_values, is_allow_none_values, **arguments): """Update object's attributes values.""" for obj_attr_name in arguments: if (obj_attr_name in Entity().get_attrs_names_for_entities( obj.__class__)): _obj_attr_value = arguments.get(obj_attr_name) condition = (True if is_allow_none_values else _obj_attr_value) if condition and not is_replace_values_of_dicts: # convert repr from objects to dicts exclude datetime objects obj_attr_value = ( cls.convert_obj_repr_from_obj_to_dict( _obj_attr_value) if not isinstance(_obj_attr_value, datetime) else _obj_attr_value) if not is_replace_attrs_values: origin_obj_attr_value = getattr(obj, obj_attr_name) obj_attr_value = ( dict(origin_obj_attr_value.items() + obj_attr_value.items()) if obj_attr_name == "custom_attributes" else string_utils. convert_to_list(origin_obj_attr_value) + string_utils.convert_to_list(obj_attr_value)) setattr(obj, obj_attr_name, obj_attr_value) if is_replace_values_of_dicts and isinstance( _obj_attr_value, dict): obj_attr_value = string_utils.exchange_dicts_items( transform_dict=_obj_attr_value, dicts=string_utils.convert_to_list( getattr(obj, obj_attr_name)), is_keys_not_values=False) obj_attr_value = (obj_attr_value if isinstance( getattr(obj, obj_attr_name), list) else obj_attr_value[0]) setattr(obj, obj_attr_name, obj_attr_value) return obj