Skip to content

Commit

Permalink
add manage task for importing mapping preset. flake8 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nllong committed May 13, 2020
1 parent 5f8c536 commit d812a9b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 deletions.
26 changes: 26 additions & 0 deletions seed/data_importer/tests/data/mappings/example-data-properties.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Raw Columns,units,SEED Table,SEED Columns
jurisdiction property identifier,,PropertyState,JPID
custom id 1,,PropertyState,custom_id_1
pm property id,,PropertyState,pm_property_id
pm parent propery id,,PropertyState,pm_parent_property_id
UBID,,PropertyState,ubid
property name,,PropertyState,property_name
address line 1,,PropertyState,address_line_1
city,,PropertyState,city
jurisdiction tax lot id,,TaxLotState,jurisdiction_tax_lot_id
use description,,PropertyState,use_description
energy score,,PropertyState,energy_star_score
site eui,kBtu/ft**2/year,PropertyState,site_eui
year ending,,PropertyState,year_ending
gross floor area,ft**2,PropertyState,gross_floor_area
owner,,PropertyState,owner
owner email,,PropertyState,owner_email
owner telephone,,PropertyState,owner_telephone
property notes,,PropertyState,property_notes
latitude,,PropertyState,latitude
longitude,,PropertyState,longitude
recent sale date,,PropertyState,recent_sale_date
generation date,,PropertyState,generation_date
release date,,PropertyState,release_date
extra data 1,,PropertyState,extra 1
extra data 2,,PropertyState,extra 2
76 changes: 76 additions & 0 deletions seed/management/commands/add_mapping_preset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
"""
:copyright (c) 2014 - 2020, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Department of Energy) and contributors. All rights reserved. # NOQA
:author
"""
import csv
import os

from django.core.management.base import BaseCommand

from seed.lib.superperms.orgs.models import (
Organization,
)


class Command(BaseCommand):
help = 'Add a mapping profile to an organization from a CSV file'

def add_arguments(self, parser):
parser.add_argument('--organization_name',
help='Organization name',
action='store')

parser.add_argument('--csv_file',
help='Mapping profile CSV file, must follow specific format. Relative to location '
'of manage.py call.',
action='store',
required=True)

parser.add_argument('--name',
help='Name of the mapping profile',
action='store')

parser.add_argument('--overwrite',
help='Overwrite if column mapping profile name exists',
action='store_true')

def handle(self, *args, **options):
# verify that the user exists
org = Organization.objects.filter(name=options['organization_name']).first()
if not org:
self.stdout.write("No organization found for %s" % options['organization_name'])
exit(1)

if not os.path.exists(options['csv_file']):
self.stdout.write(f"Mapping CSV file does not exist: {options['csv_file']}")
exit(1)

mappings = []
with open(options['csv_file'], 'r') as f:
data = csv.reader(f, delimiter=',', quotechar="\"")
data.__next__() # skip the header row
for row in data:
units = row[1]
if units == '':
units = None
mappings.append(
{
'from_field': row[0],
'from_units': units,
'to_table_name': row[2],
'to_field': row[3],
}
)

# create the mapping preset
cmp, created = org.columnmappingpreset_set.get_or_create(name=options['name'])
if not created and not options['overwrite']:
self.stdout.write(f"Column mapping preset already exists: {options['name']}")
self.stdout.write("Pass --overwrite to overwrite existing mappings in preset if desired")
exit(0)

cmp.mappings = mappings
cmp.save()

self.stdout.write(f"Finished adding column mapping preset for {options['name']}")
2 changes: 1 addition & 1 deletion seed/management/commands/create_geojson_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def convert_list_to_wkt(self, geom):
:return:
"""
if geom['type'] == "Polygon":
coords = [f"{l[0]} {l[1]}" for l in geom['coordinates'][0]]
coords = [f"{coord[0]} {coord[1]}" for coord in geom['coordinates'][0]]
return f"POLYGON (( {', '.join(coords)} ))"
else:
raise Exception(f"Unknown type of Geomoetry in GeoJSON of {geom['type']}")
Expand Down
2 changes: 1 addition & 1 deletion seed/models/column_list_settings_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class ColumnListSettingColumn(models.Model):
pinned = models.BooleanField(default=False)

def __str__(self):
return self.column_list_setting.name + " %s %s".format(self.order, self.pinned)
return f"{self.column_list_setting.name} {self.order} {self.pinned}"
13 changes: 13 additions & 0 deletions seed/tests/data/mappings/covered-buildings-mapping-v2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Raw Columns,units,SEED Table,SEED Columns
UBI,,TaxLotState,jurisdiction_tax_lot_id
UBID,,PropertyState,ubid
GBA,ft**2,PropertyState,gross_floor_area
BLDGS,,PropertyState,building_count
Address,,PropertyState,address_line_1
Owner,,PropertyState,owner
State,,PropertyState,state_province
City,,PropertyState,city
Zip,,PropertyState,postal_code
Property Type,,PropertyState,use_description
AYB_YearBuilt,,PropertyState,year_built
Note,,PropertyState,property_notes

This file was deleted.

0 comments on commit d812a9b

Please sign in to comment.