Skip to content

Commit

Permalink
Merge pull request #3054 from SEED-platform/refactor/list-related-data
Browse files Browse the repository at this point in the history
Refactor/list related data
  • Loading branch information
macintoshpie committed Dec 27, 2021
2 parents 2650dda + cbe1e81 commit 98fd454
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 162 deletions.
4 changes: 1 addition & 3 deletions seed/models/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ def retrieve_mapping_columns(org_id, inventory_type=None):
return columns

@staticmethod
def retrieve_all(org_id, inventory_type=None, only_used=False):
def retrieve_all(org_id, inventory_type=None, only_used=False) -> list[dict]:
"""
Retrieve all the columns for an organization. This method will query for all the columns in the
database assigned to the organization. It will then go through and cleanup the names to ensure that
Expand All @@ -1257,8 +1257,6 @@ def retrieve_all(org_id, inventory_type=None, only_used=False):
:param org_id: Organization ID
:param inventory_type: Inventory Type (property|taxlot) from the requester. This sets the related columns if requested.
:param only_used: View only the used columns that exist in the Column's table
:return: dict
"""
from seed.serializers.columns import ColumnSerializer

Expand Down
286 changes: 151 additions & 135 deletions seed/models/tax_lot_properties.py

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions seed/static/seed/js/controllers/inventory_list_beta_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,14 +765,22 @@ angular.module('BE.seed.controller.inventory_list_beta', [])
};

var fetch = function (page, chunk) {
// console.log('Fetching page ' + page + ' (chunk size ' + chunk + ')');
var fn;
if ($scope.inventory_type === 'properties') {
fn = inventory_service.get_properties;
} else if ($scope.inventory_type === 'taxlots') {
fn = inventory_service.get_taxlots;
}
return fn(page, chunk, $scope.cycle.selected_cycle, _.get($scope, 'currentProfile.id')).then(function (data) {
return fn(
page,
chunk,
$scope.cycle.selected_cycle,
_.get($scope, 'currentProfile.id'),
undefined,
true,
$scope.organization.id,
false,
).then(function (data) {
return data;
});
};
Expand Down
10 changes: 6 additions & 4 deletions seed/static/seed/js/services/inventory_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ angular.module('BE.seed.service.inventory', []).factory('inventory_service', [
total_taxlots_for_user: 0
};

inventory_service.get_properties = function (page, per_page, cycle, profile_id, property_view_ids, save_last_cycle = true, organization_id = null) {
inventory_service.get_properties = function (page, per_page, cycle, profile_id, property_view_ids, save_last_cycle = true, organization_id = null, include_related = true) {
organization_id = organization_id == undefined ? user_service.get_organization().id : organization_id;

var params = {
organization_id: organization_id,
page: page,
per_page: per_page || 999999999
per_page: per_page || 999999999,
include_related: include_related
};

return cycle_service.get_cycles().then(function (cycles) {
Expand Down Expand Up @@ -252,13 +253,14 @@ angular.module('BE.seed.service.inventory', []).factory('inventory_service', [
};


inventory_service.get_taxlots = function (page, per_page, cycle, profile_id, inventory_ids, save_last_cycle = true, organization_id = null) {
inventory_service.get_taxlots = function (page, per_page, cycle, profile_id, inventory_ids, save_last_cycle = true, organization_id = null, include_related = true) {
organization_id = organization_id == undefined ? user_service.get_organization().id : organization_id;

var params = {
organization_id: organization_id,
page: page,
per_page: per_page || 999999999
per_page: per_page || 999999999,
include_related: include_related
};

return cycle_service.get_cycles().then(function (cycles) {
Expand Down
4 changes: 0 additions & 4 deletions seed/tests/test_analysis_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from os import path

from seed.analysis_pipelines.utils import SimpleMeterReading
from unittest.case import skip
from unittest.mock import patch
from zipfile import ZipFile
from lxml import etree
Expand Down Expand Up @@ -616,9 +615,6 @@ def _mock_request(file_, model_type):

return _mock_request

# Skipping this test b/c of an unexpected error validating BuildingSync files
# See here for more info: https://github.com/SEED-platform/seed/pull/2901
@skip('See https://github.com/SEED-platform/seed/pull/2901')
def test_build_bsyncr_input_returns_valid_bsync_document(self):
# Act
doc, errors = _build_bsyncr_input(self.analysis_property_view, self.meter)
Expand Down
2 changes: 1 addition & 1 deletion seed/tests/test_tax_lot_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_tax_lot_property_get_related(self):
'gross_floor_area', 'owner_city_state', 'owner_telephone', 'recent_sale_date',
]
columns_from_database = Column.retrieve_all(self.org.id, 'property', False)
data = TaxLotProperty.get_related(qs, columns, columns_from_database)
data = TaxLotProperty.serialize(qs, columns, columns_from_database)

self.assertEqual(len(data), 50)
self.assertEqual(len(data[0]['related']), 0)
Expand Down
2 changes: 1 addition & 1 deletion seed/utils/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def properties_across_cycles(org_id, profile_id, cycle_ids=[]):
.filter(property__organization_id=org_id, cycle_id=cycle_id) \
.order_by('id')

related_results = TaxLotProperty.get_related(property_views, show_columns, columns_from_database)
related_results = TaxLotProperty.serialize(property_views, show_columns, columns_from_database)

org = Organization.objects.get(pk=org_id)
unit_collapsed_results = [apply_display_unit_preferences(org, x) for x in related_results]
Expand Down
2 changes: 1 addition & 1 deletion seed/utils/taxlots.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def taxlots_across_cycles(org_id, profile_id, cycle_ids=[]):
.filter(taxlot__organization_id=org_id, cycle_id=cycle_id) \
.order_by('id')

related_results = TaxLotProperty.get_related(taxlot_views, show_columns, columns_from_database)
related_results = TaxLotProperty.serialize(taxlot_views, show_columns, columns_from_database)

org = Organization.objects.get(pk=org_id)
unit_collapsed_results = [apply_display_unit_preferences(org, x) for x in related_results]
Expand Down
4 changes: 2 additions & 2 deletions seed/views/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ def _get_filtered_results(self, request, profile_id):
except ColumnListProfile.DoesNotExist:
show_columns = None

related_results = TaxLotProperty.get_related(property_views, show_columns,
columns_from_database)
related_results = TaxLotProperty.serialize(property_views, show_columns,
columns_from_database)

# collapse units here so we're only doing the last page; we're already a
# realized list by now and not a lazy queryset
Expand Down
2 changes: 1 addition & 1 deletion seed/views/tax_lot_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def export(self, request):
*prefetch_related).filter(**filter_str).order_by('id')

# get the data in a dict which includes the related data
data = TaxLotProperty.get_related(model_views, column_ids, columns_from_database)
data = TaxLotProperty.serialize(model_views, column_ids, columns_from_database)

# add labels and notes
for i, record in enumerate(model_views):
Expand Down
4 changes: 2 additions & 2 deletions seed/views/taxlots.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def _get_filtered_results(self, request, profile_id):
except ColumnListProfile.DoesNotExist:
show_columns = None

related_results = TaxLotProperty.get_related(taxlot_views, show_columns,
columns_from_database)
related_results = TaxLotProperty.serialize(taxlot_views, show_columns,
columns_from_database)

# collapse units here so we're only doing the last page; we're already a
# realized list by now and not a lazy queryset
Expand Down
21 changes: 19 additions & 2 deletions seed/views/v3/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,15 @@ def _get_filtered_results(self, request, profile_id):
except ColumnListProfile.DoesNotExist:
show_columns = None

related_results = TaxLotProperty.get_related(property_views, show_columns,
columns_from_database)
include_related = (
str(request.query_params.get('include_related', 'true')).lower() == 'true'
)
related_results = TaxLotProperty.serialize(
property_views,
show_columns,
columns_from_database,
include_related
)

# collapse units here so we're only doing the last page; we're already a
# realized list by now and not a lazy queryset
Expand Down Expand Up @@ -381,6 +388,11 @@ def meters(self, request, pk):
required=False,
description='Page to fetch'
),
AutoSchemaHelper.query_boolean_field(
'include_related',
required=False,
description='If False, related data (i.e. Tax Lot data) is not added to the response (default is True)'
),
]
)
@api_endpoint_class
Expand Down Expand Up @@ -447,6 +459,11 @@ def filter_by_cycle(self, request):
required=False,
description='Page to fetch'
),
AutoSchemaHelper.query_boolean_field(
'include_related',
required=False,
description='If False, related data (i.e. Tax Lot data) is not added to the response (default is True)'
),
],
request_body=AutoSchemaHelper.schema_factory(
{
Expand Down
2 changes: 1 addition & 1 deletion seed/views/v3/tax_lot_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def export(self, request):
*prefetch_related).filter(**filter_str).order_by('id')

# get the data in a dict which includes the related data
data = TaxLotProperty.get_related(model_views, column_ids, columns_from_database)
data = TaxLotProperty.serialize(model_views, column_ids, columns_from_database)

derived_columns = column_profile.derived_columns.all() if column_profile is not None else []
column_name_mappings.update({dc.name: dc.name for dc in derived_columns})
Expand Down
23 changes: 20 additions & 3 deletions seed/views/v3/taxlots.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,15 @@ def _get_filtered_results(self, request, profile_id):
except ColumnListProfile.DoesNotExist:
show_columns = None

related_results = TaxLotProperty.get_related(taxlot_views, show_columns,
columns_from_database)
include_related = (
str(request.query_params.get('include_related', 'true')).lower() == 'true'
)
related_results = TaxLotProperty.serialize(
taxlot_views,
show_columns,
columns_from_database,
include_related,
)

# collapse units here so we're only doing the last page; we're already a
# realized list by now and not a lazy queryset
Expand Down Expand Up @@ -192,7 +199,12 @@ def _get_filtered_results(self, request, profile_id):
'profile_id',
required=False,
description='The ID of the column profile to use'
)
),
AutoSchemaHelper.query_boolean_field(
'include_related',
required=False,
description='If False, related data (i.e. Property data) is not added to the response (default is True)'
),
]
)
@api_endpoint_class
Expand Down Expand Up @@ -260,6 +272,11 @@ def filter_by_cycle(self, request):
required=False,
description='The number of items per page to return'
),
AutoSchemaHelper.query_boolean_field(
'include_related',
required=False,
description='If False, related data (i.e. Property data) is not added to the response (default is True)'
),
],
request_body=AutoSchemaHelper.schema_factory(
{
Expand Down

0 comments on commit 98fd454

Please sign in to comment.