Ejemplo n.º 1
0
class Human2(ndb.Model):
    name = ndb.StringProperty("na", indexed=True)
    gender = ndb.msgprop.EnumProperty(Gender, "g", required=True, indexed=True)
    age = ndb.IntegerProperty("ag", indexed=False)
    items = ndb.StructuredProperty(Items, "i", required=True)
    numbers = ndb.JsonProperty('json', indexed=False)
    description = ndb.TextProperty("t", indexed=False)
    description2 = ndb.TextProperty("t2", compressed=True, indexed=False)
    meters_tall = ndb.FloatProperty("mtrs", indexed=False)
    datetime_of_birth = ndb.DateTimeProperty("dtb", indexed=False)
    date_of_birth = ndb.DateProperty("db", indexed=False)
    time_of_birth = ndb.TimeProperty("tb", indexed=False)
    hobbies = ndb.StringProperty('hob', repeated=True, indexed=False)
    pickle = ndb.PickleProperty('pi', indexed=False)
    binary = ndb.BlobProperty("bi", indexed=False)
    home = ndb.GeoPtProperty("ho", indexed=False)
    generic = ndb.GenericProperty("gen", indexed=False)
    model = ndb.LocalStructuredProperty(Items, "mo", indexed=False)
    person_details = ndb.msgprop.MessageProperty(person_pb2.Person, "pd")
    key_prop = ndb.KeyProperty(Department)
    key_prop2 = ndb.KeyProperty()

    number_of_hobbies = ndb.ComputedProperty(
        name="num_hob", func=lambda self: len(self.hobbies), indexed=False)
    default_info = ndb.StringProperty("di", indexed=False, default='unknown')
    update = ndb.DateTimeProperty("up", indexed=False, auto_now=True)

    def _pre_put_hook(self):
        pass
Ejemplo n.º 2
0
class BaseModel(ndb.Model):
    """Common model properties and functionality."""

    # String used for properties with no available data (None).
    NOT_SET = "N/A"

    created_at = ndb.DateTimeProperty(auto_now_add=True)

    def __init__(self, *args, **kwargs):
        self._get_client()  # Activates all NDB ORM required features.
        kwargs.update(NDB_KWARGS)
        super().__init__(*args, **kwargs)

    @classmethod
    def normalize(cls, value):
        """Normalizes a property value which needs to be rendered."""
        if value is None:
            return cls.NOT_SET
        return value

    @staticmethod
    def _get_client():
        """Singleton for the Datastore client."""
        global client
        if not client:
            client = datastore.Client(**NDB_KWARGS)
            ndb.enable_use_with_gcd(client=client, **NDB_KWARGS)
        return client

    @classmethod
    def query(cls, **kwargs):
        """Creates a Datastore query out of this model."""
        query = cls._get_client().query(kind=cls._get_kind(), **kwargs)
        return query

    @classmethod
    def all(cls, query=None, keys_only=False, **kwargs):
        """Returns all the items in the DB created by this model.

        Args:
            query: Optionally you can supply a custom `query`.
            keys_only (bool): Keep the Key properties only if this is True.
        Returns:
            list: Fetched items.
        """
        query = query or cls.query()
        query.order = ["-created_at"]
        if keys_only:
            query.keys_only()
        return list(query.fetch(**kwargs))

    @property
    def myself(self):
        """Returns the current DB version of the same object."""
        return self.key.get()

    @property
    def exists(self):
        """Checks if the entity is saved into the Datastore."""
        try:
            return bool(self.myself) if self.key and self.key.id else False
        except Exception:
            return False

    def put(self):
        """Saves the entity into the Datastore."""
        self._get_client().put(self)
        return self.key

    @classmethod
    def put_multi(cls, entities):
        """Multiple save in the DB without interfering with the `cls.put` function."""
        cls._get_client().put_multi(entities)
        return [entity.key for entity in entities]

    def remove(self):
        """Removes current entity and its dependencies (if covered and any)."""
        self._get_client().delete(self.key)

    @classmethod
    def remove_multi(cls, keys):
        """Multiple removal of entities based on the given `keys`."""
        cls._get_client().delete_multi(keys)

    @property
    def urlsafe(self):
        """Returns an URL safe Key string which uniquely identifies this entity."""
        return self.key.to_legacy_urlsafe().decode(settings.ENCODING)

    @classmethod
    def get(cls, urlsafe_or_key):
        """Retrieves an entity object based on an URL safe Key string or Key object."""
        cls._get_client()
        if isinstance(urlsafe_or_key, (str, bytes)):
            key = ndb.Key(cls, **NDB_KWARGS)
            complete_key = key.from_legacy_urlsafe(urlsafe_or_key)
        else:
            complete_key = urlsafe_or_key

        item = complete_key.get()
        if not item:
            raise Exception("item doesn't exist")
        return item