def load_suboffices(officeID, suboffice_type, api): """ Loads all suboffices of the office with the given officeID, and gives them the appropriate office_type (region, MC or LC) """ suboffices = api.getSuboffices(officeID) for so in suboffices: print "Loading %s %s(%s)" % (suboffice_type, so['name'], so['id']) try: values = { 'name':so['name'], 'office_type':suboffice_type, 'superoffice_id':officeID, } item, created = Office.objects.update_or_create(expaID=so['id'], defaults=values) except IntegrityError as e: print e superoffice = Office.objects.get(expaID=officeID) values['name'] = so['name'] + '_' + superoffice.name item, created = Office.objects.update_or_create(expaID=so['id'], defaults=values) print item.name if suboffice_type=="Region": sub_suboffice_type = "MC" elif suboffice_type=="MC": sub_suboffice_type = "LC" elif suboffice_type=="LC": continue else: raise Exception("Unknown office type") load_suboffices(so['id'], sub_suboffice_type, api)
def loadLCs(mc): api = expaApi.ExpaApi() lcs = api.getSuboffices(mc) for lc in lcs: item = LC(nombre=lc['name'], expaID=lc['id'], mc=MC.objects.get(expaID=mc)) try: item.save() except IntegrityError as e: print e item.nombre += '_' + item.mc.nombre print item.nombre
def load_suboffices(officeID, suboffice_type, api): """ Loads all suboffices of the office with the given officeID, and gives them the appropriate office_type (region, MC or LC) """ suboffices = api.getSuboffices(officeID) for so in suboffices: print "Loading %s %s(%s)" % (suboffice_type, so['name'], so['id']) try: values = { 'name':so['name'], 'office_type':suboffice_type, 'superoffice_id':officeID, } item, created = Office.objects.update_or_create(expaID=so['id'], defaults=values) except IntegrityError as e: #Because of the way the update or create works, a constraint other than the primary key is being violated here. It must be the unique name constraint. Therefore we will attempt to create a new LC with a modified version of the name. lc_number = 2 print "Ya existe esta oficina" print e while True: try: values['name'] = so['name'] + '_' + str(lc_number) print "Nuevo nombre" print values['name'] item, created = Office.objects.update_or_create(expaID=so['id'], defaults=values) print "Nuevo LC creado" break except IntegrityError as e2: lc_number += 1 print "todavía existe, continuando..." if suboffice_type=="Region": sub_suboffice_type = "MC" elif suboffice_type=="MC": sub_suboffice_type = "LC" elif suboffice_type=="LC": continue else: raise Exception("Unknown office type") load_suboffices(so['id'], sub_suboffice_type, api)