【发布时间】:2017-06-11 05:31:08
【问题描述】:
我正在构建一个简单的时事通讯应用程序来收集电子邮件地址。我有一个通过 ajax 将电子邮件值发送到我的 python 应用程序的表单,但是我一辈子都无法弄清楚如何查看电子邮件是否已经存在。下面的代码目前可以正常工作,我只是不知道在哪里/如何添加“检查预先存在的实体”的东西。
import webapp2
import json
from google.appengine.ext import ndb
class Email(ndb.Model):
email = ndb.StringProperty()
subscribed = ndb.BooleanProperty()
@staticmethod
def create(email):
ekey = ndb.Key("Email", email)
entity = Email.get_or_insert(ekey)
if entity.email: ###
# This email already exists
return None
entity.email = email
entity.subscribed = True
entity.put()
return entity
class Subscribe(webapp2.RequestHandler):
def post(self):
add = Email.create(self.request.get('email'))
success = add is not None
self.response.headers['Content-Type'] = 'application/json'
obj = {
'success': success
}
self.response.out.write(json.dumps(obj))
app = webapp2.WSGIApplication([
webapp2.Route(r'/newsletter/new', Subscribe),
], debug=True)
【问题讨论】:
标签: python google-app-engine datastore