예제 #1
0
    def make_notification(self, job):
        notification = Notification()
        notification.test_job = job
        notification.verbosity = Notification.QUIET

        notification.callback_url = "http://localhost/"
        notification.callback_token = "token"
        notification.callback_method = Notification.POST
        notification.callback_dataset = Notification.MINIMAL
        notification.save()

        return notification
예제 #2
0
def create_notification(job, data):
    # Create notification object.
    notification = Notification()

    if "verbosity" in data:
        notification.verbosity = Notification.VERBOSITY_MAP[data["verbosity"]]

    if "type" in data["criteria"]:
        notification.type = Notification.TYPE_MAP[data["criteria"]["type"]]

    if "compare" in data:
        if "blacklist" in data["compare"]:
            notification.blacklist = data["compare"]["blacklist"]
        if "query" in data["compare"]:
            query_data = data["compare"]["query"]
            if "username" in query_data:
                # DoesNotExist scenario already verified in validate
                username = query_data["username"]
                notification.query_owner = User.objects.get(username=username)
                notification.query_name = query_data["name"]
            else:  # Custom query.
                notification.entity = Query.get_content_type(
                    query_data["entity"])
                if "conditions" in query_data:
                    # Save conditions as a string.
                    conditions = [
                        "%s%s%s" % (key, Query.CONDITION_DIVIDER, value)
                        for (key, value) in query_data["conditions"].items()
                    ]
                    notification.conditions = Query.CONDITIONS_SEPARATOR.join(
                        conditions)

    notification.test_job = job
    notification.template = Notification.DEFAULT_TEMPLATE
    notification.save()

    if "recipients" in data:
        for recipient in data["recipients"]:
            notification_recipient = NotificationRecipient(
                notification=notification)
            notification_recipient.method = NotificationRecipient.METHOD_MAP[
                recipient["to"]["method"]]
            if "user" in recipient["to"]:
                user = User.objects.get(username=recipient["to"]["user"])
                notification_recipient.user = user
            if "email" in recipient["to"]:
                notification_recipient.email = recipient["to"]["email"]
            if "handle" in recipient["to"]:
                notification_recipient.irc_handle = recipient["to"]["handle"]
            if "server" in recipient["to"]:
                notification_recipient.irc_server = recipient["to"]["server"]

            # Ignore unique constraint violation.
            with contextlib.suppress(IntegrityError):
                notification_recipient.save()

    else:
        # You can do "callbacks only" without having recipients, in that
        # case, no notification will be sent.
        if "callbacks" not in data and "callback" not in data:
            # But if there's no callback and no recipients then we add a
            # submitter as a default recipient.
            # Ignore unique constraint violation.
            with contextlib.suppress(IntegrityError):
                notification_recipient = NotificationRecipient.objects.create(
                    user=job.submitter, notification=notification)

    # Add callbacks.
    if "callbacks" in data:
        for callback in data["callbacks"]:
            create_callback(job, callback, notification)
    if "callback" in data:
        create_callback(job, data["callback"], notification)