Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#for each member containing an Attribute object that is no 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 attributes:
value = obj #start in the object itself to search for value
value_path = attributes[attr].mapping.split('.') #get mapping 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
try: #Did a simple try/except, because hassattr actually calls the member
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
except AttributeError:
value = None
if value == None: #check if this field is required
if attributes[attr].required:
janus_logger.error('Missing required field ' + str(attributes[attr].name) + ".")
raise Exception('Missing required field ' + str(attributes[attr].name) + ".")
else:
if isinstance(value,attributes[attr].value_type) == False: #check if actual value fit's value_type
if ((attributes[attr].value_type == str or attributes[attr].value_type == str) and (isinstance(value,bytes) or isinstance(value,str))) == False:
janus_logger.error('Expected ' + str(attributes[attr].value_type) + " got " + str(type(value)) + " for " + str(attributes[attr].name) + " of " + str(self.__type_name) + ".")
raise Exception('Expected ' + str(attributes[attr].value_type) + " got " + str(type(value)) + " for " + str(attributes[attr].name) + " of " + str(self.__type_name) + ".")
if attributes[attr].name == 'id': #if the attributes name is id, set it to the object'S id, because id is not inside "attributes"
setattr(self,'id',value)
else:
attributes[attr].value = value #set loaded value to the Attribute object's value.
if do_nesting:
#nested records
#get all members of the subclass containing Attribute members that are nested as a dict.
#key => member name in the sub class.
#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
else:
if relations[attr].required:
janus_logger.error("Keypath: " + str(value_path) + " returned None for path element " + path_element + " on message type " + self.__type_name)
raise InternalServerErrorException("Keypath: " + str(value_path) + " returned None for path element " + path_element + " on message type " + self.__type_name)
else:
value = None
continue # skip this not required relationship, because it'S value is None.
#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 == None:
if relations[attr].required:
janus_logger.error("Keypath: " + str(value_path) + " returned None for path element " + path_element + " on message type " + self.__type_name)
raise InternalServerErrorException("Keypath: " + str(value_path) + " returned None for path element " + path_element + " on message type " + self.__type_name)
else:
continue # skip this not required relationship, because it'S value is None.
def __check_list(self,list_value):
for item in list_value:
if not item in self.__primitive_types:
janus_logger.error('Attribute ' + self.name + ' contains invalid object of type ' + str(type(item)) + ". Valid types are " + str(self.__primitive_types))
raise Exception('Attribute ' + self.name + ' contains invalid object of type ' + str(type(item)) + ". Valid types are " + str(self.__primitive_types))
janus_logger.error("JanusResponse data can't be None.")
raise Exception("JanusResponse data can't be None.")
#check message
if self.message == None:
janus_logger.error("JanusResponse message can't be None.")
raise Exception("JanusResponse message can't be None.")
#check message type
if issubclass(self.message,DataMessage) == False:
janus_logger.error("JanusResponse message must be subclass of janus.DataMessage.")
raise Exception("JanusResponse message must be subclass of janus.DataMessage.")
#check meta
if self.meta != None and isinstance(self.meta,dict) == False:
janus_logger.error("Meta has to be a dict with non jsonapi standard information.")
raise Exception('Meta has to be a dict with non jsonapi standard information.')
def __get_id_attribute(self):
#check if there is a id attribute in the subclass
result = [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) == False
and object.__getattribute__(self,attr).mapping != None
and object.__getattribute__(self,attr).name == 'id'
and not attr.startswith("__")]
if len(result) == 1: #id attribute found
return result[0]
else:
janus_logger.error(self.__type_name + " is missing Attribute 'id'.")
raise Exception(self.__type_name + " is missing Attribute 'id'.")
def __convert_to_value_type(self,name,value):
if value == None:
return None
else:
#try to convert to desired type for simple types
if issubclass(object.__getattribute__(self,name).value_type,DataMessage) == False:
_type = object.__getattribute__(self,name).value_type
try:
return _type(value)
except:
janus_logger.error("Failed to convert " + str(value) + " to " + str(_type) + " in " + self.__type_name)
raise AttributeError("Failed to convert " + str(value) + " to " + str(_type) + " in " + self.__type_name)
else:
return value
"""
Used to get a DataMessage (an object derived from DataMessage) with values in its
Attribute members loaded from a jsonapi request object (raw string request) according to Attribute objects mapping.
msg_class => the class (derived from DataMessage) which should be used as message class. (This class will be initialized and returned)
"""
json_message = json.loads(raw_message) #parse raw_message to json
if json_message == None: #no request body
return None
#get data part
data = None
if 'data' in json_message:
data = json_message['data']
else:
janus_logger.error("Message is missing data.")
raise Exception("Message is missing data.")
msg = msg_class()
msg.map_message(data)
return msg
self.message = message
self.include_relationships = include_relationships
#check data
if self.data == None:
janus_logger.error("JanusResponse data can't be None.")
raise Exception("JanusResponse data can't be None.")
#check message
if self.message == None:
janus_logger.error("JanusResponse message can't be None.")
raise Exception("JanusResponse message can't be None.")
#check message type
if issubclass(self.message,DataMessage) == False:
janus_logger.error("JanusResponse message must be subclass of janus.DataMessage.")
raise Exception("JanusResponse message must be subclass of janus.DataMessage.")
#check meta
if self.meta != None and isinstance(self.meta,dict) == False:
janus_logger.error("Meta has to be a dict with non jsonapi standard information.")
raise Exception('Meta has to be a dict with non jsonapi standard information.')