Exemple #1
0
 def _convert_to_entity(self):
     init_map = {}
     nested_map = {}
     mapped_class = self._data.mapping.mapped_class
     for attr in \
         self.__mapping.attribute_iterator(mapped_class=mapped_class,
                                           key=self.__attribute_key):
         try:
             val = self._get_proxied_attribute_value(attr)
         except AttributeError:
             continue
         else:
             attr_name = attr.entity_attr
             if not '.' in attr_name:
                 init_map[attr_name] = val
             else:
                 nested_map[attr_name] = val
     ent_cls = get_entity_class(mapped_class)
     entity = ent_cls.create_from_data(init_map)
     for nested_name, nested_value in iteritems_(nested_map):
         set_nested_attribute(entity, nested_name, nested_value)
     return entity
Exemple #2
0
 def __action_one_direction(self, cardinality_relatee, rel_op,
                            relator, related, attr_name, safe):
     if cardinality_relatee == CARDINALITY_CONSTANTS.ONE:
         if rel_op == RELATION_OPERATIONS.ADD:
             set_nested_attribute(relator, attr_name, related)
         elif rel_op == RELATION_OPERATIONS.REMOVE:
             set_nested_attribute(relator, attr_name, None)
     else:
         relatee = get_nested_attribute(relator, attr_name)
         if rel_op == RELATION_OPERATIONS.ADD:
             if not (safe and related in relatee):
                 # FIXME: Assuming a list here.
                 relatee.append(related)
         elif rel_op == RELATION_OPERATIONS.REMOVE:
             # FIXME: Assuming a list here.
             if safe:
                 try:
                     relatee.remove(related)
                 except ValueError:
                     pass
             else:
                 relatee.remove(related)
Exemple #3
0
 def _convert_to_entity(self):
     init_map = {}
     nested_map = {}
     mapped_class = self._data.mapping.mapped_class
     for attr in \
         self.__mapping.attribute_iterator(mapped_class=mapped_class,
                                           key=self.__attribute_key):
         try:
             val = self._get_proxied_attribute_value(attr)
         except AttributeError:
             continue
         else:
             attr_name = attr.entity_attr
             if not '.' in attr_name:
                 init_map[attr_name] = val
             else:
                 nested_map[attr_name] = val
     ent_cls = get_entity_class(mapped_class)
     entity = ent_cls.create_from_data(init_map)
     for nested_name, nested_value in iteritems_(nested_map):
         set_nested_attribute(entity, nested_name, nested_value)
     return entity
Exemple #4
0
 def __action_one_direction(self, cardinality_relatee, rel_op, relator,
                            related, attr_name, safe):
     if cardinality_relatee == CARDINALITY_CONSTANTS.ONE:
         if rel_op == RELATION_OPERATIONS.ADD:
             set_nested_attribute(relator, attr_name, related)
         elif rel_op == RELATION_OPERATIONS.REMOVE:
             set_nested_attribute(relator, attr_name, None)
     else:
         relatee = get_nested_attribute(relator, attr_name)
         if rel_op == RELATION_OPERATIONS.ADD:
             if not (safe and related in relatee):
                 # FIXME: Assuming a list here.
                 relatee.append(related)
         elif rel_op == RELATION_OPERATIONS.REMOVE:
             # FIXME: Assuming a list here.
             if safe:
                 try:
                     relatee.remove(related)
                 except ValueError:
                     pass
             else:
                 relatee.remove(related)
Exemple #5
0
    def set_state_data(cls, entity, data):
        """
        Sets the state data for the given entity to the given data.

        This also works for unmanaged entities.
        """
        attr_names = get_domain_class_attribute_names(type(entity))
        nested_items = []
        for attr, new_attr_value in iteritems_(data):
            if not attr.entity_attr in attr_names:
                raise ValueError('Can not set attribute "%s" for entity '
                                 '"%s".' % (attr.entity_attr, entity))
            if '.' in attr.entity_attr:
                nested_items.append((attr, new_attr_value))
                continue
            else:
                setattr(entity, attr.entity_attr, new_attr_value)
        for attr, new_attr_value in nested_items:
            try:
                set_nested_attribute(entity, attr.entity_attr, new_attr_value)
            except AttributeError as exc:
                if not new_attr_value is None:
                    raise exc
Exemple #6
0
    def set_state_data(cls, entity, data):
        """
        Sets the state data for the given entity to the given data.

        This also works for unmanaged entities.
        """
        attr_names = get_domain_class_attribute_names(type(entity))
        nested_items = []
        for attr, new_attr_value in iteritems_(data):
            if not attr.entity_attr in attr_names:
                raise ValueError('Can not set attribute "%s" for entity '
                                 '"%s".' % (attr.entity_attr, entity))
            if '.' in attr.entity_attr:
                nested_items.append((attr, new_attr_value))
                continue
            else:
                setattr(entity, attr.entity_attr, new_attr_value)
        for attr, new_attr_value in nested_items:
            try:
                set_nested_attribute(entity, attr.entity_attr, new_attr_value)
            except AttributeError as exc:
                if not new_attr_value is None:
                    raise exc
Exemple #7
0
 def __set__(self, resource, value):
     set_nested_attribute(resource.get_entity(), self.entity_attr, value)
Exemple #8
0
 def __set__(self, resource, value):
     if not value is None:
         ent = value.get_entity()
     else:
         ent = None
     set_nested_attribute(resource.get_entity(), self.entity_attr, ent)
Exemple #9
0
 def __set__(self, resource, value):
     if not value is None:
         ent = value.get_entity()
     else:
         ent = None
     set_nested_attribute(resource.get_entity(), self.entity_attr, ent)
Exemple #10
0
 def __set__(self, resource, value):
     set_nested_attribute(resource.get_entity(), self.entity_attr, value)