def execute(id): if Branch.get_by_key_name(key): error[0] = 'Branch "%s" already exists' % key return branch = Branch(id=id, name=name, key_name=key) branch.put() return branch
class BranchTestCase(unittest.TestCase): def setUp(self): #the memcache will contain values that will break the tests #dont run this on production! memcache.flush_all() # First, create an instance of the Testbed class. self.testbed = testbed.Testbed() # Then activate the testbed, which prepares the service stubs for use. self.testbed.activate() # Create a consistency policy that will simulate the High Replication consistency model. self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0) # Initialize the datastore stub with this policy. self.testbed.init_datastore_v3_stub(consistency_policy=self.policy) millis = int(round(time.time() * 1000)) tree_name = 'test_tree' + str(millis) username = '******' + str(millis) self.parent_branch = Branch(id=tree_name) self.parent_branch.author_name = username self.parent_branch.link = '' self.parent_branch.content = '' self.parent_branch.put() self.child_branchs = [] def tearDown(self): for branch in self.child_branchs: branch.key.delete() self.parent_branch.key.delete() self.testbed.deactivate() def testUpdateNoDuplicate(self): branch = Branch() branch.link = 'testIdenticalLink.some_link' branch.content = 'some_content' branch.revision = 0 branch.parent_branch = self.parent_branch.key branch.parent_branch_authorname = self.parent_branch.authorname branch.put() self.parent_branch.append_child(branch) self.assertTrue(len(self.parent_branch.children()) == 1) self.child_branchs.append(branch)
class BranchTestCase(unittest.TestCase): def setUp(self): #the memcache will contain values that will break the tests #dont run this on production! memcache.flush_all() # First, create an instance of the Testbed class. self.testbed = testbed.Testbed() # Then activate the testbed, which prepares the service stubs for use. self.testbed.activate() # Create a consistency policy that will simulate the High Replication consistency model. self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy( probability=0) # Initialize the datastore stub with this policy. self.testbed.init_datastore_v3_stub(consistency_policy=self.policy) millis = int(round(time.time() * 1000)) tree_name = 'test_tree' + str(millis) username = '******' + str(millis) self.parent_branch = Branch(id=tree_name) self.parent_branch.author_name = username self.parent_branch.link = '' self.parent_branch.content = '' self.parent_branch.put() self.child_branchs = [] def tearDown(self): for branch in self.child_branchs: branch.key.delete() self.parent_branch.key.delete() self.testbed.deactivate() def testUpdateNoDuplicate(self): branch = Branch() branch.link = 'testIdenticalLink.some_link' branch.content = 'some_content' branch.revision = 0 branch.parent_branch = self.parent_branch.key branch.parent_branch_authorname = self.parent_branch.authorname branch.put() self.parent_branch.append_child(branch) self.assertTrue(len(self.parent_branch.children()) == 1) self.child_branchs.append(branch)
def get(self): a = Ari_types() a.ari = "Ad Hominem" a.description = "Against the person" a.put() a = Ari_types() a.ari = "Ad Hominem Tu Quoque" a.description = "You Too Fallacy" a.put() b = Branch() b.branch = "Business" b.description ="Business" b.put() b = Branch() b.branch = "Entertainment" b.description = "Entertainment" b.put() b = Branch() b.branch = "Economics" b.description = "Economics" b.put() b = Branch() b.branch = "Politics" b.description = "Politics" b.put()
def testUpdateNoDuplicate(self): branch = Branch() branch.link = 'testIdenticalLink.some_link' branch.content = 'some_content' branch.revision = 0 branch.parent_branch = self.parent_branch.key branch.parent_branch_authorname = self.parent_branch.authorname branch.put() self.parent_branch.append_child(branch) self.assertTrue(len(self.parent_branch.children()) == 1) self.child_branchs.append(branch)
def testValidator(self): test_link_text = "a" * 24 test_content_text = "b" * 24 branch = Branch() branch.link = test_link_text branch.content = test_content_text branch.put() self.assertEqual(branch.link, test_link_text) self.assertEqual(branch.content, test_content_text) branch = Branch() branch.link = "<a>" + test_link_text + "</a>" branch.content = "<a>" + test_content_text + "</a>" branch.put() self.assertEqual(branch.link, test_link_text) self.assertEqual(branch.content, test_content_text)
def testValidator(self): test_link_text = "a"*24 test_content_text = "b"*24 branch = Branch() branch.link = test_link_text branch.content = test_content_text branch.put() self.assertEqual(branch.link, test_link_text) self.assertEqual(branch.content, test_content_text) branch = Branch() branch.link = "<a>"+test_link_text+"</a>" branch.content = "<a>"+test_content_text+"</a>" branch.put() self.assertEqual(branch.link, test_link_text) self.assertEqual(branch.content, test_content_text)
def save_branch(self,authorname,parent_urlsafe_key,link,content): userinfo = UserInfo.get_by_username(authorname) if userinfo is None or not self.is_user_info_current(userinfo): return False, [ 'unauthenticated' ] branch = Branch() branch.authorname = authorname parent_key = ndb.Key(urlsafe=parent_urlsafe_key) parent_branch = parent_key.get() if parent_branch is None: return False, ['parent_branch_not_found'] branch.tree_name = parent_branch.tree_name tree = Tree.get_by_name(parent_branch.tree_name) if tree is None: return False, ['tree_not_found'] if tree.moderatorname == authorname or not tree.link_moderator_only: branch.link = link else: branch.link = "" if tree.moderatorname == authorname or not tree.content_moderator_only: branch.content = content else: branch.content = "" errors = self.validate_branch(tree, branch,authorname) #currently there is a db lock on unique links #eventually we should have a memcache lcok authored_branch_count = 0 branchs = parent_branch.children() if tree.single_thread and parent_branch.authorname == authorname: errors.append('has_single_thread_parent_branch') if tree.single_thread and len(branchs) > 0: errors.append('has_single_thread_branch') for branch_branch in branchs: if branch_branch.link == branch.link: errors.append('has_identical_link') if branch_branch.authorname == authorname: authored_branch_count += 1 if tree.branch_max > 0 and tree.branch_max <= authored_branch_count: errors.append('has_branches') if len(errors) == 0: branch.revision = 0 branch.parent_branch = parent_key branch.parent_branch_authorname = parent_branch.authorname branch.put() self.create_branch_version(branch) parent_branch.append_child(branch) notification = Notification() notification.from_username = branch.authorname if branch.authorname != parent_branch.authorname: notification.to_username = parent_branch.authorname notification.notification_type = 'new_branch' notification.branch = branch.key notification.branch_link = branch.link notification.tree_name = branch.tree_name notification.put() return True, branch else: return False, errors
def save_tree(self,tree_dict): try: tree_dict = self.merged_tree(tree_dict) except: return False, ['invalid_parameters'] #tree_name,moderatorname,conventions,root_branch_link,root_branch_content if tree_dict['moderatorname'] is None: return False, ['unauthenticated','no_moderator'] if tree_dict['tree_name'] is None: return False, ['empty_name'] author_info = UserInfo.get_by_username(tree_dict['moderatorname']) if author_info is None: return False, ['unauthenticated','moderator_not_found'] if not self.is_user_info_current(author_info): return False, ['unauthenticated','moderator_not_current'] if author_info.username is None: return False, ['invalid_user'] errors = [] if tree_dict['content_max'] < 16: errors.append('min_content_max') if tree_dict['link_max'] < 16: errors.append('min_link_max') if len(tree_dict['link_prompt']) > tree_dict['link_max']: errors.append('link_prompt_too_large') if len(tree_dict['content_prompt']) > tree_dict['content_max']: errors.append('content_prompt_too_large') tree_key = Tree.create_key(tree_dict['tree_name']) empty_name = tree_dict['tree_name'] is None or len(tree_dict['tree_name']) == 0 if empty_name: errors.append('empty_name') else: match = re.search(r'^[\d\w_\-]+$', tree_dict['tree_name']) isvalid = match and 4 <= len(tree_dict['tree_name']) and len(tree_dict['tree_name']) <= 20; if not isvalid: errors.append('invalid_name') branch = Branch(id=tree_dict['tree_name']) branch.authorname = tree_dict['moderatorname'] branch.link = tree_dict['root_branch_link'] branch.content = tree_dict['root_branch_content'] branch.tree_name = tree_dict['tree_name'] if branch.link == None or len(branch.link) == 0: errors.append('empty_root_branch_link') if branch.content == None or len(branch.content) == 0: errors.append('empty_root_branch_content') #let the user complete the other validation before trying to create the tree if len(errors) != 0: return False, errors tree = tree_key.get(); if tree: errors.append('tree_exists') if len(errors) == 0: #if two users enter identical information at the same time, then #whoever gets it second is the winner tree = Tree(id=tree_dict['tree_name'].lower(),name=tree_dict['tree_name']) tree.moderatorname = tree_dict['moderatorname'] tree.conventions = tree_dict['conventions'] tree.link_moderator_only = tree_dict['link_moderator_only'] tree.link_max = tree_dict['link_max'] tree.link_prompt = tree_dict['link_prompt'] tree.content_moderator_only = tree_dict['content_moderator_only'] tree.content_max = tree_dict['content_max'] tree.content_prompt = tree_dict['content_prompt'] tree.single_thread = tree_dict['single_thread'] tree.branch_max = tree_dict['branch_max'] branch.put() tree.put() notification = Notification() notification.from_username = tree.moderatorname #only set the to_username when a different user is performing the action notification.notification_type = 'new_tree' notification.tree_name = tree_dict['tree_name'] notification.put() if len(errors) == 0: return True, tree else: return False, errors