Skip to content
Merged

Zip #2012

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
065c7a1
Added leading zeros to postal code for w/ and w/o extension cases.
lainsworth8801 Nov 12, 2019
c5c2d0b
Modify data properties excel with postal codes for testing
lainsworth8801 Nov 12, 2019
212bcbf
clean up
lainsworth8801 Nov 12, 2019
2283bf4
mapper cleanup
lainsworth8801 Nov 12, 2019
691844b
Merge branch 'develop' of https://github.com/SEED-platform/seed into zip
lainsworth8801 Nov 12, 2019
4c5f944
Modify postal excel file and util.py for testing; added test cases fo…
lainsworth8801 Nov 13, 2019
d2c39ec
Added leading zeros to 0 zip; modified test to do explicit check
lainsworth8801 Nov 18, 2019
a9bc1d7
Added owner postal for filling zeros
lainsworth8801 Nov 18, 2019
e970d47
Db rehash with zero-packed zips
lainsworth8801 Dec 8, 2019
a8a52cd
Merge branch 'develop' of https://github.com/SEED-platform/seed into zip
lainsworth8801 Dec 9, 2019
8da33ee
rerun travis
lainsworth8801 Dec 9, 2019
3f76064
Merge branch 'develop' of https://github.com/SEED-platform/seed into zip
lainsworth8801 Dec 9, 2019
a751163
Merge branch 'develop' of https://github.com/SEED-platform/seed into zip
lainsworth8801 Dec 10, 2019
1b7b06f
Minor tweak
lainsworth8801 Dec 11, 2019
88993b3
Merge branch 'develop' of https://github.com/SEED-platform/seed into zip
lainsworth8801 Dec 11, 2019
1dd389c
Merge branch 'develop' into zip
nllong Dec 12, 2019
024c3eb
Merge branch 'develop' into zip
nllong Dec 14, 2019
5e8115f
fix migration order
nllong Dec 16, 2019
1864338
fix migration to only find postal codes of length 4 to fix
nllong Dec 16, 2019
86af8b5
Merge branch 'develop' into zip
nllong Dec 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
54 changes: 54 additions & 0 deletions seed/data_importer/tests/integration/test_data_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,57 @@ def import_exported_data(self, filename):

# call the mapping function from the tasks file
map_data(self.import_file.id)


class TestPostalCode(DataMappingBaseTestCase):
def setUp(self):
filename = getattr(self, 'filename', 'example-data-properties-postal.xlsx')
import_file_source_type = ASSESSED_RAW
self.fake_mappings = FAKE_MAPPINGS
selfvars = self.set_up(import_file_source_type)
self.user, self.org, self.import_file, self.import_record, self.cycle = selfvars
filepath = osp.join(osp.dirname(__file__), '..', 'data', filename)
self.import_file.file = SimpleUploadedFile(
name=filename,
content=open(filepath, 'rb').read()
)
self.import_file.save()

def test_postal_code_property(self):

new_mappings = copy.deepcopy(self.fake_mappings['portfolio'])

tasks.save_raw_data(self.import_file.pk)
Column.create_mappings(new_mappings, self.org, self.user, self.import_file.pk)
tasks.map_data(self.import_file.pk)

# get mapped property postal_code
ps = PropertyState.objects.filter(address_line_1='11 Ninth Street')[0]
self.assertEqual(ps.postal_code, '00340')

ps = PropertyState.objects.filter(address_line_1='20 Tenth Street')[0]
self.assertEqual(ps.postal_code, '00000')

ps = PropertyState.objects.filter(address_line_1='93029 Wellington Blvd')[0]
self.assertEqual(ps.postal_code, '00001-0002')

def test_postal_code_taxlot(self):

new_mappings = copy.deepcopy(self.fake_mappings['taxlot'])

tasks.save_raw_data(self.import_file.pk)
Column.create_mappings(new_mappings, self.org, self.user, self.import_file.pk)
tasks.map_data(self.import_file.pk)

# get mapped taxlot postal_code
ts = TaxLotState.objects.filter(address_line_1='35 Tenth Street').first()

if ts is None:
raise TypeError("Invalid Taxlot Address!")
self.assertEqual(ts.postal_code, '00333')

