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