<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import operator <EOL> import json <EOL> from django . db . models import Q <EOL> from superperms . orgs . models import Organization <EOL> from . models import BuildingSnapshot <EOL> from . utils . mapping import get_mappable_types <EOL> from public . models import PUBLIC <EOL> def convert_to_js_timestamp ( timestamp ) : <EOL> """<STR_LIT>""" <EOL> return int ( timestamp . strftime ( "<STR_LIT:%s>" ) ) * <NUM_LIT:1000> <EOL> def get_building_fieldnames ( ) : <EOL> """<STR_LIT>""" <EOL> return [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def search_buildings ( q , fieldnames = None , queryset = None ) : <EOL> """<STR_LIT>""" <EOL> if fieldnames is None : <EOL> fieldnames = get_building_fieldnames ( ) <EOL> if queryset is None : <EOL> queryset = BuildingSnapshot . objects . none ( ) <EOL> if q == '<STR_LIT>' : <EOL> return queryset <EOL> qgroup = reduce ( operator . or_ , ( <EOL> Q ( ** { fieldname + '<STR_LIT>' : q } ) for fieldname in fieldnames <EOL> ) ) <EOL> return queryset . filter ( qgroup ) <EOL> def generate_paginated_results ( queryset , number_per_page = <NUM_LIT> , page = <NUM_LIT:1> , <EOL> whitelist_orgs = None , below_threshold = False ) : <EOL> """<STR_LIT>""" <EOL> parent_org = None <EOL> if whitelist_orgs : <EOL> parent_org = whitelist_orgs . first ( ) . parent_org <EOL> page = page - <NUM_LIT:1> if page > <NUM_LIT:0> else <NUM_LIT:0> <EOL> MAX_RESULTS = <NUM_LIT:100> <EOL> number_per_page = min ( MAX_RESULTS , number_per_page ) <EOL> start = page * number_per_page <EOL> end = start + number_per_page <EOL> if isinstance ( queryset , list ) : <EOL> building_count = len ( queryset ) <EOL> else : <EOL> building_count = queryset . count ( ) <EOL> if start > building_count : <EOL> return [ ] <EOL> if end > building_count : <EOL> end = building_count <EOL> building_list = [ ] <EOL> buildings_from_query = queryset [ start : end ] <EOL> if parent_org : <EOL> exportable_fields = parent_org . exportable_fields <EOL> exportable_field_names = exportable_fields . values_list ( <EOL> '<STR_LIT:name>' , flat = True <EOL> ) <EOL> else : <EOL> exportable_field_names = None <EOL> for b in buildings_from_query : <EOL> if is_not_whitelist_building ( parent_org , b , whitelist_orgs ) : <EOL> building_dict = b . to_dict ( exportable_field_names ) <EOL> else : <EOL> building_dict = b . to_dict ( ) <EOL> co_parent = b . co_parent <EOL> if co_parent : <EOL> building_dict [ '<STR_LIT>' ] = True <EOL> building_dict [ '<STR_LIT>' ] = co_parent . to_dict ( ) <EOL> child = b . children . first ( ) <EOL> if child : <EOL> building_dict [ '<STR_LIT>' ] = child . confidence <EOL> else : <EOL> building_dict [ '<STR_LIT>' ] = False <EOL> if not below_threshold or not is_not_whitelist_building ( <EOL> parent_org , b , whitelist_orgs <EOL> ) : <EOL> building_list . append ( building_dict ) <EOL> return building_list , building_count <EOL> def is_not_whitelist_building ( parent_org , building , whitelist_orgs ) : <EOL> """<STR_LIT>""" <EOL> return ( parent_org and building . super_organization not in whitelist_orgs ) <EOL> def filter_other_params ( queryset , other_params , db_columns ) : <EOL> """<STR_LIT>""" <EOL> def strip_suffix ( k , suffix ) : <EOL> match = k . find ( suffix ) <EOL> if match >= <NUM_LIT:0> : <EOL> return k [ : match ] <EOL> else : <EOL> return k <EOL> def strip_suffixes ( k , suffixes ) : <EOL> return reduce ( strip_suffix , suffixes , k ) <EOL> def is_column ( k , columns ) : <EOL> sanitized = strip_suffixes ( k , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if sanitized in columns : <EOL> return True <EOL> else : <EOL> return False <EOL> query_dict = { } <EOL> for k , v in other_params . iteritems ( ) : <EOL> in_columns = is_column ( k , db_columns ) <EOL> if in_columns and k != '<STR_LIT:q>' and v is not None and v != '<STR_LIT>' : <EOL> if ( '<STR_LIT>' in k or <EOL> '<STR_LIT>' in k or <EOL> '<STR_LIT>' in k or <EOL> '<STR_LIT>' in k or <EOL> '<STR_LIT>' in k or <EOL> k == '<STR_LIT>' or k == '<STR_LIT>' ) : <EOL> query_dict [ "<STR_LIT:%s>" % k ] = v <EOL> else : <EOL> query_dict [ "<STR_LIT>" % k ] = v <EOL> queryset = queryset . filter ( ** query_dict ) <EOL> for k , v in other_params . iteritems ( ) : <EOL> if ( not is_column ( k , db_columns ) ) and k != '<STR_LIT:q>' and v != '<STR_LIT>' : <EOL> if k . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> k = strip_suffixes ( k , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> cond = '<STR_LIT:>>' <EOL> key_cast = '<STR_LIT:float>' <EOL> elif k . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> k = strip_suffixes ( k , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> cond = '<STR_LIT:<>' <EOL> key_cast = '<STR_LIT:float>' <EOL> else : <EOL> cond = '<STR_LIT>' <EOL> key_cast = '<STR_LIT:text>' <EOL> v = "<STR_LIT>" . format ( v ) <EOL> case_insensitive = key_cast == '<STR_LIT:text>' <EOL> queryset = queryset . json_query ( <EOL> k , <EOL> cond = cond , <EOL> key_cast = key_cast , <EOL> value = v , <EOL> case_insensitive = case_insensitive , <EOL> ) <EOL> return queryset <EOL> def parse_body ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> body = json . loads ( request . body ) <EOL> except ValueError : <EOL> body = { } <EOL> q = body . get ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> other_search_params = body . get ( '<STR_LIT>' , { } ) <EOL> if '<STR_LIT>' in other_search_params : <EOL> exclude = other_search_params [ '<STR_LIT>' ] <EOL> del ( other_search_params [ '<STR_LIT>' ] ) <EOL> else : <EOL> exclude = { } <EOL> order_by = body . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if order_by == '<STR_LIT>' : <EOL> order_by = '<STR_LIT>' <EOL> sort_reverse = body . get ( '<STR_LIT>' , False ) <EOL> page = int ( body . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> number_per_page = int ( body . get ( '<STR_LIT>' , <NUM_LIT:10> ) ) <EOL> if '<STR_LIT>' in body : <EOL> show_shared_buildings = body . get ( '<STR_LIT>' ) <EOL> elif not getattr ( request , '<STR_LIT>' , False ) : <EOL> show_shared_buildings = getattr ( <EOL> request . user , '<STR_LIT>' , False <EOL> ) <EOL> else : <EOL> show_shared_buildings = False <EOL> return { <EOL> '<STR_LIT>' : exclude , <EOL> '<STR_LIT>' : order_by , <EOL> '<STR_LIT>' : sort_reverse , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : number_per_page , <EOL> '<STR_LIT>' : show_shared_buildings , <EOL> '<STR_LIT:q>' : q , <EOL> '<STR_LIT>' : other_search_params , <EOL> '<STR_LIT>' : body . get ( '<STR_LIT>' ) <EOL> } <EOL> def build_shared_buildings_orgs ( orgs ) : <EOL> """<STR_LIT>""" <EOL> other_orgs = [ ] <EOL> for org in orgs : <EOL> if org . parent_org : <EOL> other_orgs . extend ( org . parent_org . child_orgs . all ( ) ) <EOL> other_orgs . append ( org . parent_org ) <EOL> else : <EOL> other_orgs . extend ( org . child_orgs . all ( ) ) <EOL> other_orgs . append ( org ) <EOL> other_orgs = list ( set ( other_orgs ) ) <EOL> return other_orgs <EOL> def build_json_params ( order_by , sort_reverse ) : <EOL> """<STR_LIT>""" <EOL> extra_data_sort = False <EOL> db_columns = get_mappable_types ( ) <EOL> db_columns [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> db_columns [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if order_by not in db_columns : <EOL> extra_data_sort = True <EOL> if sort_reverse and not extra_data_sort : <EOL> order_by = "<STR_LIT>" % order_by <EOL> return db_columns , extra_data_sort , order_by <EOL> def get_orgs_w_public_fields ( ) : <EOL> """<STR_LIT>""" <EOL> return list ( Organization . objects . filter ( <EOL> sharedbuildingfield__field_type = PUBLIC <EOL> ) . distinct ( ) ) <EOL> def search_public_buildings ( request , orgs ) : <EOL> """<STR_LIT>""" <EOL> params = parse_body ( request ) <EOL> other_search_params = params [ '<STR_LIT>' ] <EOL> db_columns , extra_data_sort , params [ '<STR_LIT>' ] = build_json_params ( <EOL> params [ '<STR_LIT>' ] , params [ '<STR_LIT>' ] <EOL> ) <EOL> building_snapshots = create_building_queryset ( <EOL> orgs , <EOL> params [ '<STR_LIT>' ] , <EOL> params [ '<STR_LIT>' ] , <EOL> extra_data_sort = extra_data_sort , <EOL> ) <EOL> buildings_queryset = search_buildings ( <EOL> params [ '<STR_LIT:q>' ] , queryset = building_snapshots <EOL> ) <EOL> buildings_queryset = filter_other_params ( <EOL> buildings_queryset , other_search_params , db_columns <EOL> ) <EOL> if extra_data_sort : <EOL> buildings_queryset = buildings_queryset . json_query ( <EOL> params [ '<STR_LIT>' ] , <EOL> order_by = params [ '<STR_LIT>' ] , <EOL> order_by_rev = params [ '<STR_LIT>' ] , <EOL> ) <EOL> if isinstance ( buildings_queryset , list ) : <EOL> buildings_count = len ( buildings_queryset ) <EOL> else : <EOL> buildings_count = buildings_queryset . count ( ) <EOL> return buildings_queryset , buildings_count <EOL> def create_building_queryset ( <EOL> orgs , <EOL> exclude , <EOL> order_by , <EOL> other_orgs = None , <EOL> extra_data_sort = False , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> distinct_order_by = order_by . lstrip ( '<STR_LIT:->' ) <EOL> if other_orgs : <EOL> if extra_data_sort : <EOL> return BuildingSnapshot . objects . filter ( <EOL> ( <EOL> Q ( super_organization__in = orgs ) | <EOL> Q ( super_organization__in = other_orgs ) <EOL> ) , <EOL> canonicalbuilding__active = True <EOL> ) . exclude ( ** exclude ) <EOL> else : <EOL> return BuildingSnapshot . objects . order_by ( <EOL> order_by , '<STR_LIT>' <EOL> ) . filter ( <EOL> ( <EOL> Q ( super_organization__in = orgs ) | <EOL> Q ( super_organization__in = other_orgs ) <EOL> ) , <EOL> canonicalbuilding__active = True <EOL> ) . exclude ( ** exclude ) . distinct ( distinct_order_by , '<STR_LIT>' ) <EOL> else : <EOL> if extra_data_sort : <EOL> return BuildingSnapshot . objects . filter ( <EOL> super_organization__in = orgs , <EOL> canonicalbuilding__active = True <EOL> ) . exclude ( ** exclude ) <EOL> else : <EOL> result = BuildingSnapshot . objects . order_by ( <EOL> order_by , '<STR_LIT>' <EOL> ) . filter ( <EOL> super_organization__in = orgs , <EOL> canonicalbuilding__active = True <EOL> ) . exclude ( ** exclude ) . distinct ( distinct_order_by , '<STR_LIT>' ) <EOL> return result <EOL> def remove_results_below_q_threshold ( search_results ) : <EOL> """<STR_LIT>""" <EOL> manual_group_by = { } <EOL> thresholds = { } <EOL> for b in search_results : <EOL> parent_org = b . super_organization . get_parent ( ) <EOL> if parent_org . id not in manual_group_by : <EOL> manual_group_by [ parent_org . id ] = <NUM_LIT:1> <EOL> thresholds [ parent_org . id ] = parent_org . query_threshold <EOL> else : <EOL> manual_group_by [ parent_org . id ] += <NUM_LIT:1> <EOL> orgs_below_threshold = [ ] <EOL> for org_id , count in manual_group_by . items ( ) : <EOL> if count < thresholds [ org_id ] : <EOL> orgs_below_threshold . append ( org_id ) <EOL> results = [ ] <EOL> for sr in search_results : <EOL> if sr . super_organization . get_parent ( ) not in orgs_below_threshold : <EOL> results . append ( sr ) <EOL> return results <EOL> def paginate_results ( request , search_results ) : <EOL> """<STR_LIT>""" <EOL> params = parse_body ( request ) <EOL> page = params [ '<STR_LIT>' ] - <NUM_LIT:1> if params [ '<STR_LIT>' ] > <NUM_LIT:0> else <NUM_LIT:0> <EOL> MAX_RESULTS = <NUM_LIT:100> <EOL> number_per_page = min ( MAX_RESULTS , params [ '<STR_LIT>' ] ) <EOL> start = page * number_per_page <EOL> end = start + number_per_page <EOL> search_results = search_results [ start : end ] <EOL> building_list = [ ] <EOL> for b in search_results : <EOL> building_dict = b . to_dict ( ) <EOL> co_parent = b . co_parent <EOL> if co_parent : <EOL> building_dict [ '<STR_LIT>' ] = True <EOL> building_dict [ '<STR_LIT>' ] = co_parent . to_dict ( ) <EOL> child = b . children . first ( ) <EOL> if child : <EOL> building_dict [ '<STR_LIT>' ] = child . confidence <EOL> else : <EOL> building_dict [ '<STR_LIT>' ] = False <EOL> building_list . append ( building_dict ) <EOL> return building_list <EOL> def mask_results ( search_results ) : <EOL> """<STR_LIT>""" <EOL> whitelist_fields = { } <EOL> results = [ ] <EOL> for b in search_results : <EOL> parent_org = Organization . objects . get ( pk = b [ '<STR_LIT>' ] ) <EOL> parent_org = parent_org . get_parent ( ) <EOL> if parent_org . id not in whitelist_fields : <EOL> whitelist_fields [ parent_org . id ] = [ ] <EOL> for s in parent_org . sharedbuildingfield_set . filter ( <EOL> field_type = PUBLIC <EOL> ) : <EOL> whitelist_fields [ parent_org . id ] . append ( s . field . name ) <EOL> d = { } <EOL> for key in b : <EOL> if key in whitelist_fields [ parent_org . id ] : <EOL> d [ key ] = b [ key ] <EOL> results . append ( d ) <EOL> return results </s>
