Python google.appengine.api.datastore() Examples

The following are code examples for showing how to use google.appengine.api.datastore(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: python-compat-runtime   Author: GoogleCloudPlatform   File:    Apache License 2.0 6 votes vote down vote up
def _ToBlobstoreError(error):
  """Translates an application error to a datastore error, if possible.

    error: An `ApplicationError` to translate.

    The `BlobstoreError` for the passed-in `ApplicationError`, if one is found.
    Otherwise, the original `ApplicationError` is returned.
  error_map = {
  desired_exc = error_map.get(error.application_error)
  return desired_exc(error.error_detail) if desired_exc else error 
Example 2
Project: python-compat-runtime   Author: GoogleCloudPlatform   File:    Apache License 2.0 6 votes vote down vote up
def create_foreign_key(kind, key_is_id=False):
  """A method to make one-level Key objects.

  These are typically used in ReferenceProperty in Python, where the reference
  value is a key with kind (or model) name name.

  This helper method does not support keys with parents. Use create_deep_key
  instead to create keys with parents.

    kind: The kind name of the reference as a string.
    key_is_id: If true, convert the key into an integer to be used as an id.
      If false, leave the key in the input format (typically a string).

    Single argument method which parses a value into a Key of kind entity_kind.

  def generate_foreign_key_lambda(value):
    if key_is_id:
      value = int(value)
    return datastore.Key.from_path(kind, value)

  return generate_foreign_key_lambda 
Example 3
Project: python-compat-runtime   Author: GoogleCloudPlatform   File:    Apache License 2.0 6 votes vote down vote up
def _key_id_or_name_n(key, index):
  """Internal helper function for key id and name transforms.

    key: A datastore key.
    index: The depth in the key to return; 0 is root, -1 is leaf.

    The id or name of the nth deep sub key in key.
  if not key:
    return None
  path = key.to_path()
  if not path:
    return None
  path_index = (index * 2) + 1
  return path[path_index] 
Example 4
Project: python-compat-runtime   Author: GoogleCloudPlatform   File:    Apache License 2.0 5 votes vote down vote up
def blobproperty_from_base64(value):
  """Return a datastore blob property containing the base64 decoded value."""
  decoded_value = base64.b64decode(value)
  return datastore_types.Blob(decoded_value) 
Example 5
Project: python-compat-runtime   Author: GoogleCloudPlatform   File:    Apache License 2.0 4 votes vote down vote up
def create_deep_key(*path_info):
  """A method to make multi-level Key objects.

  Generates multi-level key from multiple fields in the input dictionary.

  This is typically used for Keys for entities which have variable parent keys,
  e.g. ones with owned relationships. It can used for both __key__ and

  Use create_foreign_key as a simpler way to create single level keys.

    path_info: List of tuples, describing (kind, property, is_id=False).
      kind: The kind name.
      property: The external property in the current import dictionary, or
        transform.CURRENT_PROPERTY for the value passed to the transform.
      is_id: Converts value to int and treats as numeric ID if True, otherwise
        the value is a string name. Default is False.
        create_deep_key(('rootkind', 'rootcolumn'),
                        ('childkind', 'childcolumn', True),
                        ('leafkind', transform.CURRENT_PROPERTY))

    Transform method which parses the info from the current neutral dictionary
    into a Key with parents as described by path_info.

  validated_path_info = []
  for level_info in path_info:
    if len(level_info) == 3:
      key_is_id = level_info[2]
    elif len(level_info) == 2:
      key_is_id = False
      raise bulkloader_errors.InvalidConfiguration(
          'Each list in create_deep_key must specify exactly 2 or 3 '
          'parameters, (kind, property, is_id=False). You specified: %s' %
    kind_name = level_info[0]
    property_name = level_info[1]
    validated_path_info.append((kind_name, property_name, key_is_id))

  def create_deep_key_lambda(value, bulkload_state):
    path = []
    for kind_name, property_name, key_is_id in validated_path_info:
      if property_name is CURRENT_PROPERTY:
        name_or_id = value
        name_or_id = bulkload_state.current_dictionary[property_name]

      if key_is_id:
        name_or_id = int(name_or_id)

      path += [kind_name, name_or_id]

    return datastore.Key.from_path(*path)

  return create_deep_key_lambda