def store_legislador_item(x): try: distrito = Distrito.objects.get(nombre=x['distrito_nombre']) except Distrito.DoesNotExist: distrito = Distrito(nombre=x['distrito_nombre'], origin=AUDIT_ORIGIN) distrito.resource_source = x['resource_source'] distrito.resource_url = x['resource_url'] distrito.save() if x.get('bloque_nombre'): try: bloque = Bloque.objects.get(nombre=x['bloque_nombre']) except Bloque.DoesNotExist: bloque = Bloque(nombre=x['bloque_nombre'], origin=AUDIT_ORIGIN) bloque.resource_source = x['resource_source'] bloque.resource_url = x['resource_url'] bloque.save() else: bloque = None if x.get('partido_nombre'): try: partido = Partido.objects.get(nombre=x['partido_nombre']) except Partido.DoesNotExist: partido = Partido(nombre=x['partido_nombre'], origin=AUDIT_ORIGIN) partido.resource_source = x['resource_source'] partido.resource_url = x['resource_url'] partido.save() else: partido = None persona_created = True try: persona = Persona.objects.get(nombre=x['nombre'], apellido=x['apellido'], legislador__camara=x['camara'], legislador__bloque=bloque, legislador__distrito=distrito, legislador__inicio=isodate.parse_date(x['mandato_inicio']), legislador__fin=isodate.parse_date(x['mandato_fin'])) persona_created = False except Persona.DoesNotExist: try: persona = Persona.objects.get(nombre=x['nombre'], apellido=x['apellido']) persona_created = False except Persona.MultipleObjectsReturned: log.error((u"This is an expected error! Aparently you have more than one Persona named: " u"%(apellido)s, %(nombre)s. You'll have to fix this by hand. Set var 'persona' " u"to the desired Persona instance and continue (c)") % x) import ipdb; ipdb.set_trace() except Persona.DoesNotExist: persona = Persona(nombre=x['nombre'], apellido=x['apellido'], origin=AUDIT_ORIGIN) try: assert isinstance(persona, Persona) except (NameError, AssertionError): raise RuntimeError(u"Missing Persona, sorry, need to abort.") persona.email = x.get('email') or None # the 'or None' thing is cuz we don't want empty strings. persona.telefono = x.get('telefono') or None persona.foto = x.get('foto_url') or None # <--- makes no sense, but we don't care right now. persona.save() if persona_created: persona.resource_source = x['resource_source'] persona.resource_url = x['resource_url'] persona.resource_id = x['resource_id'] persona.save() log.debug(u'Created %s Persona' % persona.uuid) else: log.debug(u'Updated %s Persona' % persona.uuid) try: legislador = Legislador.objects.get(persona=persona, camara=x['camara'], bloque=bloque, distrito=distrito, inicio=isodate.parse_date(x['mandato_inicio']), fin=isodate.parse_date(x['mandato_fin'])) log.debug(u'Updated %s Legislador' % legislador.uuid) except Legislador.DoesNotExist: legislador = Legislador(persona=persona, camara=x['camara'], bloque=bloque, partido=partido, distrito=distrito, inicio=isodate.parse_date(x['mandato_inicio']), fin=isodate.parse_date(x['mandato_fin'])) legislador.resource_source = x['resource_source'] legislador.resource_url = x['resource_url'] legislador.resource_id = x['resource_id'] legislador.origin = AUDIT_ORIGIN legislador.save() log.debug(u'Created %s Legislador' % legislador.uuid) return True
def store_firmaproyecto_item(x): try: proyecto = Proyecto.objects.get(camara_origen_expediente=x['proyecto_camara_origen_expediente'], camara_origen=x['proyecto_camara_origen']) except Proyecto.DoesNotExist: return False if x.get('firmante_bloque'): try: bloque = Bloque.objects.get(nombre=x['firmante_bloque']) except Bloque.DoesNotExist: bloque = Bloque(nombre=x['firmante_bloque'], origin=AUDIT_ORIGIN) bloque.resource_source = x['resource_source'] bloque.resource_url = x['resource_url'] bloque.save() else: bloque = None if x.get('firmante_distrito'): try: distrito = Distrito.objects.get(nombre=x['firmante_distrito']) except Distrito.DoesNotExist: distrito = Distrito(nombre=x['firmante_distrito'], origin=AUDIT_ORIGIN) distrito.resource_source = x['resource_source'] distrito.resource_url = x['resource_url'] distrito.save() else: distrito = None poder =x['firmante_poder'] firmante_special = x.get('firmante_special') or u'' if not firmante_special: firmante_apellido = x.get('firmante_apellido') or u'' firmante_nombre = x.get('firmante_nombre') or u'' try: persona = Persona.objects.get(apellido=firmante_apellido, nombre=firmante_nombre) except Persona.DoesNotExist: persona = Persona(apellido=firmante_apellido, nombre=firmante_nombre, origin=AUDIT_ORIGIN) persona.resource_source = x['resource_source'] persona.resource_url = x['resource_url'] persona.save() try: legislador = Legislador.objects.get(persona=persona, bloque=bloque, distrito=distrito) except Legislador.DoesNotExist: # if legislador created, inicio and fin will be missing. Whatever. legislador = Legislador(persona=persona, bloque=bloque, distrito=distrito, camara='?', origin=AUDIT_ORIGIN) legislador.resource_source = x['resource_source'] legislador.resource_url = x['resource_url'] legislador.save() else: persona = legislador = None try: fp = FirmaProyecto.objects.get(proyecto=proyecto, legislador=legislador, poder=poder, poder_who=firmante_special, tipo_firma=x['tipo_firma']) log.debug(u'Updated %s FirmaProyecto' % fp.uuid) except FirmaProyecto.DoesNotExist: fp = FirmaProyecto(proyecto=proyecto, legislador=legislador, poder=poder, poder_who=firmante_special, tipo_firma=x['tipo_firma'], origin=AUDIT_ORIGIN) fp.resource_source = x['resource_source'] fp.resource_url = x['resource_url'] fp.resource_id = x.get('resource_id') fp.save() log.debug(u'Created %s FirmaProyecto' % fp.uuid) return True