def update_tree(self,tree_dict): try: tree_dict = self.merged_tree(tree_dict) except: return False, ['invalid_parameters'] if tree_dict['tree_name'] is None: return False, ['tree_not_found'] if tree_dict['moderatorname'] is None: return False, ['unauthenticated'] author_info = UserInfo.get_by_username(tree_dict['moderatorname']) if author_info is None or not self.is_user_info_current(author_info): return False, ['unauthenticated'] if author_info.username is None: return False, ['invalid_user'] tree = Tree.get_by_name(tree_dict['tree_name']) 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'] 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 = 'edit_tree' notification.tree_name = tree_dict['tree_name'] notification.put() return True, tree
def delete_branch(self,authorname,urlsafe_key): userinfo = UserInfo.get_by_username(authorname) if userinfo is None or not self.is_user_info_current(userinfo): return False, [ 'unauthenticated' ] branch = ndb.Key(urlsafe=urlsafe_key).get() if len(branch.children()) != 0: return False, [ 'not_empty' ] tree = Tree.get_by_name(branch.tree_name) if branch.authorname != authorname and tree.moderatorname != authorname: return False, [ 'not_author' ] branch.detached_parent_branch = branch.parent_branch branch.parent_branch = None branch.put(); branch.detached_parent_branch.get().empty_children_cache() return True, None
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 update_branch(self,authorname,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 = ndb.Key(urlsafe=urlsafe_key).get() tree = Tree.get_by_name(branch.tree_name) if tree is None: return False, [ 'tree_not_found' ] if branch.authorname != authorname and tree.moderatorname != authorname: return False, [ 'not_author' ] if tree.moderatorname == authorname or not tree.link_moderator_only: branch.link = link if tree.moderatorname == authorname or not tree.content_moderator_only: branch.content = content errors = self.validate_branch(tree, branch,authorname) #Comment on thread safety: #Branch creation will be a VERY frequent operation. Multiple branchs with identical links #Isn't an application error, just an annoyance for the user. So we allow this to occur #without a lock in place parent_branch = None if branch.parent_branch is not None: parent_branch = branch.parent_branch.get(); branchs = parent_branch.children() for branch_branch in branchs: if branch_branch.link == branch.link and branch.key != branch_branch.key: errors.append('has_identical_link') if len(errors) == 0: if branch.revision is None: branch.revision = 1 else: branch.revision = branch.revision + 1 branch.put() self.create_branch_version(branch) if parent_branch is not None: parent_branch.update_child(branch) notification = Notification() notification.from_username = authorname #only set the to_username when a different user is performing the action if branch.authorname != authorname: notification.to_username = branch.authorname notification.notification_type = 'edit_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