<s> """<STR_LIT>""" <EOL> import mptt <EOL> from django . db import models <EOL> from acacia import managers , signals <EOL> class AbstractTopic ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> parent = models . ForeignKey ( "<STR_LIT>" , null = True , blank = True , <EOL> related_name = "<STR_LIT>" ) <EOL> objects = managers . TopicManager ( ) <EOL> separator = u"<STR_LIT:/>" <EOL> class Meta : <EOL> abstract = True <EOL> unique_together = [ ( "<STR_LIT:name>" , "<STR_LIT>" ) ] <EOL> def __unicode__ ( self ) : <EOL> return self . full_name ( ) <EOL> def full_name ( self ) : <EOL> if ( not hasattr ( self , "<STR_LIT>" ) or <EOL> self . parent_id != self . _cached_parent ) : <EOL> if self . parent is not None : <EOL> parents = self . separator . join ( <EOL> self . get_ancestors ( ) . values_list ( "<STR_LIT:name>" , flat = True ) ) <EOL> self . _full_name_cache = u"<STR_LIT>" % ( parents , self . separator , <EOL> self . name ) <EOL> else : <EOL> self . _full_name_cache = self . name <EOL> self . _cached_parent = self . parent_id <EOL> return self . _full_name_cache <EOL> def merge_to ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> manager = self . __class__ . objects <EOL> try : <EOL> merge_node = manager . get ( parent = parent , name = self . name ) <EOL> except self . DoesNotExist : <EOL> signals . pre_move . send ( sender = self , moving = [ ( self , parent ) ] ) <EOL> self . move_to ( parent ) <EOL> return <EOL> squash = [ ( self . id , merge_node . id ) ] <EOL> examine = [ ( self , merge_node ) ] <EOL> to_move = [ ] <EOL> while examine : <EOL> node , merge_node = examine . pop ( ) <EOL> children = node . get_children ( ) <EOL> child_names = [ obj . name for obj in children ] <EOL> conflicts = { } <EOL> for obj in manager . filter ( parent = merge_node , name__in = child_names ) : <EOL> conflicts [ obj . name ] = obj <EOL> for child in children : <EOL> if child . name in conflicts : <EOL> squash . append ( ( child . id , conflicts [ child . name ] . id ) ) <EOL> examine . append ( ( child , conflicts [ child . name ] ) ) <EOL> else : <EOL> to_move . append ( ( child , merge_node ) ) <EOL> if squash : <EOL> signals . pre_merge . send ( sender = self , merge_pairs = squash ) <EOL> if to_move : <EOL> signals . pre_move . send ( sender = self , moving = to_move ) <EOL> for child , new_parent in to_move : <EOL> new_parent = manager . get ( id = new_parent . id ) <EOL> child . move_to ( new_parent ) <EOL> self . delete ( ) <EOL> class Topic ( AbstractTopic ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> mptt . register ( Topic , order_insertion_by = [ "<STR_LIT:name>" ] ) </s>
