@@ -1,1655 +1,1663 | |||||
1 | import ast |
|
1 | import ast | |
2 | import json |
|
2 | import json | |
3 | from datetime import datetime |
|
3 | from datetime import datetime | |
4 |
|
4 | |||
5 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
5 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse | |
6 | from django.utils.safestring import mark_safe |
|
6 | from django.utils.safestring import mark_safe | |
7 | from django.http import HttpResponseRedirect |
|
7 | from django.http import HttpResponseRedirect | |
8 | from django.core.urlresolvers import reverse |
|
8 | from django.core.urlresolvers import reverse | |
9 | from django.db.models import Q |
|
9 | from django.db.models import Q | |
10 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
|
10 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | |
11 | from django.contrib import messages |
|
11 | from django.contrib import messages | |
12 | from django.http.request import QueryDict |
|
12 | from django.http.request import QueryDict | |
13 |
|
13 | |||
14 | try: |
|
14 | try: | |
15 | from urllib.parse import urlencode |
|
15 | from urllib.parse import urlencode | |
16 | except ImportError: |
|
16 | except ImportError: | |
17 | from urllib import urlencode |
|
17 | from urllib import urlencode | |
18 |
|
18 | |||
19 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
19 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm | |
20 | from .forms import OperationSearchForm, FilterForm, ChangeIpForm |
|
20 | from .forms import OperationSearchForm, FilterForm, ChangeIpForm | |
21 |
|
21 | |||
22 | from .tasks import task_start, task_stop |
|
22 | from .tasks import task_start, task_stop | |
23 |
|
23 | |||
24 | from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm |
|
24 | from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm | |
25 | from apps.dds.forms import DDSConfigurationForm |
|
25 | from apps.dds.forms import DDSConfigurationForm | |
26 | from apps.jars.forms import JARSConfigurationForm |
|
26 | from apps.jars.forms import JARSConfigurationForm | |
27 | from apps.cgs.forms import CGSConfigurationForm |
|
27 | from apps.cgs.forms import CGSConfigurationForm | |
28 | from apps.abs.forms import ABSConfigurationForm |
|
28 | from apps.abs.forms import ABSConfigurationForm | |
29 | from apps.usrp.forms import USRPConfigurationForm |
|
29 | from apps.usrp.forms import USRPConfigurationForm | |
30 |
|
30 | |||
31 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment |
|
31 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment, DEV_STATES | |
32 | from apps.cgs.models import CGSConfiguration |
|
32 | from apps.cgs.models import CGSConfiguration | |
33 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE |
|
33 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE | |
34 | from apps.usrp.models import USRPConfiguration |
|
34 | from apps.usrp.models import USRPConfiguration | |
35 | from apps.abs.models import ABSConfiguration |
|
35 | from apps.abs.models import ABSConfiguration | |
36 | from apps.rc.models import RCConfiguration, RCLine, RCLineType |
|
36 | from apps.rc.models import RCConfiguration, RCLine, RCLineType | |
37 | from apps.dds.models import DDSConfiguration |
|
37 | from apps.dds.models import DDSConfiguration | |
38 |
|
38 | |||
39 | from django.contrib.auth.decorators import login_required |
|
39 | from django.contrib.auth.decorators import login_required | |
40 | from django.contrib.auth.decorators import user_passes_test |
|
40 | from django.contrib.auth.decorators import user_passes_test | |
41 | from django.contrib.admin.views.decorators import staff_member_required |
|
41 | from django.contrib.admin.views.decorators import staff_member_required | |
42 |
|
42 | |||
43 | CONF_FORMS = { |
|
43 | CONF_FORMS = { | |
44 | 'rc': RCConfigurationForm, |
|
44 | 'rc': RCConfigurationForm, | |
45 | 'dds': DDSConfigurationForm, |
|
45 | 'dds': DDSConfigurationForm, | |
46 | 'jars': JARSConfigurationForm, |
|
46 | 'jars': JARSConfigurationForm, | |
47 | 'cgs': CGSConfigurationForm, |
|
47 | 'cgs': CGSConfigurationForm, | |
48 | 'abs': ABSConfigurationForm, |
|
48 | 'abs': ABSConfigurationForm, | |
49 | 'usrp': USRPConfigurationForm, |
|
49 | 'usrp': USRPConfigurationForm, | |
50 | } |
|
50 | } | |
51 |
|
51 | |||
52 | CONF_MODELS = { |
|
52 | CONF_MODELS = { | |
53 | 'rc': RCConfiguration, |
|
53 | 'rc': RCConfiguration, | |
54 | 'dds': DDSConfiguration, |
|
54 | 'dds': DDSConfiguration, | |
55 | 'jars': JARSConfiguration, |
|
55 | 'jars': JARSConfiguration, | |
56 | 'cgs': CGSConfiguration, |
|
56 | 'cgs': CGSConfiguration, | |
57 | 'abs': ABSConfiguration, |
|
57 | 'abs': ABSConfiguration, | |
58 | 'usrp': USRPConfiguration, |
|
58 | 'usrp': USRPConfiguration, | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | MIX_MODES = { |
|
61 | MIX_MODES = { | |
62 | '0': 'P', |
|
62 | '0': 'P', | |
63 | '1': 'S', |
|
63 | '1': 'S', | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | MIX_OPERATIONS = { |
|
66 | MIX_OPERATIONS = { | |
67 | '0': 'OR', |
|
67 | '0': 'OR', | |
68 | '1': 'XOR', |
|
68 | '1': 'XOR', | |
69 | '2': 'AND', |
|
69 | '2': 'AND', | |
70 | '3': 'NAND', |
|
70 | '3': 'NAND', | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | def index(request): |
|
73 | def index(request): | |
74 | kwargs = {'no_sidebar':True} |
|
74 | kwargs = {'no_sidebar':True} | |
75 |
|
75 | |||
76 | return render(request, 'index.html', kwargs) |
|
76 | return render(request, 'index.html', kwargs) | |
77 |
|
77 | |||
78 |
|
78 | |||
79 | def locations(request): |
|
79 | def locations(request): | |
80 |
|
80 | |||
81 | page = request.GET.get('page') |
|
81 | page = request.GET.get('page') | |
82 | order = ('name',) |
|
82 | order = ('name',) | |
83 |
|
83 | |||
84 | kwargs = get_paginator(Location, page, order) |
|
84 | kwargs = get_paginator(Location, page, order) | |
85 |
|
85 | |||
86 | kwargs['keys'] = ['name', 'description'] |
|
86 | kwargs['keys'] = ['name', 'description'] | |
87 | kwargs['title'] = 'Radar System' |
|
87 | kwargs['title'] = 'Radar System' | |
88 | kwargs['suptitle'] = 'List' |
|
88 | kwargs['suptitle'] = 'List' | |
89 | kwargs['no_sidebar'] = True |
|
89 | kwargs['no_sidebar'] = True | |
90 |
|
90 | |||
91 | return render(request, 'base_list.html', kwargs) |
|
91 | return render(request, 'base_list.html', kwargs) | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | def location(request, id_loc): |
|
94 | def location(request, id_loc): | |
95 |
|
95 | |||
96 | location = get_object_or_404(Location, pk=id_loc) |
|
96 | location = get_object_or_404(Location, pk=id_loc) | |
97 |
|
97 | |||
98 | kwargs = {} |
|
98 | kwargs = {} | |
99 | kwargs['location'] = location |
|
99 | kwargs['location'] = location | |
100 | kwargs['location_keys'] = ['name', 'description'] |
|
100 | kwargs['location_keys'] = ['name', 'description'] | |
101 |
|
101 | |||
102 | kwargs['title'] = 'Location' |
|
102 | kwargs['title'] = 'Location' | |
103 | kwargs['suptitle'] = 'Details' |
|
103 | kwargs['suptitle'] = 'Details' | |
104 |
|
104 | |||
105 | return render(request, 'location.html', kwargs) |
|
105 | return render(request, 'location.html', kwargs) | |
106 |
|
106 | |||
107 |
|
107 | |||
108 | @user_passes_test(lambda u:u.is_staff) |
|
108 | @user_passes_test(lambda u:u.is_staff) | |
109 | def location_new(request): |
|
109 | def location_new(request): | |
110 |
|
110 | |||
111 | if request.method == 'GET': |
|
111 | if request.method == 'GET': | |
112 | form = LocationForm() |
|
112 | form = LocationForm() | |
113 |
|
113 | |||
114 | if request.method == 'POST': |
|
114 | if request.method == 'POST': | |
115 | form = LocationForm(request.POST) |
|
115 | form = LocationForm(request.POST) | |
116 |
|
116 | |||
117 | if form.is_valid(): |
|
117 | if form.is_valid(): | |
118 | form.save() |
|
118 | form.save() | |
119 | return redirect('url_locations') |
|
119 | return redirect('url_locations') | |
120 |
|
120 | |||
121 | kwargs = {} |
|
121 | kwargs = {} | |
122 | kwargs['form'] = form |
|
122 | kwargs['form'] = form | |
123 | kwargs['title'] = 'Radar System' |
|
123 | kwargs['title'] = 'Radar System' | |
124 | kwargs['suptitle'] = 'New' |
|
124 | kwargs['suptitle'] = 'New' | |
125 | kwargs['button'] = 'Create' |
|
125 | kwargs['button'] = 'Create' | |
126 |
|
126 | |||
127 | return render(request, 'base_edit.html', kwargs) |
|
127 | return render(request, 'base_edit.html', kwargs) | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | @user_passes_test(lambda u:u.is_staff) |
|
130 | @user_passes_test(lambda u:u.is_staff) | |
131 | def location_edit(request, id_loc): |
|
131 | def location_edit(request, id_loc): | |
132 |
|
132 | |||
133 | location = get_object_or_404(Location, pk=id_loc) |
|
133 | location = get_object_or_404(Location, pk=id_loc) | |
134 |
|
134 | |||
135 | if request.method=='GET': |
|
135 | if request.method=='GET': | |
136 | form = LocationForm(instance=location) |
|
136 | form = LocationForm(instance=location) | |
137 |
|
137 | |||
138 | if request.method=='POST': |
|
138 | if request.method=='POST': | |
139 | form = LocationForm(request.POST, instance=location) |
|
139 | form = LocationForm(request.POST, instance=location) | |
140 |
|
140 | |||
141 | if form.is_valid(): |
|
141 | if form.is_valid(): | |
142 | form.save() |
|
142 | form.save() | |
143 | return redirect('url_locations') |
|
143 | return redirect('url_locations') | |
144 |
|
144 | |||
145 | kwargs = {} |
|
145 | kwargs = {} | |
146 | kwargs['form'] = form |
|
146 | kwargs['form'] = form | |
147 | kwargs['title'] = 'Location' |
|
147 | kwargs['title'] = 'Location' | |
148 | kwargs['suptitle'] = 'Edit' |
|
148 | kwargs['suptitle'] = 'Edit' | |
149 | kwargs['button'] = 'Update' |
|
149 | kwargs['button'] = 'Update' | |
150 |
|
150 | |||
151 | return render(request, 'base_edit.html', kwargs) |
|
151 | return render(request, 'base_edit.html', kwargs) | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | @user_passes_test(lambda u:u.is_staff) |
|
154 | @user_passes_test(lambda u:u.is_staff) | |
155 | def location_delete(request, id_loc): |
|
155 | def location_delete(request, id_loc): | |
156 |
|
156 | |||
157 | location = get_object_or_404(Location, pk=id_loc) |
|
157 | location = get_object_or_404(Location, pk=id_loc) | |
158 |
|
158 | |||
159 | if request.method=='POST': |
|
159 | if request.method=='POST': | |
160 |
|
160 | |||
161 | if request.user.is_staff: |
|
161 | if request.user.is_staff: | |
162 | location.delete() |
|
162 | location.delete() | |
163 | return redirect('url_locations') |
|
163 | return redirect('url_locations') | |
164 |
|
164 | |||
165 | messages.error(request, 'Not enough permission to delete this object') |
|
165 | messages.error(request, 'Not enough permission to delete this object') | |
166 | return redirect(location.get_absolute_url()) |
|
166 | return redirect(location.get_absolute_url()) | |
167 |
|
167 | |||
168 | kwargs = { |
|
168 | kwargs = { | |
169 | 'title': 'Delete', |
|
169 | 'title': 'Delete', | |
170 | 'suptitle': 'Location', |
|
170 | 'suptitle': 'Location', | |
171 | 'object': location, |
|
171 | 'object': location, | |
172 | 'previous': location.get_absolute_url(), |
|
172 | 'previous': location.get_absolute_url(), | |
173 | 'delete': True |
|
173 | 'delete': True | |
174 | } |
|
174 | } | |
175 |
|
175 | |||
176 | return render(request, 'confirm.html', kwargs) |
|
176 | return render(request, 'confirm.html', kwargs) | |
177 |
|
177 | |||
178 |
|
178 | |||
179 | def devices(request): |
|
179 | def devices(request): | |
180 |
|
180 | |||
181 | page = request.GET.get('page') |
|
181 | page = request.GET.get('page') | |
182 | order = ('device_type', 'name') |
|
182 | order = ('device_type', 'name') | |
183 |
|
183 | |||
184 | kwargs = get_paginator(Device, page, order) |
|
184 | kwargs = get_paginator(Device, page, order) | |
185 | kwargs['keys'] = ['name', 'ip_address', 'port_address', 'device_type'] |
|
185 | kwargs['keys'] = ['name', 'ip_address', 'port_address', 'device_type'] | |
186 | kwargs['title'] = 'Device' |
|
186 | kwargs['title'] = 'Device' | |
187 | kwargs['suptitle'] = 'List' |
|
187 | kwargs['suptitle'] = 'List' | |
188 | kwargs['no_sidebar'] = True |
|
188 | kwargs['no_sidebar'] = True | |
189 |
|
189 | |||
190 | return render(request, 'base_list.html', kwargs) |
|
190 | return render(request, 'base_list.html', kwargs) | |
191 |
|
191 | |||
192 |
|
192 | |||
193 | def device(request, id_dev): |
|
193 | def device(request, id_dev): | |
194 |
|
194 | |||
195 | device = get_object_or_404(Device, pk=id_dev) |
|
195 | device = get_object_or_404(Device, pk=id_dev) | |
196 |
|
196 | |||
197 | kwargs = {} |
|
197 | kwargs = {} | |
198 | kwargs['device'] = device |
|
198 | kwargs['device'] = device | |
199 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
199 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] | |
200 |
|
200 | |||
201 | kwargs['title'] = 'Device' |
|
201 | kwargs['title'] = 'Device' | |
202 | kwargs['suptitle'] = 'Details' |
|
202 | kwargs['suptitle'] = 'Details' | |
203 |
|
203 | |||
204 | return render(request, 'device.html', kwargs) |
|
204 | return render(request, 'device.html', kwargs) | |
205 |
|
205 | |||
206 |
|
206 | |||
207 | @user_passes_test(lambda u:u.is_staff) |
|
207 | @user_passes_test(lambda u:u.is_staff) | |
208 | def device_new(request): |
|
208 | def device_new(request): | |
209 |
|
209 | |||
210 | if request.method == 'GET': |
|
210 | if request.method == 'GET': | |
211 | form = DeviceForm() |
|
211 | form = DeviceForm() | |
212 |
|
212 | |||
213 | if request.method == 'POST': |
|
213 | if request.method == 'POST': | |
214 | form = DeviceForm(request.POST) |
|
214 | form = DeviceForm(request.POST) | |
215 |
|
215 | |||
216 | if form.is_valid(): |
|
216 | if form.is_valid(): | |
217 | form.save() |
|
217 | form.save() | |
218 | return redirect('url_devices') |
|
218 | return redirect('url_devices') | |
219 |
|
219 | |||
220 | kwargs = {} |
|
220 | kwargs = {} | |
221 | kwargs['form'] = form |
|
221 | kwargs['form'] = form | |
222 | kwargs['title'] = 'Device' |
|
222 | kwargs['title'] = 'Device' | |
223 | kwargs['suptitle'] = 'New' |
|
223 | kwargs['suptitle'] = 'New' | |
224 | kwargs['button'] = 'Create' |
|
224 | kwargs['button'] = 'Create' | |
225 |
|
225 | |||
226 | return render(request, 'base_edit.html', kwargs) |
|
226 | return render(request, 'base_edit.html', kwargs) | |
227 |
|
227 | |||
228 |
|
228 | |||
229 | @user_passes_test(lambda u:u.is_staff) |
|
229 | @user_passes_test(lambda u:u.is_staff) | |
230 | def device_edit(request, id_dev): |
|
230 | def device_edit(request, id_dev): | |
231 |
|
231 | |||
232 | device = get_object_or_404(Device, pk=id_dev) |
|
232 | device = get_object_or_404(Device, pk=id_dev) | |
233 |
|
233 | |||
234 | if request.method=='GET': |
|
234 | if request.method=='GET': | |
235 | form = DeviceForm(instance=device) |
|
235 | form = DeviceForm(instance=device) | |
236 |
|
236 | |||
237 | if request.method=='POST': |
|
237 | if request.method=='POST': | |
238 | form = DeviceForm(request.POST, instance=device) |
|
238 | form = DeviceForm(request.POST, instance=device) | |
239 |
|
239 | |||
240 | if form.is_valid(): |
|
240 | if form.is_valid(): | |
241 | form.save() |
|
241 | form.save() | |
242 | return redirect(device.get_absolute_url()) |
|
242 | return redirect(device.get_absolute_url()) | |
243 |
|
243 | |||
244 | kwargs = {} |
|
244 | kwargs = {} | |
245 | kwargs['form'] = form |
|
245 | kwargs['form'] = form | |
246 | kwargs['title'] = 'Device' |
|
246 | kwargs['title'] = 'Device' | |
247 | kwargs['suptitle'] = 'Edit' |
|
247 | kwargs['suptitle'] = 'Edit' | |
248 | kwargs['button'] = 'Update' |
|
248 | kwargs['button'] = 'Update' | |
249 |
|
249 | |||
250 | return render(request, 'base_edit.html', kwargs) |
|
250 | return render(request, 'base_edit.html', kwargs) | |
251 |
|
251 | |||
252 |
|
252 | |||
253 | @user_passes_test(lambda u:u.is_staff) |
|
253 | @user_passes_test(lambda u:u.is_staff) | |
254 | def device_delete(request, id_dev): |
|
254 | def device_delete(request, id_dev): | |
255 |
|
255 | |||
256 | device = get_object_or_404(Device, pk=id_dev) |
|
256 | device = get_object_or_404(Device, pk=id_dev) | |
257 |
|
257 | |||
258 | if request.method=='POST': |
|
258 | if request.method=='POST': | |
259 |
|
259 | |||
260 | if request.user.is_staff: |
|
260 | if request.user.is_staff: | |
261 | device.delete() |
|
261 | device.delete() | |
262 | return redirect('url_devices') |
|
262 | return redirect('url_devices') | |
263 |
|
263 | |||
264 | messages.error(request, 'Not enough permission to delete this object') |
|
264 | messages.error(request, 'Not enough permission to delete this object') | |
265 | return redirect(device.get_absolute_url()) |
|
265 | return redirect(device.get_absolute_url()) | |
266 |
|
266 | |||
267 | kwargs = { |
|
267 | kwargs = { | |
268 | 'title': 'Delete', |
|
268 | 'title': 'Delete', | |
269 | 'suptitle': 'Device', |
|
269 | 'suptitle': 'Device', | |
270 | 'object': device, |
|
270 | 'object': device, | |
271 | 'previous': device.get_absolute_url(), |
|
271 | 'previous': device.get_absolute_url(), | |
272 | 'delete': True |
|
272 | 'delete': True | |
273 | } |
|
273 | } | |
274 |
|
274 | |||
275 | return render(request, 'confirm.html', kwargs) |
|
275 | return render(request, 'confirm.html', kwargs) | |
276 |
|
276 | |||
277 |
|
277 | |||
278 | @user_passes_test(lambda u:u.is_staff) |
|
278 | @user_passes_test(lambda u:u.is_staff) | |
279 | def device_change_ip(request, id_dev): |
|
279 | def device_change_ip(request, id_dev): | |
280 |
|
280 | |||
281 | device = get_object_or_404(Device, pk=id_dev) |
|
281 | device = get_object_or_404(Device, pk=id_dev) | |
282 |
|
282 | |||
283 | if request.method=='POST': |
|
283 | if request.method=='POST': | |
284 |
|
284 | |||
285 | if request.user.is_staff: |
|
285 | if request.user.is_staff: | |
286 | device.change_ip(**request.POST.dict()) |
|
286 | device.change_ip(**request.POST.dict()) | |
287 | level, message = device.message.split('|') |
|
287 | level, message = device.message.split('|') | |
288 | messages.add_message(request, level, message) |
|
288 | messages.add_message(request, level, message) | |
289 | else: |
|
289 | else: | |
290 | messages.error(request, 'Not enough permission to delete this object') |
|
290 | messages.error(request, 'Not enough permission to delete this object') | |
291 | return redirect(device.get_absolute_url()) |
|
291 | return redirect(device.get_absolute_url()) | |
292 |
|
292 | |||
293 | kwargs = { |
|
293 | kwargs = { | |
294 | 'title': 'Device', |
|
294 | 'title': 'Device', | |
295 | 'suptitle': 'Change IP', |
|
295 | 'suptitle': 'Change IP', | |
296 | 'object': device, |
|
296 | 'object': device, | |
297 | 'previous': device.get_absolute_url(), |
|
297 | 'previous': device.get_absolute_url(), | |
298 | 'form': ChangeIpForm(initial={'ip_address':device.ip_address}), |
|
298 | 'form': ChangeIpForm(initial={'ip_address':device.ip_address}), | |
299 | 'message' : ' ', |
|
299 | 'message' : ' ', | |
300 | } |
|
300 | } | |
301 |
|
301 | |||
302 | return render(request, 'confirm.html', kwargs) |
|
302 | return render(request, 'confirm.html', kwargs) | |
303 |
|
303 | |||
304 |
|
304 | |||
305 | def campaigns(request): |
|
305 | def campaigns(request): | |
306 |
|
306 | |||
307 | page = request.GET.get('page') |
|
307 | page = request.GET.get('page') | |
308 | order = ('start_date',) |
|
308 | order = ('start_date',) | |
309 | filters = request.GET.copy() |
|
309 | filters = request.GET.copy() | |
310 |
|
310 | |||
311 | kwargs = get_paginator(Campaign, page, order, filters) |
|
311 | kwargs = get_paginator(Campaign, page, order, filters) | |
312 |
|
312 | |||
313 | form = FilterForm(initial=request.GET, extra_fields=['range_date', 'tags','template']) |
|
313 | form = FilterForm(initial=request.GET, extra_fields=['range_date', 'tags','template']) | |
314 | kwargs['keys'] = ['name', 'start_date', 'end_date'] |
|
314 | kwargs['keys'] = ['name', 'start_date', 'end_date'] | |
315 | kwargs['title'] = 'Campaign' |
|
315 | kwargs['title'] = 'Campaign' | |
316 | kwargs['suptitle'] = 'List' |
|
316 | kwargs['suptitle'] = 'List' | |
317 | kwargs['no_sidebar'] = True |
|
317 | kwargs['no_sidebar'] = True | |
318 | kwargs['form'] = form |
|
318 | kwargs['form'] = form | |
319 | filters.pop('page', None) |
|
319 | filters.pop('page', None) | |
320 | kwargs['q'] = urlencode(filters) |
|
320 | kwargs['q'] = urlencode(filters) | |
321 |
|
321 | |||
322 | return render(request, 'base_list.html', kwargs) |
|
322 | return render(request, 'base_list.html', kwargs) | |
323 |
|
323 | |||
324 |
|
324 | |||
325 | def campaign(request, id_camp): |
|
325 | def campaign(request, id_camp): | |
326 |
|
326 | |||
327 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
327 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
328 | experiments = Experiment.objects.filter(campaign=campaign) |
|
328 | experiments = Experiment.objects.filter(campaign=campaign) | |
329 |
|
329 | |||
330 | form = CampaignForm(instance=campaign) |
|
330 | form = CampaignForm(instance=campaign) | |
331 |
|
331 | |||
332 | kwargs = {} |
|
332 | kwargs = {} | |
333 | kwargs['campaign'] = campaign |
|
333 | kwargs['campaign'] = campaign | |
334 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] |
|
334 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] | |
335 |
|
335 | |||
336 | kwargs['experiments'] = experiments |
|
336 | kwargs['experiments'] = experiments | |
337 | kwargs['experiment_keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
337 | kwargs['experiment_keys'] = ['name', 'radar_system', 'start_time', 'end_time'] | |
338 |
|
338 | |||
339 | kwargs['title'] = 'Campaign' |
|
339 | kwargs['title'] = 'Campaign' | |
340 | kwargs['suptitle'] = 'Details' |
|
340 | kwargs['suptitle'] = 'Details' | |
341 |
|
341 | |||
342 | kwargs['form'] = form |
|
342 | kwargs['form'] = form | |
343 | kwargs['button'] = 'Add Experiment' |
|
343 | kwargs['button'] = 'Add Experiment' | |
344 |
|
344 | |||
345 | return render(request, 'campaign.html', kwargs) |
|
345 | return render(request, 'campaign.html', kwargs) | |
346 |
|
346 | |||
347 |
|
347 | |||
348 | @user_passes_test(lambda u:u.is_staff) |
|
348 | @user_passes_test(lambda u:u.is_staff) | |
349 | def campaign_new(request): |
|
349 | def campaign_new(request): | |
350 |
|
350 | |||
351 | kwargs = {} |
|
351 | kwargs = {} | |
352 |
|
352 | |||
353 | if request.method == 'GET': |
|
353 | if request.method == 'GET': | |
354 |
|
354 | |||
355 | if 'template' in request.GET: |
|
355 | if 'template' in request.GET: | |
356 | if request.GET['template']=='0': |
|
356 | if request.GET['template']=='0': | |
357 | form = NewForm(initial={'create_from':2}, |
|
357 | form = NewForm(initial={'create_from':2}, | |
358 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
358 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) | |
359 | else: |
|
359 | else: | |
360 | kwargs['button'] = 'Create' |
|
360 | kwargs['button'] = 'Create' | |
361 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
361 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) | |
362 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
362 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] | |
363 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
363 | camp = Campaign.objects.get(pk=request.GET['template']) | |
364 | form = CampaignForm(instance=camp, |
|
364 | form = CampaignForm(instance=camp, | |
365 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), |
|
365 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), | |
366 | 'template':False}) |
|
366 | 'template':False}) | |
367 | elif 'blank' in request.GET: |
|
367 | elif 'blank' in request.GET: | |
368 | kwargs['button'] = 'Create' |
|
368 | kwargs['button'] = 'Create' | |
369 | form = CampaignForm() |
|
369 | form = CampaignForm() | |
370 | else: |
|
370 | else: | |
371 | form = NewForm() |
|
371 | form = NewForm() | |
372 |
|
372 | |||
373 | if request.method == 'POST': |
|
373 | if request.method == 'POST': | |
374 | kwargs['button'] = 'Create' |
|
374 | kwargs['button'] = 'Create' | |
375 | post = request.POST.copy() |
|
375 | post = request.POST.copy() | |
376 | experiments = [] |
|
376 | experiments = [] | |
377 |
|
377 | |||
378 | for id_exp in post.getlist('experiments'): |
|
378 | for id_exp in post.getlist('experiments'): | |
379 | exp = Experiment.objects.get(pk=id_exp) |
|
379 | exp = Experiment.objects.get(pk=id_exp) | |
380 | new_exp = exp.clone(template=False) |
|
380 | new_exp = exp.clone(template=False) | |
381 | experiments.append(new_exp) |
|
381 | experiments.append(new_exp) | |
382 |
|
382 | |||
383 | post.setlist('experiments', []) |
|
383 | post.setlist('experiments', []) | |
384 |
|
384 | |||
385 | form = CampaignForm(post) |
|
385 | form = CampaignForm(post) | |
386 |
|
386 | |||
387 | if form.is_valid(): |
|
387 | if form.is_valid(): | |
388 | campaign = form.save() |
|
388 | campaign = form.save() | |
389 | for exp in experiments: |
|
389 | for exp in experiments: | |
390 | campaign.experiments.add(exp) |
|
390 | campaign.experiments.add(exp) | |
391 | campaign.save() |
|
391 | campaign.save() | |
392 | return redirect('url_campaign', id_camp=campaign.id) |
|
392 | return redirect('url_campaign', id_camp=campaign.id) | |
393 |
|
393 | |||
394 | kwargs['form'] = form |
|
394 | kwargs['form'] = form | |
395 | kwargs['title'] = 'Campaign' |
|
395 | kwargs['title'] = 'Campaign' | |
396 | kwargs['suptitle'] = 'New' |
|
396 | kwargs['suptitle'] = 'New' | |
397 |
|
397 | |||
398 | return render(request, 'campaign_edit.html', kwargs) |
|
398 | return render(request, 'campaign_edit.html', kwargs) | |
399 |
|
399 | |||
400 |
|
400 | |||
401 | @user_passes_test(lambda u:u.is_staff) |
|
401 | @user_passes_test(lambda u:u.is_staff) | |
402 | def campaign_edit(request, id_camp): |
|
402 | def campaign_edit(request, id_camp): | |
403 |
|
403 | |||
404 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
404 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
405 |
|
405 | |||
406 | if request.method=='GET': |
|
406 | if request.method=='GET': | |
407 | form = CampaignForm(instance=campaign) |
|
407 | form = CampaignForm(instance=campaign) | |
408 |
|
408 | |||
409 | if request.method=='POST': |
|
409 | if request.method=='POST': | |
410 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
410 | exps = campaign.experiments.all().values_list('pk', flat=True) | |
411 | post = request.POST.copy() |
|
411 | post = request.POST.copy() | |
412 | new_exps = post.getlist('experiments') |
|
412 | new_exps = post.getlist('experiments') | |
413 | post.setlist('experiments', []) |
|
413 | post.setlist('experiments', []) | |
414 | form = CampaignForm(post, instance=campaign) |
|
414 | form = CampaignForm(post, instance=campaign) | |
415 |
|
415 | |||
416 | if form.is_valid(): |
|
416 | if form.is_valid(): | |
417 | camp = form.save() |
|
417 | camp = form.save() | |
418 | for id_exp in new_exps: |
|
418 | for id_exp in new_exps: | |
419 | if int(id_exp) in exps: |
|
419 | if int(id_exp) in exps: | |
420 | exps.pop(id_exp) |
|
420 | exps.pop(id_exp) | |
421 | else: |
|
421 | else: | |
422 | exp = Experiment.objects.get(pk=id_exp) |
|
422 | exp = Experiment.objects.get(pk=id_exp) | |
423 | if exp.template: |
|
423 | if exp.template: | |
424 | camp.experiments.add(exp.clone(template=False)) |
|
424 | camp.experiments.add(exp.clone(template=False)) | |
425 | else: |
|
425 | else: | |
426 | camp.experiments.add(exp) |
|
426 | camp.experiments.add(exp) | |
427 |
|
427 | |||
428 | for id_exp in exps: |
|
428 | for id_exp in exps: | |
429 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
429 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) | |
430 |
|
430 | |||
431 | return redirect('url_campaign', id_camp=id_camp) |
|
431 | return redirect('url_campaign', id_camp=id_camp) | |
432 |
|
432 | |||
433 | kwargs = {} |
|
433 | kwargs = {} | |
434 | kwargs['form'] = form |
|
434 | kwargs['form'] = form | |
435 | kwargs['title'] = 'Campaign' |
|
435 | kwargs['title'] = 'Campaign' | |
436 | kwargs['suptitle'] = 'Edit' |
|
436 | kwargs['suptitle'] = 'Edit' | |
437 | kwargs['button'] = 'Update' |
|
437 | kwargs['button'] = 'Update' | |
438 |
|
438 | |||
439 | return render(request, 'campaign_edit.html', kwargs) |
|
439 | return render(request, 'campaign_edit.html', kwargs) | |
440 |
|
440 | |||
441 |
|
441 | |||
442 | @user_passes_test(lambda u:u.is_staff) |
|
442 | @user_passes_test(lambda u:u.is_staff) | |
443 | def campaign_delete(request, id_camp): |
|
443 | def campaign_delete(request, id_camp): | |
444 |
|
444 | |||
445 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
445 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
446 |
|
446 | |||
447 | if request.method=='POST': |
|
447 | if request.method=='POST': | |
448 | if request.user.is_staff: |
|
448 | if request.user.is_staff: | |
449 |
|
449 | |||
450 | for exp in campaign.experiments.all(): |
|
450 | for exp in campaign.experiments.all(): | |
451 | for conf in Configuration.objects.filter(experiment=exp): |
|
451 | for conf in Configuration.objects.filter(experiment=exp): | |
452 | conf.delete() |
|
452 | conf.delete() | |
453 | exp.delete() |
|
453 | exp.delete() | |
454 | campaign.delete() |
|
454 | campaign.delete() | |
455 |
|
455 | |||
456 | return redirect('url_campaigns') |
|
456 | return redirect('url_campaigns') | |
457 |
|
457 | |||
458 | messages.error(request, 'Not enough permission to delete this object') |
|
458 | messages.error(request, 'Not enough permission to delete this object') | |
459 | return redirect(campaign.get_absolute_url()) |
|
459 | return redirect(campaign.get_absolute_url()) | |
460 |
|
460 | |||
461 | kwargs = { |
|
461 | kwargs = { | |
462 | 'title': 'Delete', |
|
462 | 'title': 'Delete', | |
463 | 'suptitle': 'Campaign', |
|
463 | 'suptitle': 'Campaign', | |
464 | 'object': campaign, |
|
464 | 'object': campaign, | |
465 | 'previous': campaign.get_absolute_url(), |
|
465 | 'previous': campaign.get_absolute_url(), | |
466 | 'delete': True |
|
466 | 'delete': True | |
467 | } |
|
467 | } | |
468 |
|
468 | |||
469 | return render(request, 'confirm.html', kwargs) |
|
469 | return render(request, 'confirm.html', kwargs) | |
470 |
|
470 | |||
471 |
|
471 | |||
472 | @user_passes_test(lambda u:u.is_staff) |
|
472 | @user_passes_test(lambda u:u.is_staff) | |
473 | def campaign_export(request, id_camp): |
|
473 | def campaign_export(request, id_camp): | |
474 |
|
474 | |||
475 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
475 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
476 | content = campaign.parms_to_dict() |
|
476 | content = campaign.parms_to_dict() | |
477 | content_type = 'application/json' |
|
477 | content_type = 'application/json' | |
478 | filename = '%s_%s.json' %(campaign.name, campaign.id) |
|
478 | filename = '%s_%s.json' %(campaign.name, campaign.id) | |
479 |
|
479 | |||
480 | response = HttpResponse(content_type=content_type) |
|
480 | response = HttpResponse(content_type=content_type) | |
481 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
481 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename | |
482 | response.write(content) |
|
482 | response.write(content) | |
483 |
|
483 | |||
484 | return response |
|
484 | return response | |
485 |
|
485 | |||
486 |
|
486 | |||
487 | @user_passes_test(lambda u:u.is_staff) |
|
487 | @user_passes_test(lambda u:u.is_staff) | |
488 | def campaign_import(request, id_camp): |
|
488 | def campaign_import(request, id_camp): | |
489 |
|
489 | |||
490 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
490 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
491 |
|
491 | |||
492 | if request.method == 'GET': |
|
492 | if request.method == 'GET': | |
493 | file_form = UploadFileForm() |
|
493 | file_form = UploadFileForm() | |
494 |
|
494 | |||
495 | if request.method == 'POST': |
|
495 | if request.method == 'POST': | |
496 | file_form = UploadFileForm(request.POST, request.FILES) |
|
496 | file_form = UploadFileForm(request.POST, request.FILES) | |
497 |
|
497 | |||
498 | if file_form.is_valid(): |
|
498 | if file_form.is_valid(): | |
499 |
|
499 | |||
500 | parms = campaign.import_from_file(request.FILES['file']) |
|
500 | parms = campaign.import_from_file(request.FILES['file']) | |
501 |
|
501 | |||
502 | if parms: |
|
502 | if parms: | |
503 | parms['name'] = parms['campaign'] |
|
503 | parms['name'] = parms['campaign'] | |
504 |
|
504 | |||
505 | new_camp = campaign.dict_to_parms(parms, CONF_MODELS) |
|
505 | new_camp = campaign.dict_to_parms(parms, CONF_MODELS) | |
506 |
|
506 | |||
507 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
507 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) | |
508 |
|
508 | |||
509 | return redirect(new_camp.get_absolute_url_edit()) |
|
509 | return redirect(new_camp.get_absolute_url_edit()) | |
510 |
|
510 | |||
511 | messages.error(request, "Could not import parameters from file") |
|
511 | messages.error(request, "Could not import parameters from file") | |
512 |
|
512 | |||
513 | kwargs = {} |
|
513 | kwargs = {} | |
514 | kwargs['title'] = 'Campaign' |
|
514 | kwargs['title'] = 'Campaign' | |
515 | kwargs['form'] = file_form |
|
515 | kwargs['form'] = file_form | |
516 | kwargs['suptitle'] = 'Importing file' |
|
516 | kwargs['suptitle'] = 'Importing file' | |
517 | kwargs['button'] = 'Import' |
|
517 | kwargs['button'] = 'Import' | |
518 |
|
518 | |||
519 | return render(request, 'campaign_import.html', kwargs) |
|
519 | return render(request, 'campaign_import.html', kwargs) | |
520 |
|
520 | |||
521 |
|
521 | |||
522 | def experiments(request): |
|
522 | def experiments(request): | |
523 |
|
523 | |||
524 | page = request.GET.get('page') |
|
524 | page = request.GET.get('page') | |
525 | order = ('location',) |
|
525 | order = ('location',) | |
526 | filters = request.GET.copy() |
|
526 | filters = request.GET.copy() | |
527 |
|
527 | |||
528 | kwargs = get_paginator(Experiment, page, order, filters) |
|
528 | kwargs = get_paginator(Experiment, page, order, filters) | |
529 |
|
529 | |||
530 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
530 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) | |
531 |
|
531 | |||
532 | kwargs['keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
532 | kwargs['keys'] = ['name', 'radar_system', 'start_time', 'end_time'] | |
533 | kwargs['title'] = 'Experiment' |
|
533 | kwargs['title'] = 'Experiment' | |
534 | kwargs['suptitle'] = 'List' |
|
534 | kwargs['suptitle'] = 'List' | |
535 | kwargs['no_sidebar'] = True |
|
535 | kwargs['no_sidebar'] = True | |
536 | kwargs['form'] = form |
|
536 | kwargs['form'] = form | |
537 | filters.pop('page', None) |
|
537 | filters.pop('page', None) | |
538 | kwargs['q'] = urlencode(filters) |
|
538 | kwargs['q'] = urlencode(filters) | |
539 |
|
539 | |||
540 | return render(request, 'base_list.html', kwargs) |
|
540 | return render(request, 'base_list.html', kwargs) | |
541 |
|
541 | |||
542 |
|
542 | |||
543 | def experiment(request, id_exp): |
|
543 | def experiment(request, id_exp): | |
544 |
|
544 | |||
545 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
545 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
546 | experiment.get_status() |
|
546 | #experiment.get_status() | |
547 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
547 | configurations = Configuration.objects.filter(experiment=experiment, type=0) | |
548 |
|
548 | |||
549 | kwargs = {} |
|
549 | kwargs = {} | |
550 |
|
550 | |||
551 | kwargs['experiment_keys'] = ['radar_system', 'name', 'start_time', 'end_time'] |
|
551 | kwargs['experiment_keys'] = ['radar_system', 'name', 'start_time', 'end_time'] | |
552 | kwargs['experiment'] = experiment |
|
552 | kwargs['experiment'] = experiment | |
553 |
|
553 | |||
554 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
554 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] | |
555 | kwargs['configurations'] = configurations |
|
555 | kwargs['configurations'] = configurations | |
556 |
|
556 | |||
557 | kwargs['title'] = 'Experiment' |
|
557 | kwargs['title'] = 'Experiment' | |
558 | kwargs['suptitle'] = 'Details' |
|
558 | kwargs['suptitle'] = 'Details' | |
559 |
|
559 | |||
560 | kwargs['button'] = 'Add Configuration' |
|
560 | kwargs['button'] = 'Add Configuration' | |
561 |
|
561 | |||
562 | ###### SIDEBAR ###### |
|
562 | ###### SIDEBAR ###### | |
563 | kwargs.update(sidebar(experiment=experiment)) |
|
563 | kwargs.update(sidebar(experiment=experiment)) | |
564 |
|
564 | |||
565 | return render(request, 'experiment.html', kwargs) |
|
565 | return render(request, 'experiment.html', kwargs) | |
566 |
|
566 | |||
567 |
|
567 | |||
568 | @user_passes_test(lambda u:u.is_staff) |
|
568 | @user_passes_test(lambda u:u.is_staff) | |
569 | def experiment_new(request, id_camp=None): |
|
569 | def experiment_new(request, id_camp=None): | |
570 |
|
570 | |||
571 | kwargs = {} |
|
571 | kwargs = {} | |
572 |
|
572 | |||
573 | if request.method == 'GET': |
|
573 | if request.method == 'GET': | |
574 | if 'template' in request.GET: |
|
574 | if 'template' in request.GET: | |
575 | if request.GET['template']=='0': |
|
575 | if request.GET['template']=='0': | |
576 | form = NewForm(initial={'create_from':2}, |
|
576 | form = NewForm(initial={'create_from':2}, | |
577 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
577 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) | |
578 | else: |
|
578 | else: | |
579 | kwargs['button'] = 'Create' |
|
579 | kwargs['button'] = 'Create' | |
580 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
580 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) | |
581 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
581 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] | |
582 | exp=Experiment.objects.get(pk=request.GET['template']) |
|
582 | exp=Experiment.objects.get(pk=request.GET['template']) | |
583 | form = ExperimentForm(instance=exp, |
|
583 | form = ExperimentForm(instance=exp, | |
584 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), |
|
584 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), | |
585 | 'template': False}) |
|
585 | 'template': False}) | |
586 | elif 'blank' in request.GET: |
|
586 | elif 'blank' in request.GET: | |
587 | kwargs['button'] = 'Create' |
|
587 | kwargs['button'] = 'Create' | |
588 | form = ExperimentForm() |
|
588 | form = ExperimentForm() | |
589 | else: |
|
589 | else: | |
590 | form = NewForm() |
|
590 | form = NewForm() | |
591 |
|
591 | |||
592 | if request.method == 'POST': |
|
592 | if request.method == 'POST': | |
593 | form = ExperimentForm(request.POST) |
|
593 | form = ExperimentForm(request.POST) | |
594 | if form.is_valid(): |
|
594 | if form.is_valid(): | |
595 | experiment = form.save() |
|
595 | experiment = form.save() | |
596 |
|
596 | |||
597 | if 'template' in request.GET: |
|
597 | if 'template' in request.GET: | |
598 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
598 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) | |
599 | for conf in configurations: |
|
599 | for conf in configurations: | |
600 | conf.clone(experiment=experiment, template=False) |
|
600 | conf.clone(experiment=experiment, template=False) | |
601 |
|
601 | |||
602 | return redirect('url_experiment', id_exp=experiment.id) |
|
602 | return redirect('url_experiment', id_exp=experiment.id) | |
603 |
|
603 | |||
604 | kwargs['form'] = form |
|
604 | kwargs['form'] = form | |
605 | kwargs['title'] = 'Experiment' |
|
605 | kwargs['title'] = 'Experiment' | |
606 | kwargs['suptitle'] = 'New' |
|
606 | kwargs['suptitle'] = 'New' | |
607 |
|
607 | |||
608 | return render(request, 'experiment_edit.html', kwargs) |
|
608 | return render(request, 'experiment_edit.html', kwargs) | |
609 |
|
609 | |||
610 |
|
610 | |||
611 | @user_passes_test(lambda u:u.is_staff) |
|
611 | @user_passes_test(lambda u:u.is_staff) | |
612 | def experiment_edit(request, id_exp): |
|
612 | def experiment_edit(request, id_exp): | |
613 |
|
613 | |||
614 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
614 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
615 |
|
615 | |||
616 | if request.method == 'GET': |
|
616 | if request.method == 'GET': | |
617 | form = ExperimentForm(instance=experiment) |
|
617 | form = ExperimentForm(instance=experiment) | |
618 |
|
618 | |||
619 | if request.method=='POST': |
|
619 | if request.method=='POST': | |
620 | form = ExperimentForm(request.POST, instance=experiment) |
|
620 | form = ExperimentForm(request.POST, instance=experiment) | |
621 |
|
621 | |||
622 | if form.is_valid(): |
|
622 | if form.is_valid(): | |
623 | experiment = form.save() |
|
623 | experiment = form.save() | |
624 | return redirect('url_experiment', id_exp=experiment.id) |
|
624 | return redirect('url_experiment', id_exp=experiment.id) | |
625 |
|
625 | |||
626 | kwargs = {} |
|
626 | kwargs = {} | |
627 | kwargs['form'] = form |
|
627 | kwargs['form'] = form | |
628 | kwargs['title'] = 'Experiment' |
|
628 | kwargs['title'] = 'Experiment' | |
629 | kwargs['suptitle'] = 'Edit' |
|
629 | kwargs['suptitle'] = 'Edit' | |
630 | kwargs['button'] = 'Update' |
|
630 | kwargs['button'] = 'Update' | |
631 |
|
631 | |||
632 | return render(request, 'experiment_edit.html', kwargs) |
|
632 | return render(request, 'experiment_edit.html', kwargs) | |
633 |
|
633 | |||
634 |
|
634 | |||
635 | @user_passes_test(lambda u:u.is_staff) |
|
635 | @user_passes_test(lambda u:u.is_staff) | |
636 | def experiment_delete(request, id_exp): |
|
636 | def experiment_delete(request, id_exp): | |
637 |
|
637 | |||
638 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
638 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
639 |
|
639 | |||
640 | if request.method=='POST': |
|
640 | if request.method=='POST': | |
641 | if request.user.is_staff: |
|
641 | if request.user.is_staff: | |
642 | for conf in Configuration.objects.filter(experiment=experiment): |
|
642 | for conf in Configuration.objects.filter(experiment=experiment): | |
643 | conf.delete() |
|
643 | conf.delete() | |
644 | experiment.delete() |
|
644 | experiment.delete() | |
645 | return redirect('url_experiments') |
|
645 | return redirect('url_experiments') | |
646 |
|
646 | |||
647 | messages.error(request, 'Not enough permission to delete this object') |
|
647 | messages.error(request, 'Not enough permission to delete this object') | |
648 | return redirect(experiment.get_absolute_url()) |
|
648 | return redirect(experiment.get_absolute_url()) | |
649 |
|
649 | |||
650 | kwargs = { |
|
650 | kwargs = { | |
651 | 'title': 'Delete', |
|
651 | 'title': 'Delete', | |
652 | 'suptitle': 'Experiment', |
|
652 | 'suptitle': 'Experiment', | |
653 | 'object': experiment, |
|
653 | 'object': experiment, | |
654 | 'previous': experiment.get_absolute_url(), |
|
654 | 'previous': experiment.get_absolute_url(), | |
655 | 'delete': True |
|
655 | 'delete': True | |
656 | } |
|
656 | } | |
657 |
|
657 | |||
658 | return render(request, 'confirm.html', kwargs) |
|
658 | return render(request, 'confirm.html', kwargs) | |
659 |
|
659 | |||
660 |
|
660 | |||
661 | @user_passes_test(lambda u:u.is_staff) |
|
661 | @user_passes_test(lambda u:u.is_staff) | |
662 | def experiment_export(request, id_exp): |
|
662 | def experiment_export(request, id_exp): | |
663 |
|
663 | |||
664 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
664 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
665 | content = experiment.parms_to_dict() |
|
665 | content = experiment.parms_to_dict() | |
666 | content_type = 'application/json' |
|
666 | content_type = 'application/json' | |
667 | filename = '%s_%s.json' %(experiment.name, experiment.id) |
|
667 | filename = '%s_%s.json' %(experiment.name, experiment.id) | |
668 |
|
668 | |||
669 | response = HttpResponse(content_type=content_type) |
|
669 | response = HttpResponse(content_type=content_type) | |
670 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
670 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename | |
671 | response.write(content) |
|
671 | response.write(content) | |
672 |
|
672 | |||
673 | return response |
|
673 | return response | |
674 |
|
674 | |||
675 |
|
675 | |||
676 | @user_passes_test(lambda u:u.is_staff) |
|
676 | @user_passes_test(lambda u:u.is_staff) | |
677 | def experiment_import(request, id_exp): |
|
677 | def experiment_import(request, id_exp): | |
678 |
|
678 | |||
679 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
679 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
680 | configurations = Configuration.objects.filter(experiment=experiment) |
|
680 | configurations = Configuration.objects.filter(experiment=experiment) | |
681 |
|
681 | |||
682 | if request.method == 'GET': |
|
682 | if request.method == 'GET': | |
683 | file_form = UploadFileForm() |
|
683 | file_form = UploadFileForm() | |
684 |
|
684 | |||
685 | if request.method == 'POST': |
|
685 | if request.method == 'POST': | |
686 | file_form = UploadFileForm(request.POST, request.FILES) |
|
686 | file_form = UploadFileForm(request.POST, request.FILES) | |
687 |
|
687 | |||
688 | if file_form.is_valid(): |
|
688 | if file_form.is_valid(): | |
689 |
|
689 | |||
690 | parms = experiment.import_from_file(request.FILES['file']) |
|
690 | parms = experiment.import_from_file(request.FILES['file']) | |
691 |
|
691 | |||
692 | if parms: |
|
692 | if parms: | |
693 |
|
693 | |||
694 | new_exp = experiment.dict_to_parms(parms, CONF_MODELS) |
|
694 | new_exp = experiment.dict_to_parms(parms, CONF_MODELS) | |
695 |
|
695 | |||
696 | if new_exp.__class__.__name__=='dict': |
|
696 | if new_exp.__class__.__name__=='dict': | |
697 | messages.error(request, new_exp['Error'] ) |
|
697 | messages.error(request, new_exp['Error'] ) | |
698 | return redirect(experiment.get_absolute_url_import()) |
|
698 | return redirect(experiment.get_absolute_url_import()) | |
699 |
|
699 | |||
700 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
700 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) | |
701 |
|
701 | |||
702 | return redirect(new_exp.get_absolute_url_edit()) |
|
702 | return redirect(new_exp.get_absolute_url_edit()) | |
703 |
|
703 | |||
704 | messages.error(request, "Could not import parameters from file") |
|
704 | messages.error(request, "Could not import parameters from file") | |
705 |
|
705 | |||
706 | kwargs = {} |
|
706 | kwargs = {} | |
707 | kwargs['title'] = 'Experiment' |
|
707 | kwargs['title'] = 'Experiment' | |
708 | kwargs['form'] = file_form |
|
708 | kwargs['form'] = file_form | |
709 | kwargs['suptitle'] = 'Importing file' |
|
709 | kwargs['suptitle'] = 'Importing file' | |
710 | kwargs['button'] = 'Import' |
|
710 | kwargs['button'] = 'Import' | |
711 |
|
711 | |||
712 | kwargs.update(sidebar(experiment=experiment)) |
|
712 | kwargs.update(sidebar(experiment=experiment)) | |
713 |
|
713 | |||
714 | return render(request, 'experiment_import.html', kwargs) |
|
714 | return render(request, 'experiment_import.html', kwargs) | |
715 |
|
715 | |||
716 |
|
716 | |||
717 | @user_passes_test(lambda u:u.is_staff) |
|
717 | @user_passes_test(lambda u:u.is_staff) | |
718 | def experiment_start(request, id_exp): |
|
718 | def experiment_start(request, id_exp): | |
719 |
|
719 | |||
720 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
720 | exp = get_object_or_404(Experiment, pk=id_exp) | |
721 |
|
721 | |||
722 | if exp.status == 2: |
|
722 | confs = Configuration.objects.filter(experiment=exp, type=0) | |
723 | messages.warning(request, 'Experiment {} already running'.format(exp)) |
|
723 | if not confs: | |
|
724 | messages.error(request, 'Experiment not running. No Device Configurations detected.') | |||
|
725 | return redirect(exp.get_absolute_url()) | |||
724 |
|
726 | |||
725 | elif exp.status == 3: |
|
727 | for conf in confs: | |
726 | messages.warning(request, 'Experiment {} already programmed'.format(exp)) |
|
728 | conf.status_device() | |
|
729 | if conf.device.status != 2: | |||
|
730 | messages.error(request, 'Experiment not running. Configuration {} state: '.format(conf) + DEV_STATES[conf.device.status][1]) | |||
|
731 | return redirect(exp.get_absolute_url()) | |||
727 |
|
732 | |||
728 | if not (exp.start_time < datetime.now().time() < exp.end_time): |
|
733 | exp.get_status() | |
729 | exp.status==1 |
|
|||
730 | exp.save() |
|
|||
731 | messages.warning(request, 'Experiment {} is out of time'.format(exp)) |
|
|||
732 | return redirect(exp.get_absolute_url()) |
|
|||
733 |
|
734 | |||
|
735 | if exp.status == 2: | |||
|
736 | messages.warning(request, 'Experiment {} already running'.format(exp)) | |||
734 | else: |
|
737 | else: | |
735 | task = task_start.delay(exp.pk) |
|
738 | task = task_start.delay(exp.pk) | |
736 | exp.status = task.wait() |
|
739 | exp.status = task.wait() | |
737 | if exp.status==0: |
|
740 | if exp.status==0: | |
738 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
741 | messages.error(request, 'Experiment {} not start'.format(exp)) | |
739 | if exp.status==2: |
|
742 | if exp.status==2: | |
740 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
743 | messages.success(request, 'Experiment {} started'.format(exp)) | |
741 |
|
744 | exp.save() | ||
742 | exp.save() |
|
|||
743 |
|
745 | |||
744 | return redirect(exp.get_absolute_url()) |
|
746 | return redirect(exp.get_absolute_url()) | |
745 |
|
747 | |||
746 |
|
748 | |||
747 | @user_passes_test(lambda u:u.is_staff) |
|
749 | @user_passes_test(lambda u:u.is_staff) | |
748 | def experiment_stop(request, id_exp): |
|
750 | def experiment_stop(request, id_exp): | |
749 |
|
751 | |||
750 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
752 | exp = get_object_or_404(Experiment, pk=id_exp) | |
|
753 | confs = Configuration.objects.filter(experiment=exp, type=0) | |||
|
754 | if not confs: | |||
|
755 | messages.error(request, 'No Device Configurations detected.') | |||
|
756 | return redirect(exp.get_absolute_url()) | |||
751 |
|
757 | |||
|
758 | exp.get_status() | |||
|
759 | ||||
752 | if exp.status == 2: |
|
760 | if exp.status == 2: | |
753 | task = task_stop.delay(exp.pk) |
|
761 | task = task_stop.delay(exp.pk) | |
754 | exp.status = task.wait() |
|
762 | exp.status = task.wait() | |
755 | messages.warning(request, 'Experiment {} stopped'.format(exp)) |
|
763 | messages.warning(request, 'Experiment {} stopped'.format(exp)) | |
756 | exp.save() |
|
764 | exp.save() | |
757 | else: |
|
765 | else: | |
758 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
766 | messages.error(request, 'Experiment {} not running'.format(exp)) | |
759 |
|
767 | |||
760 | return redirect(exp.get_absolute_url()) |
|
768 | return redirect(exp.get_absolute_url()) | |
761 |
|
769 | |||
762 |
|
770 | |||
763 | @user_passes_test(lambda u:u.is_staff) |
|
771 | @user_passes_test(lambda u:u.is_staff) | |
764 | def experiment_mix(request, id_exp): |
|
772 | def experiment_mix(request, id_exp): | |
765 |
|
773 | |||
766 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
774 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
767 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, |
|
775 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, | |
768 | mix=False)] |
|
776 | mix=False)] | |
769 |
|
777 | |||
770 | if len(rc_confs)<2: |
|
778 | if len(rc_confs)<2: | |
771 | messages.warning(request, 'You need at least two RC Configurations to make a mix') |
|
779 | messages.warning(request, 'You need at least two RC Configurations to make a mix') | |
772 | return redirect(experiment.get_absolute_url()) |
|
780 | return redirect(experiment.get_absolute_url()) | |
773 |
|
781 | |||
774 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) |
|
782 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) | |
775 |
|
783 | |||
776 | if mix_confs: |
|
784 | if mix_confs: | |
777 | mix = mix_confs[0] |
|
785 | mix = mix_confs[0] | |
778 | else: |
|
786 | else: | |
779 | mix = RCConfiguration(experiment=experiment, |
|
787 | mix = RCConfiguration(experiment=experiment, | |
780 | device=rc_confs[0].device, |
|
788 | device=rc_confs[0].device, | |
781 | ipp=rc_confs[0].ipp, |
|
789 | ipp=rc_confs[0].ipp, | |
782 | clock_in=rc_confs[0].clock_in, |
|
790 | clock_in=rc_confs[0].clock_in, | |
783 | clock_divider=rc_confs[0].clock_divider, |
|
791 | clock_divider=rc_confs[0].clock_divider, | |
784 | mix=True, |
|
792 | mix=True, | |
785 | parameters='') |
|
793 | parameters='') | |
786 | mix.save() |
|
794 | mix.save() | |
787 |
|
795 | |||
788 | line_type = RCLineType.objects.get(name='mix') |
|
796 | line_type = RCLineType.objects.get(name='mix') | |
789 | for i in range(len(rc_confs[0].get_lines())): |
|
797 | for i in range(len(rc_confs[0].get_lines())): | |
790 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
798 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) | |
791 | line.save() |
|
799 | line.save() | |
792 |
|
800 | |||
793 | initial = {'name': mix.name, |
|
801 | initial = {'name': mix.name, | |
794 | 'result': parse_mix_result(mix.parameters), |
|
802 | 'result': parse_mix_result(mix.parameters), | |
795 | 'delay': 0, |
|
803 | 'delay': 0, | |
796 | 'mask': [0,1,2,3,4,5,6,7] |
|
804 | 'mask': [0,1,2,3,4,5,6,7] | |
797 | } |
|
805 | } | |
798 |
|
806 | |||
799 | if request.method=='GET': |
|
807 | if request.method=='GET': | |
800 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
808 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) | |
801 |
|
809 | |||
802 | if request.method=='POST': |
|
810 | if request.method=='POST': | |
803 | result = mix.parameters |
|
811 | result = mix.parameters | |
804 |
|
812 | |||
805 | if '{}|'.format(request.POST['experiment']) in result: |
|
813 | if '{}|'.format(request.POST['experiment']) in result: | |
806 | messages.error(request, 'Configuration already added') |
|
814 | messages.error(request, 'Configuration already added') | |
807 | else: |
|
815 | else: | |
808 | if 'operation' in request.POST: |
|
816 | if 'operation' in request.POST: | |
809 | operation = MIX_OPERATIONS[request.POST['operation']] |
|
817 | operation = MIX_OPERATIONS[request.POST['operation']] | |
810 | else: |
|
818 | else: | |
811 | operation = ' ' |
|
819 | operation = ' ' | |
812 |
|
820 | |||
813 | mode = MIX_MODES[request.POST['mode']] |
|
821 | mode = MIX_MODES[request.POST['mode']] | |
814 |
|
822 | |||
815 | if result: |
|
823 | if result: | |
816 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, |
|
824 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, | |
817 | request.POST['experiment'], |
|
825 | request.POST['experiment'], | |
818 | mode, |
|
826 | mode, | |
819 | operation, |
|
827 | operation, | |
820 | float(request.POST['delay']), |
|
828 | float(request.POST['delay']), | |
821 | parse_mask(request.POST.getlist('mask')) |
|
829 | parse_mask(request.POST.getlist('mask')) | |
822 | ) |
|
830 | ) | |
823 | else: |
|
831 | else: | |
824 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], |
|
832 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], | |
825 | mode, |
|
833 | mode, | |
826 | operation, |
|
834 | operation, | |
827 | float(request.POST['delay']), |
|
835 | float(request.POST['delay']), | |
828 | parse_mask(request.POST.getlist('mask')) |
|
836 | parse_mask(request.POST.getlist('mask')) | |
829 | ) |
|
837 | ) | |
830 |
|
838 | |||
831 | mix.parameters = result |
|
839 | mix.parameters = result | |
832 | mix.name = request.POST['name'] |
|
840 | mix.name = request.POST['name'] | |
833 | mix.save() |
|
841 | mix.save() | |
834 | mix.update_pulses() |
|
842 | mix.update_pulses() | |
835 |
|
843 | |||
836 | initial['result'] = parse_mix_result(result) |
|
844 | initial['result'] = parse_mix_result(result) | |
837 | initial['name'] = mix.name |
|
845 | initial['name'] = mix.name | |
838 |
|
846 | |||
839 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
847 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) | |
840 |
|
848 | |||
841 |
|
849 | |||
842 | kwargs = { |
|
850 | kwargs = { | |
843 | 'title': 'Experiment', |
|
851 | 'title': 'Experiment', | |
844 | 'suptitle': 'Mix Configurations', |
|
852 | 'suptitle': 'Mix Configurations', | |
845 | 'form' : form, |
|
853 | 'form' : form, | |
846 | 'extra_button': 'Delete', |
|
854 | 'extra_button': 'Delete', | |
847 | 'button': 'Add', |
|
855 | 'button': 'Add', | |
848 | 'cancel': 'Back', |
|
856 | 'cancel': 'Back', | |
849 | 'previous': experiment.get_absolute_url(), |
|
857 | 'previous': experiment.get_absolute_url(), | |
850 | 'id_exp':id_exp, |
|
858 | 'id_exp':id_exp, | |
851 |
|
859 | |||
852 | } |
|
860 | } | |
853 |
|
861 | |||
854 | return render(request, 'experiment_mix.html', kwargs) |
|
862 | return render(request, 'experiment_mix.html', kwargs) | |
855 |
|
863 | |||
856 |
|
864 | |||
857 | @user_passes_test(lambda u:u.is_staff) |
|
865 | @user_passes_test(lambda u:u.is_staff) | |
858 | def experiment_mix_delete(request, id_exp): |
|
866 | def experiment_mix_delete(request, id_exp): | |
859 |
|
867 | |||
860 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) |
|
868 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) | |
861 | values = conf.parameters.split('-') |
|
869 | values = conf.parameters.split('-') | |
862 | conf.parameters = '-'.join(values[:-1]) |
|
870 | conf.parameters = '-'.join(values[:-1]) | |
863 | conf.save() |
|
871 | conf.save() | |
864 |
|
872 | |||
865 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
873 | return redirect('url_mix_experiment', id_exp=id_exp) | |
866 |
|
874 | |||
867 |
|
875 | |||
868 | def experiment_summary(request, id_exp): |
|
876 | def experiment_summary(request, id_exp): | |
869 |
|
877 | |||
870 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
878 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
871 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
879 | configurations = Configuration.objects.filter(experiment=experiment, type=0) | |
872 |
|
880 | |||
873 | kwargs = {} |
|
881 | kwargs = {} | |
874 |
|
882 | |||
875 | kwargs['experiment_keys'] = ['radar_system', 'name', 'start_time', 'end_time'] |
|
883 | kwargs['experiment_keys'] = ['radar_system', 'name', 'start_time', 'end_time'] | |
876 | kwargs['experiment'] = experiment |
|
884 | kwargs['experiment'] = experiment | |
877 |
|
885 | |||
878 | kwargs['configurations'] = [] |
|
886 | kwargs['configurations'] = [] | |
879 |
|
887 | |||
880 | kwargs['title'] = 'Experiment Summary' |
|
888 | kwargs['title'] = 'Experiment Summary' | |
881 | kwargs['suptitle'] = 'Details' |
|
889 | kwargs['suptitle'] = 'Details' | |
882 |
|
890 | |||
883 | kwargs['button'] = 'Verify Parameters' |
|
891 | kwargs['button'] = 'Verify Parameters' | |
884 |
|
892 | |||
885 | jars_conf = False |
|
893 | jars_conf = False | |
886 | rc_conf = False |
|
894 | rc_conf = False | |
887 |
|
895 | |||
888 | for configuration in configurations: |
|
896 | for configuration in configurations: | |
889 |
|
897 | |||
890 | #--------------------- RC ----------------------: |
|
898 | #--------------------- RC ----------------------: | |
891 | if configuration.device.device_type.name == 'rc': |
|
899 | if configuration.device.device_type.name == 'rc': | |
892 | if configuration.mix: |
|
900 | if configuration.mix: | |
893 | continue |
|
901 | continue | |
894 | rc_conf = True |
|
902 | rc_conf = True | |
895 | ipp = configuration.ipp |
|
903 | ipp = configuration.ipp | |
896 | lines = configuration.get_lines(line_type__name='tx') |
|
904 | lines = configuration.get_lines(line_type__name='tx') | |
897 | configuration.tx_lines = [] |
|
905 | configuration.tx_lines = [] | |
898 |
|
906 | |||
899 | for tx_line in lines: |
|
907 | for tx_line in lines: | |
900 | line = {'name':tx_line.get_name()} |
|
908 | line = {'name':tx_line.get_name()} | |
901 | tx_params = json.loads(tx_line.params) |
|
909 | tx_params = json.loads(tx_line.params) | |
902 | line['width'] = tx_params['pulse_width'] |
|
910 | line['width'] = tx_params['pulse_width'] | |
903 | if line['width'] in (0, '0'): |
|
911 | if line['width'] in (0, '0'): | |
904 | continue |
|
912 | continue | |
905 | delays = tx_params['delays'] |
|
913 | delays = tx_params['delays'] | |
906 |
|
914 | |||
907 | if delays not in ('', '0'): |
|
915 | if delays not in ('', '0'): | |
908 | n = len(delays.split(',')) |
|
916 | n = len(delays.split(',')) | |
909 | line['taus'] = '{} Taus: {}'.format(n, delays) |
|
917 | line['taus'] = '{} Taus: {}'.format(n, delays) | |
910 | else: |
|
918 | else: | |
911 | line['taus'] = '-' |
|
919 | line['taus'] = '-' | |
912 |
|
920 | |||
913 | for code_line in configuration.get_lines(line_type__name='codes'): |
|
921 | for code_line in configuration.get_lines(line_type__name='codes'): | |
914 | code_params = json.loads(code_line.params) |
|
922 | code_params = json.loads(code_line.params) | |
915 | if tx_line.pk==int(code_params['TX_ref']): |
|
923 | if tx_line.pk==int(code_params['TX_ref']): | |
916 | line['codes'] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), |
|
924 | line['codes'] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), | |
917 | '-'.join(code_params['codes'])) |
|
925 | '-'.join(code_params['codes'])) | |
918 |
|
926 | |||
919 | for windows_line in configuration.get_lines(line_type__name='windows'): |
|
927 | for windows_line in configuration.get_lines(line_type__name='windows'): | |
920 | win_params = json.loads(windows_line.params) |
|
928 | win_params = json.loads(windows_line.params) | |
921 | if tx_line.pk==int(win_params['TX_ref']): |
|
929 | if tx_line.pk==int(win_params['TX_ref']): | |
922 | windows = '' |
|
930 | windows = '' | |
923 | for i, params in enumerate(win_params['params']): |
|
931 | for i, params in enumerate(win_params['params']): | |
924 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format(i, **params) |
|
932 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format(i, **params) | |
925 | line['windows'] = mark_safe(windows) |
|
933 | line['windows'] = mark_safe(windows) | |
926 |
|
934 | |||
927 | configuration.tx_lines.append(line) |
|
935 | configuration.tx_lines.append(line) | |
928 |
|
936 | |||
929 | #-------------------- JARS -----------------------: |
|
937 | #-------------------- JARS -----------------------: | |
930 | if configuration.device.device_type.name == 'jars': |
|
938 | if configuration.device.device_type.name == 'jars': | |
931 | jars_conf = True |
|
939 | jars_conf = True | |
932 | kwargs['exp_type'] = EXPERIMENT_TYPE[configuration.exp_type][1] |
|
940 | kwargs['exp_type'] = EXPERIMENT_TYPE[configuration.exp_type][1] | |
933 | channels_number = configuration.channels_number |
|
941 | channels_number = configuration.channels_number | |
934 | exp_type = configuration.exp_type |
|
942 | exp_type = configuration.exp_type | |
935 | fftpoints = configuration.fftpoints |
|
943 | fftpoints = configuration.fftpoints | |
936 | filter_parms = configuration.filter_parms |
|
944 | filter_parms = configuration.filter_parms | |
937 | filter_parms = ast.literal_eval(filter_parms) |
|
945 | filter_parms = ast.literal_eval(filter_parms) | |
938 | spectral_number = configuration.spectral_number |
|
946 | spectral_number = configuration.spectral_number | |
939 |
|
947 | |||
940 |
|
948 | |||
941 | kwargs['configurations'].append(configuration) |
|
949 | kwargs['configurations'].append(configuration) | |
942 |
|
950 | |||
943 |
|
951 | |||
944 | #------ RC & JARS ------: |
|
952 | #------ RC & JARS ------: | |
945 | ipp = 937.5 # |
|
953 | ipp = 937.5 # | |
946 | nsa = 200# |
|
954 | nsa = 200# | |
947 | dh = 1.5 # |
|
955 | dh = 1.5 # | |
948 | channels_number = 5 # |
|
956 | channels_number = 5 # | |
949 |
|
957 | |||
950 | if rc_conf and jars_conf: |
|
958 | if rc_conf and jars_conf: | |
951 | if exp_type == 0: #Short |
|
959 | if exp_type == 0: #Short | |
952 | bytes = 2 |
|
960 | bytes = 2 | |
953 | b = nsa*2*bytes*channels_number |
|
961 | b = nsa*2*bytes*channels_number | |
954 | else: #Float |
|
962 | else: #Float | |
955 | bytes = 4 |
|
963 | bytes = 4 | |
956 | channels = channels_number + spectral_number |
|
964 | channels = channels_number + spectral_number | |
957 | b = nsa*2*bytes*fftpoints*channels |
|
965 | b = nsa*2*bytes*fftpoints*channels | |
958 |
|
966 | |||
959 | ipps = (ipp*pow(10,-6))/0.15 |
|
967 | ipps = (ipp*pow(10,-6))/0.15 | |
960 | GB = 1048576.0*1024.0 |
|
968 | GB = 1048576.0*1024.0 | |
961 | Hour = 3600 |
|
969 | Hour = 3600 | |
962 | rate = b/ipps |
|
970 | rate = b/ipps | |
963 | rate = rate *(1/GB)*(Hour) |
|
971 | rate = rate *(1/GB)*(Hour) | |
964 | kwargs['rate'] = str(rate)+" GB/h" |
|
972 | kwargs['rate'] = str(rate)+" GB/h" | |
965 | else: |
|
973 | else: | |
966 | kwargs['rate'] = '' |
|
974 | kwargs['rate'] = '' | |
967 |
|
975 | |||
968 | ###### SIDEBAR ###### |
|
976 | ###### SIDEBAR ###### | |
969 | kwargs.update(sidebar(experiment=experiment)) |
|
977 | kwargs.update(sidebar(experiment=experiment)) | |
970 |
|
978 | |||
971 | return render(request, 'experiment_summary.html', kwargs) |
|
979 | return render(request, 'experiment_summary.html', kwargs) | |
972 |
|
980 | |||
973 |
|
981 | |||
974 | @user_passes_test(lambda u:u.is_staff) |
|
982 | @user_passes_test(lambda u:u.is_staff) | |
975 | def experiment_verify(request, id_exp): |
|
983 | def experiment_verify(request, id_exp): | |
976 |
|
984 | |||
977 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
985 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
978 | experiment_data = json.loads(experiment.parms_to_dict()) |
|
986 | experiment_data = json.loads(experiment.parms_to_dict()) | |
979 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
987 | configurations = Configuration.objects.filter(experiment=experiment, type=0) | |
980 |
|
988 | |||
981 | kwargs = {} |
|
989 | kwargs = {} | |
982 |
|
990 | |||
983 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'start_time', 'end_time'] |
|
991 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'start_time', 'end_time'] | |
984 | kwargs['experiment'] = experiment |
|
992 | kwargs['experiment'] = experiment | |
985 |
|
993 | |||
986 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
994 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] | |
987 | kwargs['configurations'] = configurations |
|
995 | kwargs['configurations'] = configurations | |
988 | kwargs['experiment_data'] = experiment_data |
|
996 | kwargs['experiment_data'] = experiment_data | |
989 |
|
997 | |||
990 | kwargs['title'] = 'Verify Experiment' |
|
998 | kwargs['title'] = 'Verify Experiment' | |
991 | kwargs['suptitle'] = 'Parameters' |
|
999 | kwargs['suptitle'] = 'Parameters' | |
992 |
|
1000 | |||
993 | kwargs['button'] = 'Update' |
|
1001 | kwargs['button'] = 'Update' | |
994 |
|
1002 | |||
995 | jars_conf = False |
|
1003 | jars_conf = False | |
996 | rc_conf = False |
|
1004 | rc_conf = False | |
997 | dds_conf = False |
|
1005 | dds_conf = False | |
998 |
|
1006 | |||
999 | for configuration in configurations: |
|
1007 | for configuration in configurations: | |
1000 | #-------------------- JARS -----------------------: |
|
1008 | #-------------------- JARS -----------------------: | |
1001 | if configuration.device.device_type.name == 'jars': |
|
1009 | if configuration.device.device_type.name == 'jars': | |
1002 | jars_conf = True |
|
1010 | jars_conf = True | |
1003 | jars = configuration |
|
1011 | jars = configuration | |
1004 | kwargs['jars_conf'] = jars_conf |
|
1012 | kwargs['jars_conf'] = jars_conf | |
1005 | filter_parms = configuration.filter_parms |
|
1013 | filter_parms = configuration.filter_parms | |
1006 | filter_parms = ast.literal_eval(filter_parms) |
|
1014 | filter_parms = ast.literal_eval(filter_parms) | |
1007 | kwargs['filter_parms'] = filter_parms |
|
1015 | kwargs['filter_parms'] = filter_parms | |
1008 | #--Sampling Frequency |
|
1016 | #--Sampling Frequency | |
1009 | clock = filter_parms['clock'] |
|
1017 | clock = filter_parms['clock'] | |
1010 | filter_2 = filter_parms['filter_2'] |
|
1018 | filter_2 = filter_parms['filter_2'] | |
1011 | filter_5 = filter_parms['filter_5'] |
|
1019 | filter_5 = filter_parms['filter_5'] | |
1012 | filter_fir = filter_parms['filter_fir'] |
|
1020 | filter_fir = filter_parms['filter_fir'] | |
1013 | samp_freq_jars = clock/filter_2/filter_5/filter_fir |
|
1021 | samp_freq_jars = clock/filter_2/filter_5/filter_fir | |
1014 |
|
1022 | |||
1015 | kwargs['samp_freq_jars'] = samp_freq_jars |
|
1023 | kwargs['samp_freq_jars'] = samp_freq_jars | |
1016 | kwargs['jars'] = configuration |
|
1024 | kwargs['jars'] = configuration | |
1017 |
|
1025 | |||
1018 | #--------------------- RC ----------------------: |
|
1026 | #--------------------- RC ----------------------: | |
1019 | if configuration.device.device_type.name == 'rc' and not configuration.mix: |
|
1027 | if configuration.device.device_type.name == 'rc' and not configuration.mix: | |
1020 | rc_conf = True |
|
1028 | rc_conf = True | |
1021 | rc = configuration |
|
1029 | rc = configuration | |
1022 |
|
1030 | |||
1023 | rc_parms = configuration.parms_to_dict() |
|
1031 | rc_parms = configuration.parms_to_dict() | |
1024 |
|
1032 | |||
1025 | win_lines = rc.get_lines(line_type__name='windows') |
|
1033 | win_lines = rc.get_lines(line_type__name='windows') | |
1026 | if win_lines: |
|
1034 | if win_lines: | |
1027 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] |
|
1035 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] | |
1028 | #--Sampling Frequency |
|
1036 | #--Sampling Frequency | |
1029 | samp_freq_rc = 0.15/dh |
|
1037 | samp_freq_rc = 0.15/dh | |
1030 | kwargs['samp_freq_rc'] = samp_freq_rc |
|
1038 | kwargs['samp_freq_rc'] = samp_freq_rc | |
1031 |
|
1039 | |||
1032 | kwargs['rc_conf'] = rc_conf |
|
1040 | kwargs['rc_conf'] = rc_conf | |
1033 | kwargs['rc'] = configuration |
|
1041 | kwargs['rc'] = configuration | |
1034 |
|
1042 | |||
1035 | #-------------------- DDS ----------------------: |
|
1043 | #-------------------- DDS ----------------------: | |
1036 | if configuration.device.device_type.name == 'dds': |
|
1044 | if configuration.device.device_type.name == 'dds': | |
1037 | dds_conf = True |
|
1045 | dds_conf = True | |
1038 | dds = configuration |
|
1046 | dds = configuration | |
1039 | dds_parms = configuration.parms_to_dict() |
|
1047 | dds_parms = configuration.parms_to_dict() | |
1040 |
|
1048 | |||
1041 | kwargs['dds_conf'] = dds_conf |
|
1049 | kwargs['dds_conf'] = dds_conf | |
1042 | kwargs['dds'] = configuration |
|
1050 | kwargs['dds'] = configuration | |
1043 |
|
1051 | |||
1044 |
|
1052 | |||
1045 | #------------Validation------------: |
|
1053 | #------------Validation------------: | |
1046 | #Clock |
|
1054 | #Clock | |
1047 | if dds_conf and rc_conf and jars_conf: |
|
1055 | if dds_conf and rc_conf and jars_conf: | |
1048 | if float(filter_parms['clock']) != float(rc_parms['clock_in']) and float(rc_parms['clock_in']) != float(dds_parms['clock']): |
|
1056 | if float(filter_parms['clock']) != float(rc_parms['clock_in']) and float(rc_parms['clock_in']) != float(dds_parms['clock']): | |
1049 | messages.warning(request, "Devices don't have the same clock.") |
|
1057 | messages.warning(request, "Devices don't have the same clock.") | |
1050 | elif rc_conf and jars_conf: |
|
1058 | elif rc_conf and jars_conf: | |
1051 | if float(filter_parms['clock']) != float(rc_parms['clock_in']): |
|
1059 | if float(filter_parms['clock']) != float(rc_parms['clock_in']): | |
1052 | messages.warning(request, "Devices don't have the same clock.") |
|
1060 | messages.warning(request, "Devices don't have the same clock.") | |
1053 | elif rc_conf and dds_conf: |
|
1061 | elif rc_conf and dds_conf: | |
1054 | if float(rc_parms['clock_in']) != float(dds_parms['clock']): |
|
1062 | if float(rc_parms['clock_in']) != float(dds_parms['clock']): | |
1055 | messages.warning(request, "Devices don't have the same clock.") |
|
1063 | messages.warning(request, "Devices don't have the same clock.") | |
1056 | if float(samp_freq_rc) != float(dds_parms['frequencyA']): |
|
1064 | if float(samp_freq_rc) != float(dds_parms['frequencyA']): | |
1057 | messages.warning(request, "Devices don't have the same Frequency A.") |
|
1065 | messages.warning(request, "Devices don't have the same Frequency A.") | |
1058 |
|
1066 | |||
1059 |
|
1067 | |||
1060 | #------------POST METHOD------------: |
|
1068 | #------------POST METHOD------------: | |
1061 | if request.method == 'POST': |
|
1069 | if request.method == 'POST': | |
1062 | if request.POST['suggest_clock']: |
|
1070 | if request.POST['suggest_clock']: | |
1063 | try: |
|
1071 | try: | |
1064 | suggest_clock = float(request.POST['suggest_clock']) |
|
1072 | suggest_clock = float(request.POST['suggest_clock']) | |
1065 | except: |
|
1073 | except: | |
1066 | messages.warning(request, "Invalid value in CLOCK IN.") |
|
1074 | messages.warning(request, "Invalid value in CLOCK IN.") | |
1067 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1075 | return redirect('url_verify_experiment', id_exp=experiment.id) | |
1068 | else: |
|
1076 | else: | |
1069 | suggest_clock = "" |
|
1077 | suggest_clock = "" | |
1070 | if suggest_clock: |
|
1078 | if suggest_clock: | |
1071 | if rc_conf: |
|
1079 | if rc_conf: | |
1072 | rc.clock_in = suggest_clock |
|
1080 | rc.clock_in = suggest_clock | |
1073 | rc.save() |
|
1081 | rc.save() | |
1074 | if jars_conf: |
|
1082 | if jars_conf: | |
1075 | filter_parms = jars.filter_parms |
|
1083 | filter_parms = jars.filter_parms | |
1076 | filter_parms = ast.literal_eval(filter_parms) |
|
1084 | filter_parms = ast.literal_eval(filter_parms) | |
1077 | filter_parms['clock'] = suggest_clock |
|
1085 | filter_parms['clock'] = suggest_clock | |
1078 | jars.filter_parms = json.dumps(filter_parms) |
|
1086 | jars.filter_parms = json.dumps(filter_parms) | |
1079 | jars.save() |
|
1087 | jars.save() | |
1080 | kwargs['filter_parms'] = filter_parms |
|
1088 | kwargs['filter_parms'] = filter_parms | |
1081 | if dds_conf: |
|
1089 | if dds_conf: | |
1082 | dds.clock = suggest_clock |
|
1090 | dds.clock = suggest_clock | |
1083 | dds.save() |
|
1091 | dds.save() | |
1084 |
|
1092 | |||
1085 | if request.POST['suggest_frequencyA']: |
|
1093 | if request.POST['suggest_frequencyA']: | |
1086 | try: |
|
1094 | try: | |
1087 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) |
|
1095 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) | |
1088 | except: |
|
1096 | except: | |
1089 | messages.warning(request, "Invalid value in FREQUENCY A.") |
|
1097 | messages.warning(request, "Invalid value in FREQUENCY A.") | |
1090 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1098 | return redirect('url_verify_experiment', id_exp=experiment.id) | |
1091 | else: |
|
1099 | else: | |
1092 | suggest_frequencyA = "" |
|
1100 | suggest_frequencyA = "" | |
1093 | if suggest_frequencyA: |
|
1101 | if suggest_frequencyA: | |
1094 | if jars_conf: |
|
1102 | if jars_conf: | |
1095 | filter_parms = jars.filter_parms |
|
1103 | filter_parms = jars.filter_parms | |
1096 | filter_parms = ast.literal_eval(filter_parms) |
|
1104 | filter_parms = ast.literal_eval(filter_parms) | |
1097 | filter_parms['fch'] = suggest_frequencyA |
|
1105 | filter_parms['fch'] = suggest_frequencyA | |
1098 | jars.filter_parms = json.dumps(filter_parms) |
|
1106 | jars.filter_parms = json.dumps(filter_parms) | |
1099 | jars.save() |
|
1107 | jars.save() | |
1100 | kwargs['filter_parms'] = filter_parms |
|
1108 | kwargs['filter_parms'] = filter_parms | |
1101 | if dds_conf: |
|
1109 | if dds_conf: | |
1102 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] |
|
1110 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] | |
1103 | dds.save() |
|
1111 | dds.save() | |
1104 |
|
1112 | |||
1105 | ###### SIDEBAR ###### |
|
1113 | ###### SIDEBAR ###### | |
1106 | kwargs.update(sidebar(experiment=experiment)) |
|
1114 | kwargs.update(sidebar(experiment=experiment)) | |
1107 |
|
1115 | |||
1108 |
|
1116 | |||
1109 |
|
1117 | |||
1110 |
|
1118 | |||
1111 |
|
1119 | |||
1112 | return render(request, 'experiment_verify.html', kwargs) |
|
1120 | return render(request, 'experiment_verify.html', kwargs) | |
1113 |
|
1121 | |||
1114 |
|
1122 | |||
1115 | #@user_passes_test(lambda u:u.is_staff) |
|
1123 | #@user_passes_test(lambda u:u.is_staff) | |
1116 | def parse_mix_result(s): |
|
1124 | def parse_mix_result(s): | |
1117 |
|
1125 | |||
1118 | values = s.split('-') |
|
1126 | values = s.split('-') | |
1119 | html = 'EXP MOD OPE DELAY MASK\r\n' |
|
1127 | html = 'EXP MOD OPE DELAY MASK\r\n' | |
1120 |
|
1128 | |||
1121 | if not values or values[0] in ('', ' '): |
|
1129 | if not values or values[0] in ('', ' '): | |
1122 | return mark_safe(html) |
|
1130 | return mark_safe(html) | |
1123 |
|
1131 | |||
1124 | for i, value in enumerate(values): |
|
1132 | for i, value in enumerate(values): | |
1125 | if not value: |
|
1133 | if not value: | |
1126 | continue |
|
1134 | continue | |
1127 | pk, mode, operation, delay, mask = value.split('|') |
|
1135 | pk, mode, operation, delay, mask = value.split('|') | |
1128 | conf = RCConfiguration.objects.get(pk=pk) |
|
1136 | conf = RCConfiguration.objects.get(pk=pk) | |
1129 | if i==0: |
|
1137 | if i==0: | |
1130 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1138 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( | |
1131 | conf.name, |
|
1139 | conf.name, | |
1132 | mode, |
|
1140 | mode, | |
1133 | ' ', |
|
1141 | ' ', | |
1134 | delay, |
|
1142 | delay, | |
1135 | mask) |
|
1143 | mask) | |
1136 | else: |
|
1144 | else: | |
1137 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1145 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( | |
1138 | conf.name, |
|
1146 | conf.name, | |
1139 | mode, |
|
1147 | mode, | |
1140 | operation, |
|
1148 | operation, | |
1141 | delay, |
|
1149 | delay, | |
1142 | mask) |
|
1150 | mask) | |
1143 |
|
1151 | |||
1144 | return mark_safe(html) |
|
1152 | return mark_safe(html) | |
1145 |
|
1153 | |||
1146 | def parse_mask(l): |
|
1154 | def parse_mask(l): | |
1147 |
|
1155 | |||
1148 | values = [] |
|
1156 | values = [] | |
1149 |
|
1157 | |||
1150 | for x in range(8): |
|
1158 | for x in range(8): | |
1151 | if '{}'.format(x) in l: |
|
1159 | if '{}'.format(x) in l: | |
1152 | values.append(1) |
|
1160 | values.append(1) | |
1153 | else: |
|
1161 | else: | |
1154 | values.append(0) |
|
1162 | values.append(0) | |
1155 |
|
1163 | |||
1156 | values.reverse() |
|
1164 | values.reverse() | |
1157 |
|
1165 | |||
1158 | return int(''.join([str(x) for x in values]), 2) |
|
1166 | return int(''.join([str(x) for x in values]), 2) | |
1159 |
|
1167 | |||
1160 |
|
1168 | |||
1161 | def dev_confs(request): |
|
1169 | def dev_confs(request): | |
1162 |
|
1170 | |||
1163 |
|
1171 | |||
1164 | page = request.GET.get('page') |
|
1172 | page = request.GET.get('page') | |
1165 | order = ('type', 'device__device_type', 'experiment') |
|
1173 | order = ('type', 'device__device_type', 'experiment') | |
1166 | filters = request.GET.copy() |
|
1174 | filters = request.GET.copy() | |
1167 |
|
1175 | |||
1168 | kwargs = get_paginator(Configuration, page, order, filters) |
|
1176 | kwargs = get_paginator(Configuration, page, order, filters) | |
1169 |
|
1177 | |||
1170 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
1178 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) | |
1171 | kwargs['keys'] = ['name', 'experiment', 'type', 'programmed_date'] |
|
1179 | kwargs['keys'] = ['name', 'experiment', 'type', 'programmed_date'] | |
1172 | kwargs['title'] = 'Configuration' |
|
1180 | kwargs['title'] = 'Configuration' | |
1173 | kwargs['suptitle'] = 'List' |
|
1181 | kwargs['suptitle'] = 'List' | |
1174 | kwargs['no_sidebar'] = True |
|
1182 | kwargs['no_sidebar'] = True | |
1175 | kwargs['form'] = form |
|
1183 | kwargs['form'] = form | |
1176 | filters.pop('page', None) |
|
1184 | filters.pop('page', None) | |
1177 | kwargs['q'] = urlencode(filters) |
|
1185 | kwargs['q'] = urlencode(filters) | |
1178 |
|
1186 | |||
1179 | return render(request, 'base_list.html', kwargs) |
|
1187 | return render(request, 'base_list.html', kwargs) | |
1180 |
|
1188 | |||
1181 |
|
1189 | |||
1182 | def dev_conf(request, id_conf): |
|
1190 | def dev_conf(request, id_conf): | |
1183 |
|
1191 | |||
1184 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1192 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1185 |
|
1193 | |||
1186 | return redirect(conf.get_absolute_url()) |
|
1194 | return redirect(conf.get_absolute_url()) | |
1187 |
|
1195 | |||
1188 |
|
1196 | |||
1189 | @user_passes_test(lambda u:u.is_staff) |
|
1197 | @user_passes_test(lambda u:u.is_staff) | |
1190 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
1198 | def dev_conf_new(request, id_exp=0, id_dev=0): | |
1191 |
|
1199 | |||
1192 | initial = {} |
|
1200 | initial = {} | |
1193 | kwargs = {} |
|
1201 | kwargs = {} | |
1194 |
|
1202 | |||
1195 | if id_exp!=0: |
|
1203 | if id_exp!=0: | |
1196 | initial['experiment'] = id_exp |
|
1204 | initial['experiment'] = id_exp | |
1197 |
|
1205 | |||
1198 | if id_dev!=0: |
|
1206 | if id_dev!=0: | |
1199 | initial['device'] = id_dev |
|
1207 | initial['device'] = id_dev | |
1200 |
|
1208 | |||
1201 | if request.method == 'GET': |
|
1209 | if request.method == 'GET': | |
1202 |
|
1210 | |||
1203 | if id_dev: |
|
1211 | if id_dev: | |
1204 | kwargs['button'] = 'Create' |
|
1212 | kwargs['button'] = 'Create' | |
1205 | device = Device.objects.get(pk=id_dev) |
|
1213 | device = Device.objects.get(pk=id_dev) | |
1206 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1214 | DevConfForm = CONF_FORMS[device.device_type.name] | |
1207 | initial['name'] = request.GET['name'] |
|
1215 | initial['name'] = request.GET['name'] | |
1208 | form = DevConfForm(initial=initial) |
|
1216 | form = DevConfForm(initial=initial) | |
1209 | else: |
|
1217 | else: | |
1210 | if 'template' in request.GET: |
|
1218 | if 'template' in request.GET: | |
1211 | if request.GET['template']=='0': |
|
1219 | if request.GET['template']=='0': | |
1212 | choices = [(conf.pk, '{}'.format(conf)) for conf in Configuration.objects.filter(template=True)] |
|
1220 | choices = [(conf.pk, '{}'.format(conf)) for conf in Configuration.objects.filter(template=True)] | |
1213 | form = NewForm(initial={'create_from':2}, |
|
1221 | form = NewForm(initial={'create_from':2}, | |
1214 | template_choices=choices) |
|
1222 | template_choices=choices) | |
1215 | else: |
|
1223 | else: | |
1216 | kwargs['button'] = 'Create' |
|
1224 | kwargs['button'] = 'Create' | |
1217 | conf = Configuration.objects.get(pk=request.GET['template']) |
|
1225 | conf = Configuration.objects.get(pk=request.GET['template']) | |
1218 | id_dev = conf.device.pk |
|
1226 | id_dev = conf.device.pk | |
1219 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1227 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1220 | form = DevConfForm(instance=conf, |
|
1228 | form = DevConfForm(instance=conf, | |
1221 | initial={'name': '{} [{:%Y/%m/%d}]'.format(conf.name, datetime.now()), |
|
1229 | initial={'name': '{} [{:%Y/%m/%d}]'.format(conf.name, datetime.now()), | |
1222 | 'template': False, |
|
1230 | 'template': False, | |
1223 | 'experiment':id_exp}) |
|
1231 | 'experiment':id_exp}) | |
1224 | elif 'blank' in request.GET: |
|
1232 | elif 'blank' in request.GET: | |
1225 | kwargs['button'] = 'Create' |
|
1233 | kwargs['button'] = 'Create' | |
1226 | form = ConfigurationForm(initial=initial) |
|
1234 | form = ConfigurationForm(initial=initial) | |
1227 | else: |
|
1235 | else: | |
1228 | form = NewForm() |
|
1236 | form = NewForm() | |
1229 |
|
1237 | |||
1230 | if request.method == 'POST': |
|
1238 | if request.method == 'POST': | |
1231 |
|
1239 | |||
1232 | device = Device.objects.get(pk=request.POST['device']) |
|
1240 | device = Device.objects.get(pk=request.POST['device']) | |
1233 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1241 | DevConfForm = CONF_FORMS[device.device_type.name] | |
1234 |
|
1242 | |||
1235 | form = DevConfForm(request.POST) |
|
1243 | form = DevConfForm(request.POST) | |
1236 | kwargs['button'] = 'Create' |
|
1244 | kwargs['button'] = 'Create' | |
1237 | if form.is_valid(): |
|
1245 | if form.is_valid(): | |
1238 | conf = form.save() |
|
1246 | conf = form.save() | |
1239 |
|
1247 | |||
1240 | if 'template' in request.GET and conf.device.device_type.name=='rc': |
|
1248 | if 'template' in request.GET and conf.device.device_type.name=='rc': | |
1241 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) |
|
1249 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) | |
1242 | for line in lines: |
|
1250 | for line in lines: | |
1243 | line.clone(rc_configuration=conf) |
|
1251 | line.clone(rc_configuration=conf) | |
1244 |
|
1252 | |||
1245 | if conf.device.device_type.name=='jars': |
|
1253 | if conf.device.device_type.name=='jars': | |
1246 | conf.add_parms_to_filter() |
|
1254 | conf.add_parms_to_filter() | |
1247 |
|
1255 | |||
1248 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
1256 | return redirect('url_dev_conf', id_conf=conf.pk) | |
1249 |
|
1257 | |||
1250 | kwargs['id_exp'] = id_exp |
|
1258 | kwargs['id_exp'] = id_exp | |
1251 | kwargs['form'] = form |
|
1259 | kwargs['form'] = form | |
1252 | kwargs['title'] = 'Configuration' |
|
1260 | kwargs['title'] = 'Configuration' | |
1253 | kwargs['suptitle'] = 'New' |
|
1261 | kwargs['suptitle'] = 'New' | |
1254 |
|
1262 | |||
1255 |
|
1263 | |||
1256 | if id_dev != 0: |
|
1264 | if id_dev != 0: | |
1257 | device = Device.objects.get(pk=id_dev) |
|
1265 | device = Device.objects.get(pk=id_dev) | |
1258 | kwargs['device'] = device.device_type.name |
|
1266 | kwargs['device'] = device.device_type.name | |
1259 |
|
1267 | |||
1260 | return render(request, 'dev_conf_edit.html', kwargs) |
|
1268 | return render(request, 'dev_conf_edit.html', kwargs) | |
1261 |
|
1269 | |||
1262 |
|
1270 | |||
1263 | @user_passes_test(lambda u:u.is_staff) |
|
1271 | @user_passes_test(lambda u:u.is_staff) | |
1264 | def dev_conf_edit(request, id_conf): |
|
1272 | def dev_conf_edit(request, id_conf): | |
1265 |
|
1273 | |||
1266 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1274 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1267 |
|
1275 | |||
1268 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1276 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1269 |
|
1277 | |||
1270 | if request.method=='GET': |
|
1278 | if request.method=='GET': | |
1271 | form = DevConfForm(instance=conf) |
|
1279 | form = DevConfForm(instance=conf) | |
1272 |
|
1280 | |||
1273 | if request.method=='POST': |
|
1281 | if request.method=='POST': | |
1274 | form = DevConfForm(request.POST, instance=conf) |
|
1282 | form = DevConfForm(request.POST, instance=conf) | |
1275 |
|
1283 | |||
1276 | if form.is_valid(): |
|
1284 | if form.is_valid(): | |
1277 | form.save() |
|
1285 | form.save() | |
1278 | return redirect('url_dev_conf', id_conf=id_conf) |
|
1286 | return redirect('url_dev_conf', id_conf=id_conf) | |
1279 |
|
1287 | |||
1280 | kwargs = {} |
|
1288 | kwargs = {} | |
1281 | kwargs['form'] = form |
|
1289 | kwargs['form'] = form | |
1282 | kwargs['title'] = 'Device Configuration' |
|
1290 | kwargs['title'] = 'Device Configuration' | |
1283 | kwargs['suptitle'] = 'Edit' |
|
1291 | kwargs['suptitle'] = 'Edit' | |
1284 | kwargs['button'] = 'Update' |
|
1292 | kwargs['button'] = 'Update' | |
1285 |
|
1293 | |||
1286 | ###### SIDEBAR ###### |
|
1294 | ###### SIDEBAR ###### | |
1287 | kwargs.update(sidebar(conf=conf)) |
|
1295 | kwargs.update(sidebar(conf=conf)) | |
1288 |
|
1296 | |||
1289 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1297 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
1290 |
|
1298 | |||
1291 |
|
1299 | |||
1292 | @user_passes_test(lambda u:u.is_staff) |
|
1300 | @user_passes_test(lambda u:u.is_staff) | |
1293 | def dev_conf_start(request, id_conf): |
|
1301 | def dev_conf_start(request, id_conf): | |
1294 |
|
1302 | |||
1295 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1303 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1296 |
|
1304 | |||
1297 | if conf.start_device(): |
|
1305 | if conf.start_device(): | |
1298 | messages.success(request, conf.message) |
|
1306 | messages.success(request, conf.message) | |
1299 | else: |
|
1307 | else: | |
1300 | messages.error(request, conf.message) |
|
1308 | messages.error(request, conf.message) | |
1301 |
|
1309 | |||
1302 | #conf.status_device() |
|
1310 | #conf.status_device() | |
1303 |
|
1311 | |||
1304 | return redirect(conf.get_absolute_url()) |
|
1312 | return redirect(conf.get_absolute_url()) | |
1305 |
|
1313 | |||
1306 |
|
1314 | |||
1307 | @user_passes_test(lambda u:u.is_staff) |
|
1315 | @user_passes_test(lambda u:u.is_staff) | |
1308 | def dev_conf_stop(request, id_conf): |
|
1316 | def dev_conf_stop(request, id_conf): | |
1309 |
|
1317 | |||
1310 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1318 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1311 |
|
1319 | |||
1312 | if conf.stop_device(): |
|
1320 | if conf.stop_device(): | |
1313 | messages.success(request, conf.message) |
|
1321 | messages.success(request, conf.message) | |
1314 | else: |
|
1322 | else: | |
1315 | messages.error(request, conf.message) |
|
1323 | messages.error(request, conf.message) | |
1316 |
|
1324 | |||
1317 | #conf.status_device() |
|
1325 | #conf.status_device() | |
1318 |
|
1326 | |||
1319 | return redirect(conf.get_absolute_url()) |
|
1327 | return redirect(conf.get_absolute_url()) | |
1320 |
|
1328 | |||
1321 |
|
1329 | |||
1322 | def dev_conf_status(request, id_conf): |
|
1330 | def dev_conf_status(request, id_conf): | |
1323 |
|
1331 | |||
1324 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1332 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1325 |
|
1333 | |||
1326 | if conf.status_device(): |
|
1334 | if conf.status_device(): | |
1327 | messages.success(request, conf.message) |
|
1335 | messages.success(request, conf.message) | |
1328 | else: |
|
1336 | else: | |
1329 | messages.error(request, conf.message) |
|
1337 | messages.error(request, conf.message) | |
1330 |
|
1338 | |||
1331 | return redirect(conf.get_absolute_url()) |
|
1339 | return redirect(conf.get_absolute_url()) | |
1332 |
|
1340 | |||
1333 |
|
1341 | |||
1334 | @user_passes_test(lambda u:u.is_staff) |
|
1342 | @user_passes_test(lambda u:u.is_staff) | |
1335 | def dev_conf_write(request, id_conf): |
|
1343 | def dev_conf_write(request, id_conf): | |
1336 |
|
1344 | |||
1337 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1345 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1338 |
|
1346 | |||
1339 | if conf.write_device(): |
|
1347 | if conf.write_device(): | |
1340 | messages.success(request, conf.message) |
|
1348 | messages.success(request, conf.message) | |
1341 | conf.clone(type=1, template=False) |
|
1349 | conf.clone(type=1, template=False) | |
1342 | else: |
|
1350 | else: | |
1343 | messages.error(request, conf.message) |
|
1351 | messages.error(request, conf.message) | |
1344 |
|
1352 | |||
1345 | return redirect(conf.get_absolute_url()) |
|
1353 | return redirect(conf.get_absolute_url()) | |
1346 |
|
1354 | |||
1347 |
|
1355 | |||
1348 | @user_passes_test(lambda u:u.is_staff) |
|
1356 | @user_passes_test(lambda u:u.is_staff) | |
1349 | def dev_conf_read(request, id_conf): |
|
1357 | def dev_conf_read(request, id_conf): | |
1350 |
|
1358 | |||
1351 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1359 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1352 |
|
1360 | |||
1353 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1361 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1354 |
|
1362 | |||
1355 | if request.method=='GET': |
|
1363 | if request.method=='GET': | |
1356 |
|
1364 | |||
1357 | parms = conf.read_device() |
|
1365 | parms = conf.read_device() | |
1358 | #conf.status_device() |
|
1366 | #conf.status_device() | |
1359 |
|
1367 | |||
1360 | if not parms: |
|
1368 | if not parms: | |
1361 | messages.error(request, conf.message) |
|
1369 | messages.error(request, conf.message) | |
1362 | return redirect(conf.get_absolute_url()) |
|
1370 | return redirect(conf.get_absolute_url()) | |
1363 |
|
1371 | |||
1364 | form = DevConfForm(initial=parms, instance=conf) |
|
1372 | form = DevConfForm(initial=parms, instance=conf) | |
1365 |
|
1373 | |||
1366 | if request.method=='POST': |
|
1374 | if request.method=='POST': | |
1367 | form = DevConfForm(request.POST, instance=conf) |
|
1375 | form = DevConfForm(request.POST, instance=conf) | |
1368 |
|
1376 | |||
1369 | if form.is_valid(): |
|
1377 | if form.is_valid(): | |
1370 | form.save() |
|
1378 | form.save() | |
1371 | return redirect(conf.get_absolute_url()) |
|
1379 | return redirect(conf.get_absolute_url()) | |
1372 |
|
1380 | |||
1373 | messages.error(request, "Parameters could not be saved") |
|
1381 | messages.error(request, "Parameters could not be saved") | |
1374 |
|
1382 | |||
1375 | kwargs = {} |
|
1383 | kwargs = {} | |
1376 | kwargs['id_dev'] = conf.id |
|
1384 | kwargs['id_dev'] = conf.id | |
1377 | kwargs['form'] = form |
|
1385 | kwargs['form'] = form | |
1378 | kwargs['title'] = 'Device Configuration' |
|
1386 | kwargs['title'] = 'Device Configuration' | |
1379 | kwargs['suptitle'] = 'Parameters read from device' |
|
1387 | kwargs['suptitle'] = 'Parameters read from device' | |
1380 | kwargs['button'] = 'Save' |
|
1388 | kwargs['button'] = 'Save' | |
1381 |
|
1389 | |||
1382 | ###### SIDEBAR ###### |
|
1390 | ###### SIDEBAR ###### | |
1383 | kwargs.update(sidebar(conf=conf)) |
|
1391 | kwargs.update(sidebar(conf=conf)) | |
1384 |
|
1392 | |||
1385 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
1393 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) | |
1386 |
|
1394 | |||
1387 |
|
1395 | |||
1388 | @user_passes_test(lambda u:u.is_staff) |
|
1396 | @user_passes_test(lambda u:u.is_staff) | |
1389 | def dev_conf_import(request, id_conf): |
|
1397 | def dev_conf_import(request, id_conf): | |
1390 |
|
1398 | |||
1391 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1399 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1392 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1400 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1393 |
|
1401 | |||
1394 | if request.method == 'GET': |
|
1402 | if request.method == 'GET': | |
1395 | file_form = UploadFileForm() |
|
1403 | file_form = UploadFileForm() | |
1396 |
|
1404 | |||
1397 | if request.method == 'POST': |
|
1405 | if request.method == 'POST': | |
1398 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1406 | file_form = UploadFileForm(request.POST, request.FILES) | |
1399 |
|
1407 | |||
1400 | if file_form.is_valid(): |
|
1408 | if file_form.is_valid(): | |
1401 |
|
1409 | |||
1402 | parms = conf.import_from_file(request.FILES['file']) |
|
1410 | parms = conf.import_from_file(request.FILES['file']) | |
1403 |
|
1411 | |||
1404 | if parms: |
|
1412 | if parms: | |
1405 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
1413 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) | |
1406 | form = DevConfForm(initial=parms, instance=conf) |
|
1414 | form = DevConfForm(initial=parms, instance=conf) | |
1407 |
|
1415 | |||
1408 | kwargs = {} |
|
1416 | kwargs = {} | |
1409 | kwargs['id_dev'] = conf.id |
|
1417 | kwargs['id_dev'] = conf.id | |
1410 | kwargs['form'] = form |
|
1418 | kwargs['form'] = form | |
1411 | kwargs['title'] = 'Device Configuration' |
|
1419 | kwargs['title'] = 'Device Configuration' | |
1412 | kwargs['suptitle'] = 'Parameters imported' |
|
1420 | kwargs['suptitle'] = 'Parameters imported' | |
1413 | kwargs['button'] = 'Save' |
|
1421 | kwargs['button'] = 'Save' | |
1414 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1422 | kwargs['action'] = conf.get_absolute_url_edit() | |
1415 | kwargs['previous'] = conf.get_absolute_url() |
|
1423 | kwargs['previous'] = conf.get_absolute_url() | |
1416 |
|
1424 | |||
1417 | ###### SIDEBAR ###### |
|
1425 | ###### SIDEBAR ###### | |
1418 | kwargs.update(sidebar(conf=conf)) |
|
1426 | kwargs.update(sidebar(conf=conf)) | |
1419 |
|
1427 | |||
1420 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1428 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
1421 |
|
1429 | |||
1422 | messages.error(request, "Could not import parameters from file") |
|
1430 | messages.error(request, "Could not import parameters from file") | |
1423 |
|
1431 | |||
1424 | kwargs = {} |
|
1432 | kwargs = {} | |
1425 | kwargs['id_dev'] = conf.id |
|
1433 | kwargs['id_dev'] = conf.id | |
1426 | kwargs['title'] = 'Device Configuration' |
|
1434 | kwargs['title'] = 'Device Configuration' | |
1427 | kwargs['form'] = file_form |
|
1435 | kwargs['form'] = file_form | |
1428 | kwargs['suptitle'] = 'Importing file' |
|
1436 | kwargs['suptitle'] = 'Importing file' | |
1429 | kwargs['button'] = 'Import' |
|
1437 | kwargs['button'] = 'Import' | |
1430 |
|
1438 | |||
1431 | kwargs.update(sidebar(conf=conf)) |
|
1439 | kwargs.update(sidebar(conf=conf)) | |
1432 |
|
1440 | |||
1433 | return render(request, 'dev_conf_import.html', kwargs) |
|
1441 | return render(request, 'dev_conf_import.html', kwargs) | |
1434 |
|
1442 | |||
1435 |
|
1443 | |||
1436 | @user_passes_test(lambda u:u.is_staff) |
|
1444 | @user_passes_test(lambda u:u.is_staff) | |
1437 | def dev_conf_export(request, id_conf): |
|
1445 | def dev_conf_export(request, id_conf): | |
1438 |
|
1446 | |||
1439 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1447 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1440 |
|
1448 | |||
1441 | if request.method == 'GET': |
|
1449 | if request.method == 'GET': | |
1442 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1450 | file_form = DownloadFileForm(conf.device.device_type.name) | |
1443 |
|
1451 | |||
1444 | if request.method == 'POST': |
|
1452 | if request.method == 'POST': | |
1445 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
1453 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) | |
1446 |
|
1454 | |||
1447 | if file_form.is_valid(): |
|
1455 | if file_form.is_valid(): | |
1448 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
1456 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) | |
1449 |
|
1457 | |||
1450 | response = HttpResponse(content_type=fields['content_type']) |
|
1458 | response = HttpResponse(content_type=fields['content_type']) | |
1451 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
1459 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] | |
1452 | response.write(fields['content']) |
|
1460 | response.write(fields['content']) | |
1453 |
|
1461 | |||
1454 | return response |
|
1462 | return response | |
1455 |
|
1463 | |||
1456 | messages.error(request, "Could not export parameters") |
|
1464 | messages.error(request, "Could not export parameters") | |
1457 |
|
1465 | |||
1458 | kwargs = {} |
|
1466 | kwargs = {} | |
1459 | kwargs['id_dev'] = conf.id |
|
1467 | kwargs['id_dev'] = conf.id | |
1460 | kwargs['title'] = 'Device Configuration' |
|
1468 | kwargs['title'] = 'Device Configuration' | |
1461 | kwargs['form'] = file_form |
|
1469 | kwargs['form'] = file_form | |
1462 | kwargs['suptitle'] = 'Exporting file' |
|
1470 | kwargs['suptitle'] = 'Exporting file' | |
1463 | kwargs['button'] = 'Export' |
|
1471 | kwargs['button'] = 'Export' | |
1464 |
|
1472 | |||
1465 | return render(request, 'dev_conf_export.html', kwargs) |
|
1473 | return render(request, 'dev_conf_export.html', kwargs) | |
1466 |
|
1474 | |||
1467 |
|
1475 | |||
1468 | @user_passes_test(lambda u:u.is_staff) |
|
1476 | @user_passes_test(lambda u:u.is_staff) | |
1469 | def dev_conf_delete(request, id_conf): |
|
1477 | def dev_conf_delete(request, id_conf): | |
1470 |
|
1478 | |||
1471 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1479 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1472 |
|
1480 | |||
1473 | if request.method=='POST': |
|
1481 | if request.method=='POST': | |
1474 | if request.user.is_staff: |
|
1482 | if request.user.is_staff: | |
1475 | conf.delete() |
|
1483 | conf.delete() | |
1476 | return redirect('url_dev_confs') |
|
1484 | return redirect('url_dev_confs') | |
1477 |
|
1485 | |||
1478 | messages.error(request, 'Not enough permission to delete this object') |
|
1486 | messages.error(request, 'Not enough permission to delete this object') | |
1479 | return redirect(conf.get_absolute_url()) |
|
1487 | return redirect(conf.get_absolute_url()) | |
1480 |
|
1488 | |||
1481 | kwargs = { |
|
1489 | kwargs = { | |
1482 | 'title': 'Delete', |
|
1490 | 'title': 'Delete', | |
1483 | 'suptitle': 'Experiment', |
|
1491 | 'suptitle': 'Experiment', | |
1484 | 'object': conf, |
|
1492 | 'object': conf, | |
1485 | 'previous': conf.get_absolute_url(), |
|
1493 | 'previous': conf.get_absolute_url(), | |
1486 | 'delete': True |
|
1494 | 'delete': True | |
1487 | } |
|
1495 | } | |
1488 |
|
1496 | |||
1489 | return render(request, 'confirm.html', kwargs) |
|
1497 | return render(request, 'confirm.html', kwargs) | |
1490 |
|
1498 | |||
1491 |
|
1499 | |||
1492 | def sidebar(**kwargs): |
|
1500 | def sidebar(**kwargs): | |
1493 |
|
1501 | |||
1494 | side_data = {} |
|
1502 | side_data = {} | |
1495 |
|
1503 | |||
1496 | conf = kwargs.get('conf', None) |
|
1504 | conf = kwargs.get('conf', None) | |
1497 | experiment = kwargs.get('experiment', None) |
|
1505 | experiment = kwargs.get('experiment', None) | |
1498 |
|
1506 | |||
1499 | if not experiment: |
|
1507 | if not experiment: | |
1500 | experiment = conf.experiment |
|
1508 | experiment = conf.experiment | |
1501 |
|
1509 | |||
1502 | if experiment: |
|
1510 | if experiment: | |
1503 | side_data['experiment'] = experiment |
|
1511 | side_data['experiment'] = experiment | |
1504 | campaign = experiment.campaign_set.all() |
|
1512 | campaign = experiment.campaign_set.all() | |
1505 | if campaign: |
|
1513 | if campaign: | |
1506 | side_data['campaign'] = campaign[0] |
|
1514 | side_data['campaign'] = campaign[0] | |
1507 | experiments = campaign[0].experiments.all() |
|
1515 | experiments = campaign[0].experiments.all() | |
1508 | else: |
|
1516 | else: | |
1509 | experiments = [experiment] |
|
1517 | experiments = [experiment] | |
1510 | configurations = experiment.configuration_set.filter(type=0) |
|
1518 | configurations = experiment.configuration_set.filter(type=0) | |
1511 | side_data['side_experiments'] = experiments |
|
1519 | side_data['side_experiments'] = experiments | |
1512 | side_data['side_configurations'] = configurations |
|
1520 | side_data['side_configurations'] = configurations | |
1513 |
|
1521 | |||
1514 | return side_data |
|
1522 | return side_data | |
1515 |
|
1523 | |||
1516 | def get_paginator(model, page, order, filters={}, n=10): |
|
1524 | def get_paginator(model, page, order, filters={}, n=10): | |
1517 |
|
1525 | |||
1518 | kwargs = {} |
|
1526 | kwargs = {} | |
1519 | query = Q() |
|
1527 | query = Q() | |
1520 | if isinstance(filters, QueryDict): |
|
1528 | if isinstance(filters, QueryDict): | |
1521 | filters = filters.dict() |
|
1529 | filters = filters.dict() | |
1522 | [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] |
|
1530 | [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] | |
1523 | filters.pop('page', None) |
|
1531 | filters.pop('page', None) | |
1524 |
|
1532 | |||
1525 | if 'template' in filters: |
|
1533 | if 'template' in filters: | |
1526 | filters['template'] = True |
|
1534 | filters['template'] = True | |
1527 | if 'start_date' in filters: |
|
1535 | if 'start_date' in filters: | |
1528 | filters['start_date__gte'] = filters.pop('start_date') |
|
1536 | filters['start_date__gte'] = filters.pop('start_date') | |
1529 | if 'end_date' in filters: |
|
1537 | if 'end_date' in filters: | |
1530 | filters['start_date__lte'] = filters.pop('end_date') |
|
1538 | filters['start_date__lte'] = filters.pop('end_date') | |
1531 | if 'tags' in filters: |
|
1539 | if 'tags' in filters: | |
1532 | tags = filters.pop('tags') |
|
1540 | tags = filters.pop('tags') | |
1533 | fields = [f.name for f in model._meta.get_fields()] |
|
1541 | fields = [f.name for f in model._meta.get_fields()] | |
1534 |
|
1542 | |||
1535 | if 'tags' in fields: |
|
1543 | if 'tags' in fields: | |
1536 | query = query | Q(tags__icontains=tags) |
|
1544 | query = query | Q(tags__icontains=tags) | |
1537 | if 'name' in fields: |
|
1545 | if 'name' in fields: | |
1538 | query = query | Q(name__icontains=tags) |
|
1546 | query = query | Q(name__icontains=tags) | |
1539 | if 'location' in fields: |
|
1547 | if 'location' in fields: | |
1540 | query = query | Q(location__name__icontains=tags) |
|
1548 | query = query | Q(location__name__icontains=tags) | |
1541 | if 'device' in fields: |
|
1549 | if 'device' in fields: | |
1542 | query = query | Q(device__device_type__name__icontains=tags) |
|
1550 | query = query | Q(device__device_type__name__icontains=tags) | |
1543 |
|
1551 | |||
1544 | object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1552 | object_list = model.objects.filter(query, **filters).order_by(*order) | |
1545 | paginator = Paginator(object_list, n) |
|
1553 | paginator = Paginator(object_list, n) | |
1546 |
|
1554 | |||
1547 | try: |
|
1555 | try: | |
1548 | objects = paginator.page(page) |
|
1556 | objects = paginator.page(page) | |
1549 | except PageNotAnInteger: |
|
1557 | except PageNotAnInteger: | |
1550 | objects = paginator.page(1) |
|
1558 | objects = paginator.page(1) | |
1551 | except EmptyPage: |
|
1559 | except EmptyPage: | |
1552 | objects = paginator.page(paginator.num_pages) |
|
1560 | objects = paginator.page(paginator.num_pages) | |
1553 |
|
1561 | |||
1554 | kwargs['objects'] = objects |
|
1562 | kwargs['objects'] = objects | |
1555 | kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1563 | kwargs['offset'] = (int(page)-1)*n if page else 0 | |
1556 |
|
1564 | |||
1557 | return kwargs |
|
1565 | return kwargs | |
1558 |
|
1566 | |||
1559 | def operation(request, id_camp=None): |
|
1567 | def operation(request, id_camp=None): | |
1560 |
|
1568 | |||
1561 | kwargs = {} |
|
1569 | kwargs = {} | |
1562 | kwargs['title'] = 'Radars Operation' |
|
1570 | kwargs['title'] = 'Radars Operation' | |
1563 | kwargs['no_sidebar'] = True |
|
1571 | kwargs['no_sidebar'] = True | |
1564 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), |
|
1572 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), | |
1565 | end_date__gte=datetime.now()).order_by('-start_date') |
|
1573 | end_date__gte=datetime.now()).order_by('-start_date') | |
1566 |
|
1574 | |||
1567 |
|
1575 | |||
1568 | if id_camp: |
|
1576 | if id_camp: | |
1569 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1577 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1570 | form = OperationForm(initial={'campaign': campaign.id}, campaigns=campaigns) |
|
1578 | form = OperationForm(initial={'campaign': campaign.id}, campaigns=campaigns) | |
1571 | kwargs['campaign'] = campaign |
|
1579 | kwargs['campaign'] = campaign | |
1572 | else: |
|
1580 | else: | |
1573 | form = OperationForm(campaigns=campaigns) |
|
1581 | form = OperationForm(campaigns=campaigns) | |
1574 | kwargs['form'] = form |
|
1582 | kwargs['form'] = form | |
1575 | return render(request, 'operation.html', kwargs) |
|
1583 | return render(request, 'operation.html', kwargs) | |
1576 |
|
1584 | |||
1577 | #---Experiment |
|
1585 | #---Experiment | |
1578 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1586 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] | |
1579 | kwargs['experiment_keys'] = keys[1:] |
|
1587 | kwargs['experiment_keys'] = keys[1:] | |
1580 | kwargs['experiments'] = experiments |
|
1588 | kwargs['experiments'] = experiments | |
1581 | #---Radar |
|
1589 | #---Radar | |
1582 | kwargs['locations'] = campaign.get_experiments_by_radar() |
|
1590 | kwargs['locations'] = campaign.get_experiments_by_radar() | |
1583 | kwargs['form'] = form |
|
1591 | kwargs['form'] = form | |
1584 |
|
1592 | |||
1585 | return render(request, 'operation.html', kwargs) |
|
1593 | return render(request, 'operation.html', kwargs) | |
1586 |
|
1594 | |||
1587 |
|
1595 | |||
1588 | @login_required |
|
1596 | @login_required | |
1589 | def radar_start(request, id_camp, id_radar): |
|
1597 | def radar_start(request, id_camp, id_radar): | |
1590 |
|
1598 | |||
1591 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1599 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1592 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1600 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] | |
1593 | now = datetime.utcnow() |
|
1601 | now = datetime.utcnow() | |
1594 |
|
1602 | |||
1595 | for exp in experiments: |
|
1603 | for exp in experiments: | |
1596 | date = datetime.combine(datetime.now().date(), exp.start_time) |
|
1604 | date = datetime.combine(datetime.now().date(), exp.start_time) | |
1597 |
|
1605 | |||
1598 | if exp.status == 2: |
|
1606 | if exp.status == 2: | |
1599 | messages.warning(request, 'Experiment {} already running'.format(exp)) |
|
1607 | messages.warning(request, 'Experiment {} already running'.format(exp)) | |
1600 | continue |
|
1608 | continue | |
1601 |
|
1609 | |||
1602 | if exp.status == 3: |
|
1610 | if exp.status == 3: | |
1603 | messages.warning(request, 'Experiment {} already programmed'.format(exp)) |
|
1611 | messages.warning(request, 'Experiment {} already programmed'.format(exp)) | |
1604 | continue |
|
1612 | continue | |
1605 |
|
1613 | |||
1606 | if date>campaign.end_date or date<campaign.start_date: |
|
1614 | if date>campaign.end_date or date<campaign.start_date: | |
1607 | messages.warning(request, 'Experiment {} out of date'.format(exp)) |
|
1615 | messages.warning(request, 'Experiment {} out of date'.format(exp)) | |
1608 | continue |
|
1616 | continue | |
1609 |
|
1617 | |||
1610 | if now>=date: |
|
1618 | if now>=date: | |
1611 | task = task_start.delay(exp.pk) |
|
1619 | task = task_start.delay(exp.pk) | |
1612 | exp.status = task.wait() |
|
1620 | exp.status = task.wait() | |
1613 | if exp.status==0: |
|
1621 | if exp.status==0: | |
1614 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
1622 | messages.error(request, 'Experiment {} not start'.format(exp)) | |
1615 | if exp.status==2: |
|
1623 | if exp.status==2: | |
1616 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
1624 | messages.success(request, 'Experiment {} started'.format(exp)) | |
1617 | else: |
|
1625 | else: | |
1618 | task = task_start.apply_async((exp.pk,), eta=date) |
|
1626 | task = task_start.apply_async((exp.pk,), eta=date) | |
1619 | exp.status = 3 |
|
1627 | exp.status = 3 | |
1620 | messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, date)) |
|
1628 | messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, date)) | |
1621 |
|
1629 | |||
1622 | exp.save() |
|
1630 | exp.save() | |
1623 |
|
1631 | |||
1624 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1632 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
1625 |
|
1633 | |||
1626 |
|
1634 | |||
1627 | @login_required |
|
1635 | @login_required | |
1628 | def radar_stop(request, id_camp, id_radar): |
|
1636 | def radar_stop(request, id_camp, id_radar): | |
1629 |
|
1637 | |||
1630 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1638 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1631 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1639 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] | |
1632 |
|
1640 | |||
1633 | for exp in experiments: |
|
1641 | for exp in experiments: | |
1634 |
|
1642 | |||
1635 | if exp.status == 2: |
|
1643 | if exp.status == 2: | |
1636 | task = task_stop.delay(exp.pk) |
|
1644 | task = task_stop.delay(exp.pk) | |
1637 | exp.status = task.wait() |
|
1645 | exp.status = task.wait() | |
1638 | messages.warning(request, 'Experiment {} stopped'.format(exp)) |
|
1646 | messages.warning(request, 'Experiment {} stopped'.format(exp)) | |
1639 | exp.save() |
|
1647 | exp.save() | |
1640 | else: |
|
1648 | else: | |
1641 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
1649 | messages.error(request, 'Experiment {} not running'.format(exp)) | |
1642 |
|
1650 | |||
1643 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1651 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
1644 |
|
1652 | |||
1645 |
|
1653 | |||
1646 | @login_required |
|
1654 | @login_required | |
1647 | def radar_refresh(request, id_camp, id_radar): |
|
1655 | def radar_refresh(request, id_camp, id_radar): | |
1648 |
|
1656 | |||
1649 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1657 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1650 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1658 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] | |
1651 |
|
1659 | |||
1652 | for exp in experiments: |
|
1660 | for exp in experiments: | |
1653 | exp.get_status() |
|
1661 | exp.get_status() | |
1654 |
|
1662 | |||
1655 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1663 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
General Comments 0
You need to be logged in to leave comments.
Login now