Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.81 KB | None | 0 0
  1. def import_updates_from_fmr_spreadsheet(
  2.     xls_path
  3. ):
  4.     wb = xl.load_workbook(xls_path)
  5.     sheet = wb[wb.sheetnames[0]]
  6.  
  7.     head_row = sheet[1:1]
  8.     head_col_map = {}
  9.     idx = 0
  10.     for col in head_row:
  11.         head_col_map[col.value.split("\n")[0].split("(")[0].lower( ).strip( )] = idx
  12.         idx += 1
  13.  
  14.     total_rows = 0
  15.     for row in sheet.iter_rows(min_row=2):
  16.         total_rows += 1
  17.  
  18.     REPLACE_NAMES = {
  19.         "BLOCK - 6 OPD, DENTAL, X-RAY AND OFFICES": "BLOCK 6 - OPD, DENTAL, X-RAY AND OFFICES",
  20.         "BLOCK -11 WELNEY WARD": "PW11- OLIVER ZANGWILL & WELNEY"
  21.     }
  22.  
  23.     proc = 0
  24.     upd = 0
  25.     missing = []
  26.     unresolved = []
  27.     unresolved_multiple = []
  28.     loc_cache = {}
  29.     for row in sheet.iter_rows(min_row=2):
  30.         ast = None
  31.         proc += 1
  32.         if proc > 1000: break
  33.         print(proc, "/", total_rows)
  34.         asset_id = row[head_col_map["asset name"]].value
  35.         old_asset_id = row[head_col_map["asset id"]].value
  36.         if not asset_id or not asset_id.strip( ): continue
  37.  
  38.         try:
  39.             # Try amd look up by matching asset_id
  40.             ast = Asset.objects.get(asset_id__iexact=old_asset_id.strip( ))
  41.         except Asset.DoesNotExist:
  42.             try:
  43.                 ast = Asset.objects.get(name__iexact=asset_id.strip( ))
  44.             except Asset.MultipleObjectsReturned:
  45.                 # Try and get location.
  46.                 building, floor, room = (
  47.                     row[head_col_map["grandparent location name"]].value,
  48.                     row[head_col_map["parent location name"]].value,
  49.                     row[head_col_map["location name"]].value
  50.                 )
  51.                 key = "//".join((building, floor, room))
  52.                 prj = loc_cache.get(key)
  53.                 if not prj:
  54.                     locss = Location.objects.all( )
  55.                     prj = None
  56.                     for dat in (building, floor, room):
  57.                         if not dat or prj: continue
  58.                         dt = REPLACE_NAMES.get(dat) or dat
  59.                         lc = locss.filter(name__iexact=dt)
  60.                         if lc.count( ) == 1:
  61.                             prj = lc.first( ).get_root( )
  62.                         elif lc.count( ) == 2:
  63.                             locss = lc
  64.  
  65.                     if not prj:
  66.                         if 'BLOCK -' in building:
  67.                             building = " ".join(building.split(" ")[2:])
  68.                             lc = Location.objects.filter(
  69.                                 name__contains=building
  70.                             )
  71.  
  72.                         rt = set([l.get_root( ) for l in lc])
  73.                         if len(rt) == 1:
  74.                             prj = list(rt)[0]
  75.                         else:
  76.                             unresolved.append((proc+1, asset_id, old_asset_id))
  77.                             continue
  78.                 if prj:
  79.                     loc_cache[key] = prj
  80.  
  81.                 try:
  82.                     ast = Asset.objects.get(
  83.                         name__iexact=asset_id.strip( ),
  84.                         main_location__in=prj.get_descendants( )
  85.                     )
  86.                 except Asset.DoesNotExist:
  87.                     unresolved.append((proc+1, asset_id, old_asset_id))
  88.                     continue
  89.                 except Asset.MultipleObjectsReturned:
  90.                     unresolved_multiple.append((proc+1, asset_id, old_asset_id))
  91.                     continue
  92.             except Asset.DoesNotExist:
  93.                 # Probably Virtual
  94.                 missing.append((proc+1, asset_id, old_asset_id))
  95.                 continue
  96.  
  97.         '''
  98.        [('besa-sfg20', 'BESA SFG20', 'Manual Overrides'),
  99.         ('comments', 'Comments', 'Audit'),
  100.         ('condition_comments', 'Condition Comments', 'Audit'),
  101.         ('six_facet_survey', 'Condition Rating', 'Audit'),
  102.         ('description', 'Description of Asset', 'Audit'),
  103.         ('description-tag', 'Description (TAG)', 'Audit'),
  104.         ('investment_year', 'Investment Year', 'Audit'),
  105.         ('life_expectancy', 'Life Expectancy', 'Manual Overrides'),
  106.         ('lifts-sfg20', 'Lifts (SFG20)', 'Lifts'),
  107.         ('old_asset_id_system', 'Old Asset Id (System)', None),
  108.         ('old_code', 'Old Code', 'Audit'),
  109.         ('parent_asset_code_lifts_only', 'Parent Asset Code (Lifts Only)', 'Lifts'),
  110.         ('quantity', 'Quantity', 'Audit'),
  111.         ('replacement_cost', 'Replacement Cost', 'Audit'),
  112.         ('sfg20', 'SFG20', 'Audit'),
  113.         ('sizecapacity', 'Size/Capacity', 'Audit'),
  114.         ('updated_from_fmr', 'Updated from FMR', None),
  115.         ('virtual_asset_description',
  116.          'Virtual Asset Description',
  117.          'Virtual Assets Only'),
  118.         ('year_of_installation', 'Year of Installation', 'Audit')]'''
  119.  
  120.         if ast:
  121.             ast.asset_id = old_asset_id
  122.             ast.eav.updated_from_fmr = True
  123.  
  124.             for sheet, eav_attr in (
  125.                 ('condition rating', 'six_facet_survey'),
  126.                 ('condition comments', 'condition_comments'),
  127.                 ('investment year', 'investment_year'),
  128.                 ('life expectancy', 'life_expectancy'),
  129.                 ('replacement cost', 'replacement_cost'),
  130.                 ('year of installation', 'year_of_installation')
  131.             ):
  132.                 val = str(row[head_col_map[sheet]].value or '').strip( ).strip("\xa0")
  133.                 setattr(ast.eav, eav_attr, val)
  134.  
  135.             upd += 1
  136.             ast.save( )
  137.             #old_asset_id_system'''
  138.  
  139.     print("Processed {}, Success {}".format(proc, upd))
  140.     print("Missing: {}".format(len(failed)))
  141.     print("Unresolved (missing): {}".format(len(unresolved)))
  142.     print("Unresolved (multiple): {}".format(len(unresolved_multiple)))
  143.  
  144.     return (proc, upd, failed, unresolved, unresolved_multiple)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement