2017-01-25 7 views
0

私は電子メールアドレスを収集する簡単なニュースレターアプリを構築しています。私はPythonアプリケーションにajax経由で電子メールの値を送信するフォームを持っていますが、私の人生のために、電子メールがすでに存在するかどうかを調べる方法を理解できません。以下のコードは現在機能していますが、 "既存のエンティティを確認する"ものを追加する場所や場所はわかりません。ndbデータストアの重複するプロパティを防止する

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) 

答えて

1

Danのコメントに基づいて回答が更新されました。ダン、訂正してくれてありがとう。更新された質問に対応するためにさらに更新されました。

メールエンティティのIDとしてメールアドレスを設定し、get_or_insertを使用できます。ここでは、電子メールストレージはIDであり、プロパティでもあるため、冗長です。この冗長性を取り除くには、おそらく###の別のプロパティをチェックしてください。

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)) 

私は上記のコードをテストしていないので、間違いがあるかもしれませんが、正しい経路を導くはずです。

+2

トランザクション内で祖先以外のクエリを作成できないため、#2オプションは機能しません。また、そのようなクエリは最終的に一貫しているので、重複は可能です。 –

+0

それはクロス・グループ・トランザクションなので、それはうまくいけませんか? –

+1

"トランザクションでは、祖先クエリのみが許可されます。" - どんな種類の取引でも問題ありません。 –

1

これは、常に別のモデルクラスを作成するための良いアプローチです。別のモデルクラスを作成し、Subscribersクラスのpostメソッドを更新しました。電子メールが存在する場合はfalseを返します。それ以外の場合はtrueを返します。私はあなたの問題を解決することを願っています。

class EmailModel(ndb.Model): 
     email = ndb.StringProperty() 
     subscribed = ndb.BooleanProperty() 

class Subscribe(webapp2.RequestHandler): 
     def post(self): 
     email = self.request.get('email') 
     entity = EmailModel.query(EmailModel.email == email).get() 
     if entity: 
      # Duplicate 
      check = False 
     else: 
      add = Email() 
      add.email = self.request.get('email') 
      add.subscribed = True 
      add.put() 
      check = True 
     if check: 
       self.response.headers['Content-Type'] = 'application/json' 
       obj = { 
       'success': True 
       } 
       self.response.out.write(json.dumps(obj)) 
     else: 
       self.response.headers['Content-Type'] = 'application/json' 
       obj = { 
       'success': False 
       } 
       self.response.out.write(json.dumps(obj)) 
+2

このメソッドは、最終的な整合性のために重複したエントリのための余地を残すことに注意してください - エンティティが存在する(最近作成された) –

+0

@DanCornilescu私はこの方法を私の異なるウェブサイトで使用していますが、まだ何らかの複製に直面していません。信頼できるソリューションを提案して、私のウェブサイトでも更新できるようにしてください。 –

関連する問題