-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
36 lines (32 loc) · 1.09 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# """utils.py - File for collecting general utility functions."""
import logging
from google.appengine.ext import ndb
import endpoints
def get_by_urlsafe(urlsafe, model):
"""Returns an ndb.Model entity that the urlsafe key points to. Checks
that the type of entity returned is of the correct kind. Raises an
error if the key String is malformed or the entity is of the incorrect
kind
Args:
urlsafe: A urlsafe key string
model: The expected entity kind
Returns:
The entity that the urlsafe Key string points to or None if no entity
exists.
Raises:
ValueError:"""
try:
key = ndb.Key(urlsafe=urlsafe)
except TypeError:
raise endpoints.BadRequestException('Invalid Key')
except Exception, e:
if e.__class__.__name__ == 'ProtocolBufferDecodeError':
raise endpoints.BadRequestException('Invalid Key')
else:
raise
entity = key.get()
if not entity:
return None
if not isinstance(entity, model):
raise ValueError('Incorrect Kind')
return entity