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