Ejemplo n.º 1
0
def _CoerceQueryParam(field, query_param):
  """Attempts to coerce `query_param` to match the ndb type of `field`.

  Args:
    field: The ndb field being queried.
    query_param: The query term to be coerced.

  Returns:
    The query param coerced if a coercion was possible.

  Raises:
    QueryTypeError: If there is an error with the type conversion.
  """
  if isinstance(field, ndb.IntegerProperty):
    try:
      return int(query_param)
    except ValueError:
      raise QueryTypeError(
          'Query param "%s" could not be converted to integer' % query_param)
  elif isinstance(field, ndb.BooleanProperty):
    if query_param.lower() == 'true':
      return True
    elif query_param.lower() == 'false':
      return False
    else:
      raise QueryTypeError(
          'Query param "%s" could not be converted to boolean' % query_param)
  elif isinstance(field, ndb.KeyProperty):
    key = model_utils.GetKeyFromUrlsafe(query_param)
    if not key:
      raise QueryTypeError(
          'Query param "%s" could not be converted to ndb.Key' % query_param)
    return key
  else:
    return query_param
Ejemplo n.º 2
0
  def get(self, vote_key):
    logging.debug('Vote handler get method called with key: %s', vote_key)
    key = utils.GetKeyFromUrlsafe(vote_key)
    if not key:
      self.abort(
          httplib.BAD_REQUEST,
          explanation='Vote key %s could not be parsed' % vote_key)

    vote = key.get()
    if vote:
      response = vote.to_dict()
      response['candidate_id'] = vote.key.parent().parent().id()
      self.respond_json(response)
    else:
      self.abort(httplib.NOT_FOUND, explanation='Vote not found.')
Ejemplo n.º 3
0
    def testAdminGetQuery(self):
        """Admin queries a user."""
        params = {'search': self.user_1.email, 'searchBase': 'userEmail'}

        with self.LoggedInUser(admin=True):
            response = self.testapp.get('', params)

        output = response.json

        self.assertIn('application/json', response.headers['Content-type'])
        self.assertIsInstance(output, dict)
        self.assertEqual(len(output['content']), 3)

        key = utils.GetKeyFromUrlsafe(output['content'][0]['key'])
        self.assertEqual(key.flat()[1], output['content'][0]['candidateId'])
Ejemplo n.º 4
0
    def get(self, rule_key):
        logging.debug('Rule handler get method called with key: %s', rule_key)
        key = utils.GetKeyFromUrlsafe(rule_key)
        if not key:
            self.abort(httplib.BAD_REQUEST,
                       explanation='Rule key %s could not be parsed' %
                       rule_key)

        rule = key.get()
        if rule:
            response = rule.to_dict()
            response['target_id'] = rule.key.parent().id()
            self.respond_json(response)
        else:
            self.abort(httplib.NOT_FOUND, explanation='Rule not found')
Ejemplo n.º 5
0
 def testGetKeyFromUrlsafe_Error(self):
   self.assertIsNone(utils.GetKeyFromUrlsafe('not a real ndb key string'))
Ejemplo n.º 6
0
 def testGetKeyFromUrlsafe(self):
   key = ndb.Key('A', 'a', 'B', 'b')
   self.assertEqual(key, utils.GetKeyFromUrlsafe(key.urlsafe()))
Ejemplo n.º 7
0
 def testError(self):
     self.assertIsNone(
         datastore_utils.GetKeyFromUrlsafe('not a real ndb key string'))
Ejemplo n.º 8
0
 def testSuccess(self):
     key = ndb.Key('A', 'a', 'B', 'b')
     self.assertEqual(key, datastore_utils.GetKeyFromUrlsafe(key.urlsafe()))