Exemple #1
0
def edit(request, provider_id=None, notification_id=None):
    provider_data = notifications_model.get_by_id(notification_id)

    all_for_provider = notifications_model.get_all_for_provider(
        provider_id=provider_id)

    provider_form = PROVIDERS.get(provider_id, False)

    if not provider_form:
        return redirect(reverse('notifications_all'))

    if request.method == "POST":
        form = provider_form(request.POST)

        if form.is_valid():
            data = form.cleaned_data
            notifications_model.update(data=data, id=notification_id)

            if 'test' in request.POST:
                redirect_url = reverse('notifications_test',
                                       kwargs={
                                           'provider_id': provider_id,
                                           'notification_id': notification_id
                                       })

            else:
                redirect_url = reverse('notifications_edit',
                                       kwargs={
                                           'provider_id': provider_id,
                                           'notification_id': notification_id
                                       })

            messages.add_message(
                request, messages.INFO,
                '{0} settings updated'.format(provider_id.title()))

            return redirect(redirect_url)
    else:
        form = provider_form(provider_data=provider_data)

    return render(
        request, 'notifications/view.html', {
            "form": form,
            "provider_id": provider_id,
            "provider_data": provider_data,
            "all_for_provider": all_for_provider,
            "notification_id": notification_id
        })
Exemple #2
0
def edit(request, provider_id=None, notification_id=None):
    provider_data = notifications_model.get_by_id(notification_id)

    all_for_provider = notifications_model.get_all_for_provider(provider_id=provider_id)

    provider_form = PROVIDERS.get(provider_id, False)

    if not provider_form:
        return redirect(reverse('notifications_all'))

    if request.method == "POST":
        form = provider_form(request.POST)

        if form.is_valid():
            data = form.cleaned_data
            notifications_model.update(data=data, id=notification_id)

            if 'test' in request.POST:
                redirect_url = reverse('notifications_test', kwargs={'provider_id': provider_id, 'notification_id': notification_id})

            else:
                redirect_url = reverse('notifications_edit', kwargs={'provider_id': provider_id, 'notification_id': notification_id})

            messages.add_message(request, messages.INFO, '{0} settings updated'.format(provider_id.title()))

            return redirect(redirect_url)
    else:
        form = provider_form(provider_data=provider_data)

    return render(request, 'notifications/view.html', {
        "form": form,
        "provider_id": provider_id,
        "provider_data": provider_data,
        "all_for_provider": all_for_provider,
        "notification_id": notification_id
    })
Exemple #3
0
def migrate_four_to_five(request):

    from amon.apps.notifications.models import notifications_model
    from amon.apps.notifications.legacymodels import email_recepient_model
    from amon.apps.notifications.legacymodels import webhooks_model
    from amon.apps.alerts.models import alerts_model

    # Move emails
    all_emails = email_recepient_model.get_all()
    for e in all_emails:
        email_exists = notifications_model.collection.find_one({
            'email':
            e.get('email'),
            'provider_id':
            'email'
        })
        if email_exists == None:
            notifications_model.save(data={"email": e.get('email')},
                                     provider_id='email')

    # Move webhooks
    all_webhooks = webhooks_model.get_all()
    for e in all_webhooks:
        hook_exists = notifications_model.collection.find_one({
            'url':
            e.get('url'),
            'provider_id':
            'webhook'
        })
        if hook_exists == None:
            data = {
                'url': e.get('url'),
                'name': e.get('url'),
                'secret': e.get('secret')
            }
            notifications_model.save(data=data, provider_id='webhook')

    # Set default names
    all_notifications = notifications_model.get_all()
    for noti in all_notifications:
        if noti.get('provider_id') not in ['webhook', 'email']:
            name = noti.get('name', None)
            if name == None:
                notifications_model.update(data={'name': 'default'},
                                           id=noti.get('_id'))

    # Update all alerts
    alerts = alerts_model.get_all()
    for alert in alerts:
        new_notifications_list = []
        emails = alert.get('email_recepients', [])
        webhooks = alert.get('webhooks', [])
        old_notifications = alert.get('notifications', [])

        for email in emails:
            email_from_old_model = email_recepient_model.get_by_id(email)
            if email_from_old_model:
                new_email_location = notifications_model.collection.find_one({
                    'email':
                    email_from_old_model.get('email'),
                    'provider_id':
                    'email'
                })

                if new_email_location:
                    _id = "email:{0}".format(new_email_location.get('_id'))
                    new_notifications_list.append(_id)

        for hook in webhooks:
            hook_from_old_model = webhooks_model.get_by_id(hook)
            if hook_from_old_model:
                new_hook_location = notifications_model.collection.find_one({
                    'url':
                    hook_from_old_model.get('url'),
                    'provider_id':
                    'webhook'
                })

                if new_hook_location:
                    _id = "webhook:{0}".format(new_hook_location.get('_id'))
                    new_notifications_list.append(_id)

        for provider in old_notifications:

            new_notification = notifications_model.collection.find_one(
                {'provider_id': provider})
            if new_notification:
                _id = "{0}:{1}".format(provider, new_notification.get('_id'))
                new_notifications_list.append(_id)

        try:
            del alert['email_recepients']
            del alert['webhooks']
        except:
            pass

        alert['notifications'] = new_notifications_list
        alerts_model.update(alert, alert.get('_id'))

    messages.add_message(request, messages.INFO, 'Migration complete.')

    return redirect(reverse('servers'))