How to use the janus.janus.DataMessage.from_object function in janus

To help you get started, we’ve selected a few janus examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xamoom / xamoom-janus / janus / janus.py View on Github external
and not attr.startswith("__")
                            and object.__getattribute__(self,attr).name != 'id'
                            and object.__getattribute__(self,attr).key_value != None}

        #if there are relations we add them to the dict using "relations" as key.
        if len(relations.keys()) > 0: msg['relationships'] = relations

        ##nested records
        #get all members of the subclass containing Attribute members that are nested records, which do not contain
        #None as value value and their name is not id, because id is treated diferrently.
        #key => attribute name as specified in the Attribute object
        #value => the loaded object serialized to json api message
        nested = {}
        if do_nesting:
            nested = {
                            object.__getattribute__(self,attr).name:{'data':self.nested_to_dict(DataMessage.from_object(object.__getattribute__(self,attr).value,object.__getattribute__(self,attr).value_type,include_relationships=True,do_nesting=True))}
                                        for attr in dir(self)
                                            if not callable(object.__getattribute__(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 == True
                                            and not attr.startswith("__")
                                            and object.__getattribute__(self,attr).name != 'id'
                                            and object.__getattribute__(self,attr).value != None
                                }
        else:
            nested = {
                        object.__getattribute__(self,attr).name:object.__getattribute__(self,attr).key_value
                                    for attr in dir(self)
                                        if not callable(object.__getattribute__(self,attr))
                                        and type(object.__getattribute__(self,attr)) == Attribute
                                        and issubclass(object.__getattribute__(self,attr).value_type,DataMessage) == True
github xamoom / xamoom-janus / janus / decorators.py View on Github external
#caching
                    loaded_from_cache = False

                    if self.cached_get_hook != None:
                        cached_object = self.cached_get_hook(response_obj)
                        if cached_object != None:
                            loaded_from_cache = True
                            message = cached_object #returned cached, already mapped, response

                            janus_logger.info("Will return cached message: " + str(loaded_from_cache))

                    if loaded_from_cache == False: #nothing in cache or cache deactivated
                        self.message = response_obj.message #get the message type to return
                        obj = response_obj.data #get the data to return

                        data = DataMessage.from_object(obj,self.message,do_nesting=self.nest_in_responses) #generate data message with data

                        #take care of includes
                        if response_obj.include_relationships != None: self.include_relationships = response_obj.include_relationships
                        included = None

                        janus_logger.info("Should map included: " + str(self.include_relationships))
                        if self.include_relationships:
                            included = self.__load_included(data,self.nest_in_responses)

                        #is there custome meta?
                        if response_obj.meta != None:
                            if self.meta == None:
                                self.meta = response_obj.meta
                            else:
                                self.meta.update(response_obj.meta)
github xamoom / xamoom-janus / janus / janus.py View on Github external
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.


            #data = DataMessage.from_object(value,object.__getattribute__(self,attr).value_type,include_relationships=False) #map but without relationships
            data = DataMessage.from_object(value,object.__getattribute__(self,attr).value_type,include_relationships=True,do_nesting=do_nesting) #map now with relationships

            if isinstance(data,list) == True:
                for d in data: included.append(d.to_dict())
            else:
                included.append(data.to_dict())

        if do_nesting:
            nested_attributes = self.get_all_nested_included()

            for attribute in nested_attributes:
                if isinstance(attribute.value,list) == True:
                    for v in attribute.value: included.append(DataMessage.from_object(v,attribute.value_type,include_relationships=True,do_nesting=True).to_dict(do_nesting=True))
                else:
                    included.append(included.append(DataMessage.from_object(attribute.value,attribute.value_type,include_relationships=True,do_nesting=True).to_dict(do_nesting=True)))

        janus_logger.debug("Loaded and mapped " + str(len(included)) + " included objects.")
github xamoom / xamoom-janus / janus / janus.py View on Github external
#data = DataMessage.from_object(value,object.__getattribute__(self,attr).value_type,include_relationships=False) #map but without relationships
            data = DataMessage.from_object(value,object.__getattribute__(self,attr).value_type,include_relationships=True,do_nesting=do_nesting) #map now with relationships

            if isinstance(data,list) == True:
                for d in data: included.append(d.to_dict())
            else:
                included.append(data.to_dict())

        if do_nesting:
            nested_attributes = self.get_all_nested_included()

            for attribute in nested_attributes:
                if isinstance(attribute.value,list) == True:
                    for v in attribute.value: included.append(DataMessage.from_object(v,attribute.value_type,include_relationships=True,do_nesting=True).to_dict(do_nesting=True))
                else:
                    included.append(included.append(DataMessage.from_object(attribute.value,attribute.value_type,include_relationships=True,do_nesting=True).to_dict(do_nesting=True)))

        janus_logger.debug("Loaded and mapped " + str(len(included)) + " included objects.")

        return included
github xamoom / xamoom-janus / janus / janus.py View on Github external
def get_nested_included(self,nested_included):
        nested = [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 == True
                            and object.__getattribute__(self,attr).mapping != None
                            and not attr.startswith("__")]

        nested_included += nested

        for nested_obj in nested:
            obj = nested_obj.value
            msg = DataMessage.from_object(obj,nested_obj.value_type,include_relationships=True,do_nesting=True)
            if isinstance(msg,list) == True:
                for m in msg:
                    m.get_nested_included(nested_included)
            else:
                msg.get_nested_included(nested_included)

        return nested_included
github xamoom / xamoom-janus / janus / janus.py View on Github external
#data = DataMessage.from_object(value,object.__getattribute__(self,attr).value_type,include_relationships=False) #map but without relationships
            data = DataMessage.from_object(value,object.__getattribute__(self,attr).value_type,include_relationships=True,do_nesting=do_nesting) #map now with relationships

            if isinstance(data,list) == True:
                for d in data: included.append(d.to_dict())
            else:
                included.append(data.to_dict())

        if do_nesting:
            nested_attributes = self.get_all_nested_included()

            for attribute in nested_attributes:
                if isinstance(attribute.value,list) == True:
                    for v in attribute.value: included.append(DataMessage.from_object(v,attribute.value_type,include_relationships=True,do_nesting=True).to_dict(do_nesting=True))
                else:
                    included.append(included.append(DataMessage.from_object(attribute.value,attribute.value_type,include_relationships=True,do_nesting=True).to_dict(do_nesting=True)))

        janus_logger.debug("Loaded and mapped " + str(len(included)) + " included objects.")

        return included