Blame view

src/core/pyros_django/obsconfig/views.py 17.8 KB
1ba49504   Alexis Koralewski   fixing CSS and JS...
1
from collections import OrderedDict
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
2
3
from django import conf
from django.shortcuts import render
a2dbbde1   Alexis Koralewski   Adding new config...
4
from .obsconfig_class import OBSConfig
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
5
6
7
from django.conf import settings
from src.core.pyros_django.dashboard.decorator import level_required
from django.contrib.auth.decorators import login_required
d0501f5e   Alexis Koralewski   use environment v...
8
9
from django.http import HttpResponse
from datetime import datetime
c27a895d   Alexis Koralewski   add display of ra...
10
from django.views.decorators.csrf import csrf_exempt
1ba49504   Alexis Koralewski   fixing CSS and JS...
11
12
13
14
15
import re
import yaml
import tempfile
import json
import os
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
16

1ba49504   Alexis Koralewski   fixing CSS and JS...
17
18
19
20

def get_nested_dictionaries_as_list(dic, result=[]):
    for key, value in dic.items():
        print(value, type(value))
6686d675   Alexis Koralewski   new version of ob...
21
22
23
        print(result)
        if isinstance(value, dict):
            print("recursive call")
1ba49504   Alexis Koralewski   fixing CSS and JS...
24
25
26
            get_nested_dictionaries_as_list(value, result)
        elif isinstance(value, list) and key == "CAPABILITIES":
            print("VALUE OF LIST =======          ", value)
6686d675   Alexis Koralewski   new version of ob...
27
            for element in value:
1ba49504   Alexis Koralewski   fixing CSS and JS...
28
29
30
31
                # print(element)
                if isinstance(element, dict):
                    get_nested_dictionaries_as_list(element, result)

6686d675   Alexis Koralewski   new version of ob...
32
        else:
1ba49504   Alexis Koralewski   fixing CSS and JS...
33
            result.append((key, value))
6686d675   Alexis Koralewski   new version of ob...
34
    return result
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
35

1ba49504   Alexis Koralewski   fixing CSS and JS...
36

cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
37
@login_required
1ba49504   Alexis Koralewski   fixing CSS and JS...
38
@level_required("Admin", "Observer", "Management", "Operator", "Unit-PI")
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
39
def obs_global_config(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
40
41
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
42
    units_names = list(config.get_units().keys())
d0501f5e   Alexis Koralewski   use environment v...
43
    pickle_file_mtime = os.path.getmtime(config.CONFIG_PATH+config.pickle_file)
1ba49504   Alexis Koralewski   fixing CSS and JS...
44
45
46
47
48
49
50
51
52
53
54
    pickle_datetime = datetime.utcfromtimestamp(
        pickle_file_mtime).strftime("%Y/%m/%d %H:%M:%S")
    CAN_EDIT_CONFIG = request.session.get("role") in (
        "Admin", "Operator", "Unit-PI", "Unit-board")
    CAN_VIEW_HARDWARE_CONFIG = request.session.get("role") in (
        "Admin", "Operator", "Unit-PI", "Unit-board")
    CAN_VIEW_ASTRONOMER_CONFIG = request.session.get("role") in (
        "Admin", "Operator", "Unit-PI", "Observer", "Management board manager", "Unit-board")
    CAN_VIEW_AGENTS_CONFIG = request.session.get("role") == "Admin"
    return render(request, "obsconfig/global_obs_configuration.html", {
        "units_names": units_names,
b755ff73   Alexis Koralewski   adding variables ...
55
        "obs_name": config.get_obs_name(),
1ba49504   Alexis Koralewski   fixing CSS and JS...
56
57
        "last_modification_time": pickle_datetime,
        "CAN_EDIT_CONFIG": CAN_EDIT_CONFIG,
b755ff73   Alexis Koralewski   adding variables ...
58
        "CAN_VIEW_HARDWARE_CONFIG": CAN_VIEW_HARDWARE_CONFIG,
1ba49504   Alexis Koralewski   fixing CSS and JS...
59
60
        "CAN_VIEW_ASTRONOMER_CONFIG": CAN_VIEW_ASTRONOMER_CONFIG,
        "CAN_VIEW_AGENTS_CONFIG": CAN_VIEW_AGENTS_CONFIG
b755ff73   Alexis Koralewski   adding variables ...
61
    })
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
62
63


cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
64
@login_required
1ba49504   Alexis Koralewski   fixing CSS and JS...
65
@level_required("Admin", "Unit-PI", "Operator")
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
66
def obs_hardware_config(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
67
68
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
69
70
71
72
73
74
75
76
77
78
79
80
    devices = config.get_devices()
    active_devices = config.get_active_devices()
    computers = config.get_computers()
    active_computers = config.get_active_computers()
    # give for a device name (key) the unit name where it is used (value)
    device_to_unit = {}
    # give for a computer name (key) the unit name where it is used (value)
    computer_to_unit = {}
    for device in active_devices:
        device_to_unit[device] = config.get_unit_of_device(device)
    for computer in active_computers:
        computer_to_unit[computer] = config.get_unit_of_computer(computer)
a2f47fb6   Alexis Koralewski   updating display ...
81
    devices_links = config.devices_links
1ba49504   Alexis Koralewski   fixing CSS and JS...
82
83
84
    return render(request, 'obsconfig/obs_hardware_configuration.html', {'devices': devices, "active_devices": active_devices, "computers": computers, "active_computers": active_computers, "device_to_unit": device_to_unit, "computer_to_unit": computer_to_unit, "devices_links": devices_links})


cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
85
@login_required
1ba49504   Alexis Koralewski   fixing CSS and JS...
86
87
88
89
@level_required("Admin", "Unit-PI", "Operator")
def unit_hardware_configuration(request, unit_name):
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
90
    devices = config.get_devices()
1ba49504   Alexis Koralewski   fixing CSS and JS...
91
92
93
94

    return render(request, 'obsconfig/unit_hardware_configuration.html', {'config': config})


cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
95
@login_required
1ba49504   Alexis Koralewski   fixing CSS and JS...
96
97
98
99
@level_required("Admin", "Unit-PI", "Operator")
def computer_details(request, computer_name):
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
6686d675   Alexis Koralewski   new version of ob...
100
101
    computer_detail = yaml.dump(config.get_computers()[computer_name])
    """
1ba49504   Alexis Koralewski   fixing CSS and JS...
102
103
    computer_detail = { re.sub("^_","",key):value for key,
                               value in config.get_computers()[computer_name].items() }
6686d675   Alexis Koralewski   new version of ob...
104
105

     # We're removing "_" at the beginning of each key :
1ba49504   Alexis Koralewski   fixing CSS and JS...
106
107
108
    power = { re.sub("^_","",key):value for key,
                     value in config.get_computer_power(computer_name).items() }

6686d675   Alexis Koralewski   new version of ob...
109
110
    computer_config = computer_detail["computer_config"]
    # We're removing "_" at the beginning of each key :
1ba49504   Alexis Koralewski   fixing CSS and JS...
111
112
113
    computer_detail["computer_config"] = {
        re.sub("^_","",key):value for key,value in computer_config.items() }
    # Remove power key as we have this information in the same
6686d675   Alexis Koralewski   new version of ob...
114
115
116
117
    if power is not None:
        computer_detail["computer_config"].pop("power")
    return render(request,"obsconfig/computer_details.html", {"computer_detail" : computer_detail, "power" : power})
    """
1ba49504   Alexis Koralewski   fixing CSS and JS...
118
    return render(request, "obsconfig/computer_details.html", {"computer_detail": computer_detail})
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
119

cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
120

cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
121
@login_required
a61943b5   Alexis Koralewski   Reworking Unit-bo...
122
@level_required("Admin", "Unit-PI",  "Operator", "Observer")
1ba49504   Alexis Koralewski   fixing CSS and JS...
123
124
125
def device_details(request, device_name):
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
6686d675   Alexis Koralewski   new version of ob...
126
127
    # We're removing "_" at the beginning of each key :
    device = config.get_devices()[device_name]
1ba49504   Alexis Koralewski   fixing CSS and JS...
128
129
    # device_detail  = yaml.dump(device)
    # config_device = open(config.CONFIG_PATH+device["file"],"r").read()
a2f47fb6   Alexis Koralewski   updating display ...
130
    devices_links = config.devices_links
1ba49504   Alexis Koralewski   fixing CSS and JS...
131
132
    file_name_to_device_name = {
        v: k for k, v in config.get_devices_names_and_file().items()}
a2f47fb6   Alexis Koralewski   updating display ...
133
    active_devices = config.get_active_devices()
6686d675   Alexis Koralewski   new version of ob...
134
135
    """
    Alternative solutions:
1ba49504   Alexis Koralewski   fixing CSS and JS...
136
        - test_device -> we're using a recursive function to get all nested dicitonaries but we loose a level of
6686d675   Alexis Koralewski   new version of ob...
137
138
        "precision" (no separations of capabilities for example)
        - device_detail -> we use multiple function to get specific value (not global enougth, if we had another key we have to change the code)
ee2a5e47   Alexis Koralewski   New version of ob...
139
    """
6686d675   Alexis Koralewski   new version of ob...
140

1ba49504   Alexis Koralewski   fixing CSS and JS...
141
142
143
    device_detail = {re.sub("^_", "", key): value for key,
                     value in config.get_devices()[device_name].items()}
    # test_device=  get_nested_dictionaries_as_list(device_detail,[])
6686d675   Alexis Koralewski   new version of ob...
144

6686d675   Alexis Koralewski   new version of ob...
145
146
    if config.get_device_power(device_name) is not None:
        # We're removing "_" at the beginning of each key :
1ba49504   Alexis Koralewski   fixing CSS and JS...
147
148
149
        power = {re.sub("^_", "", key): value for key,
                 value in config.get_device_power(device_name).items()}
    else:
6686d675   Alexis Koralewski   new version of ob...
150
151
152
        power = None
    if config.get_device_connector(device_name) is not None:
        # We're removing "_" at the beginning of each key :
1ba49504   Alexis Koralewski   fixing CSS and JS...
153
154
        connector = {re.sub("^_", "", key): value for key,
                     value in config.get_device_connector(device_name).items()}
6686d675   Alexis Koralewski   new version of ob...
155
156
    else:
        connector = None
1ba49504   Alexis Koralewski   fixing CSS and JS...
157

6686d675   Alexis Koralewski   new version of ob...
158
159
160
161
162
    capabilities = []
    if config.get_device_capabilities(device_name) is not None:
        copy_capabilities = config.get_device_capabilities(device_name)
        for capability in copy_capabilities:
            # We're removing "_" at the beginning of each key :
1ba49504   Alexis Koralewski   fixing CSS and JS...
163
164
165
            capabilities.append(
                {re.sub("^_", "", key): value for key, value in capability.items()})

6686d675   Alexis Koralewski   new version of ob...
166
167
    device_config = device_detail["device_config"]
    # We're removing "_" at the beginning of each key :
1ba49504   Alexis Koralewski   fixing CSS and JS...
168
169
170
    device_detail["device_config"] = {
        re.sub("^_", "", key): value for key, value in device_config.items()}
    # Remove power key as we have this information in the same
6686d675   Alexis Koralewski   new version of ob...
171
172
173
174
    if power is not None:
        device_detail["device_config"].pop("power")
    if connector is not None:
        device_detail["device_config"].pop("connector")
1ba49504   Alexis Koralewski   fixing CSS and JS...
175
    if len(capabilities) != 0:
6686d675   Alexis Koralewski   new version of ob...
176
        device_detail["device_config"].pop("CAPABILITIES")
1ba49504   Alexis Koralewski   fixing CSS and JS...
177
178
179
180
    return render(request, "obsconfig/device_details.html", {"device_detail": device_detail, "power": power, "connector": connector, "capabilities": capabilities, "devices_links": devices_links, "file_name_to_device_name": file_name_to_device_name, "active_devices": active_devices})

    # return render(request,"obsconfig/device_details.html", { "device_detail" : device_detail, "config" : config_device })

cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
181

cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
182
@login_required
1ba49504   Alexis Koralewski   fixing CSS and JS...
183
@level_required("Admin", "Observer", "Management", "Operator", "Unit-PI", "TAC")
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
184
def obs_astronomer_config(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
185
186
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
187
188
189
190
    units = config.get_units()
    units_topologies = {}
    # for each unit
    for unit_name in units:
1ba49504   Alexis Koralewski   fixing CSS and JS...
191

4290c2cf   Alexis Koralewski   adding unit choic...
192
        units_topologies[unit_name] = config.get_topology(unit_name)
a2dbbde1   Alexis Koralewski   Adding new config...
193
194
        layouts = units_topologies[unit_name].pop("LAYOUTS")["layouts"]
        albums = units_topologies[unit_name].pop("ALBUMS")["albums"]
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
195
196
197
198
199
200
201
202
        # for each category (security, mount, channels)
        for category in units_topologies[unit_name]:
            if category != "CHANNELS":
                # Security and Mount are directly a dictionary containing the attributes of those categories
                # However, component_agents is a list so we need to iterate through this list
                for component_agent in units_topologies[unit_name][category]["COMPONENT_AGENTS"]:
                    component_name = list(component_agent.keys())[0]
                    agent = component_agent[component_name]
1ba49504   Alexis Koralewski   fixing CSS and JS...
203
204
                    device_of_agent = config.get_device_for_agent(
                        unit_name, agent)
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
205
                    # get the index of the current component, agent couple
1ba49504   Alexis Koralewski   fixing CSS and JS...
206
207
                    index = units_topologies[unit_name][category]["COMPONENT_AGENTS"].index(
                        component_agent)
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
208
209
210
                    units_topologies[unit_name][category]["COMPONENT_AGENTS"][index][component_name] = device_of_agent
            else:
                # Channels is composed of a list of channel, we're looping through it
1ba49504   Alexis Koralewski   fixing CSS and JS...
211
212
                for channel_name in units_topologies[unit_name]["CHANNELS"]:
                    for component_agent in units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"]:
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
213
214
                        component_name = list(component_agent.keys())[0]
                        agent = component_agent[component_name]
1ba49504   Alexis Koralewski   fixing CSS and JS...
215
216
217
218
                        device_of_agent = config.get_device_for_agent(
                            unit_name, agent)
                        index = units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"].index(
                            component_agent)
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
219
                        units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"][index][component_name] = device_of_agent
1ba49504   Alexis Koralewski   fixing CSS and JS...
220
        # Re add layouts and albums
a2dbbde1   Alexis Koralewski   Adding new config...
221
        units_topologies[unit_name]["LAYOUTS"] = layouts
1ba49504   Alexis Koralewski   fixing CSS and JS...
222
223
224
225
226
227
228
        units_topologies[unit_name]["ALBUMS"] = albums
        order_of_keys = ["SECURITY", "MOUNT", "LAYOUTS", "ALBUMS", "CHANNELS"]
        list_of_tuples = [(key, units_topologies[unit_name][key])
                          for key in order_of_keys]
        units_topologies[unit_name] = OrderedDict(list_of_tuples)
    return render(request, "obsconfig/obs_astronomer_config.html", {
        "units_topologies": units_topologies,
a2dbbde1   Alexis Koralewski   Adding new config...
229
    })
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
230
231


1ba49504   Alexis Koralewski   fixing CSS and JS...
232
233
@ login_required
@ level_required("Admin")
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
234
def obs_agents_config(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
235
236
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
237
238
239
240
241
    units = config.get_units()
    units_topologies = {}
    active_agents_by_unit = {}
    # for each unit
    for unit_name in units:
4290c2cf   Alexis Koralewski   adding unit choic...
242
        agents = config.get_agents(unit_name)
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
243
        # list of active agents of the current unit
4290c2cf   Alexis Koralewski   adding unit choic...
244
        active_agents_by_unit[unit_name] = config.get_active_agents(unit_name)
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
245
        # topology of the current unit
4290c2cf   Alexis Koralewski   adding unit choic...
246
        units_topologies[unit_name] = config.get_topology(unit_name)
a2dbbde1   Alexis Koralewski   Adding new config...
247
248
249
        # removing albums and layouts, not useful in this view
        units_topologies[unit_name].pop("LAYOUTS")
        units_topologies[unit_name].pop("ALBUMS")
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
250
251
252
253
254
255
        for category in units_topologies[unit_name]:
            if category != "CHANNELS":
                # Security and Mount are directly a dictionary containing the attributes of those categories
                # However, component_agents is a list so we need to iterate through this list
                for component_agent in units_topologies[unit_name][category]["COMPONENT_AGENTS"]:
                    component_name = list(component_agent.keys())[0]
b7becde4   Alexis Koralewski   Updating UI (foot...
256
                    agent = agents[component_agent[component_name]]
1ba49504   Alexis Koralewski   fixing CSS and JS...
257
258
259
                    # get the index of the current component, agent couple
                    index = units_topologies[unit_name][category]["COMPONENT_AGENTS"].index(
                        component_agent)
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
260
261
262
                    units_topologies[unit_name][category]["COMPONENT_AGENTS"][index][component_name] = agent
            else:
                # Channels is composed of a list of channel, we're looping through it
1ba49504   Alexis Koralewski   fixing CSS and JS...
263
264
                for channel_name in units_topologies[unit_name]["CHANNELS"]:
                    for component_agent in units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"]:
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
265
                        component_name = list(component_agent.keys())[0]
b7becde4   Alexis Koralewski   Updating UI (foot...
266
                        agent = agents[component_agent[component_name]]
1ba49504   Alexis Koralewski   fixing CSS and JS...
267
268
269
                        # get the index of the current component, agent couple
                        index = units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"].index(
                            component_agent)
cc4e5a62   Alexis Koralewski   Add ConfigPyrosv2...
270
271
                        units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"][index][component_name] = agent

0901eb9b   Alexis Koralewski   Add agent per com...
272
273
274
275
276
277
        agents = config.get_agents_per_computer(config.unit_name)
        computers = {}
        for computer in config.get_computers():
            computers[config.get_computers().get(computer).get("computer_config").get("hostname")] = computer
        units_topologies[unit_name]["COMPUTERS"] = agents
    return render(request, "obsconfig/obs_agents_config.html", {"units_topologies": units_topologies, "active_agents_by_unit": active_agents_by_unit,"computers":computers})
d0501f5e   Alexis Koralewski   use environment v...
278
279


1ba49504   Alexis Koralewski   fixing CSS and JS...
280
281
@ login_required
@ level_required("Admin", "Operator", "Unit-PI", "Unit-board")
d0501f5e   Alexis Koralewski   use environment v...
282
def edit_config(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
283
284
285
    config = OBSConfig(
        os.environ["PATH_TO_OBSCONF_FILE"], os.environ["unit_name"])
    return render(request, "obsconfig/edit_config.html", {"config_file": config.raw_config})
d0501f5e   Alexis Koralewski   use environment v...
286

1ba49504   Alexis Koralewski   fixing CSS and JS...
287
288
289

@ login_required
@ level_required("Admin", "Operator", "Unit-PI", "Unit-board")
d0501f5e   Alexis Koralewski   use environment v...
290
291
def verify_config(request):
    if request.POST.get("config"):
1ba49504   Alexis Koralewski   fixing CSS and JS...
292
293
        temp_config_file = tempfile.NamedTemporaryFile(
            mode='w+', suffix=".yml")
d0501f5e   Alexis Koralewski   use environment v...
294
295
296
        temp_config_file.write(request.POST.get("config"))
        temp_config_file.seek(0)
        response_data = {}
81f42637   Alexis Koralewski   F14 : When editin...
297
298
299
300
301
302
        try:
            config_file = yaml.safe_load(temp_config_file.read())
        except yaml.YAMLError as exc:
            if hasattr(exc, 'problem_mark'):
                yaml_error_message = ""
                if exc.context != None:
1ba49504   Alexis Koralewski   fixing CSS and JS...
303
304
                    yaml_error_message += str(exc.problem_mark) + \
                        '\n  ' + str(exc.problem) + ' ' + str(exc.context)
81f42637   Alexis Koralewski   F14 : When editin...
305
                else:
1ba49504   Alexis Koralewski   fixing CSS and JS...
306
307
                    yaml_error_message = str(
                        exc.problem_mark.name) + '\n  ' + str(exc.problem)
81f42637   Alexis Koralewski   F14 : When editin...
308
309
310
                response_data["is_valid"] = False
                response_data["yaml_error_message"] = yaml_error_message
            return HttpResponse(json.dumps(response_data), content_type="application/json")
d0501f5e   Alexis Koralewski   use environment v...
311
312
313
314
        temp_config_file.seek(0)
        schema = config_file["schema"]
        errors = []
        if schema == None:
b755ff73   Alexis Koralewski   adding variables ...
315
            response_data["is_valid"] = False
d0501f5e   Alexis Koralewski   use environment v...
316
            response_data["message"] = "Missing schema"
1ba49504   Alexis Koralewski   fixing CSS and JS...
317
318
319
320
        schema_path = os.path.join(
            os.environ["DJANGO_PATH"], "../../../config/schemas/")
        config = OBSConfig.check_config(
            temp_config_file.name, schema_path+schema)
d0501f5e   Alexis Koralewski   use environment v...
321
322
        if type(config) == bool and config:
            response_data["is_valid"] = True
1ba49504   Alexis Koralewski   fixing CSS and JS...
323
        else:
d0501f5e   Alexis Koralewski   use environment v...
324
325
            response_data["is_valid"] = False
            for error in config:
1ba49504   Alexis Koralewski   fixing CSS and JS...
326
327
                errors.append(
                    (f"Error : {str(error).split('. Path')[0]}", f"Path to error : '{error.path}'"))
d0501f5e   Alexis Koralewski   use environment v...
328
329
330
331
            response_data["message"] = errors
        temp_config_file.close()
        return HttpResponse(json.dumps(response_data), content_type="application/json")

1ba49504   Alexis Koralewski   fixing CSS and JS...
332
333
334

@ login_required
@ level_required("Admin", "Operator", "Unit-PI", "Unit-board")
d0501f5e   Alexis Koralewski   use environment v...
335
336
337
def save_config(request):
    if request.POST:
        if request.POST["config"]:
1ba49504   Alexis Koralewski   fixing CSS and JS...
338
            with open(os.environ["PATH_TO_OBSCONF_FILE"], "w") as obs_config_file:
d0501f5e   Alexis Koralewski   use environment v...
339
340
                obs_config_file.write(request.POST.get("config"))

c27a895d   Alexis Koralewski   add display of ra...
341
342
            return HttpResponse("Ok !")

1ba49504   Alexis Koralewski   fixing CSS and JS...
343
344

@ login_required
a61943b5   Alexis Koralewski   Reworking Unit-bo...
345
@ level_required("Admin", "Unit-PI",  "Operator", "Observer")
1ba49504   Alexis Koralewski   fixing CSS and JS...
346
@ csrf_exempt
c27a895d   Alexis Koralewski   add display of ra...
347
def view_raw_component_config_file(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
348
349
    COMPONENT_PATH = os.path.join(
        os.environ["DJANGO_PATH"], "../../../config/components/")
c27a895d   Alexis Koralewski   add display of ra...
350
351
352
353
354
355
356
357
358

    if request.POST:
        try:
            yaml_file = open(COMPONENT_PATH+request.POST["yaml_file"])
            content = yaml_file.readlines()
        except:
            content = "Component defined within the device configuration file"
        return HttpResponse(content)

1ba49504   Alexis Koralewski   fixing CSS and JS...
359
360

@ login_required
a61943b5   Alexis Koralewski   Reworking Unit-bo...
361
@ level_required("Admin", "Unit-PI",  "Operator", "Observer")
1ba49504   Alexis Koralewski   fixing CSS and JS...
362
@ csrf_exempt
c27a895d   Alexis Koralewski   add display of ra...
363
def view_raw_generic_device_config_file(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
364
365
    GENERIC_DEVICES_PATH = os.path.join(
        os.environ["DJANGO_PATH"], "../../../config/devices/")
c27a895d   Alexis Koralewski   add display of ra...
366
367
368
369
370
    if request.POST:
        yaml_file = open(GENERIC_DEVICES_PATH+request.POST["yaml_file"])
        content = yaml_file.readlines()
        return HttpResponse(content)

1ba49504   Alexis Koralewski   fixing CSS and JS...
371
372

@ login_required
a61943b5   Alexis Koralewski   Reworking Unit-bo...
373
@ level_required("Admin", "Unit-PI",  "Operator", "Observer")
1ba49504   Alexis Koralewski   fixing CSS and JS...
374
@ csrf_exempt
c27a895d   Alexis Koralewski   add display of ra...
375
376
377
378
379
def view_raw_device_config_file(request):
    obs_folder = os.environ["PATH_TO_OBSCONF_FOLDER"]
    if request.POST:
        yaml_file = open(obs_folder+request.POST["yaml_file"])
        content = yaml_file.readlines()
53ce3372   Alexis Koralewski   add diagrams that...
380
381
        return HttpResponse(content)

1ba49504   Alexis Koralewski   fixing CSS and JS...
382
383

@ login_required
a61943b5   Alexis Koralewski   Reworking Unit-bo...
384
@ level_required("Admin", "Unit-PI",  "Operator", "Observer")
53ce3372   Alexis Koralewski   add diagrams that...
385
def obs_config_help(request):
1ba49504   Alexis Koralewski   fixing CSS and JS...
386
    return render(request, "obsconfig/obs_config_help.html")