@@ -1,541 +1,564 | |||
|
1 | 1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
2 | 2 | from datetime import datetime |
|
3 | 3 | |
|
4 | 4 | from django.db import models |
|
5 | 5 | from polymorphic import PolymorphicModel |
|
6 | 6 | |
|
7 | 7 | from django.core.urlresolvers import reverse |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | CONF_STATES = ( |
|
11 | 11 | (0, 'Disconnected'), |
|
12 | 12 | (1, 'Connected'), |
|
13 | 13 | (2, 'Running'), |
|
14 | 14 | ) |
|
15 | 15 | |
|
16 | 16 | EXP_STATES = ( |
|
17 | 17 | (0,'Error'), #RED |
|
18 | 18 | (1,'Configurated'), #BLUE |
|
19 | 19 | (2,'Running'), #GREEN |
|
20 | 20 | (3,'Waiting'), #YELLOW |
|
21 | 21 | (4,'Not Configured'), #WHITE |
|
22 | 22 | ) |
|
23 | 23 | |
|
24 | 24 | CONF_TYPES = ( |
|
25 | 25 | (0, 'Active'), |
|
26 | 26 | (1, 'Historical'), |
|
27 | 27 | ) |
|
28 | 28 | |
|
29 | 29 | DEV_STATES = ( |
|
30 | 30 | (0, 'No connected'), |
|
31 | 31 | (1, 'Connected'), |
|
32 | 32 | (2, 'Configured'), |
|
33 | 33 | (3, 'Running'), |
|
34 | 34 | ) |
|
35 | 35 | |
|
36 | 36 | DEV_TYPES = ( |
|
37 | 37 | ('', 'Select a device type'), |
|
38 | 38 | ('rc', 'Radar Controller'), |
|
39 | 39 | ('rc_mix', 'Radar Controller (Mix)'), |
|
40 | 40 | ('dds', 'Direct Digital Synthesizer'), |
|
41 | 41 | ('jars', 'Jicamarca Radar Acquisition System'), |
|
42 | 42 | ('usrp', 'Universal Software Radio Peripheral'), |
|
43 | 43 | ('cgs', 'Clock Generator System'), |
|
44 | 44 | ('abs', 'Automatic Beam Switching'), |
|
45 | 45 | ) |
|
46 | 46 | |
|
47 | 47 | DEV_PORTS = { |
|
48 | 48 | 'rc' : 2000, |
|
49 | 49 | 'rc_mix': 2000, |
|
50 | 50 | 'dds' : 2000, |
|
51 | 51 | 'jars' : 2000, |
|
52 | 52 | 'usrp' : 2000, |
|
53 | 53 | 'cgs' : 8080, |
|
54 | 54 | 'abs' : 8080 |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | RADAR_STATES = ( |
|
58 | 58 | (0, 'No connected'), |
|
59 | 59 | (1, 'Connected'), |
|
60 | 60 | (2, 'Configured'), |
|
61 | 61 | (3, 'Running'), |
|
62 | 62 | (4, 'Scheduled'), |
|
63 | 63 | ) |
|
64 | 64 | # Create your models here. |
|
65 | 65 | |
|
66 | 66 | class Location(models.Model): |
|
67 | 67 | |
|
68 | 68 | name = models.CharField(max_length = 30) |
|
69 | 69 | description = models.TextField(blank=True, null=True) |
|
70 | 70 | |
|
71 | 71 | class Meta: |
|
72 | 72 | db_table = 'db_location' |
|
73 | 73 | |
|
74 | 74 | def __unicode__(self): |
|
75 | 75 | return u'%s' % self.name |
|
76 | 76 | |
|
77 | 77 | def get_absolute_url(self): |
|
78 | 78 | return reverse('url_device', args=[str(self.id)]) |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | class DeviceType(models.Model): |
|
82 | 82 | |
|
83 | 83 | name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'rc') |
|
84 | 84 | description = models.TextField(blank=True, null=True) |
|
85 | 85 | |
|
86 | 86 | class Meta: |
|
87 | 87 | db_table = 'db_device_types' |
|
88 | 88 | |
|
89 | 89 | def __unicode__(self): |
|
90 | 90 | return u'%s' % self.get_name_display() |
|
91 | 91 | |
|
92 | 92 | class Device(models.Model): |
|
93 | 93 | |
|
94 | 94 | device_type = models.ForeignKey(DeviceType, on_delete=models.CASCADE) |
|
95 | 95 | location = models.ForeignKey(Location, on_delete=models.CASCADE) |
|
96 | 96 | |
|
97 | 97 | name = models.CharField(max_length=40, default='') |
|
98 | 98 | ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0') |
|
99 | 99 | port_address = models.PositiveSmallIntegerField(default=2000) |
|
100 | 100 | description = models.TextField(blank=True, null=True) |
|
101 | 101 | status = models.PositiveSmallIntegerField(default=0, choices=DEV_STATES) |
|
102 | 102 | |
|
103 | 103 | class Meta: |
|
104 | 104 | db_table = 'db_devices' |
|
105 | 105 | |
|
106 | 106 | def __unicode__(self): |
|
107 | 107 | return u'%s | %s' % (self.name, self.ip_address) |
|
108 | 108 | |
|
109 | 109 | def get_status(self): |
|
110 | 110 | return self.status |
|
111 | 111 | |
|
112 | 112 | def get_absolute_url(self): |
|
113 | 113 | return reverse('url_device', args=[str(self.id)]) |
|
114 | 114 | |
|
115 | 115 | |
|
116 | 116 | class Campaign(models.Model): |
|
117 | 117 | |
|
118 | 118 | template = models.BooleanField(default=False) |
|
119 | 119 | name = models.CharField(max_length=60, unique=True) |
|
120 | 120 | start_date = models.DateTimeField(blank=True, null=True) |
|
121 | 121 | end_date = models.DateTimeField(blank=True, null=True) |
|
122 | 122 | tags = models.CharField(max_length=40) |
|
123 | 123 | description = models.TextField(blank=True, null=True) |
|
124 | 124 | experiments = models.ManyToManyField('Experiment', blank=True) |
|
125 | 125 | |
|
126 | 126 | class Meta: |
|
127 | 127 | db_table = 'db_campaigns' |
|
128 | 128 | ordering = ('name',) |
|
129 | 129 | |
|
130 | 130 | def __unicode__(self): |
|
131 | 131 | return u'%s' % (self.name) |
|
132 | 132 | |
|
133 | 133 | def get_absolute_url(self): |
|
134 | 134 | return reverse('url_campaign', args=[str(self.id)]) |
|
135 | 135 | |
|
136 | 136 | def parms_to_dict(self): |
|
137 | 137 | |
|
138 | 138 | import json |
|
139 | 139 | |
|
140 | 140 | parameters = {} |
|
141 | 141 | exp_parameters = {} |
|
142 | 142 | experiments = Experiment.objects.filter(campaign = self) |
|
143 | 143 | |
|
144 | 144 | i=1 |
|
145 | 145 | for experiment in experiments: |
|
146 | 146 | exp_parameters['experiment-'+str(i)] = json.loads(experiment.parms_to_dict()) |
|
147 | 147 | i += 1 |
|
148 | 148 | |
|
149 | 149 | |
|
150 | 150 | parameters['experiments'] = exp_parameters |
|
151 | 151 | parameters['end_date'] = self.end_date.strftime("%Y-%m-%d") |
|
152 | 152 | parameters['start_date'] = self.start_date.strftime("%Y-%m-%d") |
|
153 | 153 | parameters['campaign'] = self.__unicode__() |
|
154 | 154 | parameters['tags'] =self.tags |
|
155 | 155 | |
|
156 | 156 | parameters = json.dumps(parameters, indent=2, sort_keys=False) |
|
157 | 157 | |
|
158 | 158 | return parameters |
|
159 | 159 | |
|
160 | 160 | def import_from_file(self, fp): |
|
161 | 161 | |
|
162 | 162 | import os, json |
|
163 | 163 | |
|
164 | 164 | parms = {} |
|
165 | 165 | |
|
166 | 166 | path, ext = os.path.splitext(fp.name) |
|
167 | 167 | |
|
168 | 168 | if ext == '.json': |
|
169 | 169 | parms = json.load(fp) |
|
170 | 170 | |
|
171 | 171 | return parms |
|
172 | 172 | |
|
173 | def dict_to_parms(self, parms, CONF_MODELS): | |
|
174 | ||
|
175 | experiments = Experiment.objects.filter(campaign = self) | |
|
176 | configurations = Configuration.objects.filter(experiment = experiments) | |
|
177 | ||
|
178 | if configurations: | |
|
179 | for configuration in configurations: | |
|
180 | configuration.delete() | |
|
181 | ||
|
182 | if experiments: | |
|
183 | for experiment in experiments: | |
|
184 | experiment.delete() | |
|
185 | ||
|
186 | for parms_exp in parms['experiments']: | |
|
187 | location = Location.objects.get(name = parms['experiments'][parms_exp]['radar']) | |
|
188 | new_exp = Experiment( | |
|
189 | name = parms['experiments'][parms_exp]['experiment'], | |
|
190 | location = location, | |
|
191 | start_time = parms['experiments'][parms_exp]['start_time'], | |
|
192 | end_time = parms['experiments'][parms_exp]['end_time'], | |
|
193 | ) | |
|
194 | new_exp.save() | |
|
195 | new_exp.dict_to_parms(parms['experiments'][parms_exp],CONF_MODELS) | |
|
196 | new_exp.save() | |
|
197 | ||
|
198 | self.experiments.add(new_exp) | |
|
199 | self.save() | |
|
200 | ||
|
173 | 201 | def get_absolute_url(self): |
|
174 | 202 | return reverse('url_campaign', args=[str(self.id)]) |
|
175 | 203 | |
|
176 | 204 | def get_absolute_url_edit(self): |
|
177 | 205 | return reverse('url_edit_campaign', args=[str(self.id)]) |
|
178 | 206 | |
|
179 | 207 | def get_absolute_url_export(self): |
|
180 | 208 | return reverse('url_export_campaign', args=[str(self.id)]) |
|
181 | 209 | |
|
182 | 210 | def get_absolute_url_import(self): |
|
183 | 211 | return reverse('url_import_campaign', args=[str(self.id)]) |
|
184 | 212 | |
|
185 | 213 | |
|
186 | 214 | |
|
187 | 215 | class RunningExperiment(models.Model): |
|
188 | 216 | radar = models.OneToOneField('Location', on_delete=models.CASCADE) |
|
189 | 217 | running_experiment = models.ManyToManyField('Experiment', blank = True) |
|
190 | 218 | status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES) |
|
191 | 219 | |
|
192 | 220 | |
|
193 | 221 | class Experiment(models.Model): |
|
194 | 222 | |
|
195 | 223 | template = models.BooleanField(default=False) |
|
196 | 224 | name = models.CharField(max_length=40, default='', unique=True) |
|
197 | 225 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) |
|
198 | 226 | start_time = models.TimeField(default='00:00:00') |
|
199 | 227 | end_time = models.TimeField(default='23:59:59') |
|
200 | 228 | status = models.PositiveSmallIntegerField(default=0, choices=EXP_STATES) |
|
201 | 229 | |
|
202 | 230 | class Meta: |
|
203 | 231 | db_table = 'db_experiments' |
|
204 | 232 | ordering = ('template', 'name') |
|
205 | 233 | |
|
206 | 234 | def __unicode__(self): |
|
207 | 235 | if self.template: |
|
208 | 236 | return u'%s (template)' % (self.name) |
|
209 | 237 | else: |
|
210 | 238 | return u'%s' % (self.name) |
|
211 | 239 | |
|
212 | 240 | @property |
|
213 | 241 | def radar(self): |
|
214 | 242 | return self.location |
|
215 | 243 | |
|
216 | 244 | def clone(self, **kwargs): |
|
217 | 245 | |
|
218 | 246 | confs = Configuration.objects.filter(experiment=self, type=0) |
|
219 | 247 | self.pk = None |
|
220 | 248 | self.name = '{} [{:%Y/%m/%d}]'.format(self.name, datetime.now()) |
|
221 | 249 | for attr, value in kwargs.items(): |
|
222 | 250 | setattr(self, attr, value) |
|
223 | 251 | |
|
224 | 252 | self.save() |
|
225 | 253 | |
|
226 | 254 | for conf in confs: |
|
227 | 255 | conf.clone(experiment=self, template=False) |
|
228 | 256 | |
|
229 | 257 | return self |
|
230 | 258 | |
|
231 | 259 | def get_status(self): |
|
232 | 260 | configurations = Configuration.objects.filter(experiment=self) |
|
233 | 261 | exp_status=[] |
|
234 | 262 | for conf in configurations: |
|
235 | 263 | print conf.status_device() |
|
236 | 264 | exp_status.append(conf.status_device()) |
|
237 | 265 | |
|
238 | 266 | if not exp_status: #No Configuration |
|
239 | 267 | self.status = 4 |
|
240 | 268 | self.save() |
|
241 | 269 | return |
|
242 | 270 | |
|
243 | 271 | total = 1 |
|
244 | 272 | for e_s in exp_status: |
|
245 | 273 | total = total*e_s |
|
246 | 274 | |
|
247 | 275 | if total == 0: #Error |
|
248 | 276 | status = 0 |
|
249 | 277 | elif total == (3**len(exp_status)): #Running |
|
250 | 278 | status = 2 |
|
251 | 279 | else: |
|
252 | 280 | status = 1 #Configurated |
|
253 | 281 | |
|
254 | 282 | self.status = status |
|
255 | 283 | self.save() |
|
256 | 284 | |
|
257 | 285 | def status_color(self): |
|
258 | 286 | color = 'danger' |
|
259 | 287 | if self.status == 0: |
|
260 | 288 | color = "danger" |
|
261 | 289 | elif self.status == 1: |
|
262 | 290 | color = "info" |
|
263 | 291 | elif self.status == 2: |
|
264 | 292 | color = "success" |
|
265 | 293 | elif self.status == 3: |
|
266 | 294 | color = "warning" |
|
267 | 295 | else: |
|
268 | 296 | color = "muted" |
|
269 | 297 | |
|
270 | 298 | return color |
|
271 | 299 | |
|
272 | 300 | def get_absolute_url(self): |
|
273 | 301 | return reverse('url_experiment', args=[str(self.id)]) |
|
274 | 302 | |
|
275 | 303 | def parms_to_dict(self): |
|
276 | 304 | |
|
277 | 305 | import json |
|
278 | 306 | |
|
279 | 307 | configurations = Configuration.objects.filter(experiment=self) |
|
280 | 308 | conf_parameters = {} |
|
281 | 309 | parameters={} |
|
282 | 310 | |
|
283 | 311 | for configuration in configurations: |
|
284 | 312 | if 'cgs' in configuration.device.device_type.name: |
|
285 | 313 | conf_parameters['cgs'] = configuration.parms_to_dict() |
|
286 | 314 | if 'dds' in configuration.device.device_type.name: |
|
287 | 315 | conf_parameters['dds'] = configuration.parms_to_dict() |
|
288 | 316 | if 'rc' in configuration.device.device_type.name: |
|
289 | 317 | conf_parameters['rc'] = configuration.parms_to_dict() |
|
290 | 318 | if 'jars' in configuration.device.device_type.name: |
|
291 | 319 | conf_parameters['jars'] = configuration.parms_to_dict() |
|
292 | 320 | if 'usrp' in configuration.device.device_type.name: |
|
293 | 321 | conf_parameters['usrp'] = configuration.parms_to_dict() |
|
294 | 322 | if 'abs' in configuration.device.device_type.name: |
|
295 | 323 | conf_parameters['abs'] = configuration.parms_to_dict() |
|
296 | 324 | |
|
297 | 325 | parameters['configurations'] = conf_parameters |
|
298 | 326 | parameters['end_time'] = self.end_time.strftime("%H:%M:%S") |
|
299 | 327 | parameters['start_time'] = self.start_time.strftime("%H:%M:%S") |
|
300 | 328 | parameters['radar'] = self.radar.name |
|
301 | 329 | parameters['experiment'] = self.name |
|
302 | 330 | parameters = json.dumps(parameters, indent=2) |
|
303 | 331 | |
|
304 | 332 | return parameters |
|
305 | 333 | |
|
306 | 334 | def import_from_file(self, fp): |
|
307 | 335 | |
|
308 | 336 | import os, json |
|
309 | 337 | |
|
310 | 338 | parms = {} |
|
311 | 339 | |
|
312 | 340 | path, ext = os.path.splitext(fp.name) |
|
313 | 341 | |
|
314 | 342 | if ext == '.json': |
|
315 | 343 | parms = json.load(fp) |
|
316 | 344 | |
|
317 | 345 | return parms |
|
318 | 346 | |
|
319 | 347 | def dict_to_parms(self, parms, CONF_MODELS): |
|
320 | 348 | |
|
321 | #self.name = parameters['experiment'] | |
|
322 | #self.location = parameters['radar'] | |
|
323 | #self.start_time = parameters['start_time'] | |
|
324 | #self.end_time = parameters['end_time'] | |
|
325 | ||
|
326 | 349 | configurations = Configuration.objects.filter(experiment=self) |
|
327 | 350 | |
|
328 | 351 | if configurations: |
|
329 | 352 | for configuration in configurations: |
|
330 | 353 | configuration.delete() |
|
331 | 354 | |
|
332 | 355 | for conf_type in parms['configurations']: |
|
333 | 356 | #--For ABS Device: |
|
334 | 357 | #--For USRP Device: |
|
335 | 358 | #--For JARS Device: |
|
336 | 359 | #--For RC Device: |
|
337 | 360 | if conf_type == 'rc': |
|
338 | 361 | device = get_object_or_404(Device, pk=parms['configurations']['rc']['device_id']) |
|
339 | 362 | DevConfModel = CONF_MODELS[conf_type] |
|
340 | 363 | confrc_form = DevConfModel( |
|
341 | 364 | experiment = self, |
|
342 | 365 | name = 'RC', |
|
343 | 366 | device=device, |
|
344 | 367 | ) |
|
345 | 368 | confrc_form.dict_to_parms(parms['configurations']['rc']) |
|
346 | 369 | confrc_form.save() |
|
347 | 370 | #--For DDS Device: |
|
348 | 371 | if conf_type == 'dds': |
|
349 | 372 | device = get_object_or_404(Device, pk=parms['configurations']['dds']['device_id']) |
|
350 | 373 | DevConfModel = CONF_MODELS[conf_type] |
|
351 | 374 | confdds_form = DevConfModel( |
|
352 | 375 | experiment = self, |
|
353 | 376 | name = 'DDS', |
|
354 | 377 | device=device, |
|
355 | 378 | ) |
|
356 | 379 | confdds_form.dict_to_parms(parms['configurations']['dds']) |
|
357 | 380 | confdds_form.save() |
|
358 | 381 | #--For CGS Device: |
|
359 | 382 | if conf_type == 'cgs': |
|
360 | 383 | device = get_object_or_404(Device, pk=parms['configurations']['cgs']['device_id']) |
|
361 | 384 | DevConfModel = CONF_MODELS[conf_type] |
|
362 | 385 | confcgs_form = DevConfModel( |
|
363 | 386 | experiment = self, |
|
364 | 387 | name = 'CGS', |
|
365 | 388 | device=device, |
|
366 | 389 | ) |
|
367 | 390 | confcgs_form.dict_to_parms(parms['configurations']['cgs']) |
|
368 | 391 | confcgs_form.save() |
|
369 | 392 | |
|
370 | 393 | def get_absolute_url_edit(self): |
|
371 | 394 | return reverse('url_edit_experiment', args=[str(self.id)]) |
|
372 | 395 | |
|
373 | 396 | def get_absolute_url_import(self): |
|
374 | 397 | return reverse('url_import_experiment', args=[str(self.id)]) |
|
375 | 398 | |
|
376 | 399 | def get_absolute_url_export(self): |
|
377 | 400 | return reverse('url_export_experiment', args=[str(self.id)]) |
|
378 | 401 | |
|
379 | 402 | |
|
380 | 403 | class Configuration(PolymorphicModel): |
|
381 | 404 | |
|
382 | 405 | template = models.BooleanField(default=False) |
|
383 | 406 | |
|
384 | 407 | name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') |
|
385 | 408 | |
|
386 | 409 | experiment = models.ForeignKey('Experiment', null=True, blank=True, on_delete=models.CASCADE) |
|
387 | 410 | device = models.ForeignKey(Device, null=True, on_delete=models.CASCADE) |
|
388 | 411 | |
|
389 | 412 | type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) |
|
390 | 413 | |
|
391 | 414 | created_date = models.DateTimeField(auto_now_add=True) |
|
392 | 415 | programmed_date = models.DateTimeField(auto_now=True) |
|
393 | 416 | |
|
394 | 417 | parameters = models.TextField(default='{}') |
|
395 | 418 | |
|
396 | 419 | message = "" |
|
397 | 420 | |
|
398 | 421 | class Meta: |
|
399 | 422 | db_table = 'db_configurations' |
|
400 | 423 | |
|
401 | 424 | def __unicode__(self): |
|
402 | 425 | |
|
403 | 426 | return u'[%s]: %s' % (self.device.name, self.name) |
|
404 | 427 | |
|
405 | 428 | def clone(self, **kwargs): |
|
406 | 429 | |
|
407 | 430 | self.pk = None |
|
408 | 431 | self.id = None |
|
409 | 432 | for attr, value in kwargs.items(): |
|
410 | 433 | setattr(self, attr, value) |
|
411 | 434 | |
|
412 | 435 | self.save() |
|
413 | 436 | |
|
414 | 437 | return self |
|
415 | 438 | |
|
416 | 439 | def parms_to_dict(self): |
|
417 | 440 | |
|
418 | 441 | parameters = {} |
|
419 | 442 | |
|
420 | 443 | for key in self.__dict__.keys(): |
|
421 | 444 | parameters[key] = getattr(self, key) |
|
422 | 445 | |
|
423 | 446 | return parameters |
|
424 | 447 | |
|
425 | 448 | def parms_to_text(self): |
|
426 | 449 | |
|
427 | 450 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
428 | 451 | |
|
429 | 452 | return '' |
|
430 | 453 | |
|
431 | 454 | def parms_to_binary(self): |
|
432 | 455 | |
|
433 | 456 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
434 | 457 | |
|
435 | 458 | return '' |
|
436 | 459 | |
|
437 | 460 | def dict_to_parms(self, parameters): |
|
438 | 461 | |
|
439 | 462 | if type(parameters) != type({}): |
|
440 | 463 | return |
|
441 | 464 | |
|
442 | 465 | for key in parameters.keys(): |
|
443 | 466 | setattr(self, key, parameters[key]) |
|
444 | 467 | |
|
445 | 468 | def export_to_file(self, format="json"): |
|
446 | 469 | |
|
447 | 470 | import json |
|
448 | 471 | |
|
449 | 472 | content_type = '' |
|
450 | 473 | |
|
451 | 474 | if format == 'text': |
|
452 | 475 | content_type = 'text/plain' |
|
453 | 476 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) |
|
454 | 477 | content = self.parms_to_text() |
|
455 | 478 | |
|
456 | 479 | if format == 'binary': |
|
457 | 480 | content_type = 'application/octet-stream' |
|
458 | 481 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) |
|
459 | 482 | content = self.parms_to_binary() |
|
460 | 483 | |
|
461 | 484 | if not content_type: |
|
462 | 485 | content_type = 'application/json' |
|
463 | 486 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) |
|
464 | 487 | content = json.dumps(self.parms_to_dict(), indent=2) |
|
465 | 488 | |
|
466 | 489 | fields = {'content_type':content_type, |
|
467 | 490 | 'filename':filename, |
|
468 | 491 | 'content':content |
|
469 | 492 | } |
|
470 | 493 | |
|
471 | 494 | return fields |
|
472 | 495 | |
|
473 | 496 | def import_from_file(self, fp): |
|
474 | 497 | |
|
475 | 498 | import os, json |
|
476 | 499 | |
|
477 | 500 | parms = {} |
|
478 | 501 | |
|
479 | 502 | path, ext = os.path.splitext(fp.name) |
|
480 | 503 | |
|
481 | 504 | if ext == '.json': |
|
482 | 505 | parms = json.load(fp) |
|
483 | 506 | |
|
484 | 507 | return parms |
|
485 | 508 | |
|
486 | 509 | def status_device(self): |
|
487 | 510 | |
|
488 | 511 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
489 | 512 | |
|
490 | 513 | return None |
|
491 | 514 | |
|
492 | 515 | def stop_device(self): |
|
493 | 516 | |
|
494 | 517 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
495 | 518 | |
|
496 | 519 | return None |
|
497 | 520 | |
|
498 | 521 | def start_device(self): |
|
499 | 522 | |
|
500 | 523 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
501 | 524 | |
|
502 | 525 | return None |
|
503 | 526 | |
|
504 | 527 | def write_device(self, parms): |
|
505 | 528 | |
|
506 | 529 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
507 | 530 | |
|
508 | 531 | return None |
|
509 | 532 | |
|
510 | 533 | def read_device(self): |
|
511 | 534 | |
|
512 | 535 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
513 | 536 | |
|
514 | 537 | return None |
|
515 | 538 | |
|
516 | 539 | def get_absolute_url(self): |
|
517 | 540 | return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
518 | 541 | |
|
519 | 542 | def get_absolute_url_edit(self): |
|
520 | 543 | return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
521 | 544 | |
|
522 | 545 | def get_absolute_url_import(self): |
|
523 | 546 | return reverse('url_import_dev_conf', args=[str(self.id)]) |
|
524 | 547 | |
|
525 | 548 | def get_absolute_url_export(self): |
|
526 | 549 | return reverse('url_export_dev_conf', args=[str(self.id)]) |
|
527 | 550 | |
|
528 | 551 | def get_absolute_url_write(self): |
|
529 | 552 | return reverse('url_write_dev_conf', args=[str(self.id)]) |
|
530 | 553 | |
|
531 | 554 | def get_absolute_url_read(self): |
|
532 | 555 | return reverse('url_read_dev_conf', args=[str(self.id)]) |
|
533 | 556 | |
|
534 | 557 | def get_absolute_url_start(self): |
|
535 | 558 | return reverse('url_start_dev_conf', args=[str(self.id)]) |
|
536 | 559 | |
|
537 | 560 | def get_absolute_url_stop(self): |
|
538 | 561 | return reverse('url_stop_dev_conf', args=[str(self.id)]) |
|
539 | 562 | |
|
540 | 563 | def get_absolute_url_status(self): |
|
541 | 564 | return reverse('url_status_dev_conf', args=[str(self.id)]) No newline at end of file |
@@ -1,1405 +1,1393 | |||
|
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.contrib import messages |
|
6 | 6 | from datetime import datetime |
|
7 | 7 | |
|
8 | 8 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
9 | 9 | from .forms import OperationSearchForm |
|
10 | 10 | from apps.cgs.forms import CGSConfigurationForm |
|
11 | 11 | from apps.jars.forms import JARSConfigurationForm |
|
12 | 12 | from apps.usrp.forms import USRPConfigurationForm |
|
13 | 13 | from apps.abs.forms import ABSConfigurationForm |
|
14 | 14 | from apps.rc.forms import RCConfigurationForm, RCMixConfigurationForm |
|
15 | 15 | from apps.dds.forms import DDSConfigurationForm |
|
16 | 16 | |
|
17 | 17 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment |
|
18 | 18 | from apps.cgs.models import CGSConfiguration |
|
19 | 19 | from apps.jars.models import JARSConfiguration |
|
20 | 20 | from apps.usrp.models import USRPConfiguration |
|
21 | 21 | from apps.abs.models import ABSConfiguration |
|
22 | 22 | from apps.rc.models import RCConfiguration, RCLine, RCLineType |
|
23 | 23 | from apps.dds.models import DDSConfiguration |
|
24 | 24 | |
|
25 | 25 | # Create your views here. |
|
26 | 26 | |
|
27 | 27 | CONF_FORMS = { |
|
28 | 28 | 'rc': RCConfigurationForm, |
|
29 | 29 | 'rc_mix': RCMixConfigurationForm, |
|
30 | 30 | 'dds': DDSConfigurationForm, |
|
31 | 31 | 'jars': JARSConfigurationForm, |
|
32 | 32 | 'cgs': CGSConfigurationForm, |
|
33 | 33 | 'abs': ABSConfigurationForm, |
|
34 | 34 | 'usrp': USRPConfigurationForm, |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | CONF_MODELS = { |
|
38 | 38 | 'rc': RCConfiguration, |
|
39 | 39 | 'dds': DDSConfiguration, |
|
40 | 40 | 'jars': JARSConfiguration, |
|
41 | 41 | 'cgs': CGSConfiguration, |
|
42 | 42 | 'abs': ABSConfiguration, |
|
43 | 43 | 'usrp': USRPConfiguration, |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | MIX_MODES = { |
|
47 | 47 | '0': 'OR', |
|
48 | 48 | '1': 'XOR', |
|
49 | 49 | '2': 'AND', |
|
50 | 50 | '3': 'NAND' |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | def index(request): |
|
55 | 55 | kwargs = {} |
|
56 | 56 | |
|
57 | 57 | return render(request, 'index.html', kwargs) |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | def locations(request): |
|
61 | 61 | |
|
62 | 62 | locations = Location.objects.all().order_by('name') |
|
63 | 63 | |
|
64 | 64 | keys = ['id', 'name', 'description'] |
|
65 | 65 | |
|
66 | 66 | kwargs = {} |
|
67 | 67 | kwargs['location_keys'] = keys[1:] |
|
68 | 68 | kwargs['locations'] = locations |
|
69 | 69 | kwargs['title'] = 'Location' |
|
70 | 70 | kwargs['suptitle'] = 'List' |
|
71 | 71 | kwargs['button'] = 'New Location' |
|
72 | 72 | |
|
73 | 73 | return render(request, 'locations.html', kwargs) |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | def location(request, id_loc): |
|
77 | 77 | |
|
78 | 78 | location = get_object_or_404(Location, pk=id_loc) |
|
79 | 79 | |
|
80 | 80 | kwargs = {} |
|
81 | 81 | kwargs['location'] = location |
|
82 | 82 | kwargs['location_keys'] = ['name', 'description'] |
|
83 | 83 | |
|
84 | 84 | kwargs['title'] = 'Location' |
|
85 | 85 | kwargs['suptitle'] = 'Details' |
|
86 | 86 | |
|
87 | 87 | return render(request, 'location.html', kwargs) |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | def location_new(request): |
|
91 | 91 | |
|
92 | 92 | if request.method == 'GET': |
|
93 | 93 | form = LocationForm() |
|
94 | 94 | |
|
95 | 95 | if request.method == 'POST': |
|
96 | 96 | form = LocationForm(request.POST) |
|
97 | 97 | |
|
98 | 98 | if form.is_valid(): |
|
99 | 99 | form.save() |
|
100 | 100 | return redirect('url_locations') |
|
101 | 101 | |
|
102 | 102 | kwargs = {} |
|
103 | 103 | kwargs['form'] = form |
|
104 | 104 | kwargs['title'] = 'Location' |
|
105 | 105 | kwargs['suptitle'] = 'New' |
|
106 | 106 | kwargs['button'] = 'Create' |
|
107 | 107 | |
|
108 | 108 | return render(request, 'location_edit.html', kwargs) |
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | def location_edit(request, id_loc): |
|
112 | 112 | |
|
113 | 113 | location = get_object_or_404(Location, pk=id_loc) |
|
114 | 114 | |
|
115 | 115 | if request.method=='GET': |
|
116 | 116 | form = LocationForm(instance=location) |
|
117 | 117 | |
|
118 | 118 | if request.method=='POST': |
|
119 | 119 | form = LocationForm(request.POST, instance=location) |
|
120 | 120 | |
|
121 | 121 | if form.is_valid(): |
|
122 | 122 | form.save() |
|
123 | 123 | return redirect('url_locations') |
|
124 | 124 | |
|
125 | 125 | kwargs = {} |
|
126 | 126 | kwargs['form'] = form |
|
127 | 127 | kwargs['title'] = 'Location' |
|
128 | 128 | kwargs['suptitle'] = 'Edit' |
|
129 | 129 | kwargs['button'] = 'Update' |
|
130 | 130 | |
|
131 | 131 | return render(request, 'location_edit.html', kwargs) |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | def location_delete(request, id_loc): |
|
135 | 135 | |
|
136 | 136 | location = get_object_or_404(Location, pk=id_loc) |
|
137 | 137 | |
|
138 | 138 | if request.method=='POST': |
|
139 | 139 | |
|
140 | 140 | if request.user.is_staff: |
|
141 | 141 | location.delete() |
|
142 | 142 | return redirect('url_locations') |
|
143 | 143 | |
|
144 | 144 | messages.error(request, 'Not enough permission to delete this object') |
|
145 | 145 | return redirect(location.get_absolute_url()) |
|
146 | 146 | |
|
147 | 147 | kwargs = { |
|
148 | 148 | 'title': 'Delete', |
|
149 | 149 | 'suptitle': 'Location', |
|
150 | 150 | 'object': location, |
|
151 | 151 | 'previous': location.get_absolute_url(), |
|
152 | 152 | 'delete': True |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | return render(request, 'confirm.html', kwargs) |
|
156 | 156 | |
|
157 | 157 | |
|
158 | 158 | def devices(request): |
|
159 | 159 | |
|
160 | 160 | devices = Device.objects.all().order_by('device_type__name') |
|
161 | 161 | |
|
162 | 162 | # keys = ['id', 'device_type__name', 'name', 'ip_address'] |
|
163 | 163 | keys = ['id', 'name', 'ip_address', 'port_address', 'device_type'] |
|
164 | 164 | |
|
165 | 165 | kwargs = {} |
|
166 | 166 | kwargs['device_keys'] = keys[1:] |
|
167 | 167 | kwargs['devices'] = devices#.values(*keys) |
|
168 | 168 | kwargs['title'] = 'Device' |
|
169 | 169 | kwargs['suptitle'] = 'List' |
|
170 | 170 | kwargs['button'] = 'New Device' |
|
171 | 171 | |
|
172 | 172 | return render(request, 'devices.html', kwargs) |
|
173 | 173 | |
|
174 | 174 | |
|
175 | 175 | def device(request, id_dev): |
|
176 | 176 | |
|
177 | 177 | device = get_object_or_404(Device, pk=id_dev) |
|
178 | 178 | |
|
179 | 179 | kwargs = {} |
|
180 | 180 | kwargs['device'] = device |
|
181 | 181 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
182 | 182 | |
|
183 | 183 | kwargs['title'] = 'Device' |
|
184 | 184 | kwargs['suptitle'] = 'Details' |
|
185 | 185 | |
|
186 | 186 | return render(request, 'device.html', kwargs) |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | def device_new(request): |
|
190 | 190 | |
|
191 | 191 | if request.method == 'GET': |
|
192 | 192 | form = DeviceForm() |
|
193 | 193 | |
|
194 | 194 | if request.method == 'POST': |
|
195 | 195 | form = DeviceForm(request.POST) |
|
196 | 196 | |
|
197 | 197 | if form.is_valid(): |
|
198 | 198 | form.save() |
|
199 | 199 | return redirect('url_devices') |
|
200 | 200 | |
|
201 | 201 | kwargs = {} |
|
202 | 202 | kwargs['form'] = form |
|
203 | 203 | kwargs['title'] = 'Device' |
|
204 | 204 | kwargs['suptitle'] = 'New' |
|
205 | 205 | kwargs['button'] = 'Create' |
|
206 | 206 | |
|
207 | 207 | return render(request, 'device_edit.html', kwargs) |
|
208 | 208 | |
|
209 | 209 | |
|
210 | 210 | def device_edit(request, id_dev): |
|
211 | 211 | |
|
212 | 212 | device = get_object_or_404(Device, pk=id_dev) |
|
213 | 213 | |
|
214 | 214 | if request.method=='GET': |
|
215 | 215 | form = DeviceForm(instance=device) |
|
216 | 216 | |
|
217 | 217 | if request.method=='POST': |
|
218 | 218 | form = DeviceForm(request.POST, instance=device) |
|
219 | 219 | |
|
220 | 220 | if form.is_valid(): |
|
221 | 221 | form.save() |
|
222 | 222 | return redirect(device.get_absolute_url()) |
|
223 | 223 | |
|
224 | 224 | kwargs = {} |
|
225 | 225 | kwargs['form'] = form |
|
226 | 226 | kwargs['title'] = 'Device' |
|
227 | 227 | kwargs['suptitle'] = 'Edit' |
|
228 | 228 | kwargs['button'] = 'Update' |
|
229 | 229 | |
|
230 | 230 | return render(request, 'device_edit.html', kwargs) |
|
231 | 231 | |
|
232 | 232 | |
|
233 | 233 | def device_delete(request, id_dev): |
|
234 | 234 | |
|
235 | 235 | device = get_object_or_404(Device, pk=id_dev) |
|
236 | 236 | |
|
237 | 237 | if request.method=='POST': |
|
238 | 238 | |
|
239 | 239 | if request.user.is_staff: |
|
240 | 240 | device.delete() |
|
241 | 241 | return redirect('url_devices') |
|
242 | 242 | |
|
243 | 243 | messages.error(request, 'Not enough permission to delete this object') |
|
244 | 244 | return redirect(device.get_absolute_url()) |
|
245 | 245 | |
|
246 | 246 | kwargs = { |
|
247 | 247 | 'title': 'Delete', |
|
248 | 248 | 'suptitle': 'Device', |
|
249 | 249 | 'object': device, |
|
250 | 250 | 'previous': device.get_absolute_url(), |
|
251 | 251 | 'delete': True |
|
252 | 252 | } |
|
253 | 253 | |
|
254 | 254 | return render(request, 'confirm.html', kwargs) |
|
255 | 255 | |
|
256 | 256 | |
|
257 | 257 | def campaigns(request): |
|
258 | 258 | |
|
259 | 259 | campaigns = Campaign.objects.all().order_by('start_date') |
|
260 | 260 | |
|
261 | 261 | keys = ['id', 'name', 'start_date', 'end_date'] |
|
262 | 262 | |
|
263 | 263 | kwargs = {} |
|
264 | 264 | kwargs['campaign_keys'] = keys[1:] |
|
265 | 265 | kwargs['campaigns'] = campaigns#.values(*keys) |
|
266 | 266 | kwargs['title'] = 'Campaign' |
|
267 | 267 | kwargs['suptitle'] = 'List' |
|
268 | 268 | kwargs['button'] = 'New Campaign' |
|
269 | 269 | |
|
270 | 270 | return render(request, 'campaigns.html', kwargs) |
|
271 | 271 | |
|
272 | 272 | |
|
273 | 273 | def campaign(request, id_camp): |
|
274 | 274 | |
|
275 | 275 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
276 | 276 | experiments = Experiment.objects.filter(campaign=campaign) |
|
277 | 277 | |
|
278 | 278 | form = CampaignForm(instance=campaign) |
|
279 | 279 | |
|
280 | 280 | kwargs = {} |
|
281 | 281 | kwargs['campaign'] = campaign |
|
282 | 282 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] |
|
283 | 283 | |
|
284 | 284 | kwargs['experiments'] = experiments |
|
285 | 285 | kwargs['experiment_keys'] = ['name', 'radar', 'start_time', 'end_time'] |
|
286 | 286 | |
|
287 | 287 | kwargs['title'] = 'Campaign' |
|
288 | 288 | kwargs['suptitle'] = 'Details' |
|
289 | 289 | |
|
290 | 290 | kwargs['form'] = form |
|
291 | 291 | kwargs['button'] = 'Add Experiment' |
|
292 | 292 | |
|
293 | 293 | return render(request, 'campaign.html', kwargs) |
|
294 | 294 | |
|
295 | 295 | |
|
296 | 296 | def campaign_new(request): |
|
297 | 297 | |
|
298 | 298 | kwargs = {} |
|
299 | 299 | |
|
300 | 300 | if request.method == 'GET': |
|
301 | 301 | |
|
302 | 302 | if 'template' in request.GET: |
|
303 | 303 | if request.GET['template']=='0': |
|
304 | 304 | form = NewForm(initial={'create_from':2}, |
|
305 | 305 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
306 | 306 | else: |
|
307 | 307 | kwargs['button'] = 'Create' |
|
308 | 308 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
309 | 309 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
310 | 310 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
311 | 311 | form = CampaignForm(instance=camp, |
|
312 | 312 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), |
|
313 | 313 | 'template':False}) |
|
314 | 314 | elif 'blank' in request.GET: |
|
315 | 315 | kwargs['button'] = 'Create' |
|
316 | 316 | form = CampaignForm() |
|
317 | 317 | else: |
|
318 | 318 | form = NewForm() |
|
319 | 319 | |
|
320 | 320 | if request.method == 'POST': |
|
321 | 321 | kwargs['button'] = 'Create' |
|
322 | 322 | post = request.POST.copy() |
|
323 | 323 | experiments = [] |
|
324 | 324 | |
|
325 | 325 | for id_exp in post.getlist('experiments'): |
|
326 | 326 | exp = Experiment.objects.get(pk=id_exp) |
|
327 | 327 | new_exp = exp.clone(template=False) |
|
328 | 328 | experiments.append(new_exp) |
|
329 | 329 | |
|
330 | 330 | post.setlist('experiments', []) |
|
331 | 331 | |
|
332 | 332 | form = CampaignForm(post) |
|
333 | 333 | |
|
334 | 334 | if form.is_valid(): |
|
335 | 335 | campaign = form.save() |
|
336 | 336 | for exp in experiments: |
|
337 | 337 | campaign.experiments.add(exp) |
|
338 | 338 | campaign.save() |
|
339 | 339 | return redirect('url_campaign', id_camp=campaign.id) |
|
340 | 340 | |
|
341 | 341 | kwargs['form'] = form |
|
342 | 342 | kwargs['title'] = 'Campaign' |
|
343 | 343 | kwargs['suptitle'] = 'New' |
|
344 | 344 | |
|
345 | 345 | return render(request, 'campaign_edit.html', kwargs) |
|
346 | 346 | |
|
347 | 347 | |
|
348 | 348 | def campaign_edit(request, id_camp): |
|
349 | 349 | |
|
350 | 350 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
351 | 351 | |
|
352 | 352 | if request.method=='GET': |
|
353 | 353 | form = CampaignForm(instance=campaign) |
|
354 | 354 | |
|
355 | 355 | if request.method=='POST': |
|
356 | 356 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
357 | 357 | post = request.POST.copy() |
|
358 | 358 | new_exps = post.getlist('experiments') |
|
359 | 359 | post.setlist('experiments', []) |
|
360 | 360 | form = CampaignForm(post, instance=campaign) |
|
361 | 361 | |
|
362 | 362 | if form.is_valid(): |
|
363 | 363 | camp = form.save() |
|
364 | 364 | for id_exp in new_exps: |
|
365 | 365 | if int(id_exp) in exps: |
|
366 | 366 | exps.pop(id_exp) |
|
367 | 367 | else: |
|
368 | 368 | exp = Experiment.objects.get(pk=id_exp) |
|
369 | 369 | if exp.template: |
|
370 | 370 | camp.experiments.add(exp.clone(template=False)) |
|
371 | 371 | else: |
|
372 | 372 | camp.experiments.add(exp) |
|
373 | 373 | |
|
374 | 374 | for id_exp in exps: |
|
375 | 375 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
376 | 376 | |
|
377 | 377 | return redirect('url_campaign', id_camp=id_camp) |
|
378 | 378 | |
|
379 | 379 | kwargs = {} |
|
380 | 380 | kwargs['form'] = form |
|
381 | 381 | kwargs['title'] = 'Campaign' |
|
382 | 382 | kwargs['suptitle'] = 'Edit' |
|
383 | 383 | kwargs['button'] = 'Update' |
|
384 | 384 | |
|
385 | 385 | return render(request, 'campaign_edit.html', kwargs) |
|
386 | 386 | |
|
387 | 387 | |
|
388 | 388 | def campaign_delete(request, id_camp): |
|
389 | 389 | |
|
390 | 390 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
391 | 391 | |
|
392 | 392 | if request.method=='POST': |
|
393 | 393 | if request.user.is_staff: |
|
394 | 394 | |
|
395 | 395 | for exp in campaign.experiments.all(): |
|
396 | 396 | for conf in Configuration.objects.filter(experiment=exp): |
|
397 | 397 | conf.delete() |
|
398 | 398 | exp.delete() |
|
399 | 399 | campaign.delete() |
|
400 | 400 | |
|
401 | 401 | return redirect('url_campaigns') |
|
402 | 402 | |
|
403 | 403 | messages.error(request, 'Not enough permission to delete this object') |
|
404 | 404 | return redirect(campaign.get_absolute_url()) |
|
405 | 405 | |
|
406 | 406 | kwargs = { |
|
407 | 407 | 'title': 'Delete', |
|
408 | 408 | 'suptitle': 'Campaign', |
|
409 | 409 | 'object': campaign, |
|
410 | 410 | 'previous': campaign.get_absolute_url(), |
|
411 | 411 | 'delete': True |
|
412 | 412 | } |
|
413 | 413 | |
|
414 | 414 | return render(request, 'confirm.html', kwargs) |
|
415 | 415 | |
|
416 | 416 | def campaign_export(request, id_camp): |
|
417 | 417 | |
|
418 | 418 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
419 | 419 | content = campaign.parms_to_dict() |
|
420 | 420 | content_type = 'application/json' |
|
421 | 421 | filename = '%s_%s.json' %(campaign.name, campaign.id) |
|
422 | 422 | |
|
423 | 423 | response = HttpResponse(content_type=content_type) |
|
424 | 424 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
425 | 425 | response.write(content) |
|
426 | 426 | |
|
427 | 427 | return response |
|
428 | 428 | |
|
429 | 429 | |
|
430 | 430 | def campaign_import(request, id_camp): |
|
431 | ###------FALTA CORREGIR!!!!!-----### | |
|
431 | ||
|
432 | 432 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
433 | experiments = Experiment.objects.filter(campaign=campaign) | |
|
434 | configurations = Configuration.objects.filter(experiment=experiments) | |
|
435 | 433 | |
|
436 | 434 | if request.method == 'GET': |
|
437 | 435 | file_form = UploadFileForm() |
|
438 | 436 | |
|
439 | 437 | if request.method == 'POST': |
|
440 | 438 | file_form = UploadFileForm(request.POST, request.FILES) |
|
441 | 439 | |
|
442 | 440 | if file_form.is_valid(): |
|
443 | 441 | |
|
444 | 442 | parms = campaign.import_from_file(request.FILES['file']) |
|
445 | 443 | |
|
446 | 444 | if parms: |
|
447 |
|
|
|
448 | parms['location'] = location.id | |
|
449 | parms['name'] = parms['experiment'] | |
|
445 | parms['name'] = parms['campaign'] | |
|
450 | 446 | |
|
451 | 447 | campaign.dict_to_parms(parms, CONF_MODELS) |
|
452 | 448 | |
|
453 | 449 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
454 | 450 | |
|
455 | 451 | form = CampaignForm(initial=parms, instance=campaign) |
|
456 | 452 | |
|
457 | 453 | kwargs = {} |
|
458 | #kwargs['id_dev'] = conf.id | |
|
459 | 454 | kwargs['form'] = form |
|
460 | 455 | kwargs['title'] = 'Campaign' |
|
461 | 456 | kwargs['suptitle'] = 'Parameters imported' |
|
462 | 457 | kwargs['button'] = 'Save' |
|
463 | 458 | kwargs['action'] = campaign.get_absolute_url_edit() |
|
464 | 459 | kwargs['previous'] = campaign.get_absolute_url() |
|
465 | ||
|
466 | ||
|
467 | ###### SIDEBAR ###### | |
|
468 | #kwargs.update(sidebar(conf=conf)) | |
|
469 | #kwargs.update(sidebar(campaign=campaign)) | |
|
470 | 460 | |
|
471 | 461 | return render(request, 'campaign_edit.html', kwargs) |
|
462 | ||
|
472 | 463 | |
|
473 | 464 | messages.error(request, "Could not import parameters from file") |
|
474 | 465 | |
|
475 | 466 | kwargs = {} |
|
476 | #kwargs['id_dev'] = conf.id | |
|
477 | 467 | kwargs['title'] = 'Campaign' |
|
478 | 468 | kwargs['form'] = file_form |
|
479 | 469 | kwargs['suptitle'] = 'Importing file' |
|
480 | 470 | kwargs['button'] = 'Import' |
|
481 | 471 | |
|
482 | #kwargs.update(sidebar(campaign=campaign)) | |
|
483 | ||
|
484 | 472 | return render(request, 'campaign_import.html', kwargs) |
|
485 | 473 | |
|
486 | 474 | |
|
487 | 475 | def experiments(request): |
|
488 | 476 | |
|
489 | 477 | experiment_list = Experiment.objects.all() |
|
490 | 478 | |
|
491 | 479 | keys = ['id', 'name', 'start_time', 'end_time'] |
|
492 | 480 | |
|
493 | 481 | kwargs = {} |
|
494 | 482 | |
|
495 | 483 | kwargs['experiment_keys'] = keys[1:] |
|
496 | 484 | kwargs['experiments'] = experiment_list |
|
497 | 485 | |
|
498 | 486 | kwargs['title'] = 'Experiment' |
|
499 | 487 | kwargs['suptitle'] = 'List' |
|
500 | 488 | kwargs['button'] = 'New Experiment' |
|
501 | 489 | |
|
502 | 490 | return render(request, 'experiments.html', kwargs) |
|
503 | 491 | |
|
504 | 492 | |
|
505 | 493 | def experiment(request, id_exp): |
|
506 | 494 | |
|
507 | 495 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
508 | 496 | |
|
509 | 497 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
510 | 498 | |
|
511 | 499 | kwargs = {} |
|
512 | 500 | |
|
513 | 501 | kwargs['experiment_keys'] = ['template', 'radar', 'name', 'start_time', 'end_time'] |
|
514 | 502 | kwargs['experiment'] = experiment |
|
515 | 503 | |
|
516 | 504 | kwargs['configuration_keys'] = ['name', 'device__device_type', 'device__ip_address', 'device__port_address'] |
|
517 | 505 | kwargs['configurations'] = configurations |
|
518 | 506 | |
|
519 | 507 | kwargs['title'] = 'Experiment' |
|
520 | 508 | kwargs['suptitle'] = 'Details' |
|
521 | 509 | |
|
522 | 510 | kwargs['button'] = 'Add Configuration' |
|
523 | 511 | |
|
524 | 512 | ###### SIDEBAR ###### |
|
525 | 513 | kwargs.update(sidebar(experiment=experiment)) |
|
526 | 514 | |
|
527 | 515 | return render(request, 'experiment.html', kwargs) |
|
528 | 516 | |
|
529 | 517 | |
|
530 | 518 | def experiment_new(request, id_camp=None): |
|
531 | 519 | |
|
532 | 520 | kwargs = {} |
|
533 | 521 | |
|
534 | 522 | if request.method == 'GET': |
|
535 | 523 | if 'template' in request.GET: |
|
536 | 524 | if request.GET['template']=='0': |
|
537 | 525 | form = NewForm(initial={'create_from':2}, |
|
538 | 526 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
539 | 527 | else: |
|
540 | 528 | kwargs['button'] = 'Create' |
|
541 | 529 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
542 | 530 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
543 | 531 | exp=Experiment.objects.get(pk=request.GET['template']) |
|
544 | 532 | form = ExperimentForm(instance=exp, |
|
545 | 533 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), |
|
546 | 534 | 'template': False}) |
|
547 | 535 | elif 'blank' in request.GET: |
|
548 | 536 | kwargs['button'] = 'Create' |
|
549 | 537 | form = ExperimentForm() |
|
550 | 538 | else: |
|
551 | 539 | form = NewForm() |
|
552 | 540 | |
|
553 | 541 | if request.method == 'POST': |
|
554 | 542 | form = ExperimentForm(request.POST) |
|
555 | 543 | if form.is_valid(): |
|
556 | 544 | experiment = form.save() |
|
557 | 545 | |
|
558 | 546 | if 'template' in request.GET: |
|
559 | 547 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
560 | 548 | for conf in configurations: |
|
561 | 549 | conf.clone(experiment=experiment, template=False) |
|
562 | 550 | |
|
563 | 551 | return redirect('url_experiment', id_exp=experiment.id) |
|
564 | 552 | |
|
565 | 553 | kwargs['form'] = form |
|
566 | 554 | kwargs['title'] = 'Experiment' |
|
567 | 555 | kwargs['suptitle'] = 'New' |
|
568 | 556 | |
|
569 | 557 | return render(request, 'experiment_edit.html', kwargs) |
|
570 | 558 | |
|
571 | 559 | |
|
572 | 560 | def experiment_edit(request, id_exp): |
|
573 | 561 | |
|
574 | 562 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
575 | 563 | |
|
576 | 564 | if request.method == 'GET': |
|
577 | 565 | form = ExperimentForm(instance=experiment) |
|
578 | 566 | |
|
579 | 567 | if request.method=='POST': |
|
580 | 568 | form = ExperimentForm(request.POST, instance=experiment) |
|
581 | 569 | |
|
582 | 570 | if form.is_valid(): |
|
583 | 571 | experiment = form.save() |
|
584 | 572 | return redirect('url_experiment', id_exp=experiment.id) |
|
585 | 573 | |
|
586 | 574 | kwargs = {} |
|
587 | 575 | kwargs['form'] = form |
|
588 | 576 | kwargs['title'] = 'Experiment' |
|
589 | 577 | kwargs['suptitle'] = 'Edit' |
|
590 | 578 | kwargs['button'] = 'Update' |
|
591 | 579 | |
|
592 | 580 | return render(request, 'experiment_edit.html', kwargs) |
|
593 | 581 | |
|
594 | 582 | |
|
595 | 583 | def experiment_delete(request, id_exp): |
|
596 | 584 | |
|
597 | 585 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
598 | 586 | |
|
599 | 587 | if request.method=='POST': |
|
600 | 588 | if request.user.is_staff: |
|
601 | 589 | for conf in Configuration.objects.filter(experiment=experiment): |
|
602 | 590 | conf.delete() |
|
603 | 591 | experiment.delete() |
|
604 | 592 | return redirect('url_experiments') |
|
605 | 593 | |
|
606 | 594 | messages.error(request, 'Not enough permission to delete this object') |
|
607 | 595 | return redirect(experiment.get_absolute_url()) |
|
608 | 596 | |
|
609 | 597 | kwargs = { |
|
610 | 598 | 'title': 'Delete', |
|
611 | 599 | 'suptitle': 'Experiment', |
|
612 | 600 | 'object': experiment, |
|
613 | 601 | 'previous': experiment.get_absolute_url(), |
|
614 | 602 | 'delete': True |
|
615 | 603 | } |
|
616 | 604 | |
|
617 | 605 | return render(request, 'confirm.html', kwargs) |
|
618 | 606 | |
|
619 | 607 | |
|
620 | 608 | def experiment_export(request, id_exp): |
|
621 | 609 | |
|
622 | 610 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
623 | 611 | content = experiment.parms_to_dict() |
|
624 | 612 | content_type = 'application/json' |
|
625 | 613 | filename = '%s_%s.json' %(experiment.name, experiment.id) |
|
626 | 614 | |
|
627 | 615 | response = HttpResponse(content_type=content_type) |
|
628 | 616 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
629 | 617 | response.write(content) |
|
630 | 618 | |
|
631 | 619 | return response |
|
632 | 620 | |
|
633 | 621 | def experiment_import(request, id_exp): |
|
634 | 622 | |
|
635 | 623 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
636 | 624 | configurations = Configuration.objects.filter(experiment=experiment) |
|
637 | 625 | |
|
638 | 626 | if request.method == 'GET': |
|
639 | 627 | file_form = UploadFileForm() |
|
640 | 628 | |
|
641 | 629 | if request.method == 'POST': |
|
642 | 630 | file_form = UploadFileForm(request.POST, request.FILES) |
|
643 | 631 | |
|
644 | 632 | if file_form.is_valid(): |
|
645 | 633 | |
|
646 | 634 | parms = experiment.import_from_file(request.FILES['file']) |
|
647 | 635 | |
|
648 | 636 | if parms: |
|
649 | 637 | location = Location.objects.get(name = parms['radar']) |
|
650 | 638 | parms['location'] = location.id |
|
651 | 639 | parms['name'] = parms['experiment'] |
|
652 | 640 | |
|
653 | 641 | experiment.dict_to_parms(parms, CONF_MODELS) |
|
654 | 642 | |
|
655 | 643 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
656 | 644 | |
|
657 | 645 | form = ExperimentForm(initial=parms, instance=experiment) |
|
658 | 646 | |
|
659 | 647 | kwargs = {} |
|
660 | 648 | #kwargs['id_dev'] = conf.id |
|
661 | 649 | kwargs['form'] = form |
|
662 | 650 | kwargs['title'] = 'Experiment' |
|
663 | 651 | kwargs['suptitle'] = 'Parameters imported' |
|
664 | 652 | kwargs['button'] = 'Save' |
|
665 | 653 | kwargs['action'] = experiment.get_absolute_url_edit() |
|
666 | 654 | kwargs['previous'] = experiment.get_absolute_url() |
|
667 | 655 | |
|
668 | 656 | ###### SIDEBAR ###### |
|
669 | 657 | #kwargs.update(sidebar(conf=conf)) |
|
670 | 658 | kwargs.update(sidebar(experiment=experiment)) |
|
671 | 659 | |
|
672 | 660 | return render(request, 'experiment_edit.html', kwargs) |
|
673 | 661 | |
|
674 | 662 | messages.error(request, "Could not import parameters from file") |
|
675 | 663 | |
|
676 | 664 | kwargs = {} |
|
677 | 665 | #kwargs['id_dev'] = conf.id |
|
678 | 666 | kwargs['title'] = 'Experiment' |
|
679 | 667 | kwargs['form'] = file_form |
|
680 | 668 | kwargs['suptitle'] = 'Importing file' |
|
681 | 669 | kwargs['button'] = 'Import' |
|
682 | 670 | |
|
683 | 671 | kwargs.update(sidebar(experiment=experiment)) |
|
684 | 672 | |
|
685 | 673 | return render(request, 'experiment_import.html', kwargs) |
|
686 | 674 | |
|
687 | 675 | def experiment_mix(request, id_exp): |
|
688 | 676 | |
|
689 | 677 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
690 | 678 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, |
|
691 | 679 | mix=False)] |
|
692 | 680 | |
|
693 | 681 | if len(rc_confs)<2: |
|
694 | 682 | messages.warning(request, 'You need at least two RC Configurations to make a mix') |
|
695 | 683 | return redirect(experiment.get_absolute_url()) |
|
696 | 684 | |
|
697 | 685 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) |
|
698 | 686 | |
|
699 | 687 | if mix_confs: |
|
700 | 688 | mix = mix_confs[0] |
|
701 | 689 | else: |
|
702 | 690 | mix = RCConfiguration(experiment=experiment, |
|
703 | 691 | device=rc_confs[0].device, |
|
704 | 692 | ipp=rc_confs[0].ipp, |
|
705 | 693 | clock_in=rc_confs[0].clock_in, |
|
706 | 694 | clock_divider=rc_confs[0].clock_divider, |
|
707 | 695 | mix=True, |
|
708 | 696 | parameters='') |
|
709 | 697 | mix.save() |
|
710 | 698 | |
|
711 | 699 | line_type = RCLineType.objects.get(name='mix') |
|
712 | 700 | for i in range(len(rc_confs[0].get_lines())): |
|
713 | 701 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
714 | 702 | line.save() |
|
715 | 703 | |
|
716 | 704 | initial = {'name': mix.name, |
|
717 | 705 | 'result': parse_mix_result(mix.parameters), |
|
718 | 706 | 'delay': 0, |
|
719 | 707 | 'mask': [0,1,2,3,4,5,6,7] |
|
720 | 708 | } |
|
721 | 709 | |
|
722 | 710 | if request.method=='GET': |
|
723 | 711 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
724 | 712 | |
|
725 | 713 | if request.method=='POST': |
|
726 | 714 | |
|
727 | 715 | result = mix.parameters |
|
728 | 716 | |
|
729 | 717 | if '{}|'.format(request.POST['experiment']) in result: |
|
730 | 718 | messages.error(request, 'Configuration already added') |
|
731 | 719 | else: |
|
732 | 720 | if result: |
|
733 | 721 | result = '{}-{}|{}|{}|{}'.format(mix.parameters, |
|
734 | 722 | request.POST['experiment'], |
|
735 | 723 | MIX_MODES[request.POST['operation']], |
|
736 | 724 | float(request.POST['delay']), |
|
737 | 725 | parse_mask(request.POST.getlist('mask')) |
|
738 | 726 | ) |
|
739 | 727 | else: |
|
740 | 728 | result = '{}|{}|{}|{}'.format(request.POST['experiment'], |
|
741 | 729 | MIX_MODES[request.POST['operation']], |
|
742 | 730 | float(request.POST['delay']), |
|
743 | 731 | parse_mask(request.POST.getlist('mask')) |
|
744 | 732 | ) |
|
745 | 733 | |
|
746 | 734 | mix.parameters = result |
|
747 | 735 | mix.name = request.POST['name'] |
|
748 | 736 | mix.save() |
|
749 | 737 | mix.update_pulses() |
|
750 | 738 | |
|
751 | 739 | |
|
752 | 740 | initial['result'] = parse_mix_result(result) |
|
753 | 741 | initial['name'] = mix.name |
|
754 | 742 | |
|
755 | 743 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
756 | 744 | |
|
757 | 745 | |
|
758 | 746 | kwargs = { |
|
759 | 747 | 'title': 'Experiment', |
|
760 | 748 | 'suptitle': 'Mix Configurations', |
|
761 | 749 | 'form' : form, |
|
762 | 750 | 'extra_button': 'Delete', |
|
763 | 751 | 'button': 'Add', |
|
764 | 752 | 'cancel': 'Back', |
|
765 | 753 | 'previous': experiment.get_absolute_url(), |
|
766 | 754 | 'id_exp':id_exp, |
|
767 | 755 | |
|
768 | 756 | } |
|
769 | 757 | |
|
770 | 758 | return render(request, 'experiment_mix.html', kwargs) |
|
771 | 759 | |
|
772 | 760 | |
|
773 | 761 | def experiment_mix_delete(request, id_exp): |
|
774 | 762 | |
|
775 | 763 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) |
|
776 | 764 | values = conf.parameters.split('-') |
|
777 | 765 | conf.parameters = '-'.join(values[:-1]) |
|
778 | 766 | conf.save() |
|
779 | 767 | |
|
780 | 768 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
781 | 769 | |
|
782 | 770 | |
|
783 | 771 | def parse_mix_result(s): |
|
784 | 772 | |
|
785 | 773 | values = s.split('-') |
|
786 | 774 | html = '' |
|
787 | 775 | |
|
788 | 776 | |
|
789 | 777 | for i, value in enumerate(values): |
|
790 | 778 | if not value: |
|
791 | 779 | continue |
|
792 | 780 | pk, mode, delay, mask = value.split('|') |
|
793 | 781 | conf = RCConfiguration.objects.get(pk=pk) |
|
794 | 782 | if i==0: |
|
795 | 783 | html += '{:20.18}{:4}{:9}km{:>6}\r\n'.format( |
|
796 | 784 | conf.name[:18], |
|
797 | 785 | '---', |
|
798 | 786 | delay, |
|
799 | 787 | mask) |
|
800 | 788 | else: |
|
801 | 789 | html += '{:20.18}{:4}{:9}km{:>6}\r\n'.format( |
|
802 | 790 | conf.name[:18], |
|
803 | 791 | mode, |
|
804 | 792 | delay, |
|
805 | 793 | mask) |
|
806 | 794 | |
|
807 | 795 | return mark_safe(html) |
|
808 | 796 | |
|
809 | 797 | def parse_mask(l): |
|
810 | 798 | |
|
811 | 799 | values = [] |
|
812 | 800 | |
|
813 | 801 | for x in range(8): |
|
814 | 802 | if '{}'.format(x) in l: |
|
815 | 803 | values.append(1) |
|
816 | 804 | else: |
|
817 | 805 | values.append(0) |
|
818 | 806 | |
|
819 | 807 | values.reverse() |
|
820 | 808 | |
|
821 | 809 | return int(''.join([str(x) for x in values]), 2) |
|
822 | 810 | |
|
823 | 811 | |
|
824 | 812 | def dev_confs(request): |
|
825 | 813 | |
|
826 | 814 | configurations = Configuration.objects.all().order_by('type', 'device__device_type', 'experiment') |
|
827 | 815 | |
|
828 | 816 | kwargs = {} |
|
829 | 817 | |
|
830 | 818 | kwargs['configuration_keys'] = ['device', 'name', 'experiment', 'type', 'programmed_date'] |
|
831 | 819 | kwargs['configurations'] = configurations |
|
832 | 820 | |
|
833 | 821 | kwargs['title'] = 'Configuration' |
|
834 | 822 | kwargs['suptitle'] = 'List' |
|
835 | 823 | |
|
836 | 824 | return render(request, 'dev_confs.html', kwargs) |
|
837 | 825 | |
|
838 | 826 | |
|
839 | 827 | def dev_conf(request, id_conf): |
|
840 | 828 | |
|
841 | 829 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
842 | 830 | |
|
843 | 831 | return redirect(conf.get_absolute_url()) |
|
844 | 832 | |
|
845 | 833 | |
|
846 | 834 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
847 | 835 | |
|
848 | 836 | initial = {} |
|
849 | 837 | kwargs = {} |
|
850 | 838 | |
|
851 | 839 | if id_exp<>0: |
|
852 | 840 | initial['experiment'] = id_exp |
|
853 | 841 | |
|
854 | 842 | if id_dev<>0: |
|
855 | 843 | initial['device'] = id_dev |
|
856 | 844 | |
|
857 | 845 | if request.method == 'GET': |
|
858 | 846 | |
|
859 | 847 | if id_dev: |
|
860 | 848 | kwargs['button'] = 'Create' |
|
861 | 849 | device = Device.objects.get(pk=id_dev) |
|
862 | 850 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
863 | 851 | initial['name'] = request.GET['name'] |
|
864 | 852 | form = DevConfForm(initial=initial) |
|
865 | 853 | else: |
|
866 | 854 | if 'template' in request.GET: |
|
867 | 855 | if request.GET['template']=='0': |
|
868 | 856 | form = NewForm(initial={'create_from':2}, |
|
869 | 857 | template_choices=Configuration.objects.filter(template=True).values_list('id', 'name')) |
|
870 | 858 | else: |
|
871 | 859 | kwargs['button'] = 'Create' |
|
872 | 860 | conf = Configuration.objects.get(pk=request.GET['template']) |
|
873 | 861 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
874 | 862 | form = DevConfForm(instance=conf, |
|
875 | 863 | initial={'name': '{} [{:%Y/%m/%d}]'.format(conf.name, datetime.now()), |
|
876 | 864 | 'template': False}) |
|
877 | 865 | elif 'blank' in request.GET: |
|
878 | 866 | kwargs['button'] = 'Create' |
|
879 | 867 | form = ConfigurationForm(initial=initial) |
|
880 | 868 | else: |
|
881 | 869 | form = NewForm() |
|
882 | 870 | |
|
883 | 871 | if request.method == 'POST': |
|
884 | 872 | |
|
885 | 873 | device = Device.objects.get(pk=request.POST['device']) |
|
886 | 874 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
887 | 875 | |
|
888 | 876 | form = DevConfForm(request.POST) |
|
889 | 877 | |
|
890 | 878 | if form.is_valid(): |
|
891 | 879 | conf = form.save() |
|
892 | 880 | |
|
893 | 881 | if 'template' in request.GET and conf.device.device_type.name=='rc': |
|
894 | 882 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) |
|
895 | 883 | for line in lines: |
|
896 | 884 | line.clone(rc_configuration=conf) |
|
897 | 885 | |
|
898 | 886 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
899 | 887 | |
|
900 | 888 | |
|
901 | 889 | kwargs['id_exp'] = id_exp |
|
902 | 890 | kwargs['form'] = form |
|
903 | 891 | kwargs['title'] = 'Configuration' |
|
904 | 892 | kwargs['suptitle'] = 'New' |
|
905 | 893 | |
|
906 | 894 | |
|
907 | 895 | if id_dev != 0: |
|
908 | 896 | device = Device.objects.get(pk=id_dev) |
|
909 | 897 | if 'dds' in device.device_type.name: |
|
910 | 898 | kwargs['dds_device'] = True |
|
911 | 899 | |
|
912 | 900 | return render(request, 'dev_conf_edit.html', kwargs) |
|
913 | 901 | |
|
914 | 902 | |
|
915 | 903 | def dev_conf_edit(request, id_conf): |
|
916 | 904 | |
|
917 | 905 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
918 | 906 | |
|
919 | 907 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
920 | 908 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
921 | 909 | |
|
922 | 910 | dev_conf = DevConfModel.objects.get(pk=id_conf) |
|
923 | 911 | |
|
924 | 912 | if request.method=='GET': |
|
925 | 913 | form = DevConfForm(instance=dev_conf) |
|
926 | 914 | |
|
927 | 915 | if request.method=='POST': |
|
928 | 916 | form = DevConfForm(request.POST, instance=dev_conf) |
|
929 | 917 | |
|
930 | 918 | if form.is_valid(): |
|
931 | 919 | form.save() |
|
932 | 920 | return redirect('url_dev_conf', id_conf=id_conf) |
|
933 | 921 | |
|
934 | 922 | kwargs = {} |
|
935 | 923 | kwargs['form'] = form |
|
936 | 924 | kwargs['title'] = 'Device Configuration' |
|
937 | 925 | kwargs['suptitle'] = 'Edit' |
|
938 | 926 | kwargs['button'] = 'Update' |
|
939 | 927 | |
|
940 | 928 | ###### SIDEBAR ###### |
|
941 | 929 | kwargs.update(sidebar(conf=conf)) |
|
942 | 930 | |
|
943 | 931 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
944 | 932 | |
|
945 | 933 | |
|
946 | 934 | def dev_conf_start(request, id_conf): |
|
947 | 935 | |
|
948 | 936 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
949 | 937 | |
|
950 | 938 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
951 | 939 | |
|
952 | 940 | conf = DevConfModel.objects.get(pk=id_conf) |
|
953 | 941 | |
|
954 | 942 | if conf.start_device(): |
|
955 | 943 | messages.success(request, conf.message) |
|
956 | 944 | else: |
|
957 | 945 | messages.error(request, conf.message) |
|
958 | 946 | |
|
959 | 947 | conf.status_device() |
|
960 | 948 | |
|
961 | 949 | return redirect(conf.get_absolute_url()) |
|
962 | 950 | |
|
963 | 951 | |
|
964 | 952 | def dev_conf_stop(request, id_conf): |
|
965 | 953 | |
|
966 | 954 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
967 | 955 | |
|
968 | 956 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
969 | 957 | |
|
970 | 958 | conf = DevConfModel.objects.get(pk=id_conf) |
|
971 | 959 | |
|
972 | 960 | if conf.stop_device(): |
|
973 | 961 | messages.success(request, conf.message) |
|
974 | 962 | else: |
|
975 | 963 | messages.error(request, conf.message) |
|
976 | 964 | |
|
977 | 965 | conf.status_device() |
|
978 | 966 | |
|
979 | 967 | return redirect(conf.get_absolute_url()) |
|
980 | 968 | |
|
981 | 969 | |
|
982 | 970 | def dev_conf_status(request, id_conf): |
|
983 | 971 | |
|
984 | 972 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
985 | 973 | |
|
986 | 974 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
987 | 975 | |
|
988 | 976 | conf = DevConfModel.objects.get(pk=id_conf) |
|
989 | 977 | |
|
990 | 978 | if conf.status_device(): |
|
991 | 979 | messages.success(request, conf.message) |
|
992 | 980 | else: |
|
993 | 981 | messages.error(request, conf.message) |
|
994 | 982 | |
|
995 | 983 | return redirect(conf.get_absolute_url()) |
|
996 | 984 | |
|
997 | 985 | |
|
998 | 986 | def dev_conf_write(request, id_conf): |
|
999 | 987 | |
|
1000 | 988 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1001 | 989 | |
|
1002 | 990 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
1003 | 991 | |
|
1004 | 992 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1005 | 993 | |
|
1006 | 994 | answer = conf.write_device() |
|
1007 | 995 | conf.status_device() |
|
1008 | 996 | |
|
1009 | 997 | if answer: |
|
1010 | 998 | messages.success(request, conf.message) |
|
1011 | 999 | |
|
1012 | 1000 | #Creating a historical configuration |
|
1013 | 1001 | conf.clone(type=0, template=False) |
|
1014 | 1002 | |
|
1015 | 1003 | #Original configuration |
|
1016 | 1004 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1017 | 1005 | else: |
|
1018 | 1006 | messages.error(request, conf.message) |
|
1019 | 1007 | |
|
1020 | 1008 | return redirect(conf.get_absolute_url()) |
|
1021 | 1009 | |
|
1022 | 1010 | |
|
1023 | 1011 | def dev_conf_read(request, id_conf): |
|
1024 | 1012 | |
|
1025 | 1013 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1026 | 1014 | |
|
1027 | 1015 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
1028 | 1016 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1029 | 1017 | |
|
1030 | 1018 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1031 | 1019 | |
|
1032 | 1020 | if request.method=='GET': |
|
1033 | 1021 | |
|
1034 | 1022 | parms = conf.read_device() |
|
1035 | 1023 | conf.status_device() |
|
1036 | 1024 | |
|
1037 | 1025 | if not parms: |
|
1038 | 1026 | messages.error(request, conf.message) |
|
1039 | 1027 | return redirect(conf.get_absolute_url()) |
|
1040 | 1028 | |
|
1041 | 1029 | form = DevConfForm(initial=parms, instance=conf) |
|
1042 | 1030 | |
|
1043 | 1031 | if request.method=='POST': |
|
1044 | 1032 | form = DevConfForm(request.POST, instance=conf) |
|
1045 | 1033 | |
|
1046 | 1034 | if form.is_valid(): |
|
1047 | 1035 | form.save() |
|
1048 | 1036 | return redirect(conf.get_absolute_url()) |
|
1049 | 1037 | |
|
1050 | 1038 | messages.error(request, "Parameters could not be saved") |
|
1051 | 1039 | |
|
1052 | 1040 | kwargs = {} |
|
1053 | 1041 | kwargs['id_dev'] = conf.id |
|
1054 | 1042 | kwargs['form'] = form |
|
1055 | 1043 | kwargs['title'] = 'Device Configuration' |
|
1056 | 1044 | kwargs['suptitle'] = 'Parameters read from device' |
|
1057 | 1045 | kwargs['button'] = 'Save' |
|
1058 | 1046 | |
|
1059 | 1047 | ###### SIDEBAR ###### |
|
1060 | 1048 | kwargs.update(sidebar(conf=conf)) |
|
1061 | 1049 | |
|
1062 | 1050 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
1063 | 1051 | |
|
1064 | 1052 | |
|
1065 | 1053 | def dev_conf_import(request, id_conf): |
|
1066 | 1054 | |
|
1067 | 1055 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1068 | 1056 | |
|
1069 | 1057 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
1070 | 1058 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1071 | 1059 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1072 | 1060 | |
|
1073 | 1061 | if request.method == 'GET': |
|
1074 | 1062 | file_form = UploadFileForm() |
|
1075 | 1063 | |
|
1076 | 1064 | if request.method == 'POST': |
|
1077 | 1065 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1078 | 1066 | |
|
1079 | 1067 | if file_form.is_valid(): |
|
1080 | 1068 | |
|
1081 | 1069 | parms = conf.import_from_file(request.FILES['file']) |
|
1082 | 1070 | |
|
1083 | 1071 | if parms: |
|
1084 | 1072 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
1085 | 1073 | form = DevConfForm(initial=parms, instance=conf) |
|
1086 | 1074 | |
|
1087 | 1075 | kwargs = {} |
|
1088 | 1076 | kwargs['id_dev'] = conf.id |
|
1089 | 1077 | kwargs['form'] = form |
|
1090 | 1078 | kwargs['title'] = 'Device Configuration' |
|
1091 | 1079 | kwargs['suptitle'] = 'Parameters imported' |
|
1092 | 1080 | kwargs['button'] = 'Save' |
|
1093 | 1081 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1094 | 1082 | kwargs['previous'] = conf.get_absolute_url() |
|
1095 | 1083 | |
|
1096 | 1084 | ###### SIDEBAR ###### |
|
1097 | 1085 | kwargs.update(sidebar(conf=conf)) |
|
1098 | 1086 | |
|
1099 | 1087 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1100 | 1088 | |
|
1101 | 1089 | messages.error(request, "Could not import parameters from file") |
|
1102 | 1090 | |
|
1103 | 1091 | kwargs = {} |
|
1104 | 1092 | kwargs['id_dev'] = conf.id |
|
1105 | 1093 | kwargs['title'] = 'Device Configuration' |
|
1106 | 1094 | kwargs['form'] = file_form |
|
1107 | 1095 | kwargs['suptitle'] = 'Importing file' |
|
1108 | 1096 | kwargs['button'] = 'Import' |
|
1109 | 1097 | |
|
1110 | 1098 | kwargs.update(sidebar(conf=conf)) |
|
1111 | 1099 | |
|
1112 | 1100 | return render(request, 'dev_conf_import.html', kwargs) |
|
1113 | 1101 | |
|
1114 | 1102 | |
|
1115 | 1103 | def dev_conf_export(request, id_conf): |
|
1116 | 1104 | |
|
1117 | 1105 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1118 | 1106 | |
|
1119 | 1107 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
1120 | 1108 | |
|
1121 | 1109 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1122 | 1110 | |
|
1123 | 1111 | if request.method == 'GET': |
|
1124 | 1112 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1125 | 1113 | |
|
1126 | 1114 | if request.method == 'POST': |
|
1127 | 1115 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
1128 | 1116 | |
|
1129 | 1117 | if file_form.is_valid(): |
|
1130 | 1118 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
1131 | 1119 | |
|
1132 | 1120 | response = HttpResponse(content_type=fields['content_type']) |
|
1133 | 1121 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
1134 | 1122 | response.write(fields['content']) |
|
1135 | 1123 | |
|
1136 | 1124 | return response |
|
1137 | 1125 | |
|
1138 | 1126 | messages.error(request, "Could not export parameters") |
|
1139 | 1127 | |
|
1140 | 1128 | kwargs = {} |
|
1141 | 1129 | kwargs['id_dev'] = conf.id |
|
1142 | 1130 | kwargs['title'] = 'Device Configuration' |
|
1143 | 1131 | kwargs['form'] = file_form |
|
1144 | 1132 | kwargs['suptitle'] = 'Exporting file' |
|
1145 | 1133 | kwargs['button'] = 'Export' |
|
1146 | 1134 | |
|
1147 | 1135 | return render(request, 'dev_conf_export.html', kwargs) |
|
1148 | 1136 | |
|
1149 | 1137 | |
|
1150 | 1138 | def dev_conf_delete(request, id_conf): |
|
1151 | 1139 | |
|
1152 | 1140 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1153 | 1141 | |
|
1154 | 1142 | if request.method=='POST': |
|
1155 | 1143 | if request.user.is_staff: |
|
1156 | 1144 | conf.delete() |
|
1157 | 1145 | return redirect('url_dev_confs') |
|
1158 | 1146 | |
|
1159 | 1147 | messages.error(request, 'Not enough permission to delete this object') |
|
1160 | 1148 | return redirect(conf.get_absolute_url()) |
|
1161 | 1149 | |
|
1162 | 1150 | kwargs = { |
|
1163 | 1151 | 'title': 'Delete', |
|
1164 | 1152 | 'suptitle': 'Experiment', |
|
1165 | 1153 | 'object': conf, |
|
1166 | 1154 | 'previous': conf.get_absolute_url(), |
|
1167 | 1155 | 'delete': True |
|
1168 | 1156 | } |
|
1169 | 1157 | |
|
1170 | 1158 | return render(request, 'confirm.html', kwargs) |
|
1171 | 1159 | |
|
1172 | 1160 | |
|
1173 | 1161 | def sidebar(**kwargs): |
|
1174 | 1162 | |
|
1175 | 1163 | side_data = {} |
|
1176 | 1164 | |
|
1177 | 1165 | conf = kwargs.get('conf', None) |
|
1178 | 1166 | experiment = kwargs.get('experiment', None) |
|
1179 | 1167 | |
|
1180 | 1168 | if not experiment: |
|
1181 | 1169 | experiment = conf.experiment |
|
1182 | 1170 | |
|
1183 | 1171 | if experiment: |
|
1184 | 1172 | side_data['experiment'] = experiment |
|
1185 | 1173 | campaign = experiment.campaign_set.all() |
|
1186 | 1174 | if campaign: |
|
1187 | 1175 | side_data['campaign'] = campaign[0] |
|
1188 | 1176 | experiments = campaign[0].experiments.all() |
|
1189 | 1177 | else: |
|
1190 | 1178 | experiments = [experiment] |
|
1191 | 1179 | configurations = experiment.configuration_set.filter(type=0) |
|
1192 | 1180 | side_data['side_experiments'] = experiments |
|
1193 | 1181 | side_data['side_configurations'] = configurations |
|
1194 | 1182 | |
|
1195 | 1183 | return side_data |
|
1196 | 1184 | |
|
1197 | 1185 | |
|
1198 | 1186 | def operation(request, id_camp=None): |
|
1199 | 1187 | |
|
1200 | 1188 | if not id_camp: |
|
1201 | 1189 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
1202 | 1190 | |
|
1203 | 1191 | if not campaigns: |
|
1204 | 1192 | kwargs = {} |
|
1205 | 1193 | kwargs['title'] = 'No Campaigns' |
|
1206 | 1194 | kwargs['suptitle'] = 'Empty' |
|
1207 | 1195 | return render(request, 'operation.html', kwargs) |
|
1208 | 1196 | |
|
1209 | 1197 | id_camp = campaigns[0].id |
|
1210 | 1198 | |
|
1211 | 1199 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1212 | 1200 | |
|
1213 | 1201 | if request.method=='GET': |
|
1214 | 1202 | form = OperationForm(initial={'campaign': campaign.id}, length = 5) |
|
1215 | 1203 | |
|
1216 | 1204 | if request.method=='POST': |
|
1217 | 1205 | form = OperationForm(request.POST, initial={'campaign':campaign.id}, length = 5) |
|
1218 | 1206 | |
|
1219 | 1207 | if form.is_valid(): |
|
1220 | 1208 | return redirect('url_operation', id_camp=campaign.id) |
|
1221 | 1209 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
1222 | 1210 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
1223 | 1211 | #for exs in experiments: |
|
1224 | 1212 | # exs.get_status() |
|
1225 | 1213 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
1226 | 1214 | #experiments = [Experiment.objects.filter(location__pk=location.id).filter(campaign__pk=campaign.id) for location in locations] |
|
1227 | 1215 | kwargs = {} |
|
1228 | 1216 | #---Campaign |
|
1229 | 1217 | kwargs['campaign'] = campaign |
|
1230 | 1218 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
1231 | 1219 | #---Experiment |
|
1232 | 1220 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1233 | 1221 | kwargs['experiment_keys'] = keys[1:] |
|
1234 | 1222 | kwargs['experiments'] = experiments |
|
1235 | 1223 | #---Radar |
|
1236 | 1224 | kwargs['locations'] = locations |
|
1237 | 1225 | #---Else |
|
1238 | 1226 | kwargs['title'] = 'Campaign' |
|
1239 | 1227 | kwargs['suptitle'] = campaign.name |
|
1240 | 1228 | kwargs['form'] = form |
|
1241 | 1229 | kwargs['button'] = 'Search' |
|
1242 | 1230 | kwargs['details'] = True |
|
1243 | 1231 | kwargs['search_button'] = True |
|
1244 | 1232 | |
|
1245 | 1233 | return render(request, 'operation.html', kwargs) |
|
1246 | 1234 | |
|
1247 | 1235 | |
|
1248 | 1236 | def operation_search(request, id_camp=None): |
|
1249 | 1237 | |
|
1250 | 1238 | |
|
1251 | 1239 | if not id_camp: |
|
1252 | 1240 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
1253 | 1241 | |
|
1254 | 1242 | if not campaigns: |
|
1255 | 1243 | return render(request, 'operation.html', {}) |
|
1256 | 1244 | |
|
1257 | 1245 | id_camp = campaigns[0].id |
|
1258 | 1246 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1259 | 1247 | |
|
1260 | 1248 | if request.method=='GET': |
|
1261 | 1249 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
1262 | 1250 | |
|
1263 | 1251 | if request.method=='POST': |
|
1264 | 1252 | form = OperationSearchForm(request.POST, initial={'campaign':campaign.id}) |
|
1265 | 1253 | |
|
1266 | 1254 | if form.is_valid(): |
|
1267 | 1255 | return redirect('url_operation', id_camp=campaign.id) |
|
1268 | 1256 | |
|
1269 | 1257 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
1270 | 1258 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
1271 | 1259 | #for exs in experiments: |
|
1272 | 1260 | # exs.get_status() |
|
1273 | 1261 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
1274 | 1262 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
1275 | 1263 | |
|
1276 | 1264 | kwargs = {} |
|
1277 | 1265 | #---Campaign |
|
1278 | 1266 | kwargs['campaign'] = campaign |
|
1279 | 1267 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
1280 | 1268 | #---Experiment |
|
1281 | 1269 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1282 | 1270 | kwargs['experiment_keys'] = keys[1:] |
|
1283 | 1271 | kwargs['experiments'] = experiments |
|
1284 | 1272 | #---Radar |
|
1285 | 1273 | kwargs['locations'] = locations |
|
1286 | 1274 | #---Else |
|
1287 | 1275 | kwargs['title'] = 'Campaign' |
|
1288 | 1276 | kwargs['suptitle'] = campaign.name |
|
1289 | 1277 | kwargs['form'] = form |
|
1290 | 1278 | kwargs['button'] = 'Select' |
|
1291 | 1279 | kwargs['details'] = True |
|
1292 | 1280 | kwargs['search_button'] = False |
|
1293 | 1281 | |
|
1294 | 1282 | return render(request, 'operation.html', kwargs) |
|
1295 | 1283 | |
|
1296 | 1284 | |
|
1297 | 1285 | def radar_play(request, id_camp, id_radar): |
|
1298 | 1286 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1299 | 1287 | radar = get_object_or_404(Location, pk = id_radar) |
|
1300 | 1288 | today = datetime.today() |
|
1301 | 1289 | now = today.time() |
|
1302 | 1290 | |
|
1303 | 1291 | #--Clear Old Experiments From RunningExperiment Object |
|
1304 | 1292 | running_experiment = RunningExperiment.objects.filter(radar=radar) |
|
1305 | 1293 | if running_experiment: |
|
1306 | 1294 | running_experiment = running_experiment[0] |
|
1307 | 1295 | running_experiment.running_experiment.clear() |
|
1308 | 1296 | running_experiment.save() |
|
1309 | 1297 | |
|
1310 | 1298 | #--If campaign datetime is ok: |
|
1311 | 1299 | if today >= campaign.start_date and today <= campaign.end_date: |
|
1312 | 1300 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1313 | 1301 | for exp in experiments: |
|
1314 | 1302 | #--If experiment time is ok: |
|
1315 | 1303 | if now >= exp.start_time and now <= exp.end_time: |
|
1316 | 1304 | configurations = Configuration.objects.filter(experiment = exp) |
|
1317 | 1305 | for conf in configurations: |
|
1318 | 1306 | if 'cgs' in conf.device.device_type.name: |
|
1319 | 1307 | conf.status_device() |
|
1320 | 1308 | else: |
|
1321 | 1309 | answer = conf.start_device() |
|
1322 | 1310 | conf.status_device() |
|
1323 | 1311 | #--Running Experiment |
|
1324 | 1312 | old_running_experiment = RunningExperiment.objects.filter(radar=radar) |
|
1325 | 1313 | #--If RunningExperiment element exists |
|
1326 | 1314 | if old_running_experiment: |
|
1327 | 1315 | old_running_experiment = old_running_experiment[0] |
|
1328 | 1316 | old_running_experiment.running_experiment.add(exp) |
|
1329 | 1317 | old_running_experiment.status = 3 |
|
1330 | 1318 | old_running_experiment.save() |
|
1331 | 1319 | #--Create a new Running_Experiment Object |
|
1332 | 1320 | else: |
|
1333 | 1321 | new_running_experiment = RunningExperiment( |
|
1334 | 1322 | radar = radar, |
|
1335 | 1323 | status = 3, |
|
1336 | 1324 | ) |
|
1337 | 1325 | new_running_experiment.save() |
|
1338 | 1326 | new_running_experiment.running_experiment.add(exp) |
|
1339 | 1327 | new_running_experiment.save() |
|
1340 | 1328 | |
|
1341 | 1329 | if answer: |
|
1342 | 1330 | messages.success(request, conf.message) |
|
1343 | 1331 | exp.status=2 |
|
1344 | 1332 | exp.save() |
|
1345 | 1333 | else: |
|
1346 | 1334 | messages.error(request, conf.message) |
|
1347 | 1335 | else: |
|
1348 | 1336 | if exp.status == 1 or exp.status == 3: |
|
1349 | 1337 | exp.status=3 |
|
1350 | 1338 | exp.save() |
|
1351 | 1339 | |
|
1352 | 1340 | |
|
1353 | 1341 | route = request.META['HTTP_REFERER'] |
|
1354 | 1342 | route = str(route) |
|
1355 | 1343 | if 'search' in route: |
|
1356 | 1344 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1357 | 1345 | else: |
|
1358 | 1346 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1359 | 1347 | |
|
1360 | 1348 | |
|
1361 | 1349 | def radar_stop(request, id_camp, id_radar): |
|
1362 | 1350 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1363 | 1351 | radar = get_object_or_404(Location, pk = id_radar) |
|
1364 | 1352 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1365 | 1353 | |
|
1366 | 1354 | for exp in experiments: |
|
1367 | 1355 | configurations = Configuration.objects.filter(experiment = exp) |
|
1368 | 1356 | for conf in configurations: |
|
1369 | 1357 | if 'cgs' in conf.device.device_type.name: |
|
1370 | 1358 | conf.status_device() |
|
1371 | 1359 | else: |
|
1372 | 1360 | answer = conf.stop_device() |
|
1373 | 1361 | conf.status_device() |
|
1374 | 1362 | |
|
1375 | 1363 | if answer: |
|
1376 | 1364 | messages.success(request, conf.message) |
|
1377 | 1365 | exp.status=1 |
|
1378 | 1366 | exp.save() |
|
1379 | 1367 | else: |
|
1380 | 1368 | messages.error(request, conf.message) |
|
1381 | 1369 | |
|
1382 | 1370 | |
|
1383 | 1371 | route = request.META['HTTP_REFERER'] |
|
1384 | 1372 | route = str(route) |
|
1385 | 1373 | if 'search' in route: |
|
1386 | 1374 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1387 | 1375 | else: |
|
1388 | 1376 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1389 | 1377 | |
|
1390 | 1378 | |
|
1391 | 1379 | def radar_refresh(request, id_camp, id_radar): |
|
1392 | 1380 | |
|
1393 | 1381 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1394 | 1382 | radar = get_object_or_404(Location, pk = id_radar) |
|
1395 | 1383 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1396 | 1384 | for exs in experiments: |
|
1397 | 1385 | exs.get_status() |
|
1398 | 1386 | |
|
1399 | 1387 | route = request.META['HTTP_REFERER'] |
|
1400 | 1388 | route = str(route) |
|
1401 | 1389 | if 'search' in route: |
|
1402 | 1390 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1403 | 1391 | else: |
|
1404 | 1392 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1405 | 1393 |
General Comments 0
You need to be logged in to leave comments.
Login now