Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def import_updates_from_fmr_spreadsheet(
- xls_path
- ):
- wb = xl.load_workbook(xls_path)
- sheet = wb[wb.sheetnames[0]]
- head_row = sheet[1:1]
- head_col_map = {}
- idx = 0
- for col in head_row:
- head_col_map[col.value.split("\n")[0].split("(")[0].lower( ).strip( )] = idx
- idx += 1
- total_rows = 0
- for row in sheet.iter_rows(min_row=2):
- total_rows += 1
- REPLACE_NAMES = {
- "BLOCK - 6 OPD, DENTAL, X-RAY AND OFFICES": "BLOCK 6 - OPD, DENTAL, X-RAY AND OFFICES",
- "BLOCK -11 WELNEY WARD": "PW11- OLIVER ZANGWILL & WELNEY"
- }
- proc = 0
- upd = 0
- missing = []
- unresolved = []
- unresolved_multiple = []
- loc_cache = {}
- for row in sheet.iter_rows(min_row=2):
- ast = None
- proc += 1
- if proc > 1000: break
- print(proc, "/", total_rows)
- asset_id = row[head_col_map["asset name"]].value
- old_asset_id = row[head_col_map["asset id"]].value
- if not asset_id or not asset_id.strip( ): continue
- try:
- # Try amd look up by matching asset_id
- ast = Asset.objects.get(asset_id__iexact=old_asset_id.strip( ))
- except Asset.DoesNotExist:
- try:
- ast = Asset.objects.get(name__iexact=asset_id.strip( ))
- except Asset.MultipleObjectsReturned:
- # Try and get location.
- building, floor, room = (
- row[head_col_map["grandparent location name"]].value,
- row[head_col_map["parent location name"]].value,
- row[head_col_map["location name"]].value
- )
- key = "//".join((building, floor, room))
- prj = loc_cache.get(key)
- if not prj:
- locss = Location.objects.all( )
- prj = None
- for dat in (building, floor, room):
- if not dat or prj: continue
- dt = REPLACE_NAMES.get(dat) or dat
- lc = locss.filter(name__iexact=dt)
- if lc.count( ) == 1:
- prj = lc.first( ).get_root( )
- elif lc.count( ) == 2:
- locss = lc
- if not prj:
- if 'BLOCK -' in building:
- building = " ".join(building.split(" ")[2:])
- lc = Location.objects.filter(
- name__contains=building
- )
- rt = set([l.get_root( ) for l in lc])
- if len(rt) == 1:
- prj = list(rt)[0]
- else:
- unresolved.append((proc+1, asset_id, old_asset_id))
- continue
- if prj:
- loc_cache[key] = prj
- try:
- ast = Asset.objects.get(
- name__iexact=asset_id.strip( ),
- main_location__in=prj.get_descendants( )
- )
- except Asset.DoesNotExist:
- unresolved.append((proc+1, asset_id, old_asset_id))
- continue
- except Asset.MultipleObjectsReturned:
- unresolved_multiple.append((proc+1, asset_id, old_asset_id))
- continue
- except Asset.DoesNotExist:
- # Probably Virtual
- missing.append((proc+1, asset_id, old_asset_id))
- continue
- '''
- [('besa-sfg20', 'BESA SFG20', 'Manual Overrides'),
- ('comments', 'Comments', 'Audit'),
- ('condition_comments', 'Condition Comments', 'Audit'),
- ('six_facet_survey', 'Condition Rating', 'Audit'),
- ('description', 'Description of Asset', 'Audit'),
- ('description-tag', 'Description (TAG)', 'Audit'),
- ('investment_year', 'Investment Year', 'Audit'),
- ('life_expectancy', 'Life Expectancy', 'Manual Overrides'),
- ('lifts-sfg20', 'Lifts (SFG20)', 'Lifts'),
- ('old_asset_id_system', 'Old Asset Id (System)', None),
- ('old_code', 'Old Code', 'Audit'),
- ('parent_asset_code_lifts_only', 'Parent Asset Code (Lifts Only)', 'Lifts'),
- ('quantity', 'Quantity', 'Audit'),
- ('replacement_cost', 'Replacement Cost', 'Audit'),
- ('sfg20', 'SFG20', 'Audit'),
- ('sizecapacity', 'Size/Capacity', 'Audit'),
- ('updated_from_fmr', 'Updated from FMR', None),
- ('virtual_asset_description',
- 'Virtual Asset Description',
- 'Virtual Assets Only'),
- ('year_of_installation', 'Year of Installation', 'Audit')]'''
- if ast:
- ast.asset_id = old_asset_id
- ast.eav.updated_from_fmr = True
- for sheet, eav_attr in (
- ('condition rating', 'six_facet_survey'),
- ('condition comments', 'condition_comments'),
- ('investment year', 'investment_year'),
- ('life expectancy', 'life_expectancy'),
- ('replacement cost', 'replacement_cost'),
- ('year of installation', 'year_of_installation')
- ):
- val = str(row[head_col_map[sheet]].value or '').strip( ).strip("\xa0")
- setattr(ast.eav, eav_attr, val)
- upd += 1
- ast.save( )
- #old_asset_id_system'''
- print("Processed {}, Success {}".format(proc, upd))
- print("Missing: {}".format(len(failed)))
- print("Unresolved (missing): {}".format(len(unresolved)))
- print("Unresolved (multiple): {}".format(len(unresolved_multiple)))
- return (proc, upd, failed, unresolved, unresolved_multiple)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement