<s> from __future__ import unicode_literals <EOL> from django . db import models , migrations <EOL> def merge_extra_data ( b1 , b2 , default = None ) : <EOL> """<STR_LIT>""" <EOL> default = default or b1 <EOL> non_default = b2 <EOL> if default != b1 : <EOL> non_default = b1 <EOL> extra_data_sources = { } <EOL> default_extra_data = getattr ( default , '<STR_LIT>' , { } ) <EOL> non_default_extra_data = getattr ( non_default , '<STR_LIT>' , { } ) <EOL> all_keys = set ( default_extra_data . keys ( ) + non_default_extra_data . keys ( ) ) <EOL> extra_data = { <EOL> k : default_extra_data . get ( k ) or non_default_extra_data . get ( k ) <EOL> for k in all_keys <EOL> } <EOL> for item in extra_data : <EOL> if item in default_extra_data and default_extra_data [ item ] : <EOL> extra_data_sources [ item ] = default . pk <EOL> elif item in non_default_extra_data and non_default_extra_data [ item ] : <EOL> extra_data_sources [ item ] = non_default . pk <EOL> else : <EOL> extra_data_sources [ item ] = default . pk <EOL> return extra_data , extra_data_sources <EOL> def recover_extra_data ( app , schema_editor , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> BuildingSnapshot = app . get_model ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> leaves = BuildingSnapshot . objects . filter ( <EOL> pk__in = BuildingSnapshot . objects . filter ( <EOL> parents__isnull = False , <EOL> children__isnull = True , <EOL> canonicalbuilding__isnull = False , <EOL> ) . values_list ( '<STR_LIT>' , flat = True ) , <EOL> ) <EOL> for leaf_snapshot in leaves : <EOL> merge_extra_data_from_parents ( leaf_snapshot ) <EOL> def merge_extra_data_from_parents ( bs ) : <EOL> for parent in bs . parents . order_by ( '<STR_LIT>' ) . all ( ) : <EOL> merge_extra_data_from_parents ( parent ) <EOL> extra_data , extra_data_sources = merge_extra_data ( bs , parent ) <EOL> bs . extra_data = extra_data <EOL> bs . extra_data_sources = extra_data_sources <EOL> bs . save ( ) <EOL> def stub ( * args , ** kwargs ) : <EOL> pass <EOL> class Migration ( migrations . Migration ) : <EOL> dependencies = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> operations = [ <EOL> migrations . RunPython ( recover_extra_data , stub ) , <EOL> ] </s>
