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