Commit d0501f5efe5efe195dbf1e142d3344c55fe83920
1 parent
46f967fb
Exists in
dev
use environment variable to get the path to observatory configuration file (prev…
…iously pyros was using the Django settings) and add datetime of last modification on the observatory configuration on the website
Showing
3 changed files
with
63 additions
and
12 deletions
Show diff stats
src/core/pyros_django/dashboard/views.py
... | ... | @@ -24,7 +24,7 @@ from devices.TelescopeRemoteControlDefault import TelescopeRemoteControlDefault |
24 | 24 | from devices.CameraVISRemoteControlDefault import CameraVISRemoteControlDefault |
25 | 25 | from devices.CameraNIRRemoteControlDefault import CameraNIRRemoteControlDefault |
26 | 26 | from devices import PLC |
27 | -import time | |
27 | +import time,os | |
28 | 28 | from obsconfig.configpyros import ConfigPyros |
29 | 29 | from django.conf import settings as pyros_settings |
30 | 30 | |
... | ... | @@ -41,7 +41,7 @@ MAX_LOGS_LINES = 100 |
41 | 41 | log = l.setupLogger("dashboard", "dashboard") |
42 | 42 | |
43 | 43 | def index(request): |
44 | - config = ConfigPyros(pyros_settings.PATH_TO_OBSCONF_FILE) | |
44 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
45 | 45 | observatory_name = config.get_obs_name() |
46 | 46 | first_unit_name = config.get_units_name()[0] |
47 | 47 | request.session["obsname"] = observatory_name+" "+first_unit_name |
... | ... |
src/core/pyros_django/obsconfig/templates/obsconfig/global_obs_configuration.html
1 | 1 | {% extends "base.html" %} |
2 | 2 | |
3 | 3 | {% block content %} |
4 | +<div id="last_modification"><h4>Last modification on: {{ last_modification_time }} (UTC)</h3></div> | |
4 | 5 | <div class="obs"> |
5 | 6 | <h1>{{obs_name}} </h1> |
6 | 7 | <ul> |
8 | + <li> <a href="{% url 'edit_config' %}"> edit configuration </a> </li> | |
7 | 9 | <li> <a href="{% url 'obs_hardware_config' %}"> Hardware configuration </a> </li> |
8 | 10 | |
9 | 11 | <li> <a href="{% url 'obs_astronomer_config' %}"> Astronomer configuration </a> </li> |
... | ... |
src/core/pyros_django/obsconfig/views.py
... | ... | @@ -4,7 +4,9 @@ from .configpyros import ConfigPyros |
4 | 4 | from django.conf import settings |
5 | 5 | from src.core.pyros_django.dashboard.decorator import level_required |
6 | 6 | from django.contrib.auth.decorators import login_required |
7 | -import re, yaml | |
7 | +from django.http import HttpResponse | |
8 | +from datetime import datetime | |
9 | +import re, yaml,tempfile,json,os | |
8 | 10 | |
9 | 11 | def get_nested_dictionaries_as_list(dic,result=[]): |
10 | 12 | for key,value in dic.items(): |
... | ... | @@ -27,15 +29,17 @@ def get_nested_dictionaries_as_list(dic,result=[]): |
27 | 29 | @login_required |
28 | 30 | @level_required("Admin","Observer","Management","Operator","Unit-PI") |
29 | 31 | def obs_global_config(request): |
30 | - config = ConfigPyros(settings.PATH_TO_OBSCONF_FILE) | |
32 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
31 | 33 | units_names = list(config.get_units().keys()) |
32 | - return render(request,"obsconfig/global_obs_configuration.html",{"units_names" : units_names, "obs_name": config.get_obs_name()}) | |
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") | |
36 | + return render(request,"obsconfig/global_obs_configuration.html",{"units_names" : units_names, "obs_name": config.get_obs_name(),"last_modification_time":pickle_datetime}) | |
33 | 37 | |
34 | 38 | |
35 | 39 | @login_required |
36 | 40 | @level_required("Admin","Unit-PI","Operator") |
37 | 41 | def obs_hardware_config(request): |
38 | - config = ConfigPyros(settings.PATH_TO_OBSCONF_FILE) | |
42 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
39 | 43 | devices = config.get_devices() |
40 | 44 | active_devices = config.get_active_devices() |
41 | 45 | computers = config.get_computers() |
... | ... | @@ -54,7 +58,7 @@ def obs_hardware_config(request): |
54 | 58 | @login_required |
55 | 59 | @level_required("Admin","Unit-PI","Operator") |
56 | 60 | def unit_hardware_configuration(request,unit_name): |
57 | - config = ConfigPyros(settings.PATH_TO_OBSCONF_FILE) | |
61 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
58 | 62 | devices = config.get_devices() |
59 | 63 | |
60 | 64 | return render(request, 'obsconfig/unit_hardware_configuration.html', {'config':config}) |
... | ... | @@ -62,7 +66,7 @@ def unit_hardware_configuration(request,unit_name): |
62 | 66 | @login_required |
63 | 67 | @level_required("Admin","Unit-PI","Operator") |
64 | 68 | def computer_details(request,computer_name): |
65 | - config = ConfigPyros(settings.PATH_TO_OBSCONF_FILE) | |
69 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
66 | 70 | computer_detail = yaml.dump(config.get_computers()[computer_name]) |
67 | 71 | """ |
68 | 72 | computer_detail = { re.sub("^_","",key):value for key,value in config.get_computers()[computer_name].items() } |
... | ... | @@ -84,7 +88,7 @@ def computer_details(request,computer_name): |
84 | 88 | @login_required |
85 | 89 | @level_required("Admin","Unit-PI","Operator") |
86 | 90 | def device_details(request,device_name): |
87 | - config = ConfigPyros(settings.PATH_TO_OBSCONF_FILE) | |
91 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
88 | 92 | # We're removing "_" at the beginning of each key : |
89 | 93 | device = config.get_devices()[device_name] |
90 | 94 | #device_detail = yaml.dump(device) |
... | ... | @@ -140,7 +144,7 @@ def device_details(request,device_name): |
140 | 144 | @login_required |
141 | 145 | @level_required("Admin","Observer","Management","Operator","Unit-PI","TAC") |
142 | 146 | def obs_astronomer_config(request): |
143 | - config = ConfigPyros(settings.PATH_TO_OBSCONF_FILE) | |
147 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
144 | 148 | units = config.get_units() |
145 | 149 | units_topologies = {} |
146 | 150 | # for each unit |
... | ... | @@ -177,7 +181,7 @@ def obs_astronomer_config(request): |
177 | 181 | @login_required |
178 | 182 | @level_required("Admin") |
179 | 183 | def obs_agents_config(request): |
180 | - config = ConfigPyros(settings.PATH_TO_OBSCONF_FILE) | |
184 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
181 | 185 | units = config.get_units() |
182 | 186 | units_topologies = {} |
183 | 187 | active_agents_by_unit = {} |
... | ... | @@ -210,4 +214,49 @@ def obs_agents_config(request): |
210 | 214 | index = units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"].index(component_agent) |
211 | 215 | units_topologies[unit_name]["CHANNELS"][channel_name]["COMPONENT_AGENTS"][index][component_name] = agent |
212 | 216 | |
213 | - return render(request,"obsconfig/obs_agents_config.html", { "units_topologies" : units_topologies, "active_agents_by_unit" : active_agents_by_unit}) | |
214 | 217 | \ No newline at end of file |
218 | + return render(request,"obsconfig/obs_agents_config.html", { "units_topologies" : units_topologies, "active_agents_by_unit" : active_agents_by_unit}) | |
219 | + | |
220 | + | |
221 | +@login_required | |
222 | +@level_required("Admin","Operator","Unit-PI") | |
223 | +def edit_config(request): | |
224 | + config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"]) | |
225 | + return render(request,"obsconfig/edit_config.html",{"config_file":yaml.dump(config.obs_config_file_content)}) | |
226 | + | |
227 | +@login_required | |
228 | +@level_required("Admin","Operator","Unit-PI") | |
229 | +def verify_config(request): | |
230 | + if request.POST.get("config"): | |
231 | + temp_config_file = tempfile.NamedTemporaryFile(mode = 'w+',suffix=".yml") | |
232 | + temp_config_file.write(request.POST.get("config")) | |
233 | + temp_config_file.seek(0) | |
234 | + response_data = {} | |
235 | + config_file = yaml.safe_load(temp_config_file.read()) | |
236 | + temp_config_file.seek(0) | |
237 | + schema = config_file["schema"] | |
238 | + errors = [] | |
239 | + if schema == None: | |
240 | + response["is_valid"] = False | |
241 | + response_data["message"] = "Missing schema" | |
242 | + schema_path = os.path.join(os.environ["DJANGO_PATH"],"../../../config/schemas/") | |
243 | + config = ConfigPyros.check_config(temp_config_file.name,schema_path+schema) | |
244 | + if type(config) == bool and config: | |
245 | + response_data["is_valid"] = True | |
246 | + else: | |
247 | + response_data["is_valid"] = False | |
248 | + for error in config: | |
249 | + errors.append((f"Error : {str(error).split('. Path')[0]}",f"Path to error : '{error.path}'")) | |
250 | + response_data["message"] = errors | |
251 | + temp_config_file.close() | |
252 | + return HttpResponse(json.dumps(response_data), content_type="application/json") | |
253 | + | |
254 | +@login_required | |
255 | +@level_required("Admin","Operator","Unit-PI") | |
256 | +def save_config(request): | |
257 | + if request.POST: | |
258 | + if request.POST["config"]: | |
259 | + with open(settings.PATH_TO_OBSCONF_FILE,"w") as obs_config_file: | |
260 | + obs_config_file.write(request.POST.get("config")) | |
261 | + | |
262 | + | |
263 | + return HttpResponse("Ok !") | |
215 | 264 | \ No newline at end of file |
... | ... |