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