Exemplo n.º 1
0
    def from_entity(cls, entity):

        if (_PATH_KEY_PROPERTY in entity
                and tuple(entity[_PATH_KEY_PROPERTY]) != cls.path_key()):
            key = entity[_PATH_KEY_PROPERTY].split(':')
            try:
                abspath = os.path.abspath(os.path.dirname(__file__))
                if abspath not in sys.path:
                    sys.path.insert(0, abspath)

                    module = __import__(str('.'.join(key[0:-1])), globals(),
                                        locals, str(key[-1]))  #### FIX THIS
                    imported_class = getattr(module, key[-1])
                    obj = imported_class()

                    _class_map[obj.class_key()] = obj.__class__

                    return obj.from_entity(entity)

            except ImportError:
                raise db.KindError('Could not import model hierarchy \'%s\'' %
                                   str(key))

        if (_CLASS_KEY_PROPERTY in entity
                and tuple(entity[_CLASS_KEY_PROPERTY]) != cls.class_key()):
            key = tuple(entity[_CLASS_KEY_PROPERTY])
            try:
                poly_class = _class_map[key]
            except KeyError:
                raise db.KindError('No implementation for class \'%s\'' %
                                   (key, ))

            return poly_class.from_entity(entity)
        return super(PolyPro, cls).from_entity(entity)
Exemplo n.º 2
0
    def from_entity(cls, entity):

        if(_PATH_KEY_PROPERTY in entity and
           tuple(entity[_PATH_KEY_PROPERTY]) != cls.path_key()):
            key = entity[_PATH_KEY_PROPERTY].split(':')
            try:
                abspath = os.path.abspath(os.path.dirname(__file__))
                if abspath not in sys.path:
                    sys.path.insert(0,abspath)
                    
                    imported_class = DataController.import_model(key)
                    obj = imported_class()
                    
                    _class_map[obj.class_key()] = obj.__class__
                    
                    return obj.from_entity(entity)

            except ImportError:
                raise db.KindError('Could not import model hierarchy \'%s\'' % str(key))

        if (_CLASS_KEY_PROPERTY in entity and
            tuple(entity[_CLASS_KEY_PROPERTY]) != cls.class_key()):
            key = tuple(entity[_CLASS_KEY_PROPERTY])
            try:
                poly_class = _class_map[key]
            except KeyError:
                raise db.KindError('No implementation for class \'%s\'' % key)
            return poly_class.from_entity(entity)
        return super(PolyModel, cls).from_entity(entity)
Exemplo n.º 3
0
        def get(cls, keys, **kwds):
            '''Fetch a specific Model type instance from given storage 
      layers, using keys. 
      Args:
        See pdb.get
        
        Inherited:
          keys: Key within datastore entity collection to find; or string key;
            or list of Keys or string keys.
          config: datastore_rpc.Configuration to use for this request.
  
      Returns:
        See pdb.get
        
      Raises:
        KindError if any of the retrieved objects are not instances of the
          type associated with call to 'get'.
      '''
            models = pdb.get(keys, **kwds)

            #Class kind check
            temp = models
            if isinstance(temp, dict):
                temp = dict.values()
            elif isinstance(temp, db.Model):
                temp = [temp]

            if temp is None:
                return None

            for instance in temp:
                if not isinstance(instance, cls) and instance is not None:
                    raise db.KindError('Kind %r is not a subclass of kind %r' %
                                       (instance, cls))
            return models
Exemplo n.º 4
0
    def get(cls, keys, **kwargs):
        """Fetch instance from the datastore of a specific Model type using key.
        Keeps the same comportement of db.Model.get()
        
        TODO(sahid): Needs doc.
        """
        results = get(keys, **kwargs)
        if results is None:
            return None
        if isinstance(results, db.Model):
            instances = [results]
        else:
            instances = results

        for instance in instances:
            if not(instance is None or isinstance(instance, cls)):
                raise db.KindError('Kind %r is not a subclass of kind %r' %
                                (instance.kind(), cls.kind()))
        return results
Exemplo n.º 5
0
  def from_entity(cls, entity):
    """Load from entity to class based on discriminator.

    Rather than instantiating a new Model instance based on the kind
    mapping, this creates an instance of the correct model class based
    on the entities class-key.

    Args:
      entity: Entity loaded directly from datastore.

    Raises:
      KindError when there is no class mapping based on discriminator.
    """
    if (_CLASS_KEY_PROPERTY in entity and
        tuple(entity[_CLASS_KEY_PROPERTY]) != cls.class_key()):
      key = tuple(entity[_CLASS_KEY_PROPERTY])
      try:
        poly_class = _class_map[key]
      except KeyError:
        raise db.KindError('No implementation for class \'%s\'' % (key,))
      return poly_class.from_entity(entity)
    return super(PolyModel, cls).from_entity(entity)