Commit b755ff73cd4a70b07f9038d9d0fbc46f4b05da00

Authored by Alexis Koralewski
1 parent c0087c92
Exists in dev

adding variables to check authorizations, improving edit config page

src/core/pyros_django/obsconfig/configpyros.py
... ... @@ -16,7 +16,7 @@ class ConfigPyros:
16 16 computers = None
17 17 agents = None
18 18 obs_config_file_content = None
19   - obs_config_path = os.environ["PATH_TO_OBSCONF_FOLDER"]
  19 + obs_config_path = os.environ.get("PATH_TO_OBSCONF_FOLDER",os.path.join(os.environ["DJANGO_PATH"],"../../../privatedev/config/default/"))
20 20 errors = None
21 21 def verify_if_pickle_needs_to_be_updated(self,observatory_config_file)->bool:
22 22 """[summary]
... ... @@ -42,8 +42,10 @@ class ConfigPyros:
42 42 # Create history folder if doesn't exist
43 43 Path(self.obs_config_path+"/history/").mkdir(parents=True, exist_ok=True)
44 44 file_name = f"{self.obs_config_path}/history/observatory_{pickle_datetime}.yml"
  45 + config_file = open(observatory_config_file,"r")
  46 +
45 47 with open(file_name, 'w') as f:
46   - data = yaml.dump(obs_config, f)
  48 + f.write(config_file.read())
47 49 return True
48 50 if obs_config == None:
49 51 print(f"Error when trying to read config file (path of config file : {observatory_config_file}")
... ... @@ -77,12 +79,30 @@ class ConfigPyros:
77 79 time.sleep(0.5)
78 80 except IOError:
79 81 print("Error when reading the pickle file")
  82 + try:
  83 + self.obs_config = pickle_dict["obs_config"]
  84 + self.computers = pickle_dict["computers"]
  85 + self.devices = pickle_dict["devices"]
  86 + self.devices_links = pickle_dict["devices_links"]
  87 + self.obs_config_file_content = pickle_dict["obs_config_file_content"]
  88 + self.raw_config = pickle_dict["raw_config"]
  89 + except:
  90 + # we rewrite the pickle file, the content will be the same otherwise we would be in the else case
  91 + print("Rewritting the pickle file (an error occured while reading it, the content will be the same as it was")
  92 + pickle_dict = {}
80 93  
81   - self.obs_config = pickle_dict["obs_config"]
82   - self.computers = pickle_dict["computers"]
83   - self.devices = pickle_dict["devices"]
84   - self.devices_links = pickle_dict["devices_links"]
85   - self.obs_config_file_content = pickle_dict["obs_config_file_content"]
  94 + self.obs_config = self.read_and_check_config_file(observatory_config_file)
  95 + obs_file = open(observatory_config_file,"r")
  96 + pickle_dict["raw_config"] = obs_file.read()
  97 + obs_file.close()
  98 + self.raw_config = pickle_dict["raw_config"]
  99 + pickle_dict["obs_config"] = self.obs_config
  100 + pickle_dict["devices"] = self.get_devices()
  101 + pickle_dict["computers"] = self.get_computers()
  102 + pickle_dict["devices_links"] = self.devices_links
  103 + pickle_dict["obs_config_file_content"] = self.read_and_check_config_file(observatory_config_file)
  104 + print("Writing pickle file")
  105 + pickle.dump(pickle_dict,open(self.CONFIG_PATH+self.pickle_file,"wb"))
86 106 else:
87 107 print("Pickle file needs to be created or updated")
88 108 pickle_dict = {}
... ...
src/core/pyros_django/obsconfig/templates/obsconfig/edit_config.html
... ... @@ -26,11 +26,7 @@
26 26 left:80vw;
27 27 top: 30vh;
28 28 }
29   - #message{
30   - position:absolute;
31   - left:80vw;
32   - top: 25vh;
33   - }
  29 +
34 30 .red{
35 31 color:red;
36 32 }
... ... @@ -47,14 +43,17 @@
47 43 </div>
48 44 <div>
49 45 {% csrf_token %}
50   -<button id="check_config">Check configuration</button>
  46 +<button class="btn-primary" id="check_config">Check schema</button>
51 47  
52   -<p id="message"></p>
53 48 </div>
54 49 <div id="save" class="hidden">
  50 +<p id="message"></p>
  51 + {% comment %}
55 52 <label> File name: (without ".yml" extension)</label>
  53 +
56 54 <input id="file_name"></input>
57   - <button id="save_btn">Save</button>
  55 + {% endcomment %}
  56 + <button class="btn-primary" id="save_btn">Save</button>
58 57 <p id="save_message"></p>
59 58 </div>
60 59  
... ... @@ -64,8 +63,7 @@
64 63 editor.setTheme("ace/theme/eclipse");
65 64 editor.session.setMode("ace/mode/yaml");
66 65  
67   - document.getElementById('inputfile')
68   - .addEventListener('change', function() {
  66 + document.getElementById('inputfile').addEventListener('change', function() {
69 67 // loading default file (current observatory configuration file)
70 68 var fr=new FileReader();
71 69 fr.onload=function(){
... ... @@ -74,8 +72,13 @@
74 72  
75 73 fr.readAsText(this.files[0]);
76 74 editor.resize();
77   -
78   - });
  75 + });
  76 + document.getElementById("editor").addEventListener("input",function(){
  77 + console.log("input")
  78 + $("#save").attr("class",'hidden')
  79 + $("#save_btn").removeAttr("disabled");
  80 + $("#save_message").empty();
  81 + });
79 82 document.querySelector("#check_config").addEventListener('click',function(){
80 83 //console.log(editor.getValue());
81 84 csrf_value = $("[name='csrfmiddlewaretoken']").val();
... ... @@ -85,6 +88,7 @@
85 88 if(data["is_valid"] == true){
86 89 $("#message").empty().attr('class', 'normal').append("Valid configuration, do you want to save it ?");
87 90 $("#save").toggleClass('hidden');
  91 + $("#save_message").empty();
88 92 }
89 93 else{
90 94 $("#save").attr("class",'hidden')
... ... @@ -110,6 +114,7 @@
110 114 $.post('{% url 'save_config' %}', { "config": editor.getValue(), csrfmiddlewaretoken: csrf_value }, function(data,status) {
111 115 if(status == "success"){
112 116 $("#save_message").empty().append("<p>Configuration saved !</p>");
  117 + $("#save_btn").prop("disabled","true")
113 118 }
114 119 else{
115 120 $("#save_message").empty().append("<p class='red'>Error while trying to save the configuration file</p>");
... ...
src/core/pyros_django/obsconfig/templates/obsconfig/global_obs_configuration.html
... ... @@ -5,11 +5,19 @@
5 5 <div class="obs">
6 6 <h1>{{obs_name}} </h1>
7 7 <ul>
8   - <li> <a href="{% url 'edit_config' %}"> edit configuration </a> </li>
9   - <li> <a href="{% url 'obs_hardware_config' %}"> Hardware configuration </a> </li>
  8 + {% if CAN_EDIT_CONFIG %}
10 9  
11   - <li> <a href="{% url 'obs_astronomer_config' %}"> Astronomer configuration </a> </li>
12   - <li> <a href="{% url 'obs_agents_config' %}"> Agents configuration </a> </li>
  10 + <li> <a href="{% url 'edit_config' %}"> edit configuration </a> </li>
  11 + {% endif %}
  12 + {% if CAN_VIEW_HARDWARE_CONFIG %}
  13 + <li> <a href="{% url 'obs_hardware_config' %}"> Hardware configuration </a> </li>
  14 + {% endif %}
  15 + {% if CAN_VIEW_ASTRONOMER_CONFIG %}
  16 + <li> <a href="{% url 'obs_astronomer_config' %}"> Astronomer configuration </a> </li>
  17 + {% endif %}
  18 + {% if CAN_VIEW_AGENTS_CONFIG %}
  19 + <li> <a href="{% url 'obs_agents_config' %}"> Agents configuration </a> </li>
  20 + {% endif %}
13 21 </ul>
14 22 {% for unit_name in units_names %}
15 23 <div class="unit" style="margin-left:15%;">
... ...
src/core/pyros_django/obsconfig/views.py
... ... @@ -33,7 +33,19 @@ def obs_global_config(request):
33 33 units_names = list(config.get_units().keys())
34 34 pickle_file_mtime = os.path.getmtime(config.CONFIG_PATH+config.pickle_file)
35 35 pickle_datetime = datetime.utcfromtimestamp(pickle_file_mtime).strftime("%Y/%m/%d %H:%M:%S")
36   - return render(request,"obsconfig/global_obs_configuration.html",{"units_names" : units_names, "obs_name": config.get_obs_name(),"last_modification_time":pickle_datetime})
  36 + CAN_EDIT_CONFIG = request.session.get("role") in ("Admin","Operator","Unit-PI","Unit-board")
  37 + CAN_VIEW_HARDWARE_CONFIG = request.session.get("role") in ("Admin","Operator","Unit-PI","Unit-board")
  38 + CAN_VIEW_ASTRONOMER_CONFIG = request.session.get("role") in ("Admin","Operator","Unit-PI","Observer","Management board manager","Unit-board")
  39 + CAN_VIEW_AGENTS_CONFIG = request.session.get("role") == "Admin"
  40 + return render(request,"obsconfig/global_obs_configuration.html",{
  41 + "units_names" : units_names,
  42 + "obs_name": config.get_obs_name(),
  43 + "last_modification_time":pickle_datetime,
  44 + "CAN_EDIT_CONFIG":CAN_EDIT_CONFIG,
  45 + "CAN_VIEW_HARDWARE_CONFIG": CAN_VIEW_HARDWARE_CONFIG,
  46 + "CAN_VIEW_ASTRONOMER_CONFIG":CAN_VIEW_ASTRONOMER_CONFIG,
  47 + "CAN_VIEW_AGENTS_CONFIG":CAN_VIEW_AGENTS_CONFIG
  48 + })
37 49  
38 50  
39 51 @login_required
... ... @@ -221,7 +233,7 @@ def obs_agents_config(request):
221 233 @level_required("Admin","Operator","Unit-PI")
222 234 def edit_config(request):
223 235 config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"])
224   - return render(request,"obsconfig/edit_config.html",{"config_file":yaml.dump(config.obs_config_file_content)})
  236 + return render(request,"obsconfig/edit_config.html",{"config_file":config.raw_config})
225 237  
226 238 @login_required
227 239 @level_required("Admin","Operator","Unit-PI")
... ... @@ -236,7 +248,7 @@ def verify_config(request):
236 248 schema = config_file["schema"]
237 249 errors = []
238 250 if schema == None:
239   - response["is_valid"] = False
  251 + response_data["is_valid"] = False
240 252 response_data["message"] = "Missing schema"
241 253 schema_path = os.path.join(os.environ["DJANGO_PATH"],"../../../config/schemas/")
242 254 config = ConfigPyros.check_config(temp_config_file.name,schema_path+schema)
... ...