views.py 6.13 KB
# from django.shortcuts import render
# from common.models import *
# from alert_mgmt.StrategyBuilder import StrategyBuilder
# import socket
# import majordome
# from django.contrib.auth.decorators import login_required
# import alert_mgmt.tasks

# import os, time, shutil

# TEST_FILE = "unittest_voevent.xml"

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

# VOEVENTS_TO_SEND_PATH = "alert_mgmt/events_to_send"


# @login_required
# def start_simulation(request):
#     '''
#         Called when the alert simulation button is pressed
#         Deletes then copies the test voevent file from the events_received folder
#     '''

#     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_mgmt/alerts.html", locals())

# def alerts_list(request):
#     '''
#         Display the list of all alerts
#     '''

#     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()
#     #if request.user.is_authenticated:
#     base_template = "base.html"
#     return render(request, "alert_mgmt/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()

#     return render(request, "alert_mgmt/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_mgmt/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
#     '''

#     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_mgmt/alerts.html", locals())

#     # All strategies except the current one
#     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_mgmt/strategy_change.html", locals())

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

#         sb = StrategyBuilder()
#         file = strategy.xml_file
#         # (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)
#         sb.create_request_from_strategy(file, pyros_user, scientific_program, name)
#         print("New alert request created is", sb.rb.request)
#         req = sb.validate()
#         print("New alert request created is", req)
#         # Attention : Alert n'a pas de pk !!! c'est le pk de Request !!!
#         alert.pk = None
#         alert.strategyobs = strategy
#         alert.request_id = req
#         # C'est quoi ce beans de geek ??? !!!
#         alert.__dict__.update(req.__dict__)
#         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_mgmt/strategy_change.html", locals())