Exemple #1
0
  def __init__(cls, name, bases, dct):
    """Initializes a class that belongs to a polymorphic hierarchy.

    This method configures a few built-in attributes of polymorphic
    models:

      __root_class__: If the new class is a root class, __root_class__ is set to
        itself so that it subclasses can quickly know what the root of
        their hierarchy is and what kind they are stored in.
      __class_hierarchy__: List of classes describing the new model's place
        in the class hierarchy.  The first element is always the root
        element while the last element is the new class itself.  For example:

          class Foo(PolymorphicClass): ...

          class Bar(Foo): ...

          class Baz(Bar): ...

          Foo.__class_hierarchy__ == [Foo]
          Bar.__class_hierarchy__ == [Foo, Bar]
          Baz.__class_hierarchy__ == [Foo, Bar, Baz]

    Unless the class is a root class or PolyModel itself, it is not
    inserted in to the kind-map like other models.  However, all polymorphic
    classes, are inserted in to the class-map which maps the class-key to
    implementation.  This class key is consulted using the polymorphic instances
    discriminator (the 'class' property of the entity) when loading from the
    datastore.
    """
    if name == 'PolyModel' or PolyModel not in bases:
      db._initialize_properties(cls, name, bases, dct)
      super(db.PropertiedClass, cls).__init__(name, bases, dct)
    else:
      cls.__root_class__ = cls
      super(PolymorphicClass, cls).__init__(name, bases, dct)

    if name == 'PolyModel':
      return

    if cls is not cls.__root_class__:
      poly_class = None
      for base in cls.__bases__:
        if issubclass(base, PolyModel):
          poly_class = base
          break
      else:
        raise db.ConfigurationError(
            "Polymorphic class '%s' does not inherit from PolyModel."
            % cls.__name__)

      cls.__class_hierarchy__ = poly_class.__class_hierarchy__ + [cls]
    else:
      cls.__class_hierarchy__ = [cls]

    _class_map[cls.class_key()] = cls
def add_fields_to_index_by_prefix_properties(model_class, *properties):

  # Record the prefix properties.
  if not hasattr(model_class, '_fields_to_index_by_prefix_properties'):
    model_class._fields_to_index_by_prefix_properties = []
  model_class._fields_to_index_by_prefix_properties += list(properties)

  # adding the names_prefix property used for the index
  if not hasattr(model_class, 'names_prefixes'):
    model_class.names_prefixes = []

  # Update the model class.
  db._initialize_properties(
      model_class, model_class.__name__, model_class.__bases__,
      model_class.__dict__)
Exemple #3
0
def add_prefix_properties(model_class, *properties):
    """Adds indexable properties to a model class to support prefix queries.
    All properties ending in '_' are extra properties.  The 'properties'
    arguments should be names of existing string properties on the class."""
    for property in properties:
        # This property contains a copy of the entire string normalized.
        setattr(model_class, property + '_n_', db.StringProperty())

        # This property contains just the first character, normalized.
        setattr(model_class, property + '_n1_', db.StringProperty())

        # This property contains just the first two characters, normalized.
        setattr(model_class, property + '_n2_', db.StringProperty())

    # Record the prefix properties.
    if not hasattr(model_class, '_prefix_properties'):
        model_class._prefix_properties = []
    model_class._prefix_properties += list(properties)

    # Update the model class.
    db._initialize_properties(model_class, model_class.__name__,
                              model_class.__bases__, model_class.__dict__)
Exemple #4
0
def add_prefix_properties(model_class, *properties):
    """Adds indexable properties to a model class to support prefix queries.
    All properties ending in '_' are extra properties.  The 'properties'
    arguments should be names of existing string properties on the class."""
    for property in properties:
        # This property contains a copy of the entire string normalized.
        setattr(model_class, property + '_n_', db.StringProperty())

        # This property contains just the first character, normalized.
        setattr(model_class, property + '_n1_', db.StringProperty())

        # This property contains just the first two characters, normalized.
        setattr(model_class, property + '_n2_', db.StringProperty())

    # Record the prefix properties.
    if not hasattr(model_class, '_prefix_properties'):
        model_class._prefix_properties = []
    model_class._prefix_properties += list(properties)

    # Update the model class.
    db._initialize_properties(
        model_class, model_class.__name__, model_class.__bases__,
        model_class.__dict__)