blob: 6e64db6d19b2520978f4feca0ad6010ae8e62eb0 [file] [log] [blame]
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""App Engine datastore related functions and classes."""
from django.core import validators
from google.appengine.api import datastore_errors
from google.appengine.ext import ndb
def email_validator(property, value):
"""Validates whether the input value for the specified property
is a valid email address.
The function signature is required by NBD API. Property parameter is not
actually used in this function.
Args:
property: validated model property
value: value to validate
Raises:
ValueError: if the specified value is not a valid email address.
"""
try:
# use internal django's validator
validators.validate_email(value)
except Exception:
raise ValueError('%s is not a valid email address.' % value)
_LINK_VALIDATOR = validators.URLValidator()
def link_validator(property, value):
"""Validates whether the input value for the specified property
is a valid URL.
The function signature is required by NBD API. Property parameter is not
actually used in this function.
Args:
property: validated model property
value: value to validate
Raises:
ValueError: if the specified value is not a valid URL.
"""
try:
# use internal django's validator
_LINK_VALIDATOR(value)
except Exception:
raise ValueError('%s is not a valid URL.' % value)
def min_value_validator(min_value):
"""Returns a validator that validates whether the input value is not lower
than the specified min_value.
Args:
min_value: Int that contains the minimum value for the validator.
Returns:
A validating function that for does not accept values which are lower
than the specified minimum.
"""
def validator(prop, value):
if value < min_value:
raise datastore_errors.BadValueError(
'Value %i is lower than the min_value %i' % (value, min_value))
return validator
def addFilterToQuery(query, prop, values):
"""Extends the specified query by adding a filter on the specified property
with the specified value.
Args:
query: query object to extend.
prop: property of the query model on which to add a filter.
values: sequence of values to compare with the property value.
"""
if len(values) == 1:
query.filter(prop.name, values[0])
else:
query.filter('%s IN' % prop.name, values)
def addFilterToNDBQuery(query, prop, values):
"""Extends the specified query by adding a filter on the specified property
with the specified value.
Args:
query: query object to extend.
prop: property of the query model on which to add a filter.
values: sequence of values to compare with the property value.
"""
if len(values) == 1:
return query.filter(prop == values[0])
else:
return query.filter(prop.IN(values))
def toDict(entity, exclude_computed=None):
"""Returns a dict with values of a datastore entity.
Args:
entity: Datastore entity.
exclude_computed: If set to True, computed properties are not included.
in the result.
Returns:
A dict mapping properties to the corresponding values.
Raises:
TypeError: if the specified entity is not App Engine datastore entity.
"""
if not exclude_computed:
return entity.to_dict()
else:
values = {}
for prop_name, prop_type in entity._properties.iteritems():
if type(prop_type) != ndb.ComputedProperty:
values[prop_name] = getattr(entity, prop_name)
return values