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,7 +16,7 @@ class ConfigPyros:
16 computers = None 16 computers = None
17 agents = None 17 agents = None
18 obs_config_file_content = None 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 errors = None 20 errors = None
21 def verify_if_pickle_needs_to_be_updated(self,observatory_config_file)->bool: 21 def verify_if_pickle_needs_to_be_updated(self,observatory_config_file)->bool:
22 """[summary] 22 """[summary]
@@ -42,8 +42,10 @@ class ConfigPyros: @@ -42,8 +42,10 @@ class ConfigPyros:
42 # Create history folder if doesn't exist 42 # Create history folder if doesn't exist
43 Path(self.obs_config_path+"/history/").mkdir(parents=True, exist_ok=True) 43 Path(self.obs_config_path+"/history/").mkdir(parents=True, exist_ok=True)
44 file_name = f"{self.obs_config_path}/history/observatory_{pickle_datetime}.yml" 44 file_name = f"{self.obs_config_path}/history/observatory_{pickle_datetime}.yml"
  45 + config_file = open(observatory_config_file,"r")
  46 +
45 with open(file_name, 'w') as f: 47 with open(file_name, 'w') as f:
46 - data = yaml.dump(obs_config, f) 48 + f.write(config_file.read())
47 return True 49 return True
48 if obs_config == None: 50 if obs_config == None:
49 print(f"Error when trying to read config file (path of config file : {observatory_config_file}") 51 print(f"Error when trying to read config file (path of config file : {observatory_config_file}")
@@ -77,12 +79,30 @@ class ConfigPyros: @@ -77,12 +79,30 @@ class ConfigPyros:
77 time.sleep(0.5) 79 time.sleep(0.5)
78 except IOError: 80 except IOError:
79 print("Error when reading the pickle file") 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 else: 106 else:
87 print("Pickle file needs to be created or updated") 107 print("Pickle file needs to be created or updated")
88 pickle_dict = {} 108 pickle_dict = {}
src/core/pyros_django/obsconfig/templates/obsconfig/edit_config.html
@@ -26,11 +26,7 @@ @@ -26,11 +26,7 @@
26 left:80vw; 26 left:80vw;
27 top: 30vh; 27 top: 30vh;
28 } 28 }
29 - #message{  
30 - position:absolute;  
31 - left:80vw;  
32 - top: 25vh;  
33 - } 29 +
34 .red{ 30 .red{
35 color:red; 31 color:red;
36 } 32 }
@@ -47,14 +43,17 @@ @@ -47,14 +43,17 @@
47 </div> 43 </div>
48 <div> 44 <div>
49 {% csrf_token %} 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 </div> 48 </div>
54 <div id="save" class="hidden"> 49 <div id="save" class="hidden">
  50 +<p id="message"></p>
  51 + {% comment %}
55 <label> File name: (without ".yml" extension)</label> 52 <label> File name: (without ".yml" extension)</label>
  53 +
56 <input id="file_name"></input> 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 <p id="save_message"></p> 57 <p id="save_message"></p>
59 </div> 58 </div>
60 59
@@ -64,8 +63,7 @@ @@ -64,8 +63,7 @@
64 editor.setTheme("ace/theme/eclipse"); 63 editor.setTheme("ace/theme/eclipse");
65 editor.session.setMode("ace/mode/yaml"); 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 // loading default file (current observatory configuration file) 67 // loading default file (current observatory configuration file)
70 var fr=new FileReader(); 68 var fr=new FileReader();
71 fr.onload=function(){ 69 fr.onload=function(){
@@ -74,8 +72,13 @@ @@ -74,8 +72,13 @@
74 72
75 fr.readAsText(this.files[0]); 73 fr.readAsText(this.files[0]);
76 editor.resize(); 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 document.querySelector("#check_config").addEventListener('click',function(){ 82 document.querySelector("#check_config").addEventListener('click',function(){
80 //console.log(editor.getValue()); 83 //console.log(editor.getValue());
81 csrf_value = $("[name='csrfmiddlewaretoken']").val(); 84 csrf_value = $("[name='csrfmiddlewaretoken']").val();
@@ -85,6 +88,7 @@ @@ -85,6 +88,7 @@
85 if(data["is_valid"] == true){ 88 if(data["is_valid"] == true){
86 $("#message").empty().attr('class', 'normal').append("Valid configuration, do you want to save it ?"); 89 $("#message").empty().attr('class', 'normal').append("Valid configuration, do you want to save it ?");
87 $("#save").toggleClass('hidden'); 90 $("#save").toggleClass('hidden');
  91 + $("#save_message").empty();
88 } 92 }
89 else{ 93 else{
90 $("#save").attr("class",'hidden') 94 $("#save").attr("class",'hidden')
@@ -110,6 +114,7 @@ @@ -110,6 +114,7 @@
110 $.post('{% url 'save_config' %}', { "config": editor.getValue(), csrfmiddlewaretoken: csrf_value }, function(data,status) { 114 $.post('{% url 'save_config' %}', { "config": editor.getValue(), csrfmiddlewaretoken: csrf_value }, function(data,status) {
111 if(status == "success"){ 115 if(status == "success"){
112 $("#save_message").empty().append("<p>Configuration saved !</p>"); 116 $("#save_message").empty().append("<p>Configuration saved !</p>");
  117 + $("#save_btn").prop("disabled","true")
113 } 118 }
114 else{ 119 else{
115 $("#save_message").empty().append("<p class='red'>Error while trying to save the configuration file</p>"); 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,11 +5,19 @@
5 <div class="obs"> 5 <div class="obs">
6 <h1>{{obs_name}} </h1> 6 <h1>{{obs_name}} </h1>
7 <ul> 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 </ul> 21 </ul>
14 {% for unit_name in units_names %} 22 {% for unit_name in units_names %}
15 <div class="unit" style="margin-left: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,7 +33,19 @@ def obs_global_config(request):
33 units_names = list(config.get_units().keys()) 33 units_names = list(config.get_units().keys())
34 pickle_file_mtime = os.path.getmtime(config.CONFIG_PATH+config.pickle_file) 34 pickle_file_mtime = os.path.getmtime(config.CONFIG_PATH+config.pickle_file)
35 pickle_datetime = datetime.utcfromtimestamp(pickle_file_mtime).strftime("%Y/%m/%d %H:%M:%S") 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 @login_required 51 @login_required
@@ -221,7 +233,7 @@ def obs_agents_config(request): @@ -221,7 +233,7 @@ def obs_agents_config(request):
221 @level_required("Admin","Operator","Unit-PI") 233 @level_required("Admin","Operator","Unit-PI")
222 def edit_config(request): 234 def edit_config(request):
223 config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) 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 @login_required 238 @login_required
227 @level_required("Admin","Operator","Unit-PI") 239 @level_required("Admin","Operator","Unit-PI")
@@ -236,7 +248,7 @@ def verify_config(request): @@ -236,7 +248,7 @@ def verify_config(request):
236 schema = config_file["schema"] 248 schema = config_file["schema"]
237 errors = [] 249 errors = []
238 if schema == None: 250 if schema == None:
239 - response["is_valid"] = False 251 + response_data["is_valid"] = False
240 response_data["message"] = "Missing schema" 252 response_data["message"] = "Missing schema"
241 schema_path = os.path.join(os.environ["DJANGO_PATH"],"../../../config/schemas/") 253 schema_path = os.path.join(os.environ["DJANGO_PATH"],"../../../config/schemas/")
242 config = ConfigPyros.check_config(temp_config_file.name,schema_path+schema) 254 config = ConfigPyros.check_config(temp_config_file.name,schema_path+schema)