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