@@ -1,151 +1,154 | |||
|
1 | 1 | from django import forms |
|
2 | 2 | from django.utils.safestring import mark_safe |
|
3 | 3 | |
|
4 | 4 | from .models import DeviceType, Device, Experiment, Campaign, Configuration, Location |
|
5 | 5 | |
|
6 | 6 | FILE_FORMAT = ( |
|
7 | 7 | ('json', 'json'), |
|
8 | 8 | ) |
|
9 | 9 | |
|
10 | 10 | DDS_FILE_FORMAT = ( |
|
11 | 11 | ('json', 'json'), |
|
12 | 12 | ('text', 'dds') |
|
13 | 13 | ) |
|
14 | 14 | |
|
15 | 15 | RC_FILE_FORMAT = ( |
|
16 | 16 | ('json', 'json'), |
|
17 | 17 | ('text', 'racp'), |
|
18 | 18 | ('binary', 'dat'), |
|
19 | 19 | ) |
|
20 | 20 | |
|
21 | 21 | def add_empty_choice(choices, pos=0, label='-----'): |
|
22 | 22 | if len(choices)>0: |
|
23 | 23 | choices = list(choices) |
|
24 | 24 | choices.insert(0, (0, label)) |
|
25 | 25 | return choices |
|
26 | 26 | else: |
|
27 | 27 | return [(0, label)] |
|
28 | 28 | |
|
29 | 29 | class DatepickerWidget(forms.widgets.TextInput): |
|
30 | 30 | def render(self, name, value, attrs=None): |
|
31 | 31 | input_html = super(DatepickerWidget, self).render(name, value, attrs) |
|
32 | 32 | html = '<div class="input-group date">'+input_html+'<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span></div>' |
|
33 | 33 | return mark_safe(html) |
|
34 | 34 | |
|
35 | 35 | class TimepickerWidget(forms.widgets.TextInput): |
|
36 | 36 | def render(self, name, value, attrs=None): |
|
37 | 37 | input_html = super(TimepickerWidget, self).render(name, value, attrs) |
|
38 | 38 | html = '<div class="input-group time">'+input_html+'<span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span></div>' |
|
39 | 39 | return mark_safe(html) |
|
40 | 40 | |
|
41 | 41 | class CampaignForm(forms.ModelForm): |
|
42 | 42 | |
|
43 | 43 | experiments = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), |
|
44 | 44 | queryset=Experiment.objects.filter(template=True), |
|
45 | 45 | required=False) |
|
46 | 46 | |
|
47 | 47 | def __init__(self, *args, **kwargs): |
|
48 | 48 | super(CampaignForm, self).__init__(*args, **kwargs) |
|
49 | 49 | self.fields['start_date'].widget = DatepickerWidget(self.fields['start_date'].widget.attrs) |
|
50 | 50 | self.fields['end_date'].widget = DatepickerWidget(self.fields['end_date'].widget.attrs) |
|
51 | 51 | self.fields['description'].widget.attrs = {'rows': 2} |
|
52 | ||
|
53 | if self.instance: | |
|
54 | self.fields['experiments'].queryset |= self.instance.experiments.all() | |
|
52 | 55 | |
|
53 | 56 | class Meta: |
|
54 | 57 | model = Campaign |
|
55 | 58 | exclude = [''] |
|
56 | 59 | |
|
57 | 60 | class ExperimentForm(forms.ModelForm): |
|
58 | 61 | |
|
59 | 62 | def __init__(self, *args, **kwargs): |
|
60 | 63 | super(ExperimentForm, self).__init__(*args, **kwargs) |
|
61 | 64 | self.fields['start_time'].widget = TimepickerWidget(self.fields['start_time'].widget.attrs) |
|
62 | 65 | self.fields['end_time'].widget = TimepickerWidget(self.fields['end_time'].widget.attrs) |
|
63 | 66 | |
|
64 | 67 | class Meta: |
|
65 | 68 | model = Experiment |
|
66 | 69 | exclude = ['status'] |
|
67 | 70 | |
|
68 | 71 | class LocationForm(forms.ModelForm): |
|
69 | 72 | class Meta: |
|
70 | 73 | model = Location |
|
71 | 74 | exclude = [''] |
|
72 | 75 | |
|
73 | 76 | class DeviceForm(forms.ModelForm): |
|
74 | 77 | class Meta: |
|
75 | 78 | model = Device |
|
76 | 79 | exclude = ['status'] |
|
77 | 80 | |
|
78 | 81 | class ConfigurationForm(forms.ModelForm): |
|
79 | 82 | |
|
80 | 83 | def __init__(self, *args, **kwargs): |
|
81 | 84 | super(ConfigurationForm, self).__init__(*args, **kwargs) |
|
82 | 85 | |
|
83 | 86 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
84 | 87 | self.fields['experiment'].widget.attrs['disabled'] = 'disabled' |
|
85 | 88 | |
|
86 | 89 | class Meta: |
|
87 | 90 | model = Configuration |
|
88 | 91 | exclude = ['type', 'created_date', 'programmed_date', 'parameters'] |
|
89 | 92 | |
|
90 | 93 | class DeviceTypeForm(forms.Form): |
|
91 | 94 | device_type = forms.ChoiceField(choices=add_empty_choice(DeviceType.objects.all().order_by('name').values_list('id', 'name'))) |
|
92 | 95 | |
|
93 | 96 | |
|
94 | 97 | class UploadFileForm(forms.Form): |
|
95 | 98 | |
|
96 | 99 | file = forms.FileField() |
|
97 | 100 | |
|
98 | 101 | class DownloadFileForm(forms.Form): |
|
99 | 102 | |
|
100 | 103 | format = forms.ChoiceField(choices= ((0, 'json'),) ) |
|
101 | 104 | |
|
102 | 105 | def __init__(self, device_type, *args, **kwargs): |
|
103 | 106 | |
|
104 | 107 | super(DownloadFileForm, self).__init__(*args, **kwargs) |
|
105 | 108 | |
|
106 | 109 | self.fields['format'].choices = FILE_FORMAT |
|
107 | 110 | |
|
108 | 111 | if device_type == 'dds': |
|
109 | 112 | self.fields['format'].choices = DDS_FILE_FORMAT |
|
110 | 113 | |
|
111 | 114 | if device_type == 'rc': |
|
112 | 115 | self.fields['format'].choices = RC_FILE_FORMAT |
|
113 | 116 | |
|
114 | 117 | class OperationForm(forms.Form): |
|
115 | 118 | # today = datetime.today() |
|
116 | 119 | # -----Campaigns from this month------[:5] |
|
117 | 120 | campaign = forms.ChoiceField(label="Campaign") |
|
118 | 121 | |
|
119 | 122 | def __init__(self, *args, **kwargs): |
|
120 | 123 | |
|
121 | 124 | if 'length' not in kwargs.keys(): |
|
122 | 125 | length = None |
|
123 | 126 | else: |
|
124 | 127 | length = kwargs['length'] |
|
125 | 128 | |
|
126 | 129 | kwargs.pop('length') |
|
127 | 130 | |
|
128 | 131 | super(OperationForm, self).__init__(*args, **kwargs) |
|
129 | 132 | self.fields['campaign'].choices=Campaign.objects.all().order_by('-start_date').values_list('id', 'name')[:length] |
|
130 | 133 | |
|
131 | 134 | class OperationSearchForm(forms.Form): |
|
132 | 135 | # -----ALL Campaigns------ |
|
133 | 136 | campaign = forms.ChoiceField(label="Campaign") |
|
134 | 137 | |
|
135 | 138 | def __init__(self, *args, **kwargs): |
|
136 | 139 | super(OperationSearchForm, self).__init__(*args, **kwargs) |
|
137 | 140 | self.fields['campaign'].choices=Campaign.objects.all().order_by('-start_date').values_list('id', 'name') |
|
138 | 141 | |
|
139 | 142 | class NewForm(forms.Form): |
|
140 | 143 | |
|
141 | 144 | create_from = forms.ChoiceField(choices=((0, '-----'), |
|
142 | 145 | (1, 'Empty (blank)'), |
|
143 | 146 | (2, 'Template'))) |
|
144 | 147 | choose_template = forms.ChoiceField() |
|
145 | 148 | |
|
146 | 149 | def __init__(self, *args, **kwargs): |
|
147 | 150 | |
|
148 | 151 | template_choices = kwargs.pop('template_choices', []) |
|
149 | 152 | super(NewForm, self).__init__(*args, **kwargs) |
|
150 | 153 | self.fields['choose_template'].choices = add_empty_choice(template_choices) |
|
151 | 154 | No newline at end of file |
@@ -1,387 +1,390 | |||
|
1 | 1 | |
|
2 | 2 | from datetime import datetime |
|
3 | 3 | |
|
4 | 4 | from django.db import models |
|
5 | 5 | from polymorphic import PolymorphicModel |
|
6 | 6 | |
|
7 | 7 | from django.core.urlresolvers import reverse |
|
8 | 8 | |
|
9 | 9 | CONF_STATES = ( |
|
10 | 10 | (0, 'Disconnected'), |
|
11 | 11 | (1, 'Connected'), |
|
12 | 12 | (2, 'Running'), |
|
13 | 13 | ) |
|
14 | 14 | |
|
15 | 15 | EXP_STATES = ( |
|
16 | 16 | (0,'Error'), #RED |
|
17 | 17 | (1,'Configurated'), #BLUE |
|
18 | 18 | (2,'Running'), #GREEN |
|
19 | 19 | (3,'Waiting'), #YELLOW |
|
20 | 20 | (4,'Not Configured'), #WHITE |
|
21 | 21 | ) |
|
22 | 22 | |
|
23 | 23 | CONF_TYPES = ( |
|
24 | 24 | (0, 'Active'), |
|
25 | 25 | (1, 'Historical'), |
|
26 | 26 | ) |
|
27 | 27 | |
|
28 | 28 | DEV_STATES = ( |
|
29 | 29 | (0, 'No connected'), |
|
30 | 30 | (1, 'Connected'), |
|
31 | 31 | (2, 'Configured'), |
|
32 | 32 | (3, 'Running'), |
|
33 | 33 | ) |
|
34 | 34 | |
|
35 | 35 | DEV_TYPES = ( |
|
36 | 36 | ('', 'Select a device type'), |
|
37 | 37 | ('rc', 'Radar Controller'), |
|
38 | 38 | ('dds', 'Direct Digital Synthesizer'), |
|
39 | 39 | ('jars', 'Jicamarca Radar Acquisition System'), |
|
40 | 40 | ('usrp', 'Universal Software Radio Peripheral'), |
|
41 | 41 | ('cgs', 'Clock Generator System'), |
|
42 | 42 | ('abs', 'Automatic Beam Switching'), |
|
43 | 43 | ) |
|
44 | 44 | |
|
45 | 45 | DEV_PORTS = { |
|
46 | 46 | 'rc' : 2000, |
|
47 | 47 | 'dds' : 2000, |
|
48 | 48 | 'jars' : 2000, |
|
49 | 49 | 'usrp' : 2000, |
|
50 | 50 | 'cgs' : 8080, |
|
51 | 51 | 'abs' : 8080 |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | RADAR_STATES = ( |
|
55 | 55 | (0, 'No connected'), |
|
56 | 56 | (1, 'Connected'), |
|
57 | 57 | (2, 'Configured'), |
|
58 | 58 | (3, 'Running'), |
|
59 | 59 | (4, 'Scheduled'), |
|
60 | 60 | ) |
|
61 | 61 | # Create your models here. |
|
62 | 62 | |
|
63 | 63 | class Location(models.Model): |
|
64 | 64 | |
|
65 | 65 | name = models.CharField(max_length = 30) |
|
66 | 66 | description = models.TextField(blank=True, null=True) |
|
67 | 67 | |
|
68 | 68 | class Meta: |
|
69 | 69 | db_table = 'db_location' |
|
70 | 70 | |
|
71 | 71 | def __unicode__(self): |
|
72 | 72 | return u'%s' % self.name |
|
73 | 73 | |
|
74 | 74 | def get_absolute_url(self): |
|
75 | 75 | return reverse('url_device', args=[str(self.id)]) |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | class DeviceType(models.Model): |
|
79 | 79 | |
|
80 | 80 | name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'rc') |
|
81 | 81 | description = models.TextField(blank=True, null=True) |
|
82 | 82 | |
|
83 | 83 | class Meta: |
|
84 | 84 | db_table = 'db_device_types' |
|
85 | 85 | |
|
86 | 86 | def __unicode__(self): |
|
87 | 87 | return u'%s' % self.get_name_display() |
|
88 | 88 | |
|
89 | 89 | class Device(models.Model): |
|
90 | 90 | |
|
91 | 91 | device_type = models.ForeignKey(DeviceType, on_delete=models.CASCADE) |
|
92 | 92 | location = models.ForeignKey(Location, on_delete=models.CASCADE) |
|
93 | 93 | |
|
94 | 94 | name = models.CharField(max_length=40, default='') |
|
95 | 95 | ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0') |
|
96 | 96 | port_address = models.PositiveSmallIntegerField(default=2000) |
|
97 | 97 | description = models.TextField(blank=True, null=True) |
|
98 | 98 | status = models.PositiveSmallIntegerField(default=0, choices=DEV_STATES) |
|
99 | 99 | |
|
100 | 100 | class Meta: |
|
101 | 101 | db_table = 'db_devices' |
|
102 | 102 | |
|
103 | 103 | def __unicode__(self): |
|
104 | 104 | return u'%s | %s' % (self.name, self.ip_address) |
|
105 | 105 | |
|
106 | 106 | def get_status(self): |
|
107 | 107 | return self.status |
|
108 | 108 | |
|
109 | 109 | def get_absolute_url(self): |
|
110 | 110 | return reverse('url_device', args=[str(self.id)]) |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | class Campaign(models.Model): |
|
114 | 114 | |
|
115 | 115 | template = models.BooleanField(default=False) |
|
116 | 116 | name = models.CharField(max_length=60, unique=True) |
|
117 | 117 | start_date = models.DateTimeField(blank=True, null=True) |
|
118 | 118 | end_date = models.DateTimeField(blank=True, null=True) |
|
119 | 119 | tags = models.CharField(max_length=40) |
|
120 | 120 | description = models.TextField(blank=True, null=True) |
|
121 | 121 | experiments = models.ManyToManyField('Experiment', blank=True) |
|
122 | 122 | |
|
123 | 123 | class Meta: |
|
124 | 124 | db_table = 'db_campaigns' |
|
125 | 125 | ordering = ('name',) |
|
126 | 126 | |
|
127 | 127 | def __unicode__(self): |
|
128 | 128 | return u'%s' % (self.name) |
|
129 | 129 | |
|
130 | 130 | def get_absolute_url(self): |
|
131 | 131 | return reverse('url_campaign', args=[str(self.id)]) |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | class RunningExperiment(models.Model): |
|
135 | 135 | radar = models.OneToOneField('Location', on_delete=models.CASCADE) |
|
136 | 136 | running_experiment = models.ManyToManyField('Experiment') |
|
137 | 137 | status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES) |
|
138 | 138 | |
|
139 | 139 | |
|
140 | 140 | class Experiment(models.Model): |
|
141 | 141 | |
|
142 | 142 | template = models.BooleanField(default=False) |
|
143 | 143 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) |
|
144 | 144 | name = models.CharField(max_length=40, default='', unique=True) |
|
145 | 145 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) |
|
146 | 146 | start_time = models.TimeField(default='00:00:00') |
|
147 | 147 | end_time = models.TimeField(default='23:59:59') |
|
148 | 148 | status = models.PositiveSmallIntegerField(default=0, choices=EXP_STATES) |
|
149 | 149 | |
|
150 | 150 | class Meta: |
|
151 | 151 | db_table = 'db_experiments' |
|
152 |
ordering = ('name' |
|
|
152 | ordering = ('template', 'name') | |
|
153 | 153 | |
|
154 | 154 | def __unicode__(self): |
|
155 | return u'%s' % (self.name) | |
|
155 | if self.template: | |
|
156 | return u'%s (template)' % (self.name) | |
|
157 | else: | |
|
158 | return u'%s' % (self.name) | |
|
156 | 159 | |
|
157 | 160 | @property |
|
158 | 161 | def radar(self): |
|
159 | 162 | return self.location |
|
160 | 163 | |
|
161 | 164 | def clone(self, **kwargs): |
|
162 | 165 | |
|
163 | 166 | confs = Configuration.objects.filter(experiment=self, type=0) |
|
164 | 167 | self.pk = None |
|
165 | 168 | self.name = '{} [{:%Y/%m/%d}]'.format(self.name, datetime.now()) |
|
166 | 169 | for attr, value in kwargs.items(): |
|
167 | 170 | setattr(self, attr, value) |
|
168 | 171 | |
|
169 | 172 | self.save() |
|
170 | 173 | |
|
171 | 174 | for conf in confs: |
|
172 | 175 | conf.clone(experiment=self, template=False) |
|
173 | 176 | |
|
174 | 177 | return self |
|
175 | 178 | |
|
176 | 179 | def get_status(self): |
|
177 | 180 | configurations = Configuration.objects.filter(experiment=self) |
|
178 | 181 | exp_status=[] |
|
179 | 182 | for conf in configurations: |
|
180 | 183 | print conf.status_device() |
|
181 | 184 | exp_status.append(conf.status_device()) |
|
182 | 185 | |
|
183 | 186 | if not exp_status: #No Configuration |
|
184 | 187 | self.status = 4 |
|
185 | 188 | self.save() |
|
186 | 189 | return |
|
187 | 190 | |
|
188 | 191 | total = 1 |
|
189 | 192 | for e_s in exp_status: |
|
190 | 193 | total = total*e_s |
|
191 | 194 | |
|
192 | 195 | if total == 0: #Error |
|
193 | 196 | status = 0 |
|
194 | 197 | elif total == (3**len(exp_status)): #Running |
|
195 | 198 | status = 2 |
|
196 | 199 | else: |
|
197 | 200 | status = 1 #Configurated |
|
198 | 201 | |
|
199 | 202 | self.status = status |
|
200 | 203 | self.save() |
|
201 | 204 | |
|
202 | 205 | def status_color(self): |
|
203 | 206 | color = 'danger' |
|
204 | 207 | if self.status == 0: |
|
205 | 208 | color = "danger" |
|
206 | 209 | elif self.status == 1: |
|
207 | 210 | color = "info" |
|
208 | 211 | elif self.status == 2: |
|
209 | color = "succes" | |
|
212 | color = "success" | |
|
210 | 213 | elif self.status == 3: |
|
211 | 214 | color = "warning" |
|
212 | 215 | else: |
|
213 | 216 | color = "muted" |
|
214 | 217 | |
|
215 | 218 | return color |
|
216 | 219 | |
|
217 | 220 | def get_absolute_url(self): |
|
218 | 221 | return reverse('url_experiment', args=[str(self.id)]) |
|
219 | 222 | |
|
220 | 223 | |
|
221 | 224 | class Configuration(PolymorphicModel): |
|
222 | 225 | |
|
223 | 226 | template = models.BooleanField(default=False) |
|
224 | 227 | |
|
225 | 228 | name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') |
|
226 | 229 | |
|
227 | 230 | experiment = models.ForeignKey('Experiment', null=True, blank=True, on_delete=models.CASCADE) |
|
228 | 231 | device = models.ForeignKey(Device, on_delete=models.CASCADE) |
|
229 | 232 | |
|
230 | 233 | type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) |
|
231 | 234 | |
|
232 | 235 | created_date = models.DateTimeField(auto_now_add=True) |
|
233 | 236 | programmed_date = models.DateTimeField(auto_now=True) |
|
234 | 237 | |
|
235 | 238 | parameters = models.TextField(default='{}') |
|
236 | 239 | |
|
237 | 240 | message = "" |
|
238 | 241 | |
|
239 | 242 | class Meta: |
|
240 | 243 | db_table = 'db_configurations' |
|
241 | 244 | |
|
242 | 245 | def __unicode__(self): |
|
243 | 246 | |
|
244 | 247 | if self.experiment: |
|
245 | 248 | return u'[%s, %s]: %s' % (self.experiment.name, |
|
246 | 249 | self.device.name, |
|
247 | 250 | self.name) |
|
248 | 251 | else: |
|
249 | 252 | return u'%s' % self.device.name |
|
250 | 253 | |
|
251 | 254 | def clone(self, **kwargs): |
|
252 | 255 | |
|
253 | 256 | self.pk = None |
|
254 | 257 | self.id = None |
|
255 | 258 | for attr, value in kwargs.items(): |
|
256 | 259 | setattr(self, attr, value) |
|
257 | 260 | |
|
258 | 261 | self.save() |
|
259 | 262 | |
|
260 | 263 | return self |
|
261 | 264 | |
|
262 | 265 | def parms_to_dict(self): |
|
263 | 266 | |
|
264 | 267 | parameters = {} |
|
265 | 268 | |
|
266 | 269 | for key in self.__dict__.keys(): |
|
267 | 270 | parameters[key] = getattr(self, key) |
|
268 | 271 | |
|
269 | 272 | return parameters |
|
270 | 273 | |
|
271 | 274 | def parms_to_text(self): |
|
272 | 275 | |
|
273 | 276 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
274 | 277 | |
|
275 | 278 | return '' |
|
276 | 279 | |
|
277 | 280 | def parms_to_binary(self): |
|
278 | 281 | |
|
279 | 282 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
280 | 283 | |
|
281 | 284 | return '' |
|
282 | 285 | |
|
283 | 286 | def dict_to_parms(self, parameters): |
|
284 | 287 | |
|
285 | 288 | if type(parameters) != type({}): |
|
286 | 289 | return |
|
287 | 290 | |
|
288 | 291 | for key in parameters.keys(): |
|
289 | 292 | setattr(self, key, parameters[key]) |
|
290 | 293 | |
|
291 | 294 | def export_to_file(self, format="json"): |
|
292 | 295 | |
|
293 | 296 | import json |
|
294 | 297 | |
|
295 | 298 | content_type = '' |
|
296 | 299 | |
|
297 | 300 | if format == 'text': |
|
298 | 301 | content_type = 'text/plain' |
|
299 | 302 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) |
|
300 | 303 | content = self.parms_to_text() |
|
301 | 304 | |
|
302 | 305 | if format == 'binary': |
|
303 | 306 | content_type = 'application/octet-stream' |
|
304 | 307 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) |
|
305 | 308 | content = self.parms_to_binary() |
|
306 | 309 | |
|
307 | 310 | if not content_type: |
|
308 | 311 | content_type = 'application/json' |
|
309 | 312 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) |
|
310 | 313 | content = json.dumps(self.parms_to_dict(), indent=2) |
|
311 | 314 | |
|
312 | 315 | fields = {'content_type':content_type, |
|
313 | 316 | 'filename':filename, |
|
314 | 317 | 'content':content |
|
315 | 318 | } |
|
316 | 319 | |
|
317 | 320 | return fields |
|
318 | 321 | |
|
319 | 322 | def import_from_file(self, fp): |
|
320 | 323 | |
|
321 | 324 | import os, json |
|
322 | 325 | |
|
323 | 326 | parms = {} |
|
324 | 327 | |
|
325 | 328 | path, ext = os.path.splitext(fp.name) |
|
326 | 329 | |
|
327 | 330 | if ext == '.json': |
|
328 | 331 | parms = json.load(fp) |
|
329 | 332 | |
|
330 | 333 | return parms |
|
331 | 334 | |
|
332 | 335 | def status_device(self): |
|
333 | 336 | |
|
334 | 337 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
335 | 338 | |
|
336 | 339 | return None |
|
337 | 340 | |
|
338 | 341 | def stop_device(self): |
|
339 | 342 | |
|
340 | 343 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
341 | 344 | |
|
342 | 345 | return None |
|
343 | 346 | |
|
344 | 347 | def start_device(self): |
|
345 | 348 | |
|
346 | 349 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
347 | 350 | |
|
348 | 351 | return None |
|
349 | 352 | |
|
350 | 353 | def write_device(self, parms): |
|
351 | 354 | |
|
352 | 355 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
353 | 356 | |
|
354 | 357 | return None |
|
355 | 358 | |
|
356 | 359 | def read_device(self): |
|
357 | 360 | |
|
358 | 361 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
359 | 362 | |
|
360 | 363 | return None |
|
361 | 364 | |
|
362 | 365 | def get_absolute_url(self): |
|
363 | 366 | return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
364 | 367 | |
|
365 | 368 | def get_absolute_url_edit(self): |
|
366 | 369 | return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
367 | 370 | |
|
368 | 371 | def get_absolute_url_import(self): |
|
369 | 372 | return reverse('url_import_dev_conf', args=[str(self.id)]) |
|
370 | 373 | |
|
371 | 374 | def get_absolute_url_export(self): |
|
372 | 375 | return reverse('url_export_dev_conf', args=[str(self.id)]) |
|
373 | 376 | |
|
374 | 377 | def get_absolute_url_write(self): |
|
375 | 378 | return reverse('url_write_dev_conf', args=[str(self.id)]) |
|
376 | 379 | |
|
377 | 380 | def get_absolute_url_read(self): |
|
378 | 381 | return reverse('url_read_dev_conf', args=[str(self.id)]) |
|
379 | 382 | |
|
380 | 383 | def get_absolute_url_start(self): |
|
381 | 384 | return reverse('url_start_dev_conf', args=[str(self.id)]) |
|
382 | 385 | |
|
383 | 386 | def get_absolute_url_stop(self): |
|
384 | 387 | return reverse('url_stop_dev_conf', args=[str(self.id)]) |
|
385 | 388 | |
|
386 | 389 | def get_absolute_url_status(self): |
|
387 | 390 | return reverse('url_status_dev_conf', args=[str(self.id)]) No newline at end of file |
@@ -1,80 +1,80 | |||
|
1 | 1 | {% extends "base.html" %} |
|
2 | 2 | {% load bootstrap3 %} |
|
3 | 3 | {% load static %} |
|
4 | 4 | {% load main_tags %} |
|
5 | 5 | {% block extra-head %} |
|
6 | 6 | <link href="{% static 'css/bootstrap-datetimepicker.min.css' %}" media="screen" rel="stylesheet"> |
|
7 | 7 | {% endblock %} |
|
8 | 8 | |
|
9 | 9 | {% block exp-active %}active{% endblock %} |
|
10 | 10 | |
|
11 | 11 | {% block content-title %}{{title}}{% endblock %} |
|
12 | 12 | {% block content-suptitle %}{{suptitle}}{% endblock %} |
|
13 | 13 | |
|
14 | 14 | {% block content %} |
|
15 | 15 | <table class="table table-bordered"> |
|
16 | 16 | {% for key in experiment_keys %} |
|
17 | 17 | <tr><th>{{key|title}}</th><td>{{experiment|attr:key}}</td></tr> |
|
18 | 18 | {% endfor %} |
|
19 | 19 | </table> |
|
20 | 20 | <button class="btn btn-primary pull-right" style="margin-left: 10px" id="bt_export">Export</button> |
|
21 | 21 | <button class="btn btn-primary pull-right" style="margin-left: 10px" id="bt_edit">Edit</button> |
|
22 | 22 | <br></br> |
|
23 | 23 | <br></br> |
|
24 | 24 | |
|
25 | 25 | <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> |
|
26 | 26 | |
|
27 | 27 | <div class="panel panel-default"> |
|
28 | 28 | <div class="panel-heading" role="tab" id="headingTwo"> |
|
29 | 29 | <h4 class="panel-title"> |
|
30 | 30 | <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseThree"> |
|
31 | 31 | Device Configurations |
|
32 | 32 | </a> |
|
33 | 33 | </h4> |
|
34 | 34 | </div> |
|
35 | 35 | <div id="collapseTwo" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingTwo"> |
|
36 | 36 | <div class="panel-body"> |
|
37 | 37 | <table class="table table-hover"> |
|
38 | 38 | <tr> |
|
39 | 39 | <th>#</th> |
|
40 |
{% for key in configuration_ |
|
|
40 | {% for key in configuration_keys %} | |
|
41 | 41 | <th>{{ key|title }}</th> |
|
42 | 42 | {% endfor%} |
|
43 | 43 | </tr> |
|
44 | 44 | {% for item in configurations %} |
|
45 | 45 | <tr class="clickable-row" data-href="{{item.get_absolute_url}}"> |
|
46 | 46 | <td>{{ forloop.counter }}</td> |
|
47 | 47 | {% for key in configuration_keys %} |
|
48 | 48 | <td>{{ item|value:key }}</td> |
|
49 | 49 | {% endfor %} |
|
50 | 50 | </tr> |
|
51 | 51 | {% endfor %} |
|
52 | 52 | </table> |
|
53 | 53 | <button class="btn btn-primary pull-right" id="bt_add_conf">{{button}}</button> |
|
54 | 54 | </div> |
|
55 | 55 | </div> |
|
56 | 56 | </div> |
|
57 | 57 | </div> |
|
58 | 58 | {% endblock %} |
|
59 | 59 | |
|
60 | 60 | {% block sidebar%} |
|
61 | 61 | {% include "sidebar_devices.html" %} |
|
62 | 62 | {% endblock %} |
|
63 | 63 | |
|
64 | 64 | {% block extra-js%} |
|
65 | 65 | <script type="text/javascript"> |
|
66 | 66 | |
|
67 | 67 | $(".clickable-row").click(function() { |
|
68 | 68 | document.location = $(this).data("href"); |
|
69 | 69 | }); |
|
70 | 70 | |
|
71 | 71 | $("#bt_edit").click(function() { |
|
72 | 72 | document.location = "{% url 'url_edit_experiment' experiment.id%}"; |
|
73 | 73 | }); |
|
74 | 74 | |
|
75 | 75 | $("#bt_add_conf").click(function() { |
|
76 | 76 | document.location = "{% url 'url_add_dev_conf' experiment.id %}"; |
|
77 | 77 | }); |
|
78 | 78 | |
|
79 | 79 | </script> |
|
80 | 80 | {% endblock %} No newline at end of file |
@@ -1,59 +1,37 | |||
|
1 | {% extends "base.html" %} | |
|
1 | {% extends "base_edit.html" %} | |
|
2 | 2 | {% load bootstrap3 %} |
|
3 | 3 | {% load static %} |
|
4 | 4 | {% load main_tags %} |
|
5 | 5 | {% block extra-head %} |
|
6 | 6 | <link href="{% static 'css/bootstrap-datetimepicker.min.css' %}" media="screen" rel="stylesheet"> |
|
7 | 7 | {% endblock %} |
|
8 | 8 | |
|
9 | {% block exp-active %}active{% endblock %} | |
|
10 | ||
|
11 | {% block content-title %}{{title}}{% endblock %} | |
|
12 | {% block content-suptitle %}{{suptitle}}{% endblock %} | |
|
13 | ||
|
14 | {% block content %} | |
|
15 | <form class="form" method="post" action=""> | |
|
16 | {% csrf_token %} | |
|
17 | {% bootstrap_form form layout='horizontal' size='medium' %} | |
|
18 | <div style="clear: both;"></div> | |
|
19 | <br> | |
|
20 | {% if button %} | |
|
21 | <br> | |
|
22 | <button type="submit" class="btn btn-primary pull-right">{{button}}</button> | |
|
23 | {% endif %} | |
|
24 | </form> | |
|
25 | {% endblock %} | |
|
26 | ||
|
27 | {% block sidebar%} | |
|
28 | {% include "sidebar_devices.html" %} | |
|
29 | {% endblock %} | |
|
30 | ||
|
31 | 9 | {% block extra-js%} |
|
32 | 10 | <script src="{% static 'js/moment.min.js' %}"></script> |
|
33 | 11 | <script src="{% static 'js/bootstrap-datetimepicker.min.js' %}"></script> |
|
34 | 12 | <script type="text/javascript"> |
|
35 | 13 | |
|
36 | 14 | $('.input-group.time').datetimepicker({ |
|
37 | 15 | format: 'HH:mm:ss', |
|
38 | 16 | pickDate: false, |
|
39 | 17 | pickSeconds: true |
|
40 | 18 | }); |
|
41 | 19 | |
|
42 | 20 | $('#id_create_from').change(function() { |
|
43 | 21 | var url = "{% url 'url_add_experiment' %}"; |
|
44 |
if ($(this).val()=="2"){ |
|
|
22 | if ($(this).val()=="2"){ | |
|
45 | 23 | document.location = url+"?template=0"; |
|
46 | 24 | }else if ($(this).val()=="1"){ |
|
47 | 25 | document.location = url+"?blank=0"; |
|
48 | 26 | }else{ |
|
49 | 27 | document.location = url; |
|
50 | 28 | } |
|
51 | 29 | }); |
|
52 | 30 | |
|
53 | 31 | $('#id_choose_template').change(function() { |
|
54 | 32 | var url = "{% url 'url_add_experiment' %}"; |
|
55 | 33 | document.location = url+"?template="+$(this).val(); |
|
56 | 34 | }); |
|
57 | 35 | |
|
58 | 36 | </script> |
|
59 | 37 | {% endblock %} No newline at end of file |
@@ -1,36 +1,36 | |||
|
1 | 1 | from django.template.defaulttags import register |
|
2 | 2 | from django.utils.safestring import mark_safe |
|
3 | 3 | |
|
4 | 4 | @register.filter |
|
5 | 5 | def attr(instance, key): |
|
6 | 6 | |
|
7 | 7 | display_key = "get_" + key + "_display" |
|
8 | 8 | |
|
9 | 9 | if hasattr(instance, display_key): |
|
10 | 10 | return getattr(instance, display_key)() |
|
11 | 11 | |
|
12 | 12 | if hasattr(instance, key): |
|
13 | 13 | return getattr(instance, key) |
|
14 | 14 | |
|
15 | 15 | return instance.get(key) |
|
16 | 16 | |
|
17 | 17 | @register.filter |
|
18 | 18 | def title(s): |
|
19 | return s.replace('_', ' ').title() | |
|
19 | return s.split('__')[-1].replace('_', ' ').title() | |
|
20 | 20 | |
|
21 | 21 | @register.filter |
|
22 | 22 | def value(instance, key): |
|
23 | 23 | |
|
24 | 24 | item = instance |
|
25 | 25 | for my_key in key.split("__"): |
|
26 | 26 | item = attr(item, my_key) |
|
27 | 27 | |
|
28 | 28 | return item |
|
29 | 29 | |
|
30 | 30 | @register.simple_tag |
|
31 | 31 | def get_verbose_field_name(instance, field_name): |
|
32 | 32 | """ |
|
33 | 33 | Returns verbose_name for a field. |
|
34 | 34 | """ |
|
35 | 35 | |
|
36 | 36 | return mark_safe(instance._meta.get_field(field_name).verbose_name) No newline at end of file |
@@ -1,1042 +1,1065 | |||
|
1 | 1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
2 | 2 | from django.http import HttpResponseRedirect |
|
3 | 3 | from django.core.urlresolvers import reverse |
|
4 | 4 | from django.contrib import messages |
|
5 | 5 | from datetime import datetime |
|
6 | 6 | |
|
7 | 7 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
8 | 8 | from .forms import OperationSearchForm |
|
9 | 9 | from apps.cgs.forms import CGSConfigurationForm |
|
10 | 10 | from apps.jars.forms import JARSConfigurationForm |
|
11 | 11 | from apps.usrp.forms import USRPConfigurationForm |
|
12 | 12 | from apps.abs.forms import ABSConfigurationForm |
|
13 | 13 | from apps.rc.forms import RCConfigurationForm |
|
14 | 14 | from apps.dds.forms import DDSConfigurationForm |
|
15 | 15 | |
|
16 | 16 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment |
|
17 | 17 | from apps.cgs.models import CGSConfiguration |
|
18 | 18 | from apps.jars.models import JARSConfiguration |
|
19 | 19 | from apps.usrp.models import USRPConfiguration |
|
20 | 20 | from apps.abs.models import ABSConfiguration |
|
21 | 21 | from apps.rc.models import RCConfiguration |
|
22 | 22 | from apps.dds.models import DDSConfiguration |
|
23 | 23 | |
|
24 | 24 | # Create your views here. |
|
25 | 25 | |
|
26 | 26 | CONF_FORMS = { |
|
27 | 27 | 'rc': RCConfigurationForm, |
|
28 | 28 | 'dds': DDSConfigurationForm, |
|
29 | 29 | 'jars': JARSConfigurationForm, |
|
30 | 30 | 'cgs': CGSConfigurationForm, |
|
31 | 31 | 'abs': ABSConfigurationForm, |
|
32 | 32 | 'usrp': USRPConfigurationForm, |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | CONF_MODELS = { |
|
36 | 36 | 'rc': RCConfiguration, |
|
37 | 37 | 'dds': DDSConfiguration, |
|
38 | 38 | 'jars': JARSConfiguration, |
|
39 | 39 | 'cgs': CGSConfiguration, |
|
40 | 40 | 'abs': ABSConfiguration, |
|
41 | 41 | 'usrp': USRPConfiguration, |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | def index(request): |
|
46 | 46 | kwargs = {} |
|
47 | 47 | |
|
48 | 48 | return render(request, 'index.html', kwargs) |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | def locations(request): |
|
52 | 52 | |
|
53 | 53 | locations = Location.objects.all().order_by('name') |
|
54 | 54 | |
|
55 | 55 | keys = ['id', 'name', 'description'] |
|
56 | 56 | |
|
57 | 57 | kwargs = {} |
|
58 | 58 | kwargs['location_keys'] = keys[1:] |
|
59 | 59 | kwargs['locations'] = locations |
|
60 | 60 | kwargs['title'] = 'Location' |
|
61 | 61 | kwargs['suptitle'] = 'List' |
|
62 | 62 | kwargs['button'] = 'New Location' |
|
63 | 63 | |
|
64 | 64 | return render(request, 'locations.html', kwargs) |
|
65 | 65 | |
|
66 | 66 | |
|
67 | 67 | def location(request, id_loc): |
|
68 | 68 | |
|
69 | 69 | location = get_object_or_404(Location, pk=id_loc) |
|
70 | 70 | |
|
71 | 71 | kwargs = {} |
|
72 | 72 | kwargs['location'] = location |
|
73 | 73 | kwargs['location_keys'] = ['name', 'description'] |
|
74 | 74 | |
|
75 | 75 | kwargs['title'] = 'Location' |
|
76 | 76 | kwargs['suptitle'] = 'Details' |
|
77 | 77 | |
|
78 | 78 | return render(request, 'location.html', kwargs) |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | def location_new(request): |
|
82 | 82 | |
|
83 | 83 | if request.method == 'GET': |
|
84 | 84 | form = LocationForm() |
|
85 | 85 | |
|
86 | 86 | if request.method == 'POST': |
|
87 | 87 | form = LocationForm(request.POST) |
|
88 | 88 | |
|
89 | 89 | if form.is_valid(): |
|
90 | 90 | form.save() |
|
91 | 91 | return redirect('url_locations') |
|
92 | 92 | |
|
93 | 93 | kwargs = {} |
|
94 | 94 | kwargs['form'] = form |
|
95 | 95 | kwargs['title'] = 'Location' |
|
96 | 96 | kwargs['suptitle'] = 'New' |
|
97 | 97 | kwargs['button'] = 'Create' |
|
98 | 98 | |
|
99 | 99 | return render(request, 'location_edit.html', kwargs) |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | def location_edit(request, id_loc): |
|
103 | 103 | |
|
104 | 104 | location = get_object_or_404(Location, pk=id_loc) |
|
105 | 105 | |
|
106 | 106 | if request.method=='GET': |
|
107 | 107 | form = LocationForm(instance=location) |
|
108 | 108 | |
|
109 | 109 | if request.method=='POST': |
|
110 | 110 | form = LocationForm(request.POST, instance=location) |
|
111 | 111 | |
|
112 | 112 | if form.is_valid(): |
|
113 | 113 | form.save() |
|
114 | 114 | return redirect('url_locations') |
|
115 | 115 | |
|
116 | 116 | kwargs = {} |
|
117 | 117 | kwargs['form'] = form |
|
118 | 118 | kwargs['title'] = 'Location' |
|
119 | 119 | kwargs['suptitle'] = 'Edit' |
|
120 | 120 | kwargs['button'] = 'Update' |
|
121 | 121 | |
|
122 | 122 | return render(request, 'location_edit.html', kwargs) |
|
123 | 123 | |
|
124 | 124 | |
|
125 | 125 | def location_delete(request, id_loc): |
|
126 | 126 | |
|
127 | 127 | location = get_object_or_404(Location, pk=id_loc) |
|
128 | 128 | |
|
129 | 129 | if request.method=='POST': |
|
130 | 130 | |
|
131 | 131 | if request.user.is_staff: |
|
132 | 132 | location.delete() |
|
133 | 133 | return redirect('url_locations') |
|
134 | 134 | |
|
135 | 135 | messages.error(request, 'Not enough permission to delete this object') |
|
136 | 136 | return redirect(location.get_absolute_url()) |
|
137 | 137 | |
|
138 | 138 | kwargs = { |
|
139 | 139 | 'title': 'Delete', |
|
140 | 140 | 'suptitle': 'Location', |
|
141 | 141 | 'object': location, |
|
142 | 142 | 'previous': location.get_absolute_url(), |
|
143 | 143 | 'delete': True |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | return render(request, 'confirm.html', kwargs) |
|
147 | 147 | |
|
148 | 148 | |
|
149 | 149 | def devices(request): |
|
150 | 150 | |
|
151 | 151 | devices = Device.objects.all().order_by('device_type__name') |
|
152 | 152 | |
|
153 | 153 | # keys = ['id', 'device_type__name', 'name', 'ip_address'] |
|
154 | 154 | keys = ['id', 'name', 'ip_address', 'port_address', 'device_type'] |
|
155 | 155 | |
|
156 | 156 | kwargs = {} |
|
157 | 157 | kwargs['device_keys'] = keys[1:] |
|
158 | 158 | kwargs['devices'] = devices#.values(*keys) |
|
159 | 159 | kwargs['title'] = 'Device' |
|
160 | 160 | kwargs['suptitle'] = 'List' |
|
161 | 161 | kwargs['button'] = 'New Device' |
|
162 | 162 | |
|
163 | 163 | return render(request, 'devices.html', kwargs) |
|
164 | 164 | |
|
165 | 165 | |
|
166 | 166 | def device(request, id_dev): |
|
167 | 167 | |
|
168 | 168 | device = get_object_or_404(Device, pk=id_dev) |
|
169 | 169 | |
|
170 | 170 | kwargs = {} |
|
171 | 171 | kwargs['device'] = device |
|
172 | 172 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
173 | 173 | |
|
174 | 174 | kwargs['title'] = 'Device' |
|
175 | 175 | kwargs['suptitle'] = 'Details' |
|
176 | 176 | |
|
177 | 177 | return render(request, 'device.html', kwargs) |
|
178 | 178 | |
|
179 | 179 | |
|
180 | 180 | def device_new(request): |
|
181 | 181 | |
|
182 | 182 | if request.method == 'GET': |
|
183 | 183 | form = DeviceForm() |
|
184 | 184 | |
|
185 | 185 | if request.method == 'POST': |
|
186 | 186 | form = DeviceForm(request.POST) |
|
187 | 187 | |
|
188 | 188 | if form.is_valid(): |
|
189 | 189 | form.save() |
|
190 | 190 | return redirect('url_devices') |
|
191 | 191 | |
|
192 | 192 | kwargs = {} |
|
193 | 193 | kwargs['form'] = form |
|
194 | 194 | kwargs['title'] = 'Device' |
|
195 | 195 | kwargs['suptitle'] = 'New' |
|
196 | 196 | kwargs['button'] = 'Create' |
|
197 | 197 | |
|
198 | 198 | return render(request, 'device_edit.html', kwargs) |
|
199 | 199 | |
|
200 | 200 | |
|
201 | 201 | def device_edit(request, id_dev): |
|
202 | 202 | |
|
203 | 203 | device = get_object_or_404(Device, pk=id_dev) |
|
204 | 204 | |
|
205 | 205 | if request.method=='GET': |
|
206 | 206 | form = DeviceForm(instance=device) |
|
207 | 207 | |
|
208 | 208 | if request.method=='POST': |
|
209 | 209 | form = DeviceForm(request.POST, instance=device) |
|
210 | 210 | |
|
211 | 211 | if form.is_valid(): |
|
212 | 212 | form.save() |
|
213 | 213 | return redirect(device.get_absolute_url()) |
|
214 | 214 | |
|
215 | 215 | kwargs = {} |
|
216 | 216 | kwargs['form'] = form |
|
217 | 217 | kwargs['title'] = 'Device' |
|
218 | 218 | kwargs['suptitle'] = 'Edit' |
|
219 | 219 | kwargs['button'] = 'Update' |
|
220 | 220 | |
|
221 | 221 | return render(request, 'device_edit.html', kwargs) |
|
222 | 222 | |
|
223 | 223 | |
|
224 | 224 | def device_delete(request, id_dev): |
|
225 | 225 | |
|
226 | 226 | device = get_object_or_404(Device, pk=id_dev) |
|
227 | 227 | |
|
228 | 228 | if request.method=='POST': |
|
229 | 229 | |
|
230 | 230 | if request.user.is_staff: |
|
231 | 231 | device.delete() |
|
232 | 232 | return redirect('url_devices') |
|
233 | 233 | |
|
234 | 234 | messages.error(request, 'Not enough permission to delete this object') |
|
235 | 235 | return redirect(device.get_absolute_url()) |
|
236 | 236 | |
|
237 | 237 | kwargs = { |
|
238 | 238 | 'title': 'Delete', |
|
239 | 239 | 'suptitle': 'Device', |
|
240 | 240 | 'object': device, |
|
241 | 241 | 'previous': device.get_absolute_url(), |
|
242 | 242 | 'delete': True |
|
243 | 243 | } |
|
244 | 244 | |
|
245 | 245 | return render(request, 'confirm.html', kwargs) |
|
246 | 246 | |
|
247 | 247 | |
|
248 | 248 | def campaigns(request): |
|
249 | 249 | |
|
250 | 250 | campaigns = Campaign.objects.all().order_by('start_date') |
|
251 | 251 | |
|
252 | 252 | keys = ['id', 'name', 'start_date', 'end_date'] |
|
253 | 253 | |
|
254 | 254 | kwargs = {} |
|
255 | 255 | kwargs['campaign_keys'] = keys[1:] |
|
256 | 256 | kwargs['campaigns'] = campaigns#.values(*keys) |
|
257 | 257 | kwargs['title'] = 'Campaign' |
|
258 | 258 | kwargs['suptitle'] = 'List' |
|
259 | 259 | kwargs['button'] = 'New Campaign' |
|
260 | 260 | |
|
261 | 261 | return render(request, 'campaigns.html', kwargs) |
|
262 | 262 | |
|
263 | 263 | |
|
264 | 264 | def campaign(request, id_camp): |
|
265 | 265 | |
|
266 | 266 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
267 | 267 | experiments = Experiment.objects.filter(campaign=campaign) |
|
268 | 268 | |
|
269 | 269 | form = CampaignForm(instance=campaign) |
|
270 | 270 | |
|
271 | 271 | kwargs = {} |
|
272 | 272 | kwargs['campaign'] = campaign |
|
273 | 273 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] |
|
274 | 274 | |
|
275 | 275 | kwargs['experiments'] = experiments |
|
276 | 276 | kwargs['experiment_keys'] = ['name', 'radar', 'start_time', 'end_time'] |
|
277 | 277 | |
|
278 | 278 | kwargs['title'] = 'Campaign' |
|
279 | 279 | kwargs['suptitle'] = 'Details' |
|
280 | 280 | |
|
281 | 281 | kwargs['form'] = form |
|
282 | 282 | kwargs['button'] = 'Add Experiment' |
|
283 | 283 | |
|
284 | 284 | return render(request, 'campaign.html', kwargs) |
|
285 | 285 | |
|
286 | 286 | |
|
287 | 287 | def campaign_new(request): |
|
288 | 288 | |
|
289 | 289 | kwargs = {} |
|
290 | 290 | |
|
291 | 291 | if request.method == 'GET': |
|
292 | 292 | |
|
293 | 293 | if 'template' in request.GET: |
|
294 | 294 | if request.GET['template']=='0': |
|
295 | 295 | form = NewForm(initial={'create_from':2}, |
|
296 | 296 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
297 | 297 | else: |
|
298 | 298 | kwargs['button'] = 'Create' |
|
299 | 299 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
300 | 300 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
301 |
|
|
|
302 | initial={'template':False}) | |
|
301 | camp = Campaign.objects.get(pk=request.GET['template']) | |
|
302 | form = CampaignForm(instance=camp, | |
|
303 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), | |
|
304 | 'template':False}) | |
|
303 | 305 | elif 'blank' in request.GET: |
|
304 | 306 | kwargs['button'] = 'Create' |
|
305 | 307 | form = CampaignForm() |
|
306 | 308 | else: |
|
307 | 309 | form = NewForm() |
|
308 | 310 | |
|
309 | 311 | if request.method == 'POST': |
|
310 | 312 | kwargs['button'] = 'Create' |
|
311 | 313 | post = request.POST.copy() |
|
312 | 314 | experiments = [] |
|
313 | 315 | |
|
314 | 316 | for id_exp in post.getlist('experiments'): |
|
315 | 317 | exp = Experiment.objects.get(pk=id_exp) |
|
316 | 318 | new_exp = exp.clone(template=False) |
|
317 | 319 | experiments.append(new_exp) |
|
318 | 320 | |
|
319 | 321 | post.setlist('experiments', []) |
|
320 | 322 | |
|
321 | 323 | form = CampaignForm(post) |
|
322 | 324 | |
|
323 | 325 | if form.is_valid(): |
|
324 | 326 | campaign = form.save() |
|
325 | 327 | for exp in experiments: |
|
326 | 328 | campaign.experiments.add(exp) |
|
327 | 329 | campaign.save() |
|
328 | 330 | return redirect('url_campaign', id_camp=campaign.id) |
|
329 | 331 | |
|
330 | 332 | kwargs['form'] = form |
|
331 | 333 | kwargs['title'] = 'Campaign' |
|
332 | 334 | kwargs['suptitle'] = 'New' |
|
333 | 335 | |
|
334 | 336 | return render(request, 'campaign_edit.html', kwargs) |
|
335 | 337 | |
|
336 | 338 | |
|
337 | 339 | def campaign_edit(request, id_camp): |
|
338 | 340 | |
|
339 | 341 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
340 | 342 | |
|
341 | 343 | if request.method=='GET': |
|
342 | 344 | form = CampaignForm(instance=campaign) |
|
343 | 345 | |
|
344 | 346 | if request.method=='POST': |
|
345 | form = CampaignForm(request.POST, instance=campaign) | |
|
347 | exps = campaign.experiments.all().values_list('pk', flat=True) | |
|
348 | post = request.POST.copy() | |
|
349 | new_exps = post.getlist('experiments') | |
|
350 | post.setlist('experiments', []) | |
|
351 | form = CampaignForm(post, instance=campaign) | |
|
346 | 352 | |
|
347 | 353 | if form.is_valid(): |
|
348 | form.save() | |
|
354 | camp = form.save() | |
|
355 | for id_exp in new_exps: | |
|
356 | if int(id_exp) in exps: | |
|
357 | exps.pop(id_exp) | |
|
358 | else: | |
|
359 | exp = Experiment.objects.get(pk=id_exp) | |
|
360 | if exp.template: | |
|
361 | camp.experiments.add(exp.clone(template=False)) | |
|
362 | else: | |
|
363 | camp.experiments.add(exp) | |
|
364 | ||
|
365 | for id_exp in exps: | |
|
366 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) | |
|
367 | ||
|
349 | 368 | return redirect('url_campaign', id_camp=id_camp) |
|
350 | 369 | |
|
351 | 370 | kwargs = {} |
|
352 | 371 | kwargs['form'] = form |
|
353 | 372 | kwargs['title'] = 'Campaign' |
|
354 | 373 | kwargs['suptitle'] = 'Edit' |
|
355 | 374 | kwargs['button'] = 'Update' |
|
356 | 375 | |
|
357 | 376 | return render(request, 'campaign_edit.html', kwargs) |
|
358 | 377 | |
|
359 | 378 | |
|
360 | 379 | def campaign_delete(request, id_camp): |
|
361 | 380 | |
|
362 | 381 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
363 | 382 | |
|
364 | 383 | if request.method=='POST': |
|
365 | 384 | if request.user.is_staff: |
|
366 | 385 | |
|
367 | 386 | for exp in campaign.experiments.all(): |
|
368 | 387 | for conf in Configuration.objects.filter(experiment=exp): |
|
369 | 388 | conf.delete() |
|
370 | 389 | exp.delete() |
|
371 | 390 | campaign.delete() |
|
372 | 391 | |
|
373 | 392 | return redirect('url_campaigns') |
|
374 | 393 | |
|
375 | 394 | messages.error(request, 'Not enough permission to delete this object') |
|
376 | 395 | return redirect(campaign.get_absolute_url()) |
|
377 | 396 | |
|
378 | 397 | kwargs = { |
|
379 | 398 | 'title': 'Delete', |
|
380 | 399 | 'suptitle': 'Campaign', |
|
381 | 400 | 'object': campaign, |
|
382 | 401 | 'previous': campaign.get_absolute_url(), |
|
383 | 402 | 'delete': True |
|
384 | 403 | } |
|
385 | 404 | |
|
386 | 405 | return render(request, 'confirm.html', kwargs) |
|
387 | 406 | |
|
388 | 407 | |
|
389 | 408 | def experiments(request): |
|
390 | 409 | |
|
391 | 410 | experiment_list = Experiment.objects.all() |
|
392 | 411 | |
|
393 | 412 | keys = ['id', 'name', 'start_time', 'end_time'] |
|
394 | 413 | |
|
395 | 414 | kwargs = {} |
|
396 | 415 | |
|
397 | 416 | kwargs['experiment_keys'] = keys[1:] |
|
398 | 417 | kwargs['experiments'] = experiment_list |
|
399 | 418 | |
|
400 | 419 | kwargs['title'] = 'Experiment' |
|
401 | 420 | kwargs['suptitle'] = 'List' |
|
402 | 421 | kwargs['button'] = 'New Experiment' |
|
403 | 422 | |
|
404 | 423 | return render(request, 'experiments.html', kwargs) |
|
405 | 424 | |
|
406 | 425 | |
|
407 | 426 | def experiment(request, id_exp): |
|
408 | 427 | |
|
409 | 428 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
410 | 429 | |
|
411 | 430 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
412 | 431 | |
|
413 | 432 | kwargs = {} |
|
414 | 433 | |
|
415 |
exp_keys = [' |
|
|
416 | conf_keys = ['id', 'device__name', 'device__device_type', 'device__ip_address', 'device__port_address'] | |
|
417 | ||
|
418 | conf_labels = ['id', 'device__name', 'device_type', 'ip_address', 'port_address'] | |
|
419 | ||
|
420 | kwargs['experiment_keys'] = exp_keys[1:] | |
|
434 | kwargs['experiment_keys'] = ['template', 'radar', 'name', 'start_time', 'end_time'] | |
|
421 | 435 | kwargs['experiment'] = experiment |
|
422 | 436 | |
|
423 | kwargs['configuration_labels'] = conf_labels[1:] | |
|
424 |
kwargs['configuration |
|
|
425 | kwargs['configurations'] = configurations #.values(*conf_keys) | |
|
437 | kwargs['configuration_keys'] = ['device__name', 'device__device_type', 'device__ip_address', 'device__port_address'] | |
|
438 | kwargs['configurations'] = configurations | |
|
426 | 439 | |
|
427 | 440 | kwargs['title'] = 'Experiment' |
|
428 | 441 | kwargs['suptitle'] = 'Details' |
|
429 | 442 | |
|
430 | 443 | kwargs['button'] = 'Add Configuration' |
|
431 | 444 | |
|
432 | 445 | ###### SIDEBAR ###### |
|
433 | 446 | kwargs.update(sidebar(experiment=experiment)) |
|
434 | 447 | |
|
435 | 448 | return render(request, 'experiment.html', kwargs) |
|
436 | 449 | |
|
437 | 450 | |
|
438 | 451 | def experiment_new(request, id_camp=None): |
|
439 | 452 | |
|
440 | 453 | kwargs = {} |
|
441 | 454 | |
|
442 | 455 | if request.method == 'GET': |
|
443 | 456 | if 'template' in request.GET: |
|
444 | 457 | if request.GET['template']=='0': |
|
445 | 458 | form = NewForm(initial={'create_from':2}, |
|
446 | 459 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
447 | 460 | else: |
|
448 | 461 | kwargs['button'] = 'Create' |
|
449 | 462 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
450 | 463 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
451 |
|
|
|
452 | initial={'template':False}) | |
|
464 | exp=Experiment.objects.get(pk=request.GET['template']) | |
|
465 | form = ExperimentForm(instance=exp, | |
|
466 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), | |
|
467 | 'template': False}) | |
|
453 | 468 | elif 'blank' in request.GET: |
|
454 | 469 | kwargs['button'] = 'Create' |
|
455 | 470 | form = ExperimentForm() |
|
456 | 471 | else: |
|
457 | 472 | form = NewForm() |
|
458 | 473 | |
|
459 | 474 | if request.method == 'POST': |
|
460 | 475 | form = ExperimentForm(request.POST) |
|
461 | 476 | if form.is_valid(): |
|
462 | 477 | experiment = form.save() |
|
463 | 478 | |
|
464 | 479 | if 'template' in request.GET: |
|
465 | 480 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
466 | 481 | for conf in configurations: |
|
467 | 482 | conf.clone(experiment=experiment, template=False) |
|
468 | 483 | |
|
469 | 484 | return redirect('url_experiment', id_exp=experiment.id) |
|
470 | 485 | |
|
471 | 486 | kwargs['form'] = form |
|
472 | 487 | kwargs['title'] = 'Experiment' |
|
473 | 488 | kwargs['suptitle'] = 'New' |
|
474 | 489 | |
|
475 | 490 | return render(request, 'experiment_edit.html', kwargs) |
|
476 | 491 | |
|
477 | 492 | |
|
478 | 493 | def experiment_edit(request, id_exp): |
|
479 | 494 | |
|
480 | 495 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
481 | 496 | |
|
482 | 497 | if request.method == 'GET': |
|
483 | 498 | form = ExperimentForm(instance=experiment) |
|
484 | 499 | |
|
485 | 500 | if request.method=='POST': |
|
486 | 501 | form = ExperimentForm(request.POST, instance=experiment) |
|
487 | 502 | |
|
488 | 503 | if form.is_valid(): |
|
489 | 504 | experiment = form.save() |
|
490 | 505 | return redirect('url_experiment', id_exp=experiment.id) |
|
491 | 506 | |
|
492 | 507 | kwargs = {} |
|
493 | 508 | kwargs['form'] = form |
|
494 | 509 | kwargs['title'] = 'Experiment' |
|
495 | 510 | kwargs['suptitle'] = 'Edit' |
|
496 | 511 | kwargs['button'] = 'Update' |
|
497 | 512 | |
|
498 | 513 | return render(request, 'experiment_edit.html', kwargs) |
|
499 | 514 | |
|
500 | 515 | |
|
501 | 516 | def experiment_delete(request, id_exp): |
|
502 | 517 | |
|
503 | 518 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
504 | 519 | |
|
505 | 520 | if request.method=='POST': |
|
506 | 521 | if request.user.is_staff: |
|
522 | for conf in Configuration.objects.filter(experiment=experiment): | |
|
523 | conf.delete() | |
|
507 | 524 | experiment.delete() |
|
508 | 525 | return redirect('url_experiments') |
|
509 | 526 | |
|
510 |
|
|
|
527 | messages.error(request, 'Not enough permission to delete this object') | |
|
528 | return redirect(experiment.get_absolute_url()) | |
|
511 | 529 | |
|
512 | kwargs = {'object':experiment, 'exp_active':'active', | |
|
513 | 'url_cancel':'url_experiment', 'id_item':id_exp} | |
|
530 | kwargs = { | |
|
531 | 'title': 'Delete', | |
|
532 | 'suptitle': 'Experiment', | |
|
533 | 'object': experiment, | |
|
534 | 'previous': experiment.get_absolute_url(), | |
|
535 | 'delete': True | |
|
536 | } | |
|
514 | 537 | |
|
515 |
return render(request, ' |
|
|
538 | return render(request, 'confirm.html', kwargs) | |
|
516 | 539 | |
|
517 | 540 | |
|
518 | 541 | def dev_confs(request): |
|
519 | 542 | |
|
520 | 543 | configurations = Configuration.objects.all().order_by('type', 'device__device_type', 'experiment') |
|
521 | ||
|
522 | # keys = ['id', 'device__device_type__name', 'device__name', 'experiment__campaign__name', 'experiment__name'] | |
|
523 | ||
|
524 | keys = ['id', 'device', 'experiment', 'type', 'programmed_date'] | |
|
525 | 544 | |
|
526 | 545 | kwargs = {} |
|
527 | 546 | |
|
528 | kwargs['configuration_keys'] = keys[1:] | |
|
529 |
kwargs['configurations'] = configurations |
|
|
547 | kwargs['configuration_keys'] = ['device', 'experiment', 'type', 'programmed_date'] | |
|
548 | kwargs['configurations'] = configurations | |
|
530 | 549 | |
|
531 | 550 | kwargs['title'] = 'Configuration' |
|
532 | 551 | kwargs['suptitle'] = 'List' |
|
533 | kwargs['button'] = 'New Configuration' | |
|
534 | 552 | |
|
535 | 553 | return render(request, 'dev_confs.html', kwargs) |
|
536 | 554 | |
|
537 | 555 | |
|
538 | 556 | def dev_conf(request, id_conf): |
|
539 | 557 | |
|
540 | 558 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
541 | 559 | |
|
542 | 560 | return redirect(conf.get_absolute_url()) |
|
543 | 561 | |
|
544 | 562 | |
|
545 | 563 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
546 | 564 | |
|
547 | 565 | initial = {} |
|
548 | 566 | |
|
549 | 567 | if id_exp<>0: |
|
550 | 568 | initial['experiment'] = id_exp |
|
551 | 569 | |
|
552 | 570 | if id_dev<>0: |
|
553 | 571 | initial['device'] = id_dev |
|
554 | 572 | |
|
555 | 573 | if request.method == 'GET': |
|
556 | 574 | if id_dev==0: |
|
557 | 575 | form = ConfigurationForm(initial=initial) |
|
558 | 576 | else: |
|
559 | 577 | device = Device.objects.get(pk=id_dev) |
|
560 | 578 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
561 | 579 | |
|
562 | 580 | form = DevConfForm(initial=initial) |
|
563 | 581 | |
|
564 | 582 | if request.method == 'POST': |
|
565 | 583 | |
|
566 | 584 | device = Device.objects.get(pk=request.POST['device']) |
|
567 | 585 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
568 | 586 | |
|
569 | 587 | form = DevConfForm(request.POST) |
|
570 | 588 | |
|
571 | 589 | if form.is_valid(): |
|
572 | 590 | dev_conf = form.save() |
|
573 | 591 | |
|
574 | 592 | return redirect('url_dev_confs') |
|
575 | 593 | |
|
576 | 594 | kwargs = {} |
|
577 | 595 | kwargs['id_exp'] = id_exp |
|
578 | 596 | kwargs['form'] = form |
|
579 | 597 | kwargs['title'] = 'Configuration' |
|
580 | 598 | kwargs['suptitle'] = 'New' |
|
581 | 599 | kwargs['button'] = 'Create' |
|
582 | 600 | |
|
583 | 601 | return render(request, 'dev_conf_edit.html', kwargs) |
|
584 | 602 | |
|
585 | 603 | |
|
586 | 604 | def dev_conf_edit(request, id_conf): |
|
587 | 605 | |
|
588 | 606 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
589 | 607 | |
|
590 | 608 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
591 | 609 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
592 | 610 | |
|
593 | 611 | dev_conf = DevConfModel.objects.get(pk=id_conf) |
|
594 | 612 | |
|
595 | 613 | if request.method=='GET': |
|
596 | 614 | form = DevConfForm(instance=dev_conf) |
|
597 | 615 | |
|
598 | 616 | if request.method=='POST': |
|
599 | 617 | form = DevConfForm(request.POST, instance=dev_conf) |
|
600 | 618 | |
|
601 | 619 | if form.is_valid(): |
|
602 | 620 | form.save() |
|
603 | 621 | return redirect('url_dev_conf', id_conf=id_conf) |
|
604 | 622 | |
|
605 | 623 | kwargs = {} |
|
606 | 624 | kwargs['form'] = form |
|
607 | 625 | kwargs['title'] = 'Device Configuration' |
|
608 | 626 | kwargs['suptitle'] = 'Edit' |
|
609 | 627 | kwargs['button'] = 'Update' |
|
610 | 628 | |
|
611 | 629 | ###### SIDEBAR ###### |
|
612 | 630 | kwargs.update(sidebar(conf=conf)) |
|
613 | 631 | |
|
614 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) | |
|
632 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
|
615 | 633 | |
|
616 | 634 | |
|
617 | 635 | def dev_conf_start(request, id_conf): |
|
618 | 636 | |
|
619 | 637 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
620 | 638 | |
|
621 | 639 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
622 | 640 | |
|
623 | 641 | conf = DevConfModel.objects.get(pk=id_conf) |
|
624 | 642 | |
|
625 | 643 | if conf.start_device(): |
|
626 | 644 | messages.success(request, conf.message) |
|
627 | 645 | else: |
|
628 | 646 | messages.error(request, conf.message) |
|
629 | 647 | |
|
630 | 648 | conf.status_device() |
|
631 | 649 | |
|
632 | 650 | return redirect(conf.get_absolute_url()) |
|
633 | 651 | |
|
634 | 652 | |
|
635 | 653 | def dev_conf_stop(request, id_conf): |
|
636 | 654 | |
|
637 | 655 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
638 | 656 | |
|
639 | 657 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
640 | 658 | |
|
641 | 659 | conf = DevConfModel.objects.get(pk=id_conf) |
|
642 | 660 | |
|
643 | 661 | if conf.stop_device(): |
|
644 | 662 | messages.success(request, conf.message) |
|
645 | 663 | else: |
|
646 | 664 | messages.error(request, conf.message) |
|
647 | 665 | |
|
648 | 666 | conf.status_device() |
|
649 | 667 | |
|
650 | 668 | return redirect(conf.get_absolute_url()) |
|
651 | 669 | |
|
652 | 670 | |
|
653 | 671 | def dev_conf_status(request, id_conf): |
|
654 | 672 | |
|
655 | 673 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
656 | 674 | |
|
657 | 675 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
658 | 676 | |
|
659 | 677 | conf = DevConfModel.objects.get(pk=id_conf) |
|
660 | 678 | |
|
661 | 679 | if conf.status_device(): |
|
662 | 680 | messages.success(request, conf.message) |
|
663 | 681 | else: |
|
664 | 682 | messages.error(request, conf.message) |
|
665 | 683 | |
|
666 | 684 | return redirect(conf.get_absolute_url()) |
|
667 | 685 | |
|
668 | 686 | |
|
669 | 687 | def dev_conf_write(request, id_conf): |
|
670 | 688 | |
|
671 | 689 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
672 | 690 | |
|
673 | 691 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
674 | 692 | |
|
675 | 693 | conf = DevConfModel.objects.get(pk=id_conf) |
|
676 | 694 | |
|
677 | 695 | answer = conf.write_device() |
|
678 | 696 | conf.status_device() |
|
679 | 697 | |
|
680 | 698 | if answer: |
|
681 | 699 | messages.success(request, conf.message) |
|
682 | 700 | |
|
683 | 701 | #Creating a historical configuration |
|
684 | 702 | conf.clone(type=0, template=False) |
|
685 | 703 | |
|
686 | 704 | #Original configuration |
|
687 | 705 | conf = DevConfModel.objects.get(pk=id_conf) |
|
688 | 706 | else: |
|
689 | 707 | messages.error(request, conf.message) |
|
690 | 708 | |
|
691 | 709 | return redirect(conf.get_absolute_url()) |
|
692 | 710 | |
|
693 | 711 | |
|
694 | 712 | def dev_conf_read(request, id_conf): |
|
695 | 713 | |
|
696 | 714 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
697 | 715 | |
|
698 | 716 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
699 | 717 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
700 | 718 | |
|
701 | 719 | conf = DevConfModel.objects.get(pk=id_conf) |
|
702 | 720 | |
|
703 | 721 | if request.method=='GET': |
|
704 | 722 | |
|
705 | 723 | parms = conf.read_device() |
|
706 | 724 | conf.status_device() |
|
707 | 725 | |
|
708 | 726 | if not parms: |
|
709 | 727 | messages.error(request, conf.message) |
|
710 | 728 | return redirect(conf.get_absolute_url()) |
|
711 | 729 | |
|
712 | 730 | form = DevConfForm(initial=parms, instance=conf) |
|
713 | 731 | |
|
714 | 732 | if request.method=='POST': |
|
715 | 733 | form = DevConfForm(request.POST, instance=conf) |
|
716 | 734 | |
|
717 | 735 | if form.is_valid(): |
|
718 | 736 | form.save() |
|
719 | 737 | return redirect(conf.get_absolute_url()) |
|
720 | 738 | |
|
721 | 739 | messages.error(request, "Parameters could not be saved") |
|
722 | 740 | |
|
723 | 741 | kwargs = {} |
|
724 | 742 | kwargs['id_dev'] = conf.id |
|
725 | 743 | kwargs['form'] = form |
|
726 | 744 | kwargs['title'] = 'Device Configuration' |
|
727 | 745 | kwargs['suptitle'] = 'Parameters read from device' |
|
728 | 746 | kwargs['button'] = 'Save' |
|
729 | 747 | |
|
730 | 748 | ###### SIDEBAR ###### |
|
731 | 749 | kwargs.update(sidebar(conf=conf)) |
|
732 | 750 | |
|
733 | 751 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
734 | 752 | |
|
735 | 753 | |
|
736 | 754 | def dev_conf_import(request, id_conf): |
|
737 | 755 | |
|
738 | 756 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
739 | 757 | |
|
740 | 758 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
741 | 759 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
742 | 760 | conf = DevConfModel.objects.get(pk=id_conf) |
|
743 | 761 | |
|
744 | 762 | if request.method == 'GET': |
|
745 | 763 | file_form = UploadFileForm() |
|
746 | 764 | |
|
747 | 765 | if request.method == 'POST': |
|
748 | 766 | file_form = UploadFileForm(request.POST, request.FILES) |
|
749 | 767 | |
|
750 | 768 | if file_form.is_valid(): |
|
751 | 769 | |
|
752 | 770 | parms = conf.import_from_file(request.FILES['file']) |
|
753 | 771 | |
|
754 | 772 | if parms: |
|
755 | 773 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
756 | 774 | form = DevConfForm(initial=parms, instance=conf) |
|
757 | 775 | |
|
758 | 776 | kwargs = {} |
|
759 | 777 | kwargs['id_dev'] = conf.id |
|
760 | 778 | kwargs['form'] = form |
|
761 | 779 | kwargs['title'] = 'Device Configuration' |
|
762 | 780 | kwargs['suptitle'] = 'Parameters imported' |
|
763 | 781 | kwargs['button'] = 'Save' |
|
764 | 782 | kwargs['action'] = conf.get_absolute_url_edit() |
|
765 | 783 | kwargs['previous'] = conf.get_absolute_url() |
|
766 | 784 | |
|
767 | 785 | ###### SIDEBAR ###### |
|
768 | 786 | kwargs.update(sidebar(conf=conf)) |
|
769 | 787 | |
|
770 | 788 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
771 | 789 | |
|
772 | 790 | messages.error(request, "Could not import parameters from file") |
|
773 | 791 | |
|
774 | 792 | kwargs = {} |
|
775 | 793 | kwargs['id_dev'] = conf.id |
|
776 | 794 | kwargs['title'] = 'Device Configuration' |
|
777 | 795 | kwargs['form'] = file_form |
|
778 | 796 | kwargs['suptitle'] = 'Importing file' |
|
779 | 797 | kwargs['button'] = 'Import' |
|
780 | 798 | |
|
781 | 799 | kwargs.update(sidebar(conf=conf)) |
|
782 | 800 | |
|
783 | 801 | return render(request, 'dev_conf_import.html', kwargs) |
|
784 | 802 | |
|
785 | 803 | |
|
786 | 804 | def dev_conf_export(request, id_conf): |
|
787 | 805 | |
|
788 | 806 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
789 | 807 | |
|
790 | 808 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
791 | 809 | |
|
792 | 810 | conf = DevConfModel.objects.get(pk=id_conf) |
|
793 | 811 | |
|
794 | 812 | if request.method == 'GET': |
|
795 | 813 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
796 | 814 | |
|
797 | 815 | if request.method == 'POST': |
|
798 | 816 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
799 | 817 | |
|
800 | 818 | if file_form.is_valid(): |
|
801 | 819 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
802 | 820 | |
|
803 | 821 | response = HttpResponse(content_type=fields['content_type']) |
|
804 | 822 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
805 | 823 | response.write(fields['content']) |
|
806 | 824 | |
|
807 | 825 | return response |
|
808 | 826 | |
|
809 | 827 | messages.error(request, "Could not export parameters") |
|
810 | 828 | |
|
811 | 829 | kwargs = {} |
|
812 | 830 | kwargs['id_dev'] = conf.id |
|
813 | 831 | kwargs['title'] = 'Device Configuration' |
|
814 | 832 | kwargs['form'] = file_form |
|
815 | 833 | kwargs['suptitle'] = 'Exporting file' |
|
816 | 834 | kwargs['button'] = 'Export' |
|
817 | 835 | |
|
818 | 836 | return render(request, 'dev_conf_export.html', kwargs) |
|
819 | 837 | |
|
820 | 838 | |
|
821 | 839 | def dev_conf_delete(request, id_conf): |
|
822 | 840 | |
|
823 | 841 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
824 | 842 | |
|
825 | 843 | if request.method=='POST': |
|
826 | 844 | if request.user.is_staff: |
|
827 | id_exp = conf.experiment.id | |
|
828 | 845 | conf.delete() |
|
829 |
return redirect('url_ |
|
|
846 | return redirect('url_dev_confs') | |
|
830 | 847 | |
|
831 |
|
|
|
848 | messages.error(request, 'Not enough permission to delete this object') | |
|
849 | return redirect(conf.get_absolute_url()) | |
|
832 | 850 | |
|
833 | kwargs = {'object':conf, 'conf_active':'active', | |
|
834 | 'url_cancel':'url_dev_conf', 'id_item':id_conf} | |
|
851 | kwargs = { | |
|
852 | 'title': 'Delete', | |
|
853 | 'suptitle': 'Experiment', | |
|
854 | 'object': conf, | |
|
855 | 'previous': conf.get_absolute_url(), | |
|
856 | 'delete': True | |
|
857 | } | |
|
835 | 858 | |
|
836 |
return render(request, ' |
|
|
859 | return render(request, 'confirm.html', kwargs) | |
|
837 | 860 | |
|
838 | 861 | |
|
839 | 862 | def sidebar(**kwargs): |
|
840 | 863 | |
|
841 | 864 | side_data = {} |
|
842 | 865 | |
|
843 | 866 | conf = kwargs.get('conf', None) |
|
844 | 867 | experiment = kwargs.get('experiment', None) |
|
845 | 868 | |
|
846 | 869 | if not experiment: |
|
847 | 870 | experiment = conf.experiment |
|
848 | 871 | |
|
849 | 872 | if experiment: |
|
850 | 873 | side_data['experiment'] = experiment |
|
851 | 874 | campaign = experiment.campaign_set.all() |
|
852 | 875 | if campaign: |
|
853 | 876 | side_data['campaign'] = campaign[0] |
|
854 | 877 | experiments = campaign[0].experiments.all() |
|
855 | 878 | else: |
|
856 | 879 | experiments = [experiment] |
|
857 | 880 | configurations = experiment.configuration_set.filter(type=0) |
|
858 | 881 | side_data['side_experiments'] = experiments |
|
859 | 882 | side_data['side_configurations'] = configurations |
|
860 | 883 | |
|
861 | 884 | return side_data |
|
862 | 885 | |
|
863 | 886 | |
|
864 | 887 | def operation(request, id_camp=None): |
|
865 | 888 | |
|
866 | 889 | if not id_camp: |
|
867 | 890 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
868 | 891 | |
|
869 | 892 | if not campaigns: |
|
870 | 893 | kwargs = {} |
|
871 | 894 | kwargs['title'] = 'No Campaigns' |
|
872 | 895 | kwargs['suptitle'] = 'Empty' |
|
873 | 896 | return render(request, 'operation.html', kwargs) |
|
874 | 897 | |
|
875 | 898 | id_camp = campaigns[0].id |
|
876 | 899 | |
|
877 | 900 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
878 | 901 | |
|
879 | 902 | if request.method=='GET': |
|
880 | 903 | form = OperationForm(initial={'campaign': campaign.id}, length = 5) |
|
881 | 904 | |
|
882 | 905 | if request.method=='POST': |
|
883 | 906 | form = OperationForm(request.POST, initial={'campaign':campaign.id}, length = 5) |
|
884 | 907 | |
|
885 | 908 | if form.is_valid(): |
|
886 | 909 | return redirect('url_operation', id_camp=campaign.id) |
|
887 | 910 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
888 | 911 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
889 | 912 | #for exs in experiments: |
|
890 | 913 | # exs.get_status() |
|
891 | 914 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
892 | 915 | #experiments = [Experiment.objects.filter(location__pk=location.id).filter(campaign__pk=campaign.id) for location in locations] |
|
893 | 916 | kwargs = {} |
|
894 | 917 | #---Campaign |
|
895 | 918 | kwargs['campaign'] = campaign |
|
896 | 919 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
897 | 920 | #---Experiment |
|
898 | 921 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
899 | 922 | kwargs['experiment_keys'] = keys[1:] |
|
900 | 923 | kwargs['experiments'] = experiments |
|
901 | 924 | #---Radar |
|
902 | 925 | kwargs['locations'] = locations |
|
903 | 926 | #---Else |
|
904 | 927 | kwargs['title'] = 'Campaign' |
|
905 | 928 | kwargs['suptitle'] = campaign.name |
|
906 | 929 | kwargs['form'] = form |
|
907 | 930 | kwargs['button'] = 'Search' |
|
908 | 931 | kwargs['details'] = True |
|
909 | 932 | kwargs['search_button'] = True |
|
910 | 933 | |
|
911 | 934 | return render(request, 'operation.html', kwargs) |
|
912 | 935 | |
|
913 | 936 | |
|
914 | 937 | def operation_search(request, id_camp=None): |
|
915 | 938 | |
|
916 | 939 | |
|
917 | 940 | if not id_camp: |
|
918 | 941 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
919 | 942 | |
|
920 | 943 | if not campaigns: |
|
921 | 944 | return render(request, 'operation.html', {}) |
|
922 | 945 | |
|
923 | 946 | id_camp = campaigns[0].id |
|
924 | 947 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
925 | 948 | |
|
926 | 949 | if request.method=='GET': |
|
927 | 950 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
928 | 951 | |
|
929 | 952 | if request.method=='POST': |
|
930 | 953 | form = OperationSearchForm(request.POST, initial={'campaign':campaign.id}) |
|
931 | 954 | |
|
932 | 955 | if form.is_valid(): |
|
933 | 956 | return redirect('url_operation', id_camp=campaign.id) |
|
934 | 957 | |
|
935 | 958 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
936 | 959 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
937 | 960 | #for exs in experiments: |
|
938 | 961 | # exs.get_status() |
|
939 | 962 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
940 | 963 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
941 | 964 | |
|
942 | 965 | kwargs = {} |
|
943 | 966 | #---Campaign |
|
944 | 967 | kwargs['campaign'] = campaign |
|
945 | 968 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
946 | 969 | #---Experiment |
|
947 | 970 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
948 | 971 | kwargs['experiment_keys'] = keys[1:] |
|
949 | 972 | kwargs['experiments'] = experiments |
|
950 | 973 | #---Radar |
|
951 | 974 | kwargs['locations'] = locations |
|
952 | 975 | #---Else |
|
953 | 976 | kwargs['title'] = 'Campaign' |
|
954 | 977 | kwargs['suptitle'] = campaign.name |
|
955 | 978 | kwargs['form'] = form |
|
956 | 979 | kwargs['button'] = 'Select' |
|
957 | 980 | kwargs['details'] = True |
|
958 | 981 | kwargs['search_button'] = False |
|
959 | 982 | |
|
960 | 983 | return render(request, 'operation.html', kwargs) |
|
961 | 984 | |
|
962 | 985 | |
|
963 | 986 | def radar_play(request, id_camp, id_radar): |
|
964 | 987 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
965 | 988 | radar = get_object_or_404(Location, pk = id_radar) |
|
966 | 989 | today = datetime.today() |
|
967 | 990 | now = today.time() |
|
968 | 991 | |
|
969 | 992 | #--If campaign datetime is ok: |
|
970 | 993 | if today >= campaign.start_date and today <= campaign.end_date: |
|
971 | 994 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
972 | 995 | for exp in experiments: |
|
973 | 996 | #--If experiment time is ok: |
|
974 | 997 | if now >= exp.start_time and now <= exp.end_time: |
|
975 | 998 | configurations = Configuration.objects.filter(experiment = exp) |
|
976 | 999 | for conf in configurations: |
|
977 | 1000 | if 'cgs' in conf.device.device_type.name: |
|
978 | 1001 | conf.status_device() |
|
979 | 1002 | else: |
|
980 | 1003 | answer = conf.start_device() |
|
981 | 1004 | conf.status_device() |
|
982 | 1005 | #--Running Experiment |
|
983 | 1006 | r_e = RunningExperiment.objects.filter(radar=radar) |
|
984 | 1007 | #--If RunningExperiment element exists |
|
985 | 1008 | if r_e: |
|
986 | 1009 | r_e = r_e[0] |
|
987 | 1010 | r_e.running_experiment = exp |
|
988 | 1011 | r_e.status = 3 |
|
989 | 1012 | r_e.save() |
|
990 | 1013 | else: |
|
991 | 1014 | running_experiment = RunningExperiment( |
|
992 | 1015 | radar = radar, |
|
993 | 1016 | running_experiment = exp, |
|
994 | 1017 | status = 3, |
|
995 | 1018 | ) |
|
996 | 1019 | running_experiment.save() |
|
997 | 1020 | |
|
998 | 1021 | if answer: |
|
999 | 1022 | messages.success(request, conf.message) |
|
1000 | 1023 | exp.status=2 |
|
1001 | 1024 | exp.save() |
|
1002 | 1025 | else: |
|
1003 | 1026 | messages.error(request, conf.message) |
|
1004 | 1027 | else: |
|
1005 | 1028 | if exp.status == 1 or exp.status == 3: |
|
1006 | 1029 | exp.status=3 |
|
1007 | 1030 | exp.save() |
|
1008 | 1031 | |
|
1009 | 1032 | |
|
1010 | 1033 | route = request.META['HTTP_REFERER'] |
|
1011 | 1034 | route = str(route) |
|
1012 | 1035 | if 'search' in route: |
|
1013 | 1036 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1014 | 1037 | else: |
|
1015 | 1038 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1016 | 1039 | |
|
1017 | 1040 | |
|
1018 | 1041 | def radar_stop(request, id_camp, id_radar): |
|
1019 | 1042 | |
|
1020 | 1043 | route = request.META['HTTP_REFERER'] |
|
1021 | 1044 | route = str(route) |
|
1022 | 1045 | if 'search' in route: |
|
1023 | 1046 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1024 | 1047 | else: |
|
1025 | 1048 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1026 | 1049 | |
|
1027 | 1050 | |
|
1028 | 1051 | def radar_refresh(request, id_camp, id_radar): |
|
1029 | 1052 | |
|
1030 | 1053 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1031 | 1054 | radar = get_object_or_404(Location, pk = id_radar) |
|
1032 | 1055 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1033 | 1056 | for exs in experiments: |
|
1034 | 1057 | exs.get_status() |
|
1035 | 1058 | |
|
1036 | 1059 | route = request.META['HTTP_REFERER'] |
|
1037 | 1060 | route = str(route) |
|
1038 | 1061 | if 'search' in route: |
|
1039 | 1062 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1040 | 1063 | else: |
|
1041 | 1064 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1042 | 1065 |
General Comments 0
You need to be logged in to leave comments.
Login now