Blame view

src/alert_manager/views.py 4.74 KB
3df2d31a   haribo   #3430 : dates are...
1
from django.shortcuts import render
ddf59dd4   haribo   Remaniement :
2
from common.models import *
bb45cd4a   haribo   Date: 25/05/2016
3
from alert_manager.StrategyBuilder import StrategyBuilder
5b5566ab   haribo   added celery
4
import socket
77816f10   haribo   Workflow implemen...
5
from alert_manager import tasks as am_tasks
bb45cd4a   haribo   Date: 25/05/2016
6
import majordome
94082e77   haribo   Date: 03/06/2016
7
from django.contrib.auth.decorators import login_required
5b5566ab   haribo   added celery
8

94082e77   haribo   Date: 03/06/2016
9
@login_required
5b5566ab   haribo   added celery
10
def alert_simulation(request):
94082e77   haribo   Date: 03/06/2016
11
12
13
    '''
        Opens a page with a button to simulate an alert
    '''
5b5566ab   haribo   added celery
14
15
    return render(request, "alert_manager/alert_simulation.html", {})

77816f10   haribo   Workflow implemen...
16

94082e77   haribo   Date: 03/06/2016
17
@login_required
5b5566ab   haribo   added celery
18
def alert_simulation_start(request):
94082e77   haribo   Date: 03/06/2016
19
20
21
22
    '''
        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)
    '''
77816f10   haribo   Workflow implemen...
23
    Log.objects.all().delete()
5b5566ab   haribo   added celery
24
    clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
77816f10   haribo   Workflow implemen...
25
26
27
28
29
    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)
5b5566ab   haribo   added celery
30
    return render(request, "alert_manager/alert_simulation.html", {})
bb45cd4a   haribo   Date: 25/05/2016
31
32


94082e77   haribo   Date: 03/06/2016
33
@login_required
bb45cd4a   haribo   Date: 25/05/2016
34
def alerts_list(request):
94082e77   haribo   Date: 03/06/2016
35
36
37
    '''
        Display the list of all alerts
    '''
e6dd9964   haribo   Date: 19/07/2016
38

bb45cd4a   haribo   Date: 25/05/2016
39
    alerts = Alert.objects.order_by("-request__created")
e6dd9964   haribo   Date: 19/07/2016
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    if StrategyObs.objects.filter(is_default=True).exists():
        current_default = StrategyObs.objects.filter(is_default=True)[0].name
        btn_type = "btn-primary"
    else:
        current_default = "Warning: please choose a default strategy"
        btn_type = "btn-danger"
    strat_list = StrategyObs.objects.all()
    return render(request, "alert_manager/alerts.html", locals())

@login_required
def change_default_strategy(request, strat_id):
    '''
        Changes the strategy to be used when an alert is received 
    '''

    strategies = StrategyObs.objects.all()
    if strategies.filter(id=strat_id).exists():
        for strat in strategies:
            strat.is_default = False
            strat.save()
        strat = strategies.get(id=strat_id)
        strat.is_default = True
        strat.save()

        success = True
        message = "Default strategy successfully changed"
    else:
        error = True
        message = "Could not find this strategy"

    alerts = Alert.objects.order_by("-request__created")
    if StrategyObs.objects.filter(is_default=True).exists():
        current_default = StrategyObs.objects.filter(is_default=True)[0].name
        btn_type = "btn-primary"
    else:
        current_default = "Warning: please choose a default strategy"
        btn_type = "btn-danger"
    strat_list = StrategyObs.objects.all()

bb45cd4a   haribo   Date: 25/05/2016
79
80
81
    return render(request, "alert_manager/alerts.html", locals())


94082e77   haribo   Date: 03/06/2016
82
@login_required
bb45cd4a   haribo   Date: 25/05/2016
83
def change_obs_strategy(request, alert_id):
94082e77   haribo   Date: 03/06/2016
84
85
86
87
    '''
        Open a page for a given alert, listing all the other strategies 
    '''

bb45cd4a   haribo   Date: 25/05/2016
88
    alert = Alert.objects.get(id=alert_id)
94082e77   haribo   Date: 03/06/2016
89
90
91
92
    if alert.strategyobs is not None:
        strategies = StrategyObs.objects.exclude(id=alert.strategyobs.id)
    else:
        strategies = StrategyObs.objects.all()
bb45cd4a   haribo   Date: 25/05/2016
93
94
95
    return render(request, "alert_manager/strategy_change.html", locals())


94082e77   haribo   Date: 03/06/2016
96
@login_required
bb45cd4a   haribo   Date: 25/05/2016
97
def change_obs_strategy_validate(request, alert_id):
94082e77   haribo   Date: 03/06/2016
98
99
100
    '''
        Creates a new request with the strategy given for the selected alert
    '''
fd99569d   haribo   Date: 22/06/2016
101
102
103
104
105
106
107
108
109

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

bb45cd4a   haribo   Date: 25/05/2016
110
111
    strategies = StrategyObs.objects.exclude(id=alert.strategyobs.id)
    if request.method == "POST":
fd99569d   haribo   Date: 22/06/2016
112

bb45cd4a   haribo   Date: 25/05/2016
113
114
115
116
117
118
119
120
        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())

fd99569d   haribo   Date: 22/06/2016
121
        if str(alert.strategyobs.id) == strategy_id:
bb45cd4a   haribo   Date: 25/05/2016
122
123
124
125
            error = True
            message = "This is already the current strategy for this alert"
            return render(request, "alert_manager/strategy_change.html", locals())

bb45cd4a   haribo   Date: 25/05/2016
126
127
128
129
130
        sb = StrategyBuilder()
        file = strategy.xml_file
        pyros_user = alert.request.pyros_user
        scientific_program = alert.request.scientific_program
        name = alert.request.name
d84050e6   haribo   Date: 20/07/2016
131
132
        sb.create_request_from_strategy(file, pyros_user, scientific_program, name)
        req = sb.validate()
bb45cd4a   haribo   Date: 25/05/2016
133
134
135
136
137
138
139
140
        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."
fd99569d   haribo   Date: 22/06/2016
141

bb45cd4a   haribo   Date: 25/05/2016
142
    return render(request, "alert_manager/strategy_change.html", locals())