treelib: přidané funkce insert_last_child, get_last_child a is_orphan
This commit is contained in:
		
							parent
							
								
									29687b01b5
								
							
						
					
					
						commit
						322718f436
					
				
					 1 changed files with 40 additions and 1 deletions
				
			
		|  | @ -12,6 +12,13 @@ def print_tree(node,indent=0): | |||
| 	if node.succ: | ||||
| 		print_tree(node.succ, indent=indent) | ||||
| 
 | ||||
| def is_orphan(node): | ||||
| 	""" Zjišťuje, jestli už je daný Node někde pověšený či nikoli. """ | ||||
| 	if safe_father_of_first(node) is None and safe_pred(node) is None: | ||||
| 		return True | ||||
| 	else: | ||||
| 		return False | ||||
| 
 | ||||
| # Django je trošku hloupé, takže node.prev nevrací None, ale hází django.core.exceptions.ObjectDoesNotExist | ||||
| def safe_pred(node): | ||||
| 	try: | ||||
|  | @ -43,6 +50,13 @@ def get_parent(node): | |||
| 	# ... a z prvního potomka umíme najít rodiče | ||||
| 	return safe_father_of_first(node) | ||||
| 
 | ||||
| def get_last_child(node): | ||||
| 	first = node.first_child | ||||
| 	if first is None: | ||||
| 		return None | ||||
| 	else: | ||||
| 		return last_brother(first) | ||||
| 
 | ||||
| # Obecný next: další Node v "the-right-order" pořadí (já, pak potomci, pak sousedé) | ||||
| def general_next(node): | ||||
| 	# Máme potomka? | ||||
|  | @ -100,12 +114,19 @@ def all_proper_brothers(node): | |||
| 			continue | ||||
| 		yield br | ||||
| 
 | ||||
| # Generátor potomků | ||||
| def all_children(node): | ||||
| 	""" Generátor všech potomků zadaného Node. """ | ||||
| 	brothers = all_brothers(node.first_child) | ||||
| 	for br in brothers: | ||||
| 		yield br | ||||
| 
 | ||||
| def all_children_of_type(node, type): | ||||
| 	""" Generuje všechny potomky daného Node a daného typu. """ | ||||
| 	brothers = all_brothers(node.first_child) | ||||
| 	for br in brothers: | ||||
| 		if isinstance(br, type): | ||||
| 			yield br | ||||
| 
 | ||||
| # Generátor následníků v "the-right-order" | ||||
| # Bez tohoto vrcholu | ||||
| def all_following(node): | ||||
|  | @ -173,6 +194,24 @@ def create_child(parent, type, **kwargs): | |||
| 		new_node.succ = orig_child | ||||
| 		new_node.save() | ||||
| 
 | ||||
| def insert_last_child(parent, node): | ||||
| 	""" Zadaný Node přidá jako posledního potomka otce. """ | ||||
| 	last = get_last_child(parent) | ||||
| 	if not is_orphan(node): | ||||
| 		print(safe_pred(node)) | ||||
| 		print(safe_father_of_first(node)) | ||||
| 		if len(safe_father_of_first(node).get_real_instances()) == 0: | ||||
| 			print("Related Manager je prázdný.") | ||||
| 		print(type(safe_father_of_first(node).queryset_class)) | ||||
| 		raise TreeLibError("Snažíš se přidat do stromu Node, který už je zavěšený.") | ||||
| 
 | ||||
| 	if last is None: | ||||
| 		parent.first_child = node | ||||
| 		parent.save() | ||||
| 	else: | ||||
| 		last.succ = node | ||||
| 		last.save() | ||||
| 
 | ||||
| def create_node_before(successor, type, **kwargs): | ||||
| 	if safe_pred(successor) is not None: | ||||
| 		# Easy: přidáme za předchůdce | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Anet
						Anet