def get_search_query(user, params): other_search_params = params.get('filter_params', {}) q = other_search_params.get('q', '') order_by = params.get('order_by', 'pk') sort_reverse = params.get('sort_reverse', False) project_slug = other_search_params.get('project__slug', None) mappable_types = get_mappable_types() if project_slug: mappable_types['project__slug'] = 'string' if order_by: if sort_reverse: order_by = "-%s" % order_by building_snapshots = BuildingSnapshot.objects.order_by( order_by).filter( super_organization__in=user.orgs.all(), canonicalbuilding__active=True, ) else: building_snapshots = BuildingSnapshot.objects.filter( super_organization__in=user.orgs.all(), canonicalbuilding__active=True, ) buildings_queryset = search.search_buildings(q, queryset=building_snapshots) buildings_queryset = search.filter_other_params(buildings_queryset, other_search_params, mappable_types) return buildings_queryset
def get_column_mapping_suggestions(request): """Returns probabalistic structure for each dest column. Requires that we have a PK for the ImportFile to reference. """ body = json.loads(request.body) import_file = ImportFile.objects.get(pk=body.get('import_file_id')) result = {'status': 'success'} column_types = get_mappable_types() suggested_mappings = mapper.build_column_mapping( import_file.first_row_columns, column_types.keys(), previous_mapping=get_column_mapping, map_args=[import_file.import_record.super_organization], thresh=20 # percentage match we require ) for m in suggested_mappings: dest, conf = suggested_mappings[m] if dest is None: suggested_mappings[m][0] = u'' result['suggested_column_mappings'] = suggested_mappings result['building_columns'] = column_types.keys() result['building_column_types'] = column_types return result
def get_search_query(user, params): other_search_params = params.get('filter_params', {}) q = other_search_params.get('q', '') order_by = params.get('order_by', 'pk') sort_reverse = params.get('sort_reverse', False) project_slug = other_search_params.get('project__slug', None) mappable_types = get_mappable_types() if project_slug: mappable_types['project__slug'] = 'string' if order_by: if sort_reverse: order_by = "-%s" % order_by building_snapshots = BuildingSnapshot.objects.order_by( order_by ).filter( super_organization__in=user.orgs.all(), canonicalbuilding__active=True, ) else: building_snapshots = BuildingSnapshot.objects.filter( super_organization__in=user.orgs.all(), canonicalbuilding__active=True, ) buildings_queryset = search.search_buildings( q, queryset=building_snapshots ) buildings_queryset = search.filter_other_params( buildings_queryset, other_search_params, mappable_types ) return buildings_queryset
def get_columns(is_project, org_id, all_fields=False): """gets default columns, to be overriden in future title: HTML presented title of column sort_column: semantic name used by js and for searching DB class: HTML CSS class for row td elements title_class: HTML CSS class for column td elements type: 'string', 'number', 'date' min, max: the django filter key e.g. gross_floor_area__gte field_type: assessor, pm, or compliance (currently not used) sortable: determines if the column is sortable checked: initial state of "edit columns" modal static: True if option can be toggle (ID is false because it is always needed to link to the building detail page) link: signifies that the cell's data should link to a building detail page """ cols = [] translator = { '': 'string', 'date': 'date', 'float': 'number', 'string': 'string', 'decimal': 'number', 'datetime': 'date', # 'boolean': 'bool', # 'foreignkey': 'int', } field_types = {} for k, v in get_mappable_types().items(): d = { "title": k.title().replace('_', ' '), "sort_column": k, "type": translator[v], "class": "is_aligned_right", "field_type": "assessor", "sortable": True, "checked": False, "static": False, "field_type": field_types.get(k), "link": True if '_id' in k or 'address' in k.lower() else False, } if d['sort_column'] == 'gross_floor_area': d['type'] = 'floor_area' d['subtitle'] = u"ft" + u"\u00B2" if d['type'] != 'string': d["min"] = "{0}__gte".format(k) d["max"] = "{0}__lte".format(k) cols.append(d) for col in cols: if col['sort_column'] in ASSESSOR_FIELDS_BY_COLUMN: assessor_field = ASSESSOR_FIELDS_BY_COLUMN[col['sort_column']] col['field_type'] = assessor_field['field_type'] if all_fields: qs = models.Column.objects.filter(is_extra_data=True).filter( Q(organization=None) | Q(mapped_mappings__super_organization=org_id) ).select_related('unit').distinct() else: qs = models.Column.objects.filter(is_extra_data=True).filter( mapped_mappings__super_organization=org_id ).select_related('unit').distinct() for c in qs: t = c.unit.get_unit_type_display().lower() if c.unit else 'string' link = False if '_id' in c.column_name or 'address' in c.column_name.lower(): link = True d = { "title": c.column_name, "sort_column": c.column_name, "type": translator[t], "class": "is_aligned_right", "field_type": "assessor", "sortable": True, "checked": False, "static": False, "link": link, "is_extra_data": True, } if d['type'] != 'string': d["min"] = "{0}__gte".format(c.column_name) d["max"] = "{0}__lte".format(c.column_name) cols.append(d) cols.sort(key=lambda x: x['title']) if is_project: cols.insert(0, { "title": "Status", "sort_column": "project_building_snapshots__status_label__name", "class": "", "title_class": "", "type": "string", "field_type": "assessor", "sortable": True, "checked": True, "static": True }) columns = { 'fields': cols, } return columns
def get_columns(is_project, org_id, all_fields=False): """gets default columns, to be overriden in future title: HTML presented title of column sort_column: semantic name used by js and for searching DB class: HTML CSS class for row td elements title_class: HTML CSS class for column td elements type: 'string', 'number', 'date' min, max: the django filter key e.g. gross_floor_area__gte field_type: assessor, pm, or compliance (currently not used) sortable: determines if the column is sortable checked: initial state of "edit columns" modal static: True if option can be toggle (ID is false because it is always needed to link to the building detail page) link: signifies that the cell's data should link to a building detail page """ cols = [] translator = { '': 'string', 'date': 'date', 'float': 'number', 'string': 'string', 'decimal': 'number', 'datetime': 'date', } field_types = {} for k, v in get_mappable_types().items(): d = { "title": k.title().replace('_', ' '), "sort_column": k, "type": translator[v], "class": "is_aligned_right", "field_type": "assessor", "sortable": True, "checked": False, "static": False, "field_type": field_types.get(k), "link": True if '_id' in k or 'address' in k.lower() else False, } if d['sort_column'] == 'gross_floor_area': d['type'] = 'floor_area' d['subtitle'] = u"ft" + u"\u00B2" if d['type'] != 'string': d["min"] = "{0}__gte".format(k) d["max"] = "{0}__lte".format(k) cols.append(d) for col in cols: if col['sort_column'] in ASSESSOR_FIELDS_BY_COLUMN: assessor_field = ASSESSOR_FIELDS_BY_COLUMN[col['sort_column']] col['field_type'] = assessor_field['field_type'] if all_fields: qs = models.Column.objects.filter(is_extra_data=True).filter( Q(organization=None) | Q(mapped_mappings__super_organization=org_id) ).select_related('unit').distinct() else: qs = models.Column.objects.filter(is_extra_data=True).filter( mapped_mappings__super_organization=org_id ).select_related('unit').distinct() for c in qs: t = c.unit.get_unit_type_display().lower() if c.unit else 'string' link = False if '_id' in c.column_name or 'address' in c.column_name.lower(): link = True d = { "title": c.column_name, "sort_column": c.column_name, "type": translator[t], "class": "is_aligned_right", "field_type": "assessor", "sortable": True, "checked": False, "static": False, "link": link, "is_extra_data": True, } if d['type'] != 'string': d["min"] = "{0}__gte".format(c.column_name) d["max"] = "{0}__lte".format(c.column_name) cols.append(d) cols.sort(key=lambda x: x['title']) if is_project: cols.insert(0, { "title": "Status", "sort_column": "project_building_snapshots__status_label__name", "class": "", "title_class": "", "type": "string", "field_type": "assessor", "sortable": True, "checked": True, "static": True }) columns = { 'fields': cols, } return columns