Пример #1
0
def _update_lease_send_ws_signal(customer_id: int):
    send_data2ws({
        "eventType": WsEventTypeEnum.UPDATE_CUSTOMER_LEASES.value,
        "data": {
            "customer_id": customer_id
        }
    })
Пример #2
0
def task_post_save(sender, instance: Task, created=False, **kwargs):
    if instance.priority == Task.TASK_PRIORITY_HIGHER:
        if created:
            notify_title = _("High priority task was created")
        else:
            notify_title = _("High priority task was updated")
        recipients = instance.recipients.only("pk").values_list("pk",
                                                                flat=True)
        send_data2ws({
            "eventType": WsEventTypeEnum.UPDATE_TASK.value,
            "text": notify_title,
            "data": {
                "recipients": list(recipients),
                "author": instance.author_id if instance.author else None,
                "task_id": instance.pk,
            },
        })
        # FIXME: hardcode url
        notify_text = "{customer_name}: {text}".format(
            customer_name=instance.customer.get_full_name(),
            text=instance.descr,
        )
        send_broadcast_push_notification(title=notify_title,
                                         body=notify_text,
                                         url=f"/tasks/t{instance.pk}")
        return
    send_data2ws({
        "eventType": WsEventTypeEnum.UPDATE_TASK.value,
        "data": {
            "task_id": instance.pk
        }
    })
Пример #3
0
def on_remove_ip_lease_signal(sender, instance, **kwargs):
    send_data2ws({
        "eventType": WsEventTypeEnum.UPDATE_CUSTOMER_LEASES.value,
        "data": {
            "customer_id": instance.customer_id
        }
    })
Пример #4
0
def customer_post_save_signal(sender, instance, created=False, **kwargs):
    if not created:
        send_data2ws({
            "eventType": WsEventTypeEnum.UPDATE_CUSTOMER.value,
            "data": {
                "customer_id": instance.pk
            }
        })
Пример #5
0
def handle(task, author, recipients):
    profile_ids = []
    for recipient in recipients:
        if not recipient.flags.notify_task:
            continue
        # If signal to myself then quietly
        if author == recipient:
            return
        profile_ids.append(recipient.pk)

    task_status = _("Task")

    # If task completed or failed
    if task.task_state in (1, 2):
        task_status = _("Task completed")

    fulltext = render_to_string(
        "taskapp/notification.html", {"task": task, "customer": task.abon, "task_status": task_status}
    )

    send_data2ws(
        {
            "type": "task_event",
            "customer_uname": task.abon.username,
            "status": task_status,
            "author": author.pk,
            "recipients": profile_ids,
            "text": fulltext,
        }
    )

    if task.task_state in (1, 2):
        # If task completed or failed than send one message to author
        # send_email_notify(fulltext, author.pk)
        notification_signal.send(
            sender=task.__class__,
            instance=task,
            recipients=[author.pk],
            text=fulltext,
        )
        # send_viber_message(None, author.pk, fulltext)
    else:
        # multicast_email_notify(fulltext, profile_ids)
        # multicast_viber_notify(None, profile_ids, fulltext)
        notification_signal.send(
            sender=task.__class__,
            instance=task,
            recipients=profile_ids,
            text=fulltext,
        )
Пример #6
0
    def monitoring_event(self, request):
        dev_ip = request.query_params.get("dev_ip")
        dev_status = request.query_params.get("status")
        if dev_status not in ("UP", "UNREACHABLE", "DOWN"):
            return Response('bad "status" parameter',
                            status=status.HTTP_400_BAD_REQUEST)
        if not dev_ip:
            return Response({"text": "ip does not passed"})
        if not re.match(IP_ADDR_REGEX, dev_ip):
            return Response({"text": "ip address %s is not valid" % dev_ip})

        device = self.get_queryset().filter(
            ip_address=dev_ip).defer("extra_data").first()
        if device is None:
            return Response(
                {"text": "Devices with ip %s does not exist" % dev_ip})

        status_map = {
            "UP": Device.NETWORK_STATE_UP,
            "UNREACHABLE": Device.NETWORK_STATE_UNREACHABLE,
            "DOWN": Device.NETWORK_STATE_DOWN,
        }
        status_text_map = {
            "UP": "Device %(device_name)s is up",
            "UNREACHABLE": "Device %(device_name)s is unreachable",
            "DOWN": "Device %(device_name)s is down",
        }
        device.status = status_map.get(dev_status,
                                       Device.NETWORK_STATE_UNDEFINED)

        device.save(update_fields=("status", ))

        if not device.is_noticeable:
            return {
                "text":
                "Notification for %s is unnecessary" % device.ip_address
                or device.comment
            }

        if not device.group:
            return Response({"text": "Device has not have a group"})

        recipients = UserProfile.objects.get_profiles_by_group(
            group_id=device.group.pk).filter(
                flags=UserProfile.flags.notify_mon)
        user_ids = tuple(recipient.pk
                         for recipient in recipients.only("pk").iterator())

        notify_text = status_text_map.get(
            dev_status,
            default="Device %(device_name)s getting undefined status code")
        text = gettext(notify_text) % {
            "device_name":
            "{}({}) {}".format(device.ip_address or "", device.mac_addr,
                               device.comment)
        }
        ws_connector.send_data2ws({
            "type": "monitoring_event",
            "recipients": user_ids,
            "text": text
        })
        notification_signal.send(sender=device.__class__,
                                 instance=device,
                                 recipients=user_ids,
                                 text=text)
        return Response({"text": "notification successfully sent"})