ts = TaxLotState.objects.filter(address_line_1='93030 Wellington Blvd').first()

if ts is None:
raise TypeError("Invalid Taxlot Address!")
self.assertEqual(ts.postal_code, '00000-0000')
13 changes: 9 additions & 4 deletions seed/data_importer/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"to_table_name": 'TaxLotState',
"to_field": 'block_number'
},
{
'from_field': 'postal code',
'to_table_name': 'TaxLotState',
'to_field': 'postal_code',
}
]

PROPERTIES_MAPPING = [
Expand Down Expand Up @@ -122,6 +127,10 @@
"from_field": 'recent sale date',
"to_table_name": 'PropertyState',
"to_field": 'recent_sale_date'
}, {
'from_field': 'postal code',
'to_table_name': 'PropertyState',
'to_field': 'postal_code',
}
]

Expand Down Expand Up @@ -165,10 +174,6 @@
'from_field': 'City', # raw field in import file
'to_field': 'city',
'to_table_name': 'PropertyState',
}, {
'from_field': 'Zip', # raw field in import file
'to_field': 'postal_code',
'to_table_name': 'PropertyState',
}, {
'from_field': 'GBA', # raw field in import file
'to_field': 'gross_floor_area',
Expand Down
8 changes: 8 additions & 0 deletions seed/lib/mcm/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def apply_column_value(raw_column_name, column_value, model, mapping, is_extra_d
if raw_column_name in mapping:
table_name, mapped_column_name, display_name, is_extra_data = mapping.get(raw_column_name)

# special postal case:
if mapped_column_name in ['postal_code', 'owner_postal_code']:
if '-' in str(column_value):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2019-11-18 at 11 04 44 AM

postal = str(column_value).split('-')[0].zfill(5)
ext = str(column_value).split('-')[1].zfill(4)
column_value = postal + '-' + ext
column_value = str(column_value).zfill(5)

cleaned_value = None
if cleaner:
# Get the list of Quantity fields from the Column object in SEED. This is non-ideal, since the
Expand Down
57 changes: 57 additions & 0 deletions seed/migrations/0115_rehash_postal_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-

# This rehashing file should be used in the future if needed as it has been optimized. Rehashing takes
# awhile and should be avoided if possible.
from __future__ import unicode_literals

import re

from django.db import migrations, transaction
from django.db.models import Q


def zero_fill(postal):
if postal:
if bool(re.compile(r'(\b\d{1,5}-\d{1,4}\b)').match(postal)):
return postal.split('-')[0].zfill(5) + '-' + postal.split('-')[1].zfill(4)
elif bool(re.compile(r'(\b\d{1,5}\b)').match(postal)):
return postal.zfill(5)


# Go through every property and tax lot and simply save it to create the hash_object
def update_postal_codes(apps, schema_editor):
PropertyState = apps.get_model('seed', 'PropertyState')
TaxLotState = apps.get_model('seed', 'TaxLotState')

with transaction.atomic():
print('Checking for short postal codes in property states')
objs = PropertyState.objects.filter(
Q(postal_code__iregex=r'^\d{4}-\d{3,4}$') | Q(postal_code__iregex=r'^\d{4}$') |
Q(owner_postal_code__iregex=r'^\d{4}-\d{3,4}$') | Q(owner_postal_code__iregex=r'^\d{4}$')
)
for obj in objs:
# print(
# f'fixing zip for {obj} -- postal_code | owner_postal_code = {obj.postal_code} | {obj.owner_postal_code}')
obj.postal_code = zero_fill(obj.postal_code)
obj.owner_postal_code = zero_fill(obj.owner_postal_code)
obj.save()

print('Checking for short postal codes in tax lot states')
objs = TaxLotState.objects.filter(
Q(postal_code__iregex=r'^\d{4}-\d{3,4}$') | Q(postal_code__iregex=r'^\d{4}$')
)
for obj in objs:
# print(
# f'fixing zip for {obj} -- postal_code | owner_postal_code = {obj.postal_code} | {obj.owner_postal_code}')
obj.postal_code = zero_fill(obj.postal_code)
obj.save()


class Migration(migrations.Migration):
dependencies = [
('seed', '0114_auto_20191211_0958'),
]

operations = [
migrations.RunPython(update_postal_codes),
]