views.py 3.46 KB
from django.shortcuts import render
from pyrosapp.models import *
from alert_manager.StrategyBuilder import StrategyBuilder
import socket
from alert_manager import tasks as am_tasks
import majordome
from django.contrib.auth.decorators import login_required

@login_required
def alert_simulation(request):
    '''
        Opens a page with a button to simulate an alert
    '''
    return render(request, "alert_manager/alert_simulation.html", {})


@login_required
def alert_simulation_start(request):
    '''
        Called when the alert simulation button is pressed
        Opens a connection on the alert_listener simulation socket to unlock it (so that the workflow starts)
    '''
    Log.objects.all().delete()
    clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        clientsocket.connect((am_tasks.IP, am_tasks.PORT))
        Log.objects.create(agent='Alert manager', message='Start simulation')
    except Exception as e:
        print("exception : ", e)
    return render(request, "alert_manager/alert_simulation.html", {})


@login_required
def alerts_list(request):
    '''
        Display the list of all alerts
    '''
    alerts = Alert.objects.order_by("-request__created")
    return render(request, "alert_manager/alerts.html", locals())


@login_required
def change_obs_strategy(request, alert_id):
    '''
        Open a page for a given alert, listing all the other strategies 
    '''

    alert = Alert.objects.get(id=alert_id)
    if alert.strategyobs is not None:
        strategies = StrategyObs.objects.exclude(id=alert.strategyobs.id)
    else:
        strategies = StrategyObs.objects.all()
    return render(request, "alert_manager/strategy_change.html", locals())


@login_required
def change_obs_strategy_validate(request, alert_id):
    '''
        Creates a new request with the strategy given for the selected alert
    '''
    alert = Alert.objects.get(id=alert_id)
    strategies = StrategyObs.objects.exclude(id=alert.strategyobs.id)
    if request.method == "POST":
        strategy_id = request.POST["strategy_choice"]
        try:
            strategy = StrategyObs.objects.get(id=strategy_id)
        except Exception:
            error = True
            message = "This strategy doesn't exist"
            return render(request, "alert_manager/strategy_change.html", locals())

        try:
            alert = Alert.objects.get(id=alert_id)
        except Exception:
            error = True
            message = "This alert doesn't exist"
            return render(request, "alert_manager/strategy_change.html", locals())

        print(alert.strategyobs.id, strategy_id)

        if alert.strategyobs.id == strategy_id:
            error = True
            message = "This is already the current strategy for this alert"
            return render(request, "alert_manager/strategy_change.html", locals())

        sb = StrategyBuilder()
        file = strategy.xml_file
        pyros_user = alert.request.pyros_user
        scientific_program = alert.request.scientific_program
        name = alert.request.name
        req = sb.create_request_from_strategy(file, pyros_user, scientific_program, name)
        alert.pk = None
        alert.strategyobs = strategy
        alert.request = req
        alert.save()
        strategies = StrategyObs.objects.exclude(id=alert.strategyobs.id)

        success = True
        message = "Strategy successfully changed. A new request was created."
    return render(request, "alert_manager/strategy_change.html", locals())