Blame view

src/core/pyros_django/alert_manager/views.py 5.94 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
bb45cd4a   haribo   Date: 25/05/2016
5
import majordome
94082e77   haribo   Date: 03/06/2016
6
from django.contrib.auth.decorators import login_required
945b36d2   haribo   Généricité des ca...
7
import alert_manager.tasks
5b5566ab   haribo   added celery
8

945b36d2   haribo   Généricité des ca...
9
10
11
12
13
14
15
import os, time, shutil

TEST_FILE = "unittest_voevent.xml"

TEST_FILE_PATH = os.path.join(alert_manager.tasks.VOEVENTS_PATH, TEST_FILE)

VOEVENTS_TO_SEND_PATH = "alert_manager/events_to_send"
5b5566ab   haribo   added celery
16

77816f10   haribo   Workflow implemen...
17

94082e77   haribo   Date: 03/06/2016
18
@login_required
945b36d2   haribo   Généricité des ca...
19
def start_simulation(request):
94082e77   haribo   Date: 03/06/2016
20
21
    '''
        Called when the alert simulation button is pressed
945b36d2   haribo   Généricité des ca...
22
        Deletes then copies the test voevent file from the events_received folder
94082e77   haribo   Date: 03/06/2016
23
    '''
945b36d2   haribo   Généricité des ca...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    if os.path.isfile(TEST_FILE_PATH):
        os.remove(TEST_FILE_PATH)
        print("================== DELETE FILE ==================")
        time.sleep(3)

    print("================== COPY FILE ==================")
    shutil.copyfile(os.path.join(VOEVENTS_TO_SEND_PATH, TEST_FILE),
                        TEST_FILE_PATH)
    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()

    success = True
    message = "Simulation started successfully"
    return render(request, "alert_manager/alerts.html", locals())
bb45cd4a   haribo   Date: 25/05/2016
45

bb45cd4a   haribo   Date: 25/05/2016
46
def alerts_list(request):
94082e77   haribo   Date: 03/06/2016
47
48
49
    '''
        Display the list of all alerts
    '''
e6dd9964   haribo   Date: 19/07/2016
50

bb45cd4a   haribo   Date: 25/05/2016
51
    alerts = Alert.objects.order_by("-request__created")
e6dd9964   haribo   Date: 19/07/2016
52
53
54
55
56
57
58
    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()
2c61f856   theopuhl   Url change to pat...
59
60
61
62
    if request.user.is_authenticated:
        base_template = "base.html"
    else:
        base_template = "base_unlogged.html"
e6dd9964   haribo   Date: 19/07/2016
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    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
95
96
97
    return render(request, "alert_manager/alerts.html", locals())


94082e77   haribo   Date: 03/06/2016
98
@login_required
bb45cd4a   haribo   Date: 25/05/2016
99
def change_obs_strategy(request, alert_id):
94082e77   haribo   Date: 03/06/2016
100
101
102
103
    '''
        Open a page for a given alert, listing all the other strategies 
    '''

bb45cd4a   haribo   Date: 25/05/2016
104
    alert = Alert.objects.get(id=alert_id)
94082e77   haribo   Date: 03/06/2016
105
106
107
108
    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
109
110
111
    return render(request, "alert_manager/strategy_change.html", locals())


94082e77   haribo   Date: 03/06/2016
112
@login_required
bb45cd4a   haribo   Date: 25/05/2016
113
def change_obs_strategy_validate(request, alert_id):
94082e77   haribo   Date: 03/06/2016
114
115
116
    '''
        Creates a new request with the strategy given for the selected alert
    '''
fd99569d   haribo   Date: 22/06/2016
117
118
119
120
121
122
123
124
125

    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())

e573c1f1   Etienne Pallier   All unit tests no...
126
    # All strategies except the current one
bb45cd4a   haribo   Date: 25/05/2016
127
    strategies = StrategyObs.objects.exclude(id=alert.strategyobs.id)
e573c1f1   Etienne Pallier   All unit tests no...
128

bb45cd4a   haribo   Date: 25/05/2016
129
    if request.method == "POST":
fd99569d   haribo   Date: 22/06/2016
130

bb45cd4a   haribo   Date: 25/05/2016
131
132
133
134
135
136
137
138
        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
139
        if str(alert.strategyobs.id) == strategy_id:
bb45cd4a   haribo   Date: 25/05/2016
140
141
142
143
            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
144
145
        sb = StrategyBuilder()
        file = strategy.xml_file
e573c1f1   Etienne Pallier   All unit tests no...
146
147
148
149
150
151
152
153
154
155
156
157
        # (EP) Inutile, il y a plus rapide (Alert herite de Request)
        #pyros_user = alert.request.pyros_user
        pyros_user = alert.pyros_user
        print("pyros user is", pyros_user)
        # meme remarque
        #scientific_program = alert.request.scientific_program
        scientific_program = alert.scientific_program
        print("SP is", scientific_program)
        # meme remarque
        #name = alert.request.name
        name = alert.name
        print("alert request name is", name)
d84050e6   haribo   Date: 20/07/2016
158
        sb.create_request_from_strategy(file, pyros_user, scientific_program, name)
e573c1f1   Etienne Pallier   All unit tests no...
159
        print("New alert request created is", sb.rb.request)
d84050e6   haribo   Date: 20/07/2016
160
        req = sb.validate()
e573c1f1   Etienne Pallier   All unit tests no...
161
162
        print("New alert request created is", req)
        # Attention : Alert n'a pas de pk !!! c'est le pk de Request !!!
bb45cd4a   haribo   Date: 25/05/2016
163
164
        alert.pk = None
        alert.strategyobs = strategy
53787d30   Jeremy   Alert now inherit...
165
        alert.request_id = req
e573c1f1   Etienne Pallier   All unit tests no...
166
        # C'est quoi ce beans de geek ??? !!!
53787d30   Jeremy   Alert now inherit...
167
        alert.__dict__.update(req.__dict__)
bb45cd4a   haribo   Date: 25/05/2016
168
169
170
171
172
        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
173

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