def _asignar_agente_a_campana(self, agente, campana, penalty=0):
     try:
         client = AmiManagerClient()
         client.connect()
         with transaction.atomic():
             queue_member = QueueMember(penalty=penalty)
             queue_member_defaults = QueueMember.get_defaults(
                 agente, campana)
             queue_member.member = agente
             queue_member.queue_name = campana.queue_campana
             queue_member.id_campana = queue_member_defaults['id_campana']
             queue_member.membername = queue_member_defaults['membername']
             queue_member.interface = queue_member_defaults['interface']
             # por ahora no definimos 'paused'
             queue_member.paused = queue_member_defaults['paused']
             queue_member.save()
             # adicionamos el agente a la cola actual que esta corriendo
             sip_agentes_logueados = obtener_sip_agentes_sesiones_activas()
             adicionar_agente_activo_cola(queue_member, campana,
                                          sip_agentes_logueados, client)
             activar_cola()
         client.disconnect()
     except Exception as e:
         print("Can't assign agent to campaign due to {0}".error(e))
         raise e
    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        if self.object.is_agente and self.object.get_agente_profile():
            agente_profile = self.object.get_agente_profile()
            agente_profile.borrar()
            # ahora vamos a remover el agente de la cola de asterisk
            queues_member_agente = agente_profile.campana_member.all()
            try:
                client = AmiManagerClient()
                client.connect()
            except AMIManagerConnectorError:
                logger.exception(_("QueueRemove failed "))
            for queue_member in queues_member_agente:
                campana = queue_member.queue_name.campana
                remover_agente_cola_asterisk(campana, agente_profile, client)
            client.disconnect()
            activar_cola()
            QueueMember.objects.borrar_member_queue(agente_profile)

        if self.object.is_supervisor and self.object.get_supervisor_profile():
            self.object.get_supervisor_profile().borrar()
        if self.object.is_cliente_webphone and self.object.get_cliente_webphone_profile(
        ):
            self.object.get_cliente_webphone_profile().borrar()
        self.object.borrar()
        return HttpResponseRedirect(self.get_success_url())
    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        if self.object.is_agente and self.object.get_agente_profile():
            agente_profile = self.object.get_agente_profile()
            agente_profile.borrar()
            # ahora vamos a remover el agente de la cola de asterisk
            queues_member_agente = agente_profile.campana_member.all()
            for queue_member in queues_member_agente:
                campana = queue_member.queue_name.campana
                remover_agente_cola_asterisk(campana, agente_profile)
            activar_cola()
            QueueMember.objects.borrar_member_queue(agente_profile)

        if self.object.is_supervisor and self.object.get_supervisor_profile():
            self.object.get_supervisor_profile().borrar()
        if self.object.is_cliente_webphone and self.object.get_cliente_webphone_profile(
        ):
            self.object.get_cliente_webphone_profile().borrar()
        self.object.borrar()
        return HttpResponseRedirect(self.get_success_url())