2016-05-06 12 views
0

previous questionサブクラス化を繰り返す性質が

背景

に続いて反復可能ではない私は、同じユーザーが指定したグループを追加することができないようにしたいです。

構造サブクラスプロパティコード(ユーザのコレクションを含むプロパティ)上記プロパティを使用

from google.appengine.ext import ndb 


class MembersStructuredProperty(ndb.StructuredProperty): 
    def _validate(self, value): 
     if value in self: 
      raise Exception('Duplicate detected') 

    def _call_to_base_type(self, value): 
     return value 

    def _call_from_base_type(self, value): 
     return value 

モデルコードは

from google.appengine.ext import ndb 
from mainsite.rainbow.models.CFCSocialUser import CFCSocialUser 
from mainsite.rainbow.models.CFCBaseModel import BaseModel 
from mainsite.rainbow.models.properties.CFCMembersStructuredProperty import MembersStructuredProperty 


class CFCSocialGroup(BaseModel): 

    name = ndb.StringProperty(required=True) 
    created_by = ndb.StructuredProperty(CFCSocialUser) 
    members = MembersStructuredProperty(CFCSocialUser, repeated=True) 

    @staticmethod 
    def create_group(name): 
     """Create a new group""" 
     group = CFCSocialGroup(name=name) 
     group.members = [] 
     return group 

    def add_member(self, social_user): 
     """Add a member to the local group""" 
     if self.contains_user(social_user): 
      raise Exception('Duplicate user detected') 
     else: 
      self.members.append(social_user) 

    def contains_user(self, social_user): 
     """Checks if user is a member of the given group""" 
     for user in self.members: 
      if user.username == social_user.username: 
       return True 
      else: 
       return False 

およびテストケース

from tests.cfcsocialtests.testbase import CFCTestBase_NDB 
from nose.tools import * 
from nose.plugins.attrib import attr 
from mainsite.rainbow.models.CFCSocialGroup import CFCSocialGroup 
from tests.test_CFCSocialUser import create_user 


class TestMembersStructuredProperty(CFCTestBase_NDB): 
    @attr("CRUD") 
    def test_validation_works(self): 
     """Test to see if the validation works on the property""" 
     group = CFCSocialGroup.create_group('Group1') 
     user = create_user() 
     group.members = [user] 
     group.put() 
であります

ケースが次のエラーで実行されません

vinay$ nosetests -v test_membersStructuredProperty.py 
Test to see if the validation works on the property ... ERROR 

====================================================================== 
ERROR: Test to see if the validation works on the property 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/tests/test_membersStructuredProperty.py", line 14, in test_validation_works 
    group.members = [user] 
    File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1400, in __set__ 
    self._set_value(entity, value) 
    File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1142, in _set_value 
    value = [self._do_validate(v) for v in value] 
    File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1092, in _do_validate 
    value = self._call_shallow_validation(value) 
    File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1284, in _call_shallow_validation 
    return call(value) 
    File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1331, in call 
    newvalue = method(self, value) 
    File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/mainsite/rainbow/models/properties/CFCMembersStructuredProperty.py", line 6, in _validate 
    if value in self: 
TypeError: argument of type 'MembersStructuredProperty' is not iterable 

答えて

1

documentation状態:

_validate()、...扱うようにない必要を行います

  • ...
  • 繰り返される値:インフラがかかります繰り返し値のリスト項目ごとに_from_base_type()または_to_base_type()を呼び出す注意。

これは繰り返しフィールド内のアイテム間で検証を行う場所ではありません。むしろ、それはモデル検証において行われるべきである。

関連する問題