Python 通过电子邮件查询端点用户
我正在尝试制作一种方法,允许我通过用户电子邮件查询端点。有没有更好的方法来做到这一点,然后我在下面做什么?可能只返回一条或零条记录的记录Python 通过电子邮件查询端点用户,python,google-app-engine,google-cloud-endpoints,endpoints-proto-datastore,Python,Google App Engine,Google Cloud Endpoints,Endpoints Proto Datastore,我正在尝试制作一种方法,允许我通过用户电子邮件查询端点。有没有更好的方法来做到这一点,然后我在下面做什么?可能只返回一条或零条记录的记录 @User.query_method(query_fields=('email',), path='get_by_mail', name='user.get_by_email') def get_by_email(self, query):
@User.query_method(query_fields=('email',),
path='get_by_mail',
name='user.get_by_email')
def get_by_email(self, query):
return query
我假设
User
是从EndpointsModel
继承的某个自定义模型。否则,这将失败。换句话说,你做过这样的事情:
from google.appengine.ext import ndb
from endpoints_proto_datastore.ndb import EndpointsModel
class User(EndpointsModel):
email = ndb.StringProperty()
...
解决此问题有两种主要方法,您可以使用电子邮件
作为实体的键,或者滚动您自己的查询并尝试获取两个实体,以查看结果是否唯一和存在
选项1:使用电子邮件
作为键
您可以执行一个完整的查询,而不是执行完整的查询
通过使用电子邮件作为每个实体的数据存储密钥,如中所述。例如:
from endpoints_proto_datastore.ndb import EndpointsAliasProperty
class User(EndpointsModel):
# remove email here, as it will be an alias property
...
def EmailSet(self, value):
# Validate the value any way you like
self.UpdateFromKey(ndb.Key(User, value))
@EndpointsAliasProperty(setter=IdSet, required=True)
def email(self):
if self.key is not None: return self.key.string_id()
选项2:滚动您自己的查询
但稍后我可能想通过他的电话号码找到一个用户。我该怎么办?也许我做得完全不对。我想制作一个android应用程序,由用户google帐户进行身份验证,然后基本上有一个端点模型,其中有一个对该用户的引用和一个电话号码。。。建议?你介意再问一个关于识别用户的最佳方法的问题吗?在这里,auth似乎被完全忽略了,但在存储用户数据时,auth应该是您首先关心的问题。一点也不。我可以向你保证,你的时间没有被浪费,我从你的回答中学到了一些东西。谢谢
from endpoints_proto_datastore.ndb import EndpointsAliasProperty
class User(EndpointsModel):
# remove email here, as it will be an alias property
...
def EmailSet(self, value):
# Validate the value any way you like
self.UpdateFromKey(ndb.Key(User, value))
@EndpointsAliasProperty(setter=IdSet, required=True)
def email(self):
if self.key is not None: return self.key.string_id()
@User.method(request_fields=('email',),
path='get_by_mail/{email}',
http_method='GET', name='user.get_by_email')
def get_by_email(self, user):
query = User.query(User.email == user.email)
# We fetch 2 to make sure we have
matched_users = query.fetch(2)
if len(matched_users == 0):
raise endpoints.NotFoundException('User not found.')
elif len(matched_users == 2):
raise endpoints.BadRequestException('User not unique.')
else:
return matched_users[0]