Ejemplo n.º 1
0
    def _GetFieldValue(cls, field):
        """Returns the value of a field as the correct type.

    Args:
      field: The field whose value is extracted.  If the given field is None, this
        function also returns None. This is to make it easier to chain with
        GetFieldInDocument().

    Returns:
      The value of the field with the correct type (float for number fields,
      datetime.datetime for date fields, etc).

    Raises:
      TypeError: if the type of the field isn't recognized.
    """
        if not field:
            return None
        value_type = field.value().type()

        if value_type in search_util.TEXT_DOCUMENT_FIELD_TYPES:
            return field.value().string_value()
        if value_type == document_pb.FieldValue.DATE:
            value = field.value().string_value()
            return search_util.DeserializeDate(value)
        if value_type == document_pb.FieldValue.NUMBER:
            value = field.value().string_value()
            return float(value)
        if value_type == document_pb.FieldValue.GEO:
            value = field.value().geo()
            return geo_util.LatLng(value.lat(), value.lng())
        raise TypeError('No conversion defined for type %s' % value_type)
Ejemplo n.º 2
0
 def _ResolveDistanceArg(self, node):
   if node.getType() == QueryParser.VALUE:
     return query_parser.GetQueryNodeText(node)
   if node.getType() == QueryParser.FUNCTION:
     name, args = node.children
     if name.getText() == 'geopoint':
       lat, lng = (float(query_parser.GetQueryNodeText(v)) for v in args.children)
       return geo_util.LatLng(lat, lng)
   return None
Ejemplo n.º 3
0
  def IsMatch(self, field_values, op):
    self._CheckOp(op)



    for field_value in field_values:
      geo_pb = field_value.geo()
      geopoint = geo_util.LatLng(geo_pb.lat(), geo_pb.lng())
      if self._IsDistanceMatch(geopoint, op):
        return True


    return False
Ejemplo n.º 4
0
 def _Geopoint(self, *nodes):
     latitude, longitude = (self._Eval(node) for node in nodes)
     return geo_util.LatLng(latitude, longitude)