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
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.')
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'])
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')
def testGetKeyFromUrlsafe_Error(self): self.assertIsNone(utils.GetKeyFromUrlsafe('not a real ndb key string'))
def testGetKeyFromUrlsafe(self): key = ndb.Key('A', 'a', 'B', 'b') self.assertEqual(key, utils.GetKeyFromUrlsafe(key.urlsafe()))
def testError(self): self.assertIsNone( datastore_utils.GetKeyFromUrlsafe('not a real ndb key string'))
def testSuccess(self): key = ndb.Key('A', 'a', 'B', 'b') self.assertEqual(key, datastore_utils.GetKeyFromUrlsafe(key.urlsafe()))