<s> from seed . models import Project , ProjectBuilding , StatusLabel <EOL> from seed . utils . buildings import get_search_query <EOL> def get_projects ( building , organization ) : <EOL> """<STR_LIT>""" <EOL> projects = [ ] <EOL> for p in building . project_set . filter ( <EOL> super_organization = organization <EOL> ) . distinct ( ) : <EOL> project_dict = p . __dict__ . copy ( ) <EOL> project_dict [ '<STR_LIT>' ] = p . has_compliance <EOL> if project_dict [ '<STR_LIT>' ] : <EOL> c = p . get_compliance ( ) <EOL> project_dict [ '<STR_LIT>' ] = c . compliance_type <EOL> project_dict [ '<STR_LIT>' ] = c . end_date . strftime ( "<STR_LIT>" ) <EOL> project_dict [ '<STR_LIT>' ] = c . deadline_date . strftime ( <EOL> "<STR_LIT>" <EOL> ) <EOL> del ( project_dict [ '<STR_LIT>' ] ) <EOL> del ( project_dict [ '<STR_LIT>' ] ) <EOL> del ( project_dict [ '<STR_LIT>' ] ) <EOL> pb = ProjectBuilding . objects . get ( project = p , building_snapshot = building ) <EOL> if pb . approved_date : <EOL> approved_date = pb . approved_date . strftime ( "<STR_LIT>" ) <EOL> else : <EOL> approved_date = None <EOL> project_dict [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : pb . compliant , <EOL> '<STR_LIT>' : pb . approver . email if pb . approver else None , <EOL> '<STR_LIT>' : approved_date , <EOL> } <EOL> if pb . status_label : <EOL> label = { <EOL> '<STR_LIT:name>' : pb . status_label . name , <EOL> '<STR_LIT>' : pb . status_label . color , <EOL> '<STR_LIT:id>' : pb . status_label . pk , <EOL> } <EOL> project_dict [ '<STR_LIT>' ] [ '<STR_LIT:label>' ] = label <EOL> projects . append ( project_dict ) <EOL> return projects <EOL> def get_labels ( user ) : <EOL> """<STR_LIT>""" <EOL> status_labels = StatusLabel . objects . filter ( <EOL> super_organization__in = user . orgs . all ( ) <EOL> ) <EOL> labels = [ ] <EOL> for label in status_labels : <EOL> labels . append ( { <EOL> '<STR_LIT:name>' : label . name , <EOL> '<STR_LIT>' : label . color , <EOL> '<STR_LIT:id>' : label . pk , <EOL> } ) <EOL> return labels <EOL> def update_buildings_with_labels ( buildings , project_id ) : <EOL> """<STR_LIT>""" <EOL> for b in buildings : <EOL> pb = ProjectBuilding . objects . get ( <EOL> building_snapshot__pk = b [ '<STR_LIT>' ] , <EOL> project__pk = project_id <EOL> ) <EOL> if pb . status_label : <EOL> b [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:name>' : pb . status_label . name , <EOL> '<STR_LIT>' : pb . status_label . color , <EOL> '<STR_LIT:id>' : pb . status_label . pk , <EOL> } <EOL> return buildings <EOL> def apply_label ( project_slug , buildings , select_all , label , search_params , <EOL> user ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:id>' in label : <EOL> label = StatusLabel . objects . get ( pk = label [ '<STR_LIT:id>' ] ) <EOL> else : <EOL> label = None <EOL> if select_all : <EOL> qs = get_search_query ( user , search_params ) <EOL> pb_qs = ProjectBuilding . objects . filter ( <EOL> building_snapshot__in = qs , <EOL> project__slug = project_slug <EOL> ) . exclude ( <EOL> building_snapshot__pk__in = buildings <EOL> ) <EOL> else : <EOL> pb_qs = ProjectBuilding . objects . filter ( <EOL> building_snapshot__pk__in = buildings , <EOL> project__slug = project_slug , <EOL> ) <EOL> pb_qs . update ( <EOL> status_label = label <EOL> ) <EOL> def transfer_buildings ( source_project_slug , target_project_slug , buildings , <EOL> select_all , search_params , user , copy_flag = False ) : <EOL> """<STR_LIT>""" <EOL> target_project = Project . objects . get ( slug = target_project_slug ) <EOL> source_project = Project . objects . get ( slug = source_project_slug ) <EOL> target_project . last_modified_by = user <EOL> source_project . last_modified_by = user <EOL> target_project . save ( ) <EOL> source_project . save ( ) <EOL> if copy_flag : <EOL> copy_buildings ( source_project , target_project , buildings , <EOL> select_all , search_params , user ) <EOL> else : <EOL> move_buildings ( source_project , target_project , buildings , <EOL> select_all , search_params , user ) <EOL> def copy_buildings ( source_project , target_project , buildings , <EOL> select_all , search_params , user ) : <EOL> """<STR_LIT>""" <EOL> delete_matching_buildings ( <EOL> target_project , buildings , select_all , search_params , user <EOL> ) <EOL> pb_queryset = get_transfer_buildings ( <EOL> source_project , target_project , buildings , select_all , search_params , <EOL> user <EOL> ) <EOL> pbs = [ ] <EOL> for b in pb_queryset : <EOL> b . pk = None <EOL> b . project = target_project <EOL> pbs . append ( b ) <EOL> ProjectBuilding . objects . bulk_create ( pbs ) <EOL> def move_buildings ( source_project , target_project , buildings , <EOL> select_all , search_params , user ) : <EOL> """<STR_LIT>""" <EOL> delete_matching_buildings ( <EOL> target_project , buildings , select_all , search_params , user <EOL> ) <EOL> pb_queryset = get_transfer_buildings ( <EOL> source_project , target_project , buildings , select_all , search_params , <EOL> user <EOL> ) <EOL> pb_queryset . update ( project = target_project ) <EOL> def delete_matching_buildings ( project , buildings , <EOL> select_all , search_params , user ) : <EOL> """<STR_LIT>""" <EOL> if select_all : <EOL> qs = get_search_query ( user , search_params ) <EOL> ProjectBuilding . objects . filter ( <EOL> building_snapshot__in = qs , <EOL> project = project <EOL> ) . exclude ( building_snapshot__pk__in = buildings ) . delete ( ) <EOL> else : <EOL> ProjectBuilding . objects . filter ( <EOL> building_snapshot__pk__in = buildings , <EOL> project = project <EOL> ) . delete ( ) <EOL> def get_transfer_buildings ( source_project , target_project , buildings , <EOL> select_all , search_params , user ) : <EOL> """<STR_LIT>""" <EOL> if select_all : <EOL> qs = get_search_query ( user , search_params ) <EOL> pb_queryset = ProjectBuilding . objects . filter ( <EOL> building_snapshot__in = qs , <EOL> project = source_project <EOL> ) . exclude ( building_snapshot__pk__in = buildings ) <EOL> else : <EOL> pb_queryset = ProjectBuilding . objects . filter ( <EOL> building_snapshot__pk__in = buildings , <EOL> project = source_project <EOL> ) <EOL> return pb_queryset </s>
