def import_xlsx(filename): prepare_stdout() wb = load_workbook(filename=filename) sheet = wb.get_sheet_by_name(name='Sheet1') sheet.get_highest_column() sheet.get_highest_row() sheet.cell(row=1, column=1) data = sheet.range(sheet.calculate_dimension()) headers = [v.value for v in data[0]] for row in data[1:]: vals = [str(v.value).strip() for v in row] sys.stdout.write('\r ID=%s' % vals[0]) m = MuseumObject() m.id = vals[0] m.registration_number = vals[0] m.old_registration_number = vals[1] m.other_number = vals[2] m.functional_category, c = FunctionalCategory.objects.get_or_create( name=vals[3]) m.artefact_type, c = ArtefactType.objects.get_or_create(name=vals[4]) m.storage_section = vals[5] m.storage_unit = vals[6] m.storage_bay = vals[7] m.storage_shelf_box_drawer = vals[8] if vals[9] != 'None': sys.stdout.write(' Acq_date=%s' % vals[9]) m.acquisition_date = vals[9].split(' ')[0] m.access_status, c = AccessStatus.objects.get_or_create( status=vals[10]) m.description = vals[11] m.comment = vals[12] region = vals[13] state = vals[14] m.acquisition_method, c = AcquisitionMethod.objects.get_or_create( method=vals[15]) m.loan_status, created = LoanStatus.objects.get_or_create( status=vals[16]) country = vals[17] place = vals[18] m.place, created = Place.objects.get_or_create(name=place, australian_state=state, region=region, country=country) m.cultural_bloc, c = CulturalBloc.objects.get_or_create(name=vals[19]) photographic_record = vals[20] m.collector, created = Person.objects.get_or_create(name=vals[21]) m.when_collector_obtained = vals[22] m.how_collector_obtained, created = Obtained.objects.get_or_create( how=vals[23]) m.donor, created = Person.objects.get_or_create(name=vals[24]) # All empty when_obtained = vals[25] # All empty how_obtained = vals[26] # All empty category_illustration = vals[27] # All empty artefaction_illustration = vals[28] m.raw_material = vals[29] m.indigenous_name = vals[30] m.assoc_cultural_group = vals[31] m.recorded_use = vals[32] if vals[33]: m.maker, created = Maker.objects.get_or_create(name=vals[33]) def mapint(attr, val): try: setattr(m, attr, int(val)) except ValueError: pass mapint('length', vals[34]) mapint('width', vals[35]) mapint('depth', vals[36]) mapint('circumference', vals[37]) mapint('weight', vals[38]) m.save() set_categories(m) set_photographic_record(m, photographic_record)
def process_artefact_record(r): sys.stdout.write(' Id=%s' % r['Artefact_Registration']) r = clean_row(r) m = MuseumObject() # Map simple fields m.id = r["Artefact_Registration"] m.registration_number = r["Artefact_Registration"] m.old_registration_number = r["Old_Registration_nmbr"] m.reg_counter = r["Reg_counter"] m.other_number = r["Other_nmbr"] if r["Aquisition_Date"] != "": m.acquisition_date = r["Aquisition_Date"] m.acquisition_method, created = AcquisitionMethod.objects.get_or_create( method=r['Aquisition_Method']) m.access_status, created = AccessStatus.objects.get_or_create( status=r['AccessStatus']) m.loan_status, created = LoanStatus.objects.get_or_create( status=r["Loan_Status"]) m.description = r["Description"] m.comment = r["Comment"] m.storage_section = r['Storage_Section'] m.storage_unit = r['Storage_Unit'] m.storage_bay = r['Storage_Bay'] m.storage_shelf_box_drawer = r['Shelf_Box_Drawer'] # Map relations fc, created = FunctionalCategory.objects.get_or_create( name=r['Functional_Category']) m.functional_category = fc pl, created = Place.objects.get_or_create(name=r['Place'], australian_state=r['State_Abbr'], region=r['Region'], country=r['Country_Name']) m.place = pl at, created = ArtefactType.objects.get_or_create( name=r['Artefact_TypeName']) m.artefact_type = at cb, created = CulturalBloc.objects.get_or_create( name=r['CulturalBloc']) m.cultural_bloc = cb collector_id = r["Collector_photographerID"] if collector_id: try: p = Person.objects.get(pk=int(collector_id)) m.collector = p except: sys.stdout.write('\rMO: %s Unknown collector: %s\n' % (m.id, collector_id)) donor_id = r["DonorID"] if donor_id: try: p = Person.objects.get(pk=int(donor_id)) m.donor = p except: sys.stdout.write('\rMO: %s Unknown donor: %s\n' % (m.id, donor_id)) m.save() set_categories(m)