Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_included(self,do_nesting=False):
janus_logger.debug("Loading and mapping included objects.")
included = []
#get all members of the subclass containing Attribute members that are relations and have a mapping as a dict.
#key => member name in the sub class.
#value => the Attribute inside of this member.
relations = {attr:object.__getattribute__(self,attr)
for attr in dir(self)
if not callable(getattr(self,attr))
and type(object.__getattribute__(self,attr)) == Attribute
and issubclass(object.__getattribute__(self,attr).value_type,DataMessage) == True
and (object.__getattribute__(self,attr).nested == False or do_nesting == False)
and object.__getattribute__(self,attr).mapping != None
and not attr.startswith("__")}
#for each member containing an Attribute object that is a relations set its value
#to the value retrieved from the python object as specified in the
#Attribute mapping and set it to the Attribute objects value.
for attr in relations:
#load key first (for relations element)
value = self.__data_object
value_path = relations[attr].mapping.split('.') #get mapping to the keys of this relations and split by '.', because this indicates a deeper path to get it.
for path_element in value_path: #go down this path in the python object to find the value
if hasattr(value,path_element):
current_value = getattr(value,path_element) #get the next value of current path element.
value = current_value() if callable(current_value) else current_value #call the attribute if it is callable otherwise just read value
if value_type in self.__primitive_types or value_type == list or value_type == dict or issubclass(value_type,DataMessage):
self.value_type = value_type
self.name = name
self.required = required
self.mapping = mapping
self.read_only = read_only
self.write_only = write_only
self.nested = nested
self.nested_type=nested_type
if nested == True and nested_type == None:
janus_logger.error('If nested == True nested_type has to be set.')
raise Exception('If nested == True nested_type has to be set.')
if issubclass(value_type,DataMessage): #relationship
self.key_mapping = key_mapping
else:
janus_logger.error('Value Type must be either be a simple type such as ' + str(self.__primitive_types) + ', a subclass of DataMessage or a list or dict containing these types.')
raise Exception('Value Type must be either be a simple type such as ' + str(self.__primitive_types) + ', a subclass of DataMessage or a list or dict containing these types.')
else:
setattr(self,attr,DataMessage.from_message(json.dumps(val),object.__getattribute__(self,attr).value_type))
setattr(nested[attr],'updated',True) #mark this attribute as updated for later updating the backend object
else:
if nested[attr].required == True:
janus_logger.error('Missing required field ' + str(nested[attr].name) + ".")
raise Exception('Missing required field ' + str(nested[attr].name) + ".")
if 'relationships' in message:
#get relationships
relations = {attr:object.__getattribute__(self,attr)
for attr in dir(self)
if not callable(getattr(self,attr))
and type(object.__getattribute__(self,attr)) == Attribute
and issubclass(object.__getattribute__(self,attr).value_type,DataMessage) == True
and object.__getattribute__(self,attr).nested == False
and object.__getattribute__(self,attr).key_mapping != None
and object.__getattribute__(self,attr).name != 'id'
and not attr.startswith("__")}
for attr in relations:
if relations[attr].name in message['relationships']:
rel_objects = []
if isinstance(message['relationships'][relations[attr].name]['data'], (list, tuple)):
for item in message['relationships'][relations[attr].name]['data']:
rel_object = relations[attr].value_type()
rel_object.id = item['id']
rel_objects.append(rel_object)
else:
rel_object = relations[attr].value_type()
#first check if there is an response object
#if not nothing to return so HTTP 204
#otherwise process response
if messages == None:
if self.before_send_hook != None:
self.before_send_hook(204,None,None)
return None
else:
if isinstance(messages, (list, tuple)) == False:
raise Exception('Methods using the "describe" decorator have to return a list of subclasses of DataMessage to describe.')
msg_descriptions = []
for msg in messages:
if issubclass(msg,DataMessage) == False:
raise Exception('All returned classes in the returned list have to be a subclass of DataMessage.')
msg_descriptions.append(msg().describe())
meta = {'message-types':msg_descriptions}
message = JsonApiMessage(meta=meta).to_json() #render json response
if self.before_send_hook != None: #fire before send hook
self.before_send_hook(self.success_status,message,None)
return message
except Exception as e:
err_msg = ErrorMessage.from_exception(e)
tb = traceback.format_exc()