@@ -1,173 +1,173 | |||
|
1 | 1 | from django.db import models |
|
2 | 2 | from apps.main.models import Configuration |
|
3 | 3 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
4 | 4 | |
|
5 | 5 | |
|
6 | 6 | from apps.main.models import Device, Experiment |
|
7 | 7 | |
|
8 | 8 | from files import read_json_file |
|
9 | 9 | # Create your models here. validators=[MinValueValidator(62.5e6), MaxValueValidator(450e6)] |
|
10 | 10 | |
|
11 | 11 | class CGSConfiguration(Configuration): |
|
12 | 12 | |
|
13 | 13 | freq0 = models.IntegerField(verbose_name='Frequency 0',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
14 | 14 | freq1 = models.IntegerField(verbose_name='Frequency 1',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
15 | 15 | freq2 = models.IntegerField(verbose_name='Frequency 2',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
16 | 16 | freq3 = models.IntegerField(verbose_name='Frequency 3',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
17 | 17 | |
|
18 | 18 | def verify_frequencies(self): |
|
19 | 19 | |
|
20 | 20 | return True |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | def update_from_file(self, fp): |
|
24 | 24 | |
|
25 | 25 | kwargs = read_json_file(fp) |
|
26 | 26 | |
|
27 | 27 | if not kwargs: |
|
28 | 28 | return False |
|
29 | 29 | |
|
30 | 30 | self.freq0 = kwargs['freq0'] |
|
31 | 31 | self.freq1 = kwargs['freq1'] |
|
32 | 32 | self.freq2 = kwargs['freq2'] |
|
33 | 33 | self.freq3 = kwargs['freq3'] |
|
34 | 34 | |
|
35 | 35 | return True |
|
36 | 36 | |
|
37 | 37 | def parms_to_dict(self): |
|
38 | 38 | |
|
39 | 39 | parameters = {} |
|
40 | 40 | |
|
41 | 41 | if self.freq0 == None or self.freq0 == '': |
|
42 | 42 | parameters['freq0'] = 0 |
|
43 | 43 | else: |
|
44 | 44 | parameters['freq0'] = self.freq0 |
|
45 | 45 | |
|
46 | 46 | if self.freq1 == None or self.freq1 == '': |
|
47 | 47 | parameters['freq1'] = 0 |
|
48 | 48 | else: |
|
49 | 49 | parameters['freq1'] = self.freq1 |
|
50 | 50 | |
|
51 | 51 | if self.freq2 == None or self.freq2 == '': |
|
52 | 52 | parameters['freq2'] = 0 |
|
53 | 53 | else: |
|
54 | 54 | parameters['freq2'] = self.freq2 |
|
55 | 55 | |
|
56 | 56 | if self.freq3 == None or self.freq3 == '': |
|
57 | 57 | parameters['freq3'] = 0 |
|
58 | 58 | else: |
|
59 | 59 | parameters['freq3'] = self.freq3 |
|
60 | 60 | |
|
61 | 61 | |
|
62 | 62 | return parameters |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | def status_device(self): |
|
66 | 66 | |
|
67 | 67 | import requests |
|
68 | 68 | |
|
69 | 69 | ip=self.device.ip_address |
|
70 | 70 | port=self.device.port_address |
|
71 | 71 | |
|
72 | 72 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" |
|
73 | 73 | try: |
|
74 |
r = requests.get(route,timeout= |
|
|
74 | r = requests.get(route,timeout=0.5) | |
|
75 | 75 | except: |
|
76 | 76 | self.device.status = 0 |
|
77 | 77 | self.device.save() |
|
78 | 78 | return self.device.status |
|
79 | 79 | |
|
80 | 80 | response = str(r.text) |
|
81 | 81 | response = response.split(";") |
|
82 | 82 | icon = response[0] |
|
83 | 83 | status = response[-1] |
|
84 | 84 | |
|
85 | 85 | print icon, status |
|
86 | 86 | #"icon" could be: "alert" or "okay" |
|
87 | 87 | if "alert" in icon: |
|
88 | 88 | if "Starting Up" in status: #No Esta conectado |
|
89 | 89 | self.device.status = 0 |
|
90 | 90 | else: |
|
91 | 91 | self.device.status = 1 |
|
92 | 92 | elif "okay" in icon: |
|
93 | 93 | self.device.status = 3 |
|
94 | 94 | else: |
|
95 | 95 | self.device.status = 1 |
|
96 | 96 | |
|
97 | 97 | self.message = status |
|
98 | 98 | self.device.save() |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | return self.device.status |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | def read_device(self): |
|
105 | 105 | |
|
106 | 106 | import requests |
|
107 | 107 | |
|
108 | 108 | ip=self.device.ip_address |
|
109 | 109 | port=self.device.port_address |
|
110 | 110 | |
|
111 | 111 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" |
|
112 | 112 | try: |
|
113 |
frequencies = requests.get(route,timeout= |
|
|
113 | frequencies = requests.get(route,timeout=0.5) | |
|
114 | 114 | |
|
115 | 115 | except: |
|
116 | 116 | self.message = "Could not read CGS parameters from this device" |
|
117 | 117 | return None |
|
118 | 118 | |
|
119 | 119 | frequencies = frequencies.json() |
|
120 | 120 | frequencies = frequencies.get("Frecuencias") |
|
121 | 121 | f0 = frequencies.get("f0") |
|
122 | 122 | f1 = frequencies.get("f1") |
|
123 | 123 | f2 = frequencies.get("f2") |
|
124 | 124 | f3 = frequencies.get("f3") |
|
125 | 125 | |
|
126 | 126 | parms = {'freq0': f0, |
|
127 | 127 | 'freq1': f1, |
|
128 | 128 | 'freq2': f2, |
|
129 | 129 | 'freq3': f3} |
|
130 | 130 | |
|
131 | 131 | self.message = "" |
|
132 | 132 | return parms |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | def write_device(self): |
|
136 | 136 | |
|
137 | 137 | import requests |
|
138 | 138 | |
|
139 | 139 | ip=self.device.ip_address |
|
140 | 140 | port=self.device.port_address |
|
141 | 141 | |
|
142 | 142 | #---Frequencies from form |
|
143 | 143 | f0 = self.freq0 |
|
144 | 144 | f1 = self.freq1 |
|
145 | 145 | f2 = self.freq2 |
|
146 | 146 | f3 = self.freq3 |
|
147 | 147 | post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} |
|
148 | 148 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" |
|
149 | 149 | |
|
150 | 150 | try: |
|
151 |
r = requests.post(route, post_data, timeout= |
|
|
151 | r = requests.post(route, post_data, timeout=0.5) | |
|
152 | 152 | except: |
|
153 | 153 | self.message = "Could not write CGS parameters" |
|
154 | 154 | return None |
|
155 | 155 | |
|
156 | 156 | text = r.text |
|
157 | 157 | text = text.split(',') |
|
158 | 158 | |
|
159 | 159 | if len(text)>1: |
|
160 | 160 | title = text[0] |
|
161 | 161 | status = text[1] |
|
162 | 162 | if title == "okay": |
|
163 | 163 | self.message = status |
|
164 | 164 | return 3 |
|
165 | 165 | else: |
|
166 | 166 | self.message = title + ", " + status |
|
167 | 167 | return 1 |
|
168 | 168 | |
|
169 | 169 | return 1 |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | class Meta: |
|
173 | 173 | db_table = 'cgs_configurations' |
@@ -1,1008 +1,1042 | |||
|
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 | 301 | form = CampaignForm(instance=Campaign.objects.get(pk=request.GET['template']), |
|
302 | 302 | initial={'template':False}) |
|
303 | 303 | elif 'blank' in request.GET: |
|
304 | 304 | kwargs['button'] = 'Create' |
|
305 | 305 | form = CampaignForm() |
|
306 | 306 | else: |
|
307 | 307 | form = NewForm() |
|
308 | 308 | |
|
309 | 309 | if request.method == 'POST': |
|
310 | 310 | kwargs['button'] = 'Create' |
|
311 | 311 | post = request.POST.copy() |
|
312 | 312 | experiments = [] |
|
313 | 313 | |
|
314 | 314 | for id_exp in post.getlist('experiments'): |
|
315 | 315 | exp = Experiment.objects.get(pk=id_exp) |
|
316 | 316 | new_exp = exp.clone(template=False) |
|
317 | 317 | experiments.append(new_exp) |
|
318 | 318 | |
|
319 | 319 | post.setlist('experiments', []) |
|
320 | 320 | |
|
321 | 321 | form = CampaignForm(post) |
|
322 | 322 | |
|
323 | 323 | if form.is_valid(): |
|
324 | 324 | campaign = form.save() |
|
325 | 325 | for exp in experiments: |
|
326 | 326 | campaign.experiments.add(exp) |
|
327 | 327 | campaign.save() |
|
328 | 328 | return redirect('url_campaign', id_camp=campaign.id) |
|
329 | 329 | |
|
330 | 330 | kwargs['form'] = form |
|
331 | 331 | kwargs['title'] = 'Campaign' |
|
332 | 332 | kwargs['suptitle'] = 'New' |
|
333 | 333 | |
|
334 | 334 | return render(request, 'campaign_edit.html', kwargs) |
|
335 | 335 | |
|
336 | 336 | |
|
337 | 337 | def campaign_edit(request, id_camp): |
|
338 | 338 | |
|
339 | 339 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
340 | 340 | |
|
341 | 341 | if request.method=='GET': |
|
342 | 342 | form = CampaignForm(instance=campaign) |
|
343 | 343 | |
|
344 | 344 | if request.method=='POST': |
|
345 | 345 | form = CampaignForm(request.POST, instance=campaign) |
|
346 | 346 | |
|
347 | 347 | if form.is_valid(): |
|
348 | 348 | form.save() |
|
349 | 349 | return redirect('url_campaign', id_camp=id_camp) |
|
350 | 350 | |
|
351 | 351 | kwargs = {} |
|
352 | 352 | kwargs['form'] = form |
|
353 | 353 | kwargs['title'] = 'Campaign' |
|
354 | 354 | kwargs['suptitle'] = 'Edit' |
|
355 | 355 | kwargs['button'] = 'Update' |
|
356 | 356 | |
|
357 | 357 | return render(request, 'campaign_edit.html', kwargs) |
|
358 | 358 | |
|
359 | 359 | |
|
360 | 360 | def campaign_delete(request, id_camp): |
|
361 | 361 | |
|
362 | 362 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
363 | 363 | |
|
364 | 364 | if request.method=='POST': |
|
365 | 365 | if request.user.is_staff: |
|
366 | 366 | |
|
367 | 367 | for exp in campaign.experiments.all(): |
|
368 | 368 | for conf in Configuration.objects.filter(experiment=exp): |
|
369 | 369 | conf.delete() |
|
370 | 370 | exp.delete() |
|
371 | 371 | campaign.delete() |
|
372 | 372 | |
|
373 | 373 | return redirect('url_campaigns') |
|
374 | 374 | |
|
375 | 375 | messages.error(request, 'Not enough permission to delete this object') |
|
376 | 376 | return redirect(campaign.get_absolute_url()) |
|
377 | 377 | |
|
378 | 378 | kwargs = { |
|
379 | 379 | 'title': 'Delete', |
|
380 | 380 | 'suptitle': 'Campaign', |
|
381 | 381 | 'object': campaign, |
|
382 | 382 | 'previous': campaign.get_absolute_url(), |
|
383 | 383 | 'delete': True |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | return render(request, 'confirm.html', kwargs) |
|
387 | 387 | |
|
388 | 388 | |
|
389 | 389 | def experiments(request): |
|
390 | 390 | |
|
391 | 391 | experiment_list = Experiment.objects.all() |
|
392 | 392 | |
|
393 | 393 | keys = ['id', 'name', 'start_time', 'end_time'] |
|
394 | 394 | |
|
395 | 395 | kwargs = {} |
|
396 | 396 | |
|
397 | 397 | kwargs['experiment_keys'] = keys[1:] |
|
398 | 398 | kwargs['experiments'] = experiment_list |
|
399 | 399 | |
|
400 | 400 | kwargs['title'] = 'Experiment' |
|
401 | 401 | kwargs['suptitle'] = 'List' |
|
402 | 402 | kwargs['button'] = 'New Experiment' |
|
403 | 403 | |
|
404 | 404 | return render(request, 'experiments.html', kwargs) |
|
405 | 405 | |
|
406 | 406 | |
|
407 | 407 | def experiment(request, id_exp): |
|
408 | 408 | |
|
409 | 409 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
410 | 410 | |
|
411 | 411 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
412 | 412 | |
|
413 | 413 | kwargs = {} |
|
414 | 414 | |
|
415 | 415 | exp_keys = ['id', 'location', 'name', 'start_time', 'end_time'] |
|
416 | 416 | conf_keys = ['id', 'device__name', 'device__device_type', 'device__ip_address', 'device__port_address'] |
|
417 | 417 | |
|
418 | 418 | conf_labels = ['id', 'device__name', 'device_type', 'ip_address', 'port_address'] |
|
419 | 419 | |
|
420 | 420 | kwargs['experiment_keys'] = exp_keys[1:] |
|
421 | 421 | kwargs['experiment'] = experiment |
|
422 | 422 | |
|
423 | 423 | kwargs['configuration_labels'] = conf_labels[1:] |
|
424 | 424 | kwargs['configuration_keys'] = conf_keys[1:] |
|
425 | 425 | kwargs['configurations'] = configurations #.values(*conf_keys) |
|
426 | 426 | |
|
427 | 427 | kwargs['title'] = 'Experiment' |
|
428 | 428 | kwargs['suptitle'] = 'Details' |
|
429 | 429 | |
|
430 | 430 | kwargs['button'] = 'Add Configuration' |
|
431 | 431 | |
|
432 | 432 | ###### SIDEBAR ###### |
|
433 | 433 | kwargs.update(sidebar(experiment=experiment)) |
|
434 | 434 | |
|
435 | 435 | return render(request, 'experiment.html', kwargs) |
|
436 | 436 | |
|
437 | 437 | |
|
438 | 438 | def experiment_new(request, id_camp=None): |
|
439 | 439 | |
|
440 | 440 | kwargs = {} |
|
441 | 441 | |
|
442 | 442 | if request.method == 'GET': |
|
443 | 443 | if 'template' in request.GET: |
|
444 | 444 | if request.GET['template']=='0': |
|
445 | 445 | form = NewForm(initial={'create_from':2}, |
|
446 | 446 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
447 | 447 | else: |
|
448 | 448 | kwargs['button'] = 'Create' |
|
449 | 449 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
450 | 450 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
451 | 451 | form = ExperimentForm(instance=Experiment.objects.get(pk=request.GET['template']), |
|
452 | 452 | initial={'template':False}) |
|
453 | 453 | elif 'blank' in request.GET: |
|
454 | 454 | kwargs['button'] = 'Create' |
|
455 | 455 | form = ExperimentForm() |
|
456 | 456 | else: |
|
457 | 457 | form = NewForm() |
|
458 | 458 | |
|
459 | 459 | if request.method == 'POST': |
|
460 | 460 | form = ExperimentForm(request.POST) |
|
461 | 461 | if form.is_valid(): |
|
462 | 462 | experiment = form.save() |
|
463 | 463 | |
|
464 | 464 | if 'template' in request.GET: |
|
465 | 465 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
466 | 466 | for conf in configurations: |
|
467 | 467 | conf.clone(experiment=experiment, template=False) |
|
468 | 468 | |
|
469 | 469 | return redirect('url_experiment', id_exp=experiment.id) |
|
470 | 470 | |
|
471 | 471 | kwargs['form'] = form |
|
472 | 472 | kwargs['title'] = 'Experiment' |
|
473 | 473 | kwargs['suptitle'] = 'New' |
|
474 | 474 | |
|
475 | 475 | return render(request, 'experiment_edit.html', kwargs) |
|
476 | 476 | |
|
477 | 477 | |
|
478 | 478 | def experiment_edit(request, id_exp): |
|
479 | 479 | |
|
480 | 480 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
481 | 481 | |
|
482 | 482 | if request.method == 'GET': |
|
483 | 483 | form = ExperimentForm(instance=experiment) |
|
484 | 484 | |
|
485 | 485 | if request.method=='POST': |
|
486 | 486 | form = ExperimentForm(request.POST, instance=experiment) |
|
487 | 487 | |
|
488 | 488 | if form.is_valid(): |
|
489 | 489 | experiment = form.save() |
|
490 | 490 | return redirect('url_experiment', id_exp=experiment.id) |
|
491 | 491 | |
|
492 | 492 | kwargs = {} |
|
493 | 493 | kwargs['form'] = form |
|
494 | 494 | kwargs['title'] = 'Experiment' |
|
495 | 495 | kwargs['suptitle'] = 'Edit' |
|
496 | 496 | kwargs['button'] = 'Update' |
|
497 | 497 | |
|
498 | 498 | return render(request, 'experiment_edit.html', kwargs) |
|
499 | 499 | |
|
500 | 500 | |
|
501 | 501 | def experiment_delete(request, id_exp): |
|
502 | 502 | |
|
503 | 503 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
504 | 504 | |
|
505 | 505 | if request.method=='POST': |
|
506 | 506 | if request.user.is_staff: |
|
507 | 507 | experiment.delete() |
|
508 | 508 | return redirect('url_experiments') |
|
509 | 509 | |
|
510 | 510 | return HttpResponse("Not enough permission to delete this object") |
|
511 | 511 | |
|
512 | 512 | kwargs = {'object':experiment, 'exp_active':'active', |
|
513 | 513 | 'url_cancel':'url_experiment', 'id_item':id_exp} |
|
514 | 514 | |
|
515 | 515 | return render(request, 'item_delete.html', kwargs) |
|
516 | 516 | |
|
517 | 517 | |
|
518 | 518 | def dev_confs(request): |
|
519 | 519 | |
|
520 | 520 | configurations = Configuration.objects.all().order_by('type', 'device__device_type', 'experiment') |
|
521 | 521 | |
|
522 | 522 | # keys = ['id', 'device__device_type__name', 'device__name', 'experiment__campaign__name', 'experiment__name'] |
|
523 | 523 | |
|
524 | 524 | keys = ['id', 'device', 'experiment', 'type', 'programmed_date'] |
|
525 | 525 | |
|
526 | 526 | kwargs = {} |
|
527 | 527 | |
|
528 | 528 | kwargs['configuration_keys'] = keys[1:] |
|
529 | 529 | kwargs['configurations'] = configurations#.values(*keys) |
|
530 | 530 | |
|
531 | 531 | kwargs['title'] = 'Configuration' |
|
532 | 532 | kwargs['suptitle'] = 'List' |
|
533 | 533 | kwargs['button'] = 'New Configuration' |
|
534 | 534 | |
|
535 | 535 | return render(request, 'dev_confs.html', kwargs) |
|
536 | 536 | |
|
537 | 537 | |
|
538 | 538 | def dev_conf(request, id_conf): |
|
539 | 539 | |
|
540 | 540 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
541 | 541 | |
|
542 | 542 | return redirect(conf.get_absolute_url()) |
|
543 | 543 | |
|
544 | 544 | |
|
545 | 545 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
546 | 546 | |
|
547 | 547 | initial = {} |
|
548 | 548 | |
|
549 | 549 | if id_exp<>0: |
|
550 | 550 | initial['experiment'] = id_exp |
|
551 | 551 | |
|
552 | 552 | if id_dev<>0: |
|
553 | 553 | initial['device'] = id_dev |
|
554 | 554 | |
|
555 | 555 | if request.method == 'GET': |
|
556 | 556 | if id_dev==0: |
|
557 | 557 | form = ConfigurationForm(initial=initial) |
|
558 | 558 | else: |
|
559 | 559 | device = Device.objects.get(pk=id_dev) |
|
560 | 560 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
561 | 561 | |
|
562 | 562 | form = DevConfForm(initial=initial) |
|
563 | 563 | |
|
564 | 564 | if request.method == 'POST': |
|
565 | 565 | |
|
566 | 566 | device = Device.objects.get(pk=request.POST['device']) |
|
567 | 567 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
568 | 568 | |
|
569 | 569 | form = DevConfForm(request.POST) |
|
570 | 570 | |
|
571 | 571 | if form.is_valid(): |
|
572 | 572 | dev_conf = form.save() |
|
573 | 573 | |
|
574 | 574 | return redirect('url_dev_confs') |
|
575 | 575 | |
|
576 | 576 | kwargs = {} |
|
577 | 577 | kwargs['id_exp'] = id_exp |
|
578 | 578 | kwargs['form'] = form |
|
579 | 579 | kwargs['title'] = 'Configuration' |
|
580 | 580 | kwargs['suptitle'] = 'New' |
|
581 | 581 | kwargs['button'] = 'Create' |
|
582 | 582 | |
|
583 | 583 | return render(request, 'dev_conf_edit.html', kwargs) |
|
584 | 584 | |
|
585 | 585 | |
|
586 | 586 | def dev_conf_edit(request, id_conf): |
|
587 | 587 | |
|
588 | 588 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
589 | 589 | |
|
590 | 590 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
591 | 591 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
592 | 592 | |
|
593 | 593 | dev_conf = DevConfModel.objects.get(pk=id_conf) |
|
594 | 594 | |
|
595 | 595 | if request.method=='GET': |
|
596 | 596 | form = DevConfForm(instance=dev_conf) |
|
597 | 597 | |
|
598 | 598 | if request.method=='POST': |
|
599 | 599 | form = DevConfForm(request.POST, instance=dev_conf) |
|
600 | 600 | |
|
601 | 601 | if form.is_valid(): |
|
602 | 602 | form.save() |
|
603 | 603 | return redirect('url_dev_conf', id_conf=id_conf) |
|
604 | 604 | |
|
605 | 605 | kwargs = {} |
|
606 | 606 | kwargs['form'] = form |
|
607 | 607 | kwargs['title'] = 'Device Configuration' |
|
608 | 608 | kwargs['suptitle'] = 'Edit' |
|
609 | 609 | kwargs['button'] = 'Update' |
|
610 | 610 | |
|
611 | 611 | ###### SIDEBAR ###### |
|
612 | 612 | kwargs.update(sidebar(conf=conf)) |
|
613 | 613 | |
|
614 | 614 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
615 | 615 | |
|
616 | 616 | |
|
617 | 617 | def dev_conf_start(request, id_conf): |
|
618 | 618 | |
|
619 | 619 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
620 | 620 | |
|
621 | 621 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
622 | 622 | |
|
623 | 623 | conf = DevConfModel.objects.get(pk=id_conf) |
|
624 | 624 | |
|
625 | 625 | if conf.start_device(): |
|
626 | 626 | messages.success(request, conf.message) |
|
627 | 627 | else: |
|
628 | 628 | messages.error(request, conf.message) |
|
629 | 629 | |
|
630 | 630 | conf.status_device() |
|
631 | 631 | |
|
632 | 632 | return redirect(conf.get_absolute_url()) |
|
633 | 633 | |
|
634 | 634 | |
|
635 | 635 | def dev_conf_stop(request, id_conf): |
|
636 | 636 | |
|
637 | 637 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
638 | 638 | |
|
639 | 639 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
640 | 640 | |
|
641 | 641 | conf = DevConfModel.objects.get(pk=id_conf) |
|
642 | 642 | |
|
643 | 643 | if conf.stop_device(): |
|
644 | 644 | messages.success(request, conf.message) |
|
645 | 645 | else: |
|
646 | 646 | messages.error(request, conf.message) |
|
647 | 647 | |
|
648 | 648 | conf.status_device() |
|
649 | 649 | |
|
650 | 650 | return redirect(conf.get_absolute_url()) |
|
651 | 651 | |
|
652 | 652 | |
|
653 | 653 | def dev_conf_status(request, id_conf): |
|
654 | 654 | |
|
655 | 655 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
656 | 656 | |
|
657 | 657 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
658 | 658 | |
|
659 | 659 | conf = DevConfModel.objects.get(pk=id_conf) |
|
660 | 660 | |
|
661 | 661 | if conf.status_device(): |
|
662 | 662 | messages.success(request, conf.message) |
|
663 | 663 | else: |
|
664 | 664 | messages.error(request, conf.message) |
|
665 | 665 | |
|
666 | 666 | return redirect(conf.get_absolute_url()) |
|
667 | 667 | |
|
668 | 668 | |
|
669 | 669 | def dev_conf_write(request, id_conf): |
|
670 | 670 | |
|
671 | 671 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
672 | 672 | |
|
673 | 673 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
674 | 674 | |
|
675 | 675 | conf = DevConfModel.objects.get(pk=id_conf) |
|
676 | 676 | |
|
677 | 677 | answer = conf.write_device() |
|
678 | 678 | conf.status_device() |
|
679 | 679 | |
|
680 | 680 | if answer: |
|
681 | 681 | messages.success(request, conf.message) |
|
682 | 682 | |
|
683 | 683 | #Creating a historical configuration |
|
684 | 684 | conf.clone(type=0, template=False) |
|
685 | 685 | |
|
686 | 686 | #Original configuration |
|
687 | 687 | conf = DevConfModel.objects.get(pk=id_conf) |
|
688 | 688 | else: |
|
689 | 689 | messages.error(request, conf.message) |
|
690 | 690 | |
|
691 | 691 | return redirect(conf.get_absolute_url()) |
|
692 | 692 | |
|
693 | 693 | |
|
694 | 694 | def dev_conf_read(request, id_conf): |
|
695 | 695 | |
|
696 | 696 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
697 | 697 | |
|
698 | 698 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
699 | 699 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
700 | 700 | |
|
701 | 701 | conf = DevConfModel.objects.get(pk=id_conf) |
|
702 | 702 | |
|
703 | 703 | if request.method=='GET': |
|
704 | 704 | |
|
705 | 705 | parms = conf.read_device() |
|
706 | 706 | conf.status_device() |
|
707 | 707 | |
|
708 | 708 | if not parms: |
|
709 | 709 | messages.error(request, conf.message) |
|
710 | 710 | return redirect(conf.get_absolute_url()) |
|
711 | 711 | |
|
712 | 712 | form = DevConfForm(initial=parms, instance=conf) |
|
713 | 713 | |
|
714 | 714 | if request.method=='POST': |
|
715 | 715 | form = DevConfForm(request.POST, instance=conf) |
|
716 | 716 | |
|
717 | 717 | if form.is_valid(): |
|
718 | 718 | form.save() |
|
719 | 719 | return redirect(conf.get_absolute_url()) |
|
720 | 720 | |
|
721 | 721 | messages.error(request, "Parameters could not be saved") |
|
722 | 722 | |
|
723 | 723 | kwargs = {} |
|
724 | 724 | kwargs['id_dev'] = conf.id |
|
725 | 725 | kwargs['form'] = form |
|
726 | 726 | kwargs['title'] = 'Device Configuration' |
|
727 | 727 | kwargs['suptitle'] = 'Parameters read from device' |
|
728 | 728 | kwargs['button'] = 'Save' |
|
729 | 729 | |
|
730 | 730 | ###### SIDEBAR ###### |
|
731 | 731 | kwargs.update(sidebar(conf=conf)) |
|
732 | 732 | |
|
733 | 733 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
734 | 734 | |
|
735 | 735 | |
|
736 | 736 | def dev_conf_import(request, id_conf): |
|
737 | 737 | |
|
738 | 738 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
739 | 739 | |
|
740 | 740 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
741 | 741 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
742 | 742 | conf = DevConfModel.objects.get(pk=id_conf) |
|
743 | 743 | |
|
744 | 744 | if request.method == 'GET': |
|
745 | 745 | file_form = UploadFileForm() |
|
746 | 746 | |
|
747 | 747 | if request.method == 'POST': |
|
748 | 748 | file_form = UploadFileForm(request.POST, request.FILES) |
|
749 | 749 | |
|
750 | 750 | if file_form.is_valid(): |
|
751 | 751 | |
|
752 | 752 | parms = conf.import_from_file(request.FILES['file']) |
|
753 | 753 | |
|
754 | 754 | if parms: |
|
755 | 755 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
756 | 756 | form = DevConfForm(initial=parms, instance=conf) |
|
757 | 757 | |
|
758 | 758 | kwargs = {} |
|
759 | 759 | kwargs['id_dev'] = conf.id |
|
760 | 760 | kwargs['form'] = form |
|
761 | 761 | kwargs['title'] = 'Device Configuration' |
|
762 | 762 | kwargs['suptitle'] = 'Parameters imported' |
|
763 | 763 | kwargs['button'] = 'Save' |
|
764 | 764 | kwargs['action'] = conf.get_absolute_url_edit() |
|
765 | 765 | kwargs['previous'] = conf.get_absolute_url() |
|
766 | 766 | |
|
767 | 767 | ###### SIDEBAR ###### |
|
768 | 768 | kwargs.update(sidebar(conf=conf)) |
|
769 | 769 | |
|
770 | 770 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
771 | 771 | |
|
772 | 772 | messages.error(request, "Could not import parameters from file") |
|
773 | 773 | |
|
774 | 774 | kwargs = {} |
|
775 | 775 | kwargs['id_dev'] = conf.id |
|
776 | 776 | kwargs['title'] = 'Device Configuration' |
|
777 | 777 | kwargs['form'] = file_form |
|
778 | 778 | kwargs['suptitle'] = 'Importing file' |
|
779 | 779 | kwargs['button'] = 'Import' |
|
780 | 780 | |
|
781 | 781 | kwargs.update(sidebar(conf=conf)) |
|
782 | 782 | |
|
783 | 783 | return render(request, 'dev_conf_import.html', kwargs) |
|
784 | 784 | |
|
785 | 785 | |
|
786 | 786 | def dev_conf_export(request, id_conf): |
|
787 | 787 | |
|
788 | 788 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
789 | 789 | |
|
790 | 790 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
791 | 791 | |
|
792 | 792 | conf = DevConfModel.objects.get(pk=id_conf) |
|
793 | 793 | |
|
794 | 794 | if request.method == 'GET': |
|
795 | 795 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
796 | 796 | |
|
797 | 797 | if request.method == 'POST': |
|
798 | 798 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
799 | 799 | |
|
800 | 800 | if file_form.is_valid(): |
|
801 | 801 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
802 | 802 | |
|
803 | 803 | response = HttpResponse(content_type=fields['content_type']) |
|
804 | 804 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
805 | 805 | response.write(fields['content']) |
|
806 | 806 | |
|
807 | 807 | return response |
|
808 | 808 | |
|
809 | 809 | messages.error(request, "Could not export parameters") |
|
810 | 810 | |
|
811 | 811 | kwargs = {} |
|
812 | 812 | kwargs['id_dev'] = conf.id |
|
813 | 813 | kwargs['title'] = 'Device Configuration' |
|
814 | 814 | kwargs['form'] = file_form |
|
815 | 815 | kwargs['suptitle'] = 'Exporting file' |
|
816 | 816 | kwargs['button'] = 'Export' |
|
817 | 817 | |
|
818 | 818 | return render(request, 'dev_conf_export.html', kwargs) |
|
819 | 819 | |
|
820 | 820 | |
|
821 | 821 | def dev_conf_delete(request, id_conf): |
|
822 | 822 | |
|
823 | 823 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
824 | 824 | |
|
825 | 825 | if request.method=='POST': |
|
826 | 826 | if request.user.is_staff: |
|
827 | 827 | id_exp = conf.experiment.id |
|
828 | 828 | conf.delete() |
|
829 | 829 | return redirect('url_experiment', id_exp=id_exp) |
|
830 | 830 | |
|
831 | 831 | return HttpResponse("Not enough permission to delete this object") |
|
832 | 832 | |
|
833 | 833 | kwargs = {'object':conf, 'conf_active':'active', |
|
834 | 834 | 'url_cancel':'url_dev_conf', 'id_item':id_conf} |
|
835 | 835 | |
|
836 | 836 | return render(request, 'item_delete.html', kwargs) |
|
837 | 837 | |
|
838 | 838 | |
|
839 | 839 | def sidebar(**kwargs): |
|
840 | 840 | |
|
841 | 841 | side_data = {} |
|
842 | 842 | |
|
843 | 843 | conf = kwargs.get('conf', None) |
|
844 | 844 | experiment = kwargs.get('experiment', None) |
|
845 | 845 | |
|
846 | 846 | if not experiment: |
|
847 | 847 | experiment = conf.experiment |
|
848 | 848 | |
|
849 | 849 | if experiment: |
|
850 | 850 | side_data['experiment'] = experiment |
|
851 | 851 | campaign = experiment.campaign_set.all() |
|
852 | 852 | if campaign: |
|
853 | 853 | side_data['campaign'] = campaign[0] |
|
854 | 854 | experiments = campaign[0].experiments.all() |
|
855 | 855 | else: |
|
856 | 856 | experiments = [experiment] |
|
857 | 857 | configurations = experiment.configuration_set.filter(type=0) |
|
858 | 858 | side_data['side_experiments'] = experiments |
|
859 | 859 | side_data['side_configurations'] = configurations |
|
860 | 860 | |
|
861 | 861 | return side_data |
|
862 | 862 | |
|
863 | 863 | |
|
864 | 864 | def operation(request, id_camp=None): |
|
865 | 865 | |
|
866 | 866 | if not id_camp: |
|
867 | 867 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
868 | 868 | |
|
869 | 869 | if not campaigns: |
|
870 | 870 | kwargs = {} |
|
871 | 871 | kwargs['title'] = 'No Campaigns' |
|
872 | 872 | kwargs['suptitle'] = 'Empty' |
|
873 | 873 | return render(request, 'operation.html', kwargs) |
|
874 | 874 | |
|
875 | 875 | id_camp = campaigns[0].id |
|
876 | 876 | |
|
877 | 877 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
878 | 878 | |
|
879 | 879 | if request.method=='GET': |
|
880 | 880 | form = OperationForm(initial={'campaign': campaign.id}, length = 5) |
|
881 | 881 | |
|
882 | 882 | if request.method=='POST': |
|
883 | 883 | form = OperationForm(request.POST, initial={'campaign':campaign.id}, length = 5) |
|
884 | 884 | |
|
885 | 885 | if form.is_valid(): |
|
886 | 886 | return redirect('url_operation', id_camp=campaign.id) |
|
887 | 887 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
888 | 888 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
889 | 889 | #for exs in experiments: |
|
890 | 890 | # exs.get_status() |
|
891 | 891 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
892 | 892 | #experiments = [Experiment.objects.filter(location__pk=location.id).filter(campaign__pk=campaign.id) for location in locations] |
|
893 | 893 | kwargs = {} |
|
894 | 894 | #---Campaign |
|
895 | 895 | kwargs['campaign'] = campaign |
|
896 | 896 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
897 | 897 | #---Experiment |
|
898 | 898 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
899 | 899 | kwargs['experiment_keys'] = keys[1:] |
|
900 | 900 | kwargs['experiments'] = experiments |
|
901 | 901 | #---Radar |
|
902 | 902 | kwargs['locations'] = locations |
|
903 | 903 | #---Else |
|
904 | 904 | kwargs['title'] = 'Campaign' |
|
905 | 905 | kwargs['suptitle'] = campaign.name |
|
906 | 906 | kwargs['form'] = form |
|
907 | 907 | kwargs['button'] = 'Search' |
|
908 | 908 | kwargs['details'] = True |
|
909 | 909 | kwargs['search_button'] = True |
|
910 | 910 | |
|
911 | 911 | return render(request, 'operation.html', kwargs) |
|
912 | 912 | |
|
913 | 913 | |
|
914 | 914 | def operation_search(request, id_camp=None): |
|
915 | 915 | |
|
916 | 916 | |
|
917 | 917 | if not id_camp: |
|
918 | 918 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
919 | 919 | |
|
920 | 920 | if not campaigns: |
|
921 | 921 | return render(request, 'operation.html', {}) |
|
922 | 922 | |
|
923 | 923 | id_camp = campaigns[0].id |
|
924 | 924 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
925 | 925 | |
|
926 | 926 | if request.method=='GET': |
|
927 | 927 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
928 | 928 | |
|
929 | 929 | if request.method=='POST': |
|
930 | 930 | form = OperationSearchForm(request.POST, initial={'campaign':campaign.id}) |
|
931 | 931 | |
|
932 | 932 | if form.is_valid(): |
|
933 | 933 | return redirect('url_operation', id_camp=campaign.id) |
|
934 | 934 | |
|
935 | 935 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
936 | 936 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
937 | 937 | #for exs in experiments: |
|
938 | 938 | # exs.get_status() |
|
939 | 939 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
940 | 940 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
941 | 941 | |
|
942 | 942 | kwargs = {} |
|
943 | 943 | #---Campaign |
|
944 | 944 | kwargs['campaign'] = campaign |
|
945 | 945 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
946 | 946 | #---Experiment |
|
947 | 947 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
948 | 948 | kwargs['experiment_keys'] = keys[1:] |
|
949 | 949 | kwargs['experiments'] = experiments |
|
950 | 950 | #---Radar |
|
951 | 951 | kwargs['locations'] = locations |
|
952 | 952 | #---Else |
|
953 | 953 | kwargs['title'] = 'Campaign' |
|
954 | 954 | kwargs['suptitle'] = campaign.name |
|
955 | 955 | kwargs['form'] = form |
|
956 | 956 | kwargs['button'] = 'Select' |
|
957 | 957 | kwargs['details'] = True |
|
958 | 958 | kwargs['search_button'] = False |
|
959 | 959 | |
|
960 | 960 | return render(request, 'operation.html', kwargs) |
|
961 | 961 | |
|
962 | 962 | |
|
963 | 963 | def radar_play(request, id_camp, id_radar): |
|
964 | 964 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
965 | 965 | radar = get_object_or_404(Location, pk = id_radar) |
|
966 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) | |
|
967 | current_time = datetime.today() | |
|
968 | #exp = RunningExperiment( | |
|
969 | # radar = purchase_request.user_id, | |
|
970 | # running_experiment = purchase_request, | |
|
971 | # status = , | |
|
972 | # ) | |
|
973 | #new_pos.append(exp) | |
|
974 | #exp.save() | |
|
966 | today = datetime.today() | |
|
967 | now = today.time() | |
|
968 | ||
|
969 | #--If campaign datetime is ok: | |
|
970 | if today >= campaign.start_date and today <= campaign.end_date: | |
|
971 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) | |
|
972 | for exp in experiments: | |
|
973 | #--If experiment time is ok: | |
|
974 | if now >= exp.start_time and now <= exp.end_time: | |
|
975 | configurations = Configuration.objects.filter(experiment = exp) | |
|
976 | for conf in configurations: | |
|
977 | if 'cgs' in conf.device.device_type.name: | |
|
978 | conf.status_device() | |
|
979 | else: | |
|
980 | answer = conf.start_device() | |
|
981 | conf.status_device() | |
|
982 | #--Running Experiment | |
|
983 | r_e = RunningExperiment.objects.filter(radar=radar) | |
|
984 | #--If RunningExperiment element exists | |
|
985 | if r_e: | |
|
986 | r_e = r_e[0] | |
|
987 | r_e.running_experiment = exp | |
|
988 | r_e.status = 3 | |
|
989 | r_e.save() | |
|
990 | else: | |
|
991 | running_experiment = RunningExperiment( | |
|
992 | radar = radar, | |
|
993 | running_experiment = exp, | |
|
994 | status = 3, | |
|
995 | ) | |
|
996 | running_experiment.save() | |
|
997 | ||
|
998 | if answer: | |
|
999 | messages.success(request, conf.message) | |
|
1000 | exp.status=2 | |
|
1001 | exp.save() | |
|
1002 | else: | |
|
1003 | messages.error(request, conf.message) | |
|
1004 | else: | |
|
1005 | if exp.status == 1 or exp.status == 3: | |
|
1006 | exp.status=3 | |
|
1007 | exp.save() | |
|
1008 | ||
|
975 | 1009 | |
|
976 | 1010 | route = request.META['HTTP_REFERER'] |
|
977 | 1011 | route = str(route) |
|
978 | 1012 | if 'search' in route: |
|
979 | 1013 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
980 | 1014 | else: |
|
981 | 1015 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
982 | 1016 | |
|
983 | 1017 | |
|
984 | 1018 | def radar_stop(request, id_camp, id_radar): |
|
985 | 1019 | |
|
986 | 1020 | route = request.META['HTTP_REFERER'] |
|
987 | 1021 | route = str(route) |
|
988 | 1022 | if 'search' in route: |
|
989 | 1023 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
990 | 1024 | else: |
|
991 | 1025 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
992 | 1026 | |
|
993 | 1027 | |
|
994 | 1028 | def radar_refresh(request, id_camp, id_radar): |
|
995 | 1029 | |
|
996 | 1030 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
997 | 1031 | radar = get_object_or_404(Location, pk = id_radar) |
|
998 | 1032 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
999 | 1033 | for exs in experiments: |
|
1000 | 1034 | exs.get_status() |
|
1001 | 1035 | |
|
1002 | 1036 | route = request.META['HTTP_REFERER'] |
|
1003 | 1037 | route = str(route) |
|
1004 | 1038 | if 'search' in route: |
|
1005 | 1039 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1006 | 1040 | else: |
|
1007 | 1041 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1008 | 1042 |
General Comments 0
You need to be logged in to leave comments.
Login now