Move child-lifting code to its own helper method.
This commit is contained in:
		
							parent
							
								
									ae46b097e4
								
							
						
					
					
						commit
						943deb9bf7
					
				
					 1 changed files with 18 additions and 12 deletions
				
			
		|  | @ -76,6 +76,23 @@ class GroupableMixin: | ||||||
|     group_sql_index = None |     group_sql_index = None | ||||||
|     group_table = None |     group_table = None | ||||||
| 
 | 
 | ||||||
|  |     def _lift_children(self): | ||||||
|  |         ''' | ||||||
|  |         If this object is a root, all of its children become roots. | ||||||
|  |         If this object is a child, its parent adopts all of its children. | ||||||
|  |         ''' | ||||||
|  |         parent = self.get_parent() | ||||||
|  |         if parent is None: | ||||||
|  |             pairs = { | ||||||
|  |                 'parentid': self.id, | ||||||
|  |             } | ||||||
|  |             self.photodb.sql_delete(table=self.group_table, pairs=pairs) | ||||||
|  |         else: | ||||||
|  |             pairs = { | ||||||
|  |                 'parentid': (self.id, parent.id), | ||||||
|  |             } | ||||||
|  |             self.photodb.sql_update(table=self.group_table, pairs=pairs, where_key='parentid') | ||||||
|  | 
 | ||||||
|     @decorators.transaction |     @decorators.transaction | ||||||
|     def add_child(self, member, *, commit=True): |     def add_child(self, member, *, commit=True): | ||||||
|         ''' |         ''' | ||||||
|  | @ -138,18 +155,7 @@ class GroupableMixin: | ||||||
|             for child in self.get_children(): |             for child in self.get_children(): | ||||||
|                 child.delete(delete_children=delete_children, commit=False) |                 child.delete(delete_children=delete_children, commit=False) | ||||||
|         else: |         else: | ||||||
|             # Lift children |             self._lift_children() | ||||||
|             parent = self.get_parent() |  | ||||||
|             if parent is None: |  | ||||||
|                 # Since this group was a root, children become roots by removing |  | ||||||
|                 # the row. |  | ||||||
|                 self.photodb.sql_delete(table=self.group_table, pairs={'parentid': self.id}) |  | ||||||
|             else: |  | ||||||
|                 # Since this group was a child, its parent adopts all its children. |  | ||||||
|                 data = { |  | ||||||
|                     'parentid': (self.id, parent.id), |  | ||||||
|                 } |  | ||||||
|                 self.photodb.sql_update(table=self.group_table, pairs=data, where_key='parentid') |  | ||||||
| 
 | 
 | ||||||
|         # Note that this part comes after the deletion of children to prevent |         # Note that this part comes after the deletion of children to prevent | ||||||
|         # issues of recursion. |         # issues of recursion. | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue