Skip to content

Commit

Permalink
Merge pull request #1849 from SEED-platform/1815-label_export
Browse files Browse the repository at this point in the history
Fixed Label Export in CSVs
  • Loading branch information
nllong committed Mar 28, 2019
2 parents 6e81cfb + 87304da commit 046b480
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
13 changes: 0 additions & 13 deletions seed/models/tax_lot_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,6 @@ def get_related(cls, object_list, show_columns, columns_from_database):
if obj_dict.get('measures'):
del obj_dict['measures']

# # This isn't currently used, but if we ever re-enable it we need to prefetch all of the label data using a
# # single query for performance. This code causes one query with a join for every record
# label_string = []
# if hasattr(obj, 'property'):
# for label in obj.property.labels.all().order_by('name'):
# label_string.append(label.name)
# obj_dict['property_labels'] = ','.join(label_string)
#
# elif hasattr(obj, 'taxlot'):
# for label in obj.taxlot.labels.all().order_by('name'):
# label_string.append(label.name)
# obj_dict['taxlot_labels'] = ','.join(label_string)

results.append(obj_dict)

return results
24 changes: 19 additions & 5 deletions seed/views/tax_lot_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def export(self, request):
.. code-block::
{
"ids": [1,2,3],
"columns": ["tax_jurisdiction_tax_lot_id", "address_line_1", "property_view_id"]
"ids": [1,2,3],
"columns": ["tax_jurisdiction_tax_lot_id", "address_line_1", "property_view_id"]
}
---
Expand All @@ -70,7 +70,7 @@ def export(self, request):
required: true
paramType: query
- name: ids
description: list of property ids to export (not property views)
description: list of property/taxlot ids to export (not property/taxlot views)
required: true
paramType: body
- name: columns
Expand Down Expand Up @@ -114,6 +114,7 @@ def export(self, request):
filter_str = {'cycle': cycle_pk}
if hasattr(view_klass, 'property'):
select_related.append('property')
prefetch_related = ['property__labels']
filter_str = {'property__organization_id': org_id}
if ids:
filter_str['property__id__in'] = ids
Expand All @@ -122,18 +123,31 @@ def export(self, request):

elif hasattr(view_klass, 'taxlot'):
select_related.append('taxlot')
prefetch_related = ['taxlot__labels']
filter_str = {'taxlot__organization_id': org_id}
if ids:
filter_str['taxlot__id__in'] = ids
# always export the labels
column_name_mappings['taxlot_labels'] = 'Tax Lot Labels'

model_views = view_klass.objects.select_related(*select_related).filter(
**filter_str).order_by('id')
model_views = view_klass.objects.select_related(*select_related).prefetch_related(*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)

# add labels
for i, record in enumerate(model_views):
label_string = []
if hasattr(record, 'property'):
for label in list(record.property.labels.all().order_by('name')):
label_string.append(label.name)
data[i]['property_labels'] = ','.join(label_string)

elif hasattr(record, 'taxlot'):
for label in list(record.taxlot.labels.all().order_by('name')):
label_string.append(label.name)
data[i]['taxlot_labels'] = ','.join(label_string)

# force the data into the same order as the IDs
if ids:
order_dict = {obj_id: index for index, obj_id in enumerate(ids)}
Expand Down

0 comments on commit 046b480

Please sign in to comment.