@@ -1,26 +1,34 | |||||
1 | {% extends "base_edit.html" %} |
|
1 | {% extends "base_edit.html" %} | |
2 | {% load bootstrap3 %} |
|
2 | {% load bootstrap3 %} | |
3 | {% load static %} |
|
3 | {% load static %} | |
4 | {% load main_tags %} |
|
4 | {% load main_tags %} | |
5 | {% block extra-head %} |
|
5 | {% block extra-head %} | |
6 | <link href="{% static 'css/bootstrap-datetimepicker.min.css' %}" media="screen" rel="stylesheet"> |
|
6 | <link href="{% static 'css/bootstrap-datetimepicker.min.css' %}" media="screen" rel="stylesheet"> | |
7 | <style type="text/css"> |
|
7 | <style type="text/css"> | |
8 | .tabuled {font-family: Courier New} |
|
8 | .tabuled {font-family: Courier New} | |
9 | .check-inline {display: inline} |
|
9 | .check-inline {display: inline} | |
10 | </style> |
|
10 | </style> | |
11 | {% endblock %} |
|
11 | {% endblock %} | |
12 |
|
12 | |||
13 | {% block extra-js%} |
|
13 | {% block extra-js%} | |
14 | <script src="{% static 'js/moment.min.js' %}"></script> |
|
14 | <script src="{% static 'js/moment.min.js' %}"></script> | |
15 | <script src="{% static 'js/bootstrap-datetimepicker.min.js' %}"></script> |
|
15 | <script src="{% static 'js/bootstrap-datetimepicker.min.js' %}"></script> | |
16 | <script src="{% static 'js/cr.js' %}"></script> |
|
16 | <script src="{% static 'js/cr.js' %}"></script> | |
17 |
|
17 | |||
18 | <script type="text/javascript"> |
|
18 | <script type="text/javascript"> | |
19 |
|
19 | |||
20 | $("#bt_Delete").click(function() { |
|
20 | $("#bt_Delete").click(function() { | |
21 | document.location = "{% url 'url_delete_mix_experiment' id_exp %}"; |
|
21 | document.location = "{% url 'url_delete_mix_experiment' id_exp %}"; | |
22 | }); |
|
22 | }); | |
23 |
|
23 | |||
|
24 | $('input[type=radio][name=mode]').change(function() { | |||
|
25 | if (this.value == '0') { | |||
|
26 | $('input[type=radio][name=operation]').prop('disabled', false); | |||
|
27 | }else{ | |||
|
28 | $('input[type=radio][name=operation]').prop('disabled', true); | |||
|
29 | } | |||
|
30 | }); | |||
|
31 | ||||
24 | </script> |
|
32 | </script> | |
25 |
|
33 | |||
26 | {% endblock %} No newline at end of file |
|
34 | {% endblock %} |
@@ -1,38 +1,39 | |||||
1 | from django.template.defaulttags import register |
|
1 | from django.template.defaulttags import register | |
2 | from django.utils.safestring import mark_safe |
|
2 | from django.utils.safestring import mark_safe | |
3 |
|
3 | |||
4 | @register.filter |
|
4 | @register.filter | |
5 | def attr(instance, key): |
|
5 | def attr(instance, key): | |
6 |
|
6 | |||
7 | display_key = "get_" + key + "_display" |
|
7 | display_key = "get_" + key + "_display" | |
8 |
|
8 | |||
9 | if hasattr(instance, display_key): |
|
9 | if hasattr(instance, display_key): | |
10 | return getattr(instance, display_key)() |
|
10 | return getattr(instance, display_key)() | |
11 |
|
11 | |||
12 | if hasattr(instance, key): |
|
12 | if hasattr(instance, key): | |
13 | return getattr(instance, key) |
|
13 | return getattr(instance, key) | |
14 |
|
14 | |||
15 | return instance.get(key) |
|
15 | return instance.get(key) | |
16 |
|
16 | |||
17 | @register.filter |
|
17 | @register.filter | |
18 | def title(s): |
|
18 | def title(s): | |
19 | return s.split('__')[-1].replace('_', ' ').title() |
|
19 | return s.split('__')[-1].replace('_', ' ').title() | |
20 |
|
20 | |||
21 | @register.filter |
|
21 | @register.filter | |
22 | def value(instance, key): |
|
22 | def value(instance, key): | |
23 |
|
23 | |||
24 | item = instance |
|
24 | item = instance | |
25 | if key=='name': |
|
25 | if key=='name': | |
26 | return '%s' % item |
|
26 | return '%s' % item | |
27 | for my_key in key.split("__"): |
|
27 | for my_key in key.split("__"): | |
28 | item = attr(item, my_key) |
|
28 | item = attr(item, my_key) | |
29 |
|
29 | |||
30 | return item |
|
30 | return item | |
31 |
|
31 | |||
32 | @register.simple_tag |
|
32 | @register.simple_tag | |
33 | def get_verbose_field_name(instance, field_name): |
|
33 | def get_verbose_field_name(instance, field_name): | |
34 | """ |
|
34 | """ | |
35 | Returns verbose_name for a field. |
|
35 | Returns verbose_name for a field. | |
36 | """ |
|
36 | """ | |
37 |
|
37 | if field_name=='ipp_unit': | ||
|
38 | return 'Inter pulse period [Km(Units)]' | |||
38 | return mark_safe(instance._meta.get_field(field_name).verbose_name) No newline at end of file |
|
39 | return mark_safe(instance._meta.get_field(field_name).verbose_name) |
@@ -1,1363 +1,1379 | |||||
1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse | |
2 | from django.utils.safestring import mark_safe |
|
2 | from django.utils.safestring import mark_safe | |
3 | from django.http import HttpResponseRedirect |
|
3 | from django.http import HttpResponseRedirect | |
4 | from django.core.urlresolvers import reverse |
|
4 | from django.core.urlresolvers import reverse | |
5 | from django.contrib import messages |
|
5 | from django.contrib import messages | |
6 | from datetime import datetime |
|
6 | from datetime import datetime | |
7 |
|
7 | |||
8 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
8 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm | |
9 | from .forms import OperationSearchForm |
|
9 | from .forms import OperationSearchForm | |
10 | from apps.cgs.forms import CGSConfigurationForm |
|
10 | from apps.cgs.forms import CGSConfigurationForm | |
11 | from apps.jars.forms import JARSConfigurationForm |
|
11 | from apps.jars.forms import JARSConfigurationForm | |
12 | from apps.usrp.forms import USRPConfigurationForm |
|
12 | from apps.usrp.forms import USRPConfigurationForm | |
13 | from apps.abs.forms import ABSConfigurationForm |
|
13 | from apps.abs.forms import ABSConfigurationForm | |
14 | from apps.rc.forms import RCConfigurationForm, RCMixConfigurationForm |
|
14 | from apps.rc.forms import RCConfigurationForm, RCMixConfigurationForm | |
15 | from apps.dds.forms import DDSConfigurationForm |
|
15 | from apps.dds.forms import DDSConfigurationForm | |
16 |
|
16 | |||
17 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment |
|
17 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment | |
18 | from apps.cgs.models import CGSConfiguration |
|
18 | from apps.cgs.models import CGSConfiguration | |
19 | from apps.jars.models import JARSConfiguration |
|
19 | from apps.jars.models import JARSConfiguration | |
20 | from apps.usrp.models import USRPConfiguration |
|
20 | from apps.usrp.models import USRPConfiguration | |
21 | from apps.abs.models import ABSConfiguration |
|
21 | from apps.abs.models import ABSConfiguration | |
22 | from apps.rc.models import RCConfiguration, RCLine, RCLineType |
|
22 | from apps.rc.models import RCConfiguration, RCLine, RCLineType | |
23 | from apps.dds.models import DDSConfiguration |
|
23 | from apps.dds.models import DDSConfiguration | |
24 |
|
24 | |||
25 | # Create your views here. |
|
25 | # Create your views here. | |
26 |
|
26 | |||
27 | CONF_FORMS = { |
|
27 | CONF_FORMS = { | |
28 | 'rc': RCConfigurationForm, |
|
28 | 'rc': RCConfigurationForm, | |
29 | 'rc_mix': RCMixConfigurationForm, |
|
29 | 'rc_mix': RCMixConfigurationForm, | |
30 | 'dds': DDSConfigurationForm, |
|
30 | 'dds': DDSConfigurationForm, | |
31 | 'jars': JARSConfigurationForm, |
|
31 | 'jars': JARSConfigurationForm, | |
32 | 'cgs': CGSConfigurationForm, |
|
32 | 'cgs': CGSConfigurationForm, | |
33 | 'abs': ABSConfigurationForm, |
|
33 | 'abs': ABSConfigurationForm, | |
34 | 'usrp': USRPConfigurationForm, |
|
34 | 'usrp': USRPConfigurationForm, | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | CONF_MODELS = { |
|
37 | CONF_MODELS = { | |
38 | 'rc': RCConfiguration, |
|
38 | 'rc': RCConfiguration, | |
39 | 'dds': DDSConfiguration, |
|
39 | 'dds': DDSConfiguration, | |
40 | 'jars': JARSConfiguration, |
|
40 | 'jars': JARSConfiguration, | |
41 | 'cgs': CGSConfiguration, |
|
41 | 'cgs': CGSConfiguration, | |
42 | 'abs': ABSConfiguration, |
|
42 | 'abs': ABSConfiguration, | |
43 | 'usrp': USRPConfiguration, |
|
43 | 'usrp': USRPConfiguration, | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | MIX_MODES = { |
|
46 | MIX_MODES = { | |
|
47 | '0': 'P', | |||
|
48 | '1': 'S', | |||
|
49 | } | |||
|
50 | ||||
|
51 | MIX_OPERATIONS = { | |||
47 | '0': 'OR', |
|
52 | '0': 'OR', | |
48 | '1': 'XOR', |
|
53 | '1': 'XOR', | |
49 | '2': 'AND', |
|
54 | '2': 'AND', | |
50 | '3': 'NAND' |
|
55 | '3': 'NAND', | |
51 | } |
|
56 | } | |
52 |
|
57 | |||
53 |
|
58 | |||
54 | def index(request): |
|
59 | def index(request): | |
55 | kwargs = {} |
|
60 | kwargs = {} | |
56 |
|
61 | |||
57 | return render(request, 'index.html', kwargs) |
|
62 | return render(request, 'index.html', kwargs) | |
58 |
|
63 | |||
59 |
|
64 | |||
60 | def locations(request): |
|
65 | def locations(request): | |
61 |
|
66 | |||
62 | locations = Location.objects.all().order_by('name') |
|
67 | locations = Location.objects.all().order_by('name') | |
63 |
|
68 | |||
64 | keys = ['id', 'name', 'description'] |
|
69 | keys = ['id', 'name', 'description'] | |
65 |
|
70 | |||
66 | kwargs = {} |
|
71 | kwargs = {} | |
67 | kwargs['location_keys'] = keys[1:] |
|
72 | kwargs['location_keys'] = keys[1:] | |
68 | kwargs['locations'] = locations |
|
73 | kwargs['locations'] = locations | |
69 | kwargs['title'] = 'Location' |
|
74 | kwargs['title'] = 'Location' | |
70 | kwargs['suptitle'] = 'List' |
|
75 | kwargs['suptitle'] = 'List' | |
71 | kwargs['button'] = 'New Location' |
|
76 | kwargs['button'] = 'New Location' | |
72 |
|
77 | |||
73 | return render(request, 'locations.html', kwargs) |
|
78 | return render(request, 'locations.html', kwargs) | |
74 |
|
79 | |||
75 |
|
80 | |||
76 | def location(request, id_loc): |
|
81 | def location(request, id_loc): | |
77 |
|
82 | |||
78 | location = get_object_or_404(Location, pk=id_loc) |
|
83 | location = get_object_or_404(Location, pk=id_loc) | |
79 |
|
84 | |||
80 | kwargs = {} |
|
85 | kwargs = {} | |
81 | kwargs['location'] = location |
|
86 | kwargs['location'] = location | |
82 | kwargs['location_keys'] = ['name', 'description'] |
|
87 | kwargs['location_keys'] = ['name', 'description'] | |
83 |
|
88 | |||
84 | kwargs['title'] = 'Location' |
|
89 | kwargs['title'] = 'Location' | |
85 | kwargs['suptitle'] = 'Details' |
|
90 | kwargs['suptitle'] = 'Details' | |
86 |
|
91 | |||
87 | return render(request, 'location.html', kwargs) |
|
92 | return render(request, 'location.html', kwargs) | |
88 |
|
93 | |||
89 |
|
94 | |||
90 | def location_new(request): |
|
95 | def location_new(request): | |
91 |
|
96 | |||
92 | if request.method == 'GET': |
|
97 | if request.method == 'GET': | |
93 | form = LocationForm() |
|
98 | form = LocationForm() | |
94 |
|
99 | |||
95 | if request.method == 'POST': |
|
100 | if request.method == 'POST': | |
96 | form = LocationForm(request.POST) |
|
101 | form = LocationForm(request.POST) | |
97 |
|
102 | |||
98 | if form.is_valid(): |
|
103 | if form.is_valid(): | |
99 | form.save() |
|
104 | form.save() | |
100 | return redirect('url_locations') |
|
105 | return redirect('url_locations') | |
101 |
|
106 | |||
102 | kwargs = {} |
|
107 | kwargs = {} | |
103 | kwargs['form'] = form |
|
108 | kwargs['form'] = form | |
104 | kwargs['title'] = 'Location' |
|
109 | kwargs['title'] = 'Location' | |
105 | kwargs['suptitle'] = 'New' |
|
110 | kwargs['suptitle'] = 'New' | |
106 | kwargs['button'] = 'Create' |
|
111 | kwargs['button'] = 'Create' | |
107 |
|
112 | |||
108 | return render(request, 'location_edit.html', kwargs) |
|
113 | return render(request, 'location_edit.html', kwargs) | |
109 |
|
114 | |||
110 |
|
115 | |||
111 | def location_edit(request, id_loc): |
|
116 | def location_edit(request, id_loc): | |
112 |
|
117 | |||
113 | location = get_object_or_404(Location, pk=id_loc) |
|
118 | location = get_object_or_404(Location, pk=id_loc) | |
114 |
|
119 | |||
115 | if request.method=='GET': |
|
120 | if request.method=='GET': | |
116 | form = LocationForm(instance=location) |
|
121 | form = LocationForm(instance=location) | |
117 |
|
122 | |||
118 | if request.method=='POST': |
|
123 | if request.method=='POST': | |
119 | form = LocationForm(request.POST, instance=location) |
|
124 | form = LocationForm(request.POST, instance=location) | |
120 |
|
125 | |||
121 | if form.is_valid(): |
|
126 | if form.is_valid(): | |
122 | form.save() |
|
127 | form.save() | |
123 | return redirect('url_locations') |
|
128 | return redirect('url_locations') | |
124 |
|
129 | |||
125 | kwargs = {} |
|
130 | kwargs = {} | |
126 | kwargs['form'] = form |
|
131 | kwargs['form'] = form | |
127 | kwargs['title'] = 'Location' |
|
132 | kwargs['title'] = 'Location' | |
128 | kwargs['suptitle'] = 'Edit' |
|
133 | kwargs['suptitle'] = 'Edit' | |
129 | kwargs['button'] = 'Update' |
|
134 | kwargs['button'] = 'Update' | |
130 |
|
135 | |||
131 | return render(request, 'location_edit.html', kwargs) |
|
136 | return render(request, 'location_edit.html', kwargs) | |
132 |
|
137 | |||
133 |
|
138 | |||
134 | def location_delete(request, id_loc): |
|
139 | def location_delete(request, id_loc): | |
135 |
|
140 | |||
136 | location = get_object_or_404(Location, pk=id_loc) |
|
141 | location = get_object_or_404(Location, pk=id_loc) | |
137 |
|
142 | |||
138 | if request.method=='POST': |
|
143 | if request.method=='POST': | |
139 |
|
144 | |||
140 | if request.user.is_staff: |
|
145 | if request.user.is_staff: | |
141 | location.delete() |
|
146 | location.delete() | |
142 | return redirect('url_locations') |
|
147 | return redirect('url_locations') | |
143 |
|
148 | |||
144 | messages.error(request, 'Not enough permission to delete this object') |
|
149 | messages.error(request, 'Not enough permission to delete this object') | |
145 | return redirect(location.get_absolute_url()) |
|
150 | return redirect(location.get_absolute_url()) | |
146 |
|
151 | |||
147 | kwargs = { |
|
152 | kwargs = { | |
148 | 'title': 'Delete', |
|
153 | 'title': 'Delete', | |
149 | 'suptitle': 'Location', |
|
154 | 'suptitle': 'Location', | |
150 | 'object': location, |
|
155 | 'object': location, | |
151 | 'previous': location.get_absolute_url(), |
|
156 | 'previous': location.get_absolute_url(), | |
152 | 'delete': True |
|
157 | 'delete': True | |
153 | } |
|
158 | } | |
154 |
|
159 | |||
155 | return render(request, 'confirm.html', kwargs) |
|
160 | return render(request, 'confirm.html', kwargs) | |
156 |
|
161 | |||
157 |
|
162 | |||
158 | def devices(request): |
|
163 | def devices(request): | |
159 |
|
164 | |||
160 | devices = Device.objects.all().order_by('device_type__name') |
|
165 | devices = Device.objects.all().order_by('device_type__name') | |
161 |
|
166 | |||
162 | # keys = ['id', 'device_type__name', 'name', 'ip_address'] |
|
167 | # keys = ['id', 'device_type__name', 'name', 'ip_address'] | |
163 | keys = ['id', 'name', 'ip_address', 'port_address', 'device_type'] |
|
168 | keys = ['id', 'name', 'ip_address', 'port_address', 'device_type'] | |
164 |
|
169 | |||
165 | kwargs = {} |
|
170 | kwargs = {} | |
166 | kwargs['device_keys'] = keys[1:] |
|
171 | kwargs['device_keys'] = keys[1:] | |
167 | kwargs['devices'] = devices#.values(*keys) |
|
172 | kwargs['devices'] = devices#.values(*keys) | |
168 | kwargs['title'] = 'Device' |
|
173 | kwargs['title'] = 'Device' | |
169 | kwargs['suptitle'] = 'List' |
|
174 | kwargs['suptitle'] = 'List' | |
170 | kwargs['button'] = 'New Device' |
|
175 | kwargs['button'] = 'New Device' | |
171 |
|
176 | |||
172 | return render(request, 'devices.html', kwargs) |
|
177 | return render(request, 'devices.html', kwargs) | |
173 |
|
178 | |||
174 |
|
179 | |||
175 | def device(request, id_dev): |
|
180 | def device(request, id_dev): | |
176 |
|
181 | |||
177 | device = get_object_or_404(Device, pk=id_dev) |
|
182 | device = get_object_or_404(Device, pk=id_dev) | |
178 |
|
183 | |||
179 | kwargs = {} |
|
184 | kwargs = {} | |
180 | kwargs['device'] = device |
|
185 | kwargs['device'] = device | |
181 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
186 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] | |
182 |
|
187 | |||
183 | kwargs['title'] = 'Device' |
|
188 | kwargs['title'] = 'Device' | |
184 | kwargs['suptitle'] = 'Details' |
|
189 | kwargs['suptitle'] = 'Details' | |
185 |
|
190 | |||
186 | return render(request, 'device.html', kwargs) |
|
191 | return render(request, 'device.html', kwargs) | |
187 |
|
192 | |||
188 |
|
193 | |||
189 | def device_new(request): |
|
194 | def device_new(request): | |
190 |
|
195 | |||
191 | if request.method == 'GET': |
|
196 | if request.method == 'GET': | |
192 | form = DeviceForm() |
|
197 | form = DeviceForm() | |
193 |
|
198 | |||
194 | if request.method == 'POST': |
|
199 | if request.method == 'POST': | |
195 | form = DeviceForm(request.POST) |
|
200 | form = DeviceForm(request.POST) | |
196 |
|
201 | |||
197 | if form.is_valid(): |
|
202 | if form.is_valid(): | |
198 | form.save() |
|
203 | form.save() | |
199 | return redirect('url_devices') |
|
204 | return redirect('url_devices') | |
200 |
|
205 | |||
201 | kwargs = {} |
|
206 | kwargs = {} | |
202 | kwargs['form'] = form |
|
207 | kwargs['form'] = form | |
203 | kwargs['title'] = 'Device' |
|
208 | kwargs['title'] = 'Device' | |
204 | kwargs['suptitle'] = 'New' |
|
209 | kwargs['suptitle'] = 'New' | |
205 | kwargs['button'] = 'Create' |
|
210 | kwargs['button'] = 'Create' | |
206 |
|
211 | |||
207 | return render(request, 'device_edit.html', kwargs) |
|
212 | return render(request, 'device_edit.html', kwargs) | |
208 |
|
213 | |||
209 |
|
214 | |||
210 | def device_edit(request, id_dev): |
|
215 | def device_edit(request, id_dev): | |
211 |
|
216 | |||
212 | device = get_object_or_404(Device, pk=id_dev) |
|
217 | device = get_object_or_404(Device, pk=id_dev) | |
213 |
|
218 | |||
214 | if request.method=='GET': |
|
219 | if request.method=='GET': | |
215 | form = DeviceForm(instance=device) |
|
220 | form = DeviceForm(instance=device) | |
216 |
|
221 | |||
217 | if request.method=='POST': |
|
222 | if request.method=='POST': | |
218 | form = DeviceForm(request.POST, instance=device) |
|
223 | form = DeviceForm(request.POST, instance=device) | |
219 |
|
224 | |||
220 | if form.is_valid(): |
|
225 | if form.is_valid(): | |
221 | form.save() |
|
226 | form.save() | |
222 | return redirect(device.get_absolute_url()) |
|
227 | return redirect(device.get_absolute_url()) | |
223 |
|
228 | |||
224 | kwargs = {} |
|
229 | kwargs = {} | |
225 | kwargs['form'] = form |
|
230 | kwargs['form'] = form | |
226 | kwargs['title'] = 'Device' |
|
231 | kwargs['title'] = 'Device' | |
227 | kwargs['suptitle'] = 'Edit' |
|
232 | kwargs['suptitle'] = 'Edit' | |
228 | kwargs['button'] = 'Update' |
|
233 | kwargs['button'] = 'Update' | |
229 |
|
234 | |||
230 | return render(request, 'device_edit.html', kwargs) |
|
235 | return render(request, 'device_edit.html', kwargs) | |
231 |
|
236 | |||
232 |
|
237 | |||
233 | def device_delete(request, id_dev): |
|
238 | def device_delete(request, id_dev): | |
234 |
|
239 | |||
235 | device = get_object_or_404(Device, pk=id_dev) |
|
240 | device = get_object_or_404(Device, pk=id_dev) | |
236 |
|
241 | |||
237 | if request.method=='POST': |
|
242 | if request.method=='POST': | |
238 |
|
243 | |||
239 | if request.user.is_staff: |
|
244 | if request.user.is_staff: | |
240 | device.delete() |
|
245 | device.delete() | |
241 | return redirect('url_devices') |
|
246 | return redirect('url_devices') | |
242 |
|
247 | |||
243 | messages.error(request, 'Not enough permission to delete this object') |
|
248 | messages.error(request, 'Not enough permission to delete this object') | |
244 | return redirect(device.get_absolute_url()) |
|
249 | return redirect(device.get_absolute_url()) | |
245 |
|
250 | |||
246 | kwargs = { |
|
251 | kwargs = { | |
247 | 'title': 'Delete', |
|
252 | 'title': 'Delete', | |
248 | 'suptitle': 'Device', |
|
253 | 'suptitle': 'Device', | |
249 | 'object': device, |
|
254 | 'object': device, | |
250 | 'previous': device.get_absolute_url(), |
|
255 | 'previous': device.get_absolute_url(), | |
251 | 'delete': True |
|
256 | 'delete': True | |
252 | } |
|
257 | } | |
253 |
|
258 | |||
254 | return render(request, 'confirm.html', kwargs) |
|
259 | return render(request, 'confirm.html', kwargs) | |
255 |
|
260 | |||
256 |
|
261 | |||
257 | def campaigns(request): |
|
262 | def campaigns(request): | |
258 |
|
263 | |||
259 | campaigns = Campaign.objects.all().order_by('start_date') |
|
264 | campaigns = Campaign.objects.all().order_by('start_date') | |
260 |
|
265 | |||
261 | keys = ['id', 'name', 'start_date', 'end_date'] |
|
266 | keys = ['id', 'name', 'start_date', 'end_date'] | |
262 |
|
267 | |||
263 | kwargs = {} |
|
268 | kwargs = {} | |
264 | kwargs['campaign_keys'] = keys[1:] |
|
269 | kwargs['campaign_keys'] = keys[1:] | |
265 | kwargs['campaigns'] = campaigns#.values(*keys) |
|
270 | kwargs['campaigns'] = campaigns#.values(*keys) | |
266 | kwargs['title'] = 'Campaign' |
|
271 | kwargs['title'] = 'Campaign' | |
267 | kwargs['suptitle'] = 'List' |
|
272 | kwargs['suptitle'] = 'List' | |
268 | kwargs['button'] = 'New Campaign' |
|
273 | kwargs['button'] = 'New Campaign' | |
269 |
|
274 | |||
270 | return render(request, 'campaigns.html', kwargs) |
|
275 | return render(request, 'campaigns.html', kwargs) | |
271 |
|
276 | |||
272 |
|
277 | |||
273 | def campaign(request, id_camp): |
|
278 | def campaign(request, id_camp): | |
274 |
|
279 | |||
275 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
280 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
276 | experiments = Experiment.objects.filter(campaign=campaign) |
|
281 | experiments = Experiment.objects.filter(campaign=campaign) | |
277 |
|
282 | |||
278 | form = CampaignForm(instance=campaign) |
|
283 | form = CampaignForm(instance=campaign) | |
279 |
|
284 | |||
280 | kwargs = {} |
|
285 | kwargs = {} | |
281 | kwargs['campaign'] = campaign |
|
286 | kwargs['campaign'] = campaign | |
282 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] |
|
287 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] | |
283 |
|
288 | |||
284 | kwargs['experiments'] = experiments |
|
289 | kwargs['experiments'] = experiments | |
285 | kwargs['experiment_keys'] = ['name', 'radar', 'start_time', 'end_time'] |
|
290 | kwargs['experiment_keys'] = ['name', 'radar', 'start_time', 'end_time'] | |
286 |
|
291 | |||
287 | kwargs['title'] = 'Campaign' |
|
292 | kwargs['title'] = 'Campaign' | |
288 | kwargs['suptitle'] = 'Details' |
|
293 | kwargs['suptitle'] = 'Details' | |
289 |
|
294 | |||
290 | kwargs['form'] = form |
|
295 | kwargs['form'] = form | |
291 | kwargs['button'] = 'Add Experiment' |
|
296 | kwargs['button'] = 'Add Experiment' | |
292 |
|
297 | |||
293 | return render(request, 'campaign.html', kwargs) |
|
298 | return render(request, 'campaign.html', kwargs) | |
294 |
|
299 | |||
295 |
|
300 | |||
296 | def campaign_new(request): |
|
301 | def campaign_new(request): | |
297 |
|
302 | |||
298 | kwargs = {} |
|
303 | kwargs = {} | |
299 |
|
304 | |||
300 | if request.method == 'GET': |
|
305 | if request.method == 'GET': | |
301 |
|
306 | |||
302 | if 'template' in request.GET: |
|
307 | if 'template' in request.GET: | |
303 | if request.GET['template']=='0': |
|
308 | if request.GET['template']=='0': | |
304 | form = NewForm(initial={'create_from':2}, |
|
309 | form = NewForm(initial={'create_from':2}, | |
305 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
310 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) | |
306 | else: |
|
311 | else: | |
307 | kwargs['button'] = 'Create' |
|
312 | kwargs['button'] = 'Create' | |
308 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
313 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) | |
309 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
314 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] | |
310 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
315 | camp = Campaign.objects.get(pk=request.GET['template']) | |
311 | form = CampaignForm(instance=camp, |
|
316 | form = CampaignForm(instance=camp, | |
312 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), |
|
317 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), | |
313 | 'template':False}) |
|
318 | 'template':False}) | |
314 | elif 'blank' in request.GET: |
|
319 | elif 'blank' in request.GET: | |
315 | kwargs['button'] = 'Create' |
|
320 | kwargs['button'] = 'Create' | |
316 | form = CampaignForm() |
|
321 | form = CampaignForm() | |
317 | else: |
|
322 | else: | |
318 | form = NewForm() |
|
323 | form = NewForm() | |
319 |
|
324 | |||
320 | if request.method == 'POST': |
|
325 | if request.method == 'POST': | |
321 | kwargs['button'] = 'Create' |
|
326 | kwargs['button'] = 'Create' | |
322 | post = request.POST.copy() |
|
327 | post = request.POST.copy() | |
323 | experiments = [] |
|
328 | experiments = [] | |
324 |
|
329 | |||
325 | for id_exp in post.getlist('experiments'): |
|
330 | for id_exp in post.getlist('experiments'): | |
326 | exp = Experiment.objects.get(pk=id_exp) |
|
331 | exp = Experiment.objects.get(pk=id_exp) | |
327 | new_exp = exp.clone(template=False) |
|
332 | new_exp = exp.clone(template=False) | |
328 | experiments.append(new_exp) |
|
333 | experiments.append(new_exp) | |
329 |
|
334 | |||
330 | post.setlist('experiments', []) |
|
335 | post.setlist('experiments', []) | |
331 |
|
336 | |||
332 | form = CampaignForm(post) |
|
337 | form = CampaignForm(post) | |
333 |
|
338 | |||
334 | if form.is_valid(): |
|
339 | if form.is_valid(): | |
335 | campaign = form.save() |
|
340 | campaign = form.save() | |
336 | for exp in experiments: |
|
341 | for exp in experiments: | |
337 | campaign.experiments.add(exp) |
|
342 | campaign.experiments.add(exp) | |
338 | campaign.save() |
|
343 | campaign.save() | |
339 | return redirect('url_campaign', id_camp=campaign.id) |
|
344 | return redirect('url_campaign', id_camp=campaign.id) | |
340 |
|
345 | |||
341 | kwargs['form'] = form |
|
346 | kwargs['form'] = form | |
342 | kwargs['title'] = 'Campaign' |
|
347 | kwargs['title'] = 'Campaign' | |
343 | kwargs['suptitle'] = 'New' |
|
348 | kwargs['suptitle'] = 'New' | |
344 |
|
349 | |||
345 | return render(request, 'campaign_edit.html', kwargs) |
|
350 | return render(request, 'campaign_edit.html', kwargs) | |
346 |
|
351 | |||
347 |
|
352 | |||
348 | def campaign_edit(request, id_camp): |
|
353 | def campaign_edit(request, id_camp): | |
349 |
|
354 | |||
350 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
355 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
351 |
|
356 | |||
352 | if request.method=='GET': |
|
357 | if request.method=='GET': | |
353 | form = CampaignForm(instance=campaign) |
|
358 | form = CampaignForm(instance=campaign) | |
354 |
|
359 | |||
355 | if request.method=='POST': |
|
360 | if request.method=='POST': | |
356 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
361 | exps = campaign.experiments.all().values_list('pk', flat=True) | |
357 | post = request.POST.copy() |
|
362 | post = request.POST.copy() | |
358 | new_exps = post.getlist('experiments') |
|
363 | new_exps = post.getlist('experiments') | |
359 | post.setlist('experiments', []) |
|
364 | post.setlist('experiments', []) | |
360 | form = CampaignForm(post, instance=campaign) |
|
365 | form = CampaignForm(post, instance=campaign) | |
361 |
|
366 | |||
362 | if form.is_valid(): |
|
367 | if form.is_valid(): | |
363 | camp = form.save() |
|
368 | camp = form.save() | |
364 | for id_exp in new_exps: |
|
369 | for id_exp in new_exps: | |
365 | if int(id_exp) in exps: |
|
370 | if int(id_exp) in exps: | |
366 | exps.pop(id_exp) |
|
371 | exps.pop(id_exp) | |
367 | else: |
|
372 | else: | |
368 | exp = Experiment.objects.get(pk=id_exp) |
|
373 | exp = Experiment.objects.get(pk=id_exp) | |
369 | if exp.template: |
|
374 | if exp.template: | |
370 | camp.experiments.add(exp.clone(template=False)) |
|
375 | camp.experiments.add(exp.clone(template=False)) | |
371 | else: |
|
376 | else: | |
372 | camp.experiments.add(exp) |
|
377 | camp.experiments.add(exp) | |
373 |
|
378 | |||
374 | for id_exp in exps: |
|
379 | for id_exp in exps: | |
375 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
380 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) | |
376 |
|
381 | |||
377 | return redirect('url_campaign', id_camp=id_camp) |
|
382 | return redirect('url_campaign', id_camp=id_camp) | |
378 |
|
383 | |||
379 | kwargs = {} |
|
384 | kwargs = {} | |
380 | kwargs['form'] = form |
|
385 | kwargs['form'] = form | |
381 | kwargs['title'] = 'Campaign' |
|
386 | kwargs['title'] = 'Campaign' | |
382 | kwargs['suptitle'] = 'Edit' |
|
387 | kwargs['suptitle'] = 'Edit' | |
383 | kwargs['button'] = 'Update' |
|
388 | kwargs['button'] = 'Update' | |
384 |
|
389 | |||
385 | return render(request, 'campaign_edit.html', kwargs) |
|
390 | return render(request, 'campaign_edit.html', kwargs) | |
386 |
|
391 | |||
387 |
|
392 | |||
388 | def campaign_delete(request, id_camp): |
|
393 | def campaign_delete(request, id_camp): | |
389 |
|
394 | |||
390 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
395 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
391 |
|
396 | |||
392 | if request.method=='POST': |
|
397 | if request.method=='POST': | |
393 | if request.user.is_staff: |
|
398 | if request.user.is_staff: | |
394 |
|
399 | |||
395 | for exp in campaign.experiments.all(): |
|
400 | for exp in campaign.experiments.all(): | |
396 | for conf in Configuration.objects.filter(experiment=exp): |
|
401 | for conf in Configuration.objects.filter(experiment=exp): | |
397 | conf.delete() |
|
402 | conf.delete() | |
398 | exp.delete() |
|
403 | exp.delete() | |
399 | campaign.delete() |
|
404 | campaign.delete() | |
400 |
|
405 | |||
401 | return redirect('url_campaigns') |
|
406 | return redirect('url_campaigns') | |
402 |
|
407 | |||
403 | messages.error(request, 'Not enough permission to delete this object') |
|
408 | messages.error(request, 'Not enough permission to delete this object') | |
404 | return redirect(campaign.get_absolute_url()) |
|
409 | return redirect(campaign.get_absolute_url()) | |
405 |
|
410 | |||
406 | kwargs = { |
|
411 | kwargs = { | |
407 | 'title': 'Delete', |
|
412 | 'title': 'Delete', | |
408 | 'suptitle': 'Campaign', |
|
413 | 'suptitle': 'Campaign', | |
409 | 'object': campaign, |
|
414 | 'object': campaign, | |
410 | 'previous': campaign.get_absolute_url(), |
|
415 | 'previous': campaign.get_absolute_url(), | |
411 | 'delete': True |
|
416 | 'delete': True | |
412 | } |
|
417 | } | |
413 |
|
418 | |||
414 | return render(request, 'confirm.html', kwargs) |
|
419 | return render(request, 'confirm.html', kwargs) | |
415 |
|
420 | |||
416 | def campaign_export(request, id_camp): |
|
421 | def campaign_export(request, id_camp): | |
417 |
|
422 | |||
418 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
423 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
419 | content = campaign.parms_to_dict() |
|
424 | content = campaign.parms_to_dict() | |
420 | content_type = 'application/json' |
|
425 | content_type = 'application/json' | |
421 | filename = '%s_%s.json' %(campaign.name, campaign.id) |
|
426 | filename = '%s_%s.json' %(campaign.name, campaign.id) | |
422 |
|
427 | |||
423 | response = HttpResponse(content_type=content_type) |
|
428 | response = HttpResponse(content_type=content_type) | |
424 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
429 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename | |
425 | response.write(content) |
|
430 | response.write(content) | |
426 |
|
431 | |||
427 | return response |
|
432 | return response | |
428 |
|
433 | |||
429 |
|
434 | |||
430 | def campaign_import(request, id_camp): |
|
435 | def campaign_import(request, id_camp): | |
431 |
|
436 | |||
432 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
437 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
433 |
|
438 | |||
434 | if request.method == 'GET': |
|
439 | if request.method == 'GET': | |
435 | file_form = UploadFileForm() |
|
440 | file_form = UploadFileForm() | |
436 |
|
441 | |||
437 | if request.method == 'POST': |
|
442 | if request.method == 'POST': | |
438 | file_form = UploadFileForm(request.POST, request.FILES) |
|
443 | file_form = UploadFileForm(request.POST, request.FILES) | |
439 |
|
444 | |||
440 | if file_form.is_valid(): |
|
445 | if file_form.is_valid(): | |
441 |
|
446 | |||
442 | parms = campaign.import_from_file(request.FILES['file']) |
|
447 | parms = campaign.import_from_file(request.FILES['file']) | |
443 |
|
448 | |||
444 | if parms: |
|
449 | if parms: | |
445 | parms['name'] = parms['campaign'] |
|
450 | parms['name'] = parms['campaign'] | |
446 |
|
451 | |||
447 | new_camp = campaign.dict_to_parms(parms, CONF_MODELS) |
|
452 | new_camp = campaign.dict_to_parms(parms, CONF_MODELS) | |
448 |
|
453 | |||
449 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
454 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) | |
450 |
|
455 | |||
451 | return redirect(new_camp.get_absolute_url_edit()) |
|
456 | return redirect(new_camp.get_absolute_url_edit()) | |
452 |
|
457 | |||
453 | messages.error(request, "Could not import parameters from file") |
|
458 | messages.error(request, "Could not import parameters from file") | |
454 |
|
459 | |||
455 | kwargs = {} |
|
460 | kwargs = {} | |
456 | kwargs['title'] = 'Campaign' |
|
461 | kwargs['title'] = 'Campaign' | |
457 | kwargs['form'] = file_form |
|
462 | kwargs['form'] = file_form | |
458 | kwargs['suptitle'] = 'Importing file' |
|
463 | kwargs['suptitle'] = 'Importing file' | |
459 | kwargs['button'] = 'Import' |
|
464 | kwargs['button'] = 'Import' | |
460 |
|
465 | |||
461 | return render(request, 'campaign_import.html', kwargs) |
|
466 | return render(request, 'campaign_import.html', kwargs) | |
462 |
|
467 | |||
463 |
|
468 | |||
464 | def experiments(request): |
|
469 | def experiments(request): | |
465 |
|
470 | |||
466 | experiment_list = Experiment.objects.all() |
|
471 | experiment_list = Experiment.objects.all() | |
467 |
|
472 | |||
468 | keys = ['id', 'name', 'start_time', 'end_time'] |
|
473 | keys = ['id', 'name', 'start_time', 'end_time'] | |
469 |
|
474 | |||
470 | kwargs = {} |
|
475 | kwargs = {} | |
471 |
|
476 | |||
472 | kwargs['experiment_keys'] = keys[1:] |
|
477 | kwargs['experiment_keys'] = keys[1:] | |
473 | kwargs['experiments'] = experiment_list |
|
478 | kwargs['experiments'] = experiment_list | |
474 |
|
479 | |||
475 | kwargs['title'] = 'Experiment' |
|
480 | kwargs['title'] = 'Experiment' | |
476 | kwargs['suptitle'] = 'List' |
|
481 | kwargs['suptitle'] = 'List' | |
477 | kwargs['button'] = 'New Experiment' |
|
482 | kwargs['button'] = 'New Experiment' | |
478 |
|
483 | |||
479 | return render(request, 'experiments.html', kwargs) |
|
484 | return render(request, 'experiments.html', kwargs) | |
480 |
|
485 | |||
481 |
|
486 | |||
482 | def experiment(request, id_exp): |
|
487 | def experiment(request, id_exp): | |
483 |
|
488 | |||
484 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
489 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
485 |
|
490 | |||
486 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
491 | configurations = Configuration.objects.filter(experiment=experiment, type=0) | |
487 |
|
492 | |||
488 | kwargs = {} |
|
493 | kwargs = {} | |
489 |
|
494 | |||
490 | kwargs['experiment_keys'] = ['template', 'radar', 'name', 'start_time', 'end_time'] |
|
495 | kwargs['experiment_keys'] = ['template', 'radar', 'name', 'start_time', 'end_time'] | |
491 | kwargs['experiment'] = experiment |
|
496 | kwargs['experiment'] = experiment | |
492 |
|
497 | |||
493 | kwargs['configuration_keys'] = ['name', 'device__device_type', 'device__ip_address', 'device__port_address'] |
|
498 | kwargs['configuration_keys'] = ['name', 'device__device_type', 'device__ip_address', 'device__port_address'] | |
494 | kwargs['configurations'] = configurations |
|
499 | kwargs['configurations'] = configurations | |
495 |
|
500 | |||
496 | kwargs['title'] = 'Experiment' |
|
501 | kwargs['title'] = 'Experiment' | |
497 | kwargs['suptitle'] = 'Details' |
|
502 | kwargs['suptitle'] = 'Details' | |
498 |
|
503 | |||
499 | kwargs['button'] = 'Add Configuration' |
|
504 | kwargs['button'] = 'Add Configuration' | |
500 |
|
505 | |||
501 | ###### SIDEBAR ###### |
|
506 | ###### SIDEBAR ###### | |
502 | kwargs.update(sidebar(experiment=experiment)) |
|
507 | kwargs.update(sidebar(experiment=experiment)) | |
503 |
|
508 | |||
504 | return render(request, 'experiment.html', kwargs) |
|
509 | return render(request, 'experiment.html', kwargs) | |
505 |
|
510 | |||
506 |
|
511 | |||
507 | def experiment_new(request, id_camp=None): |
|
512 | def experiment_new(request, id_camp=None): | |
508 |
|
513 | |||
509 | kwargs = {} |
|
514 | kwargs = {} | |
510 |
|
515 | |||
511 | if request.method == 'GET': |
|
516 | if request.method == 'GET': | |
512 | if 'template' in request.GET: |
|
517 | if 'template' in request.GET: | |
513 | if request.GET['template']=='0': |
|
518 | if request.GET['template']=='0': | |
514 | form = NewForm(initial={'create_from':2}, |
|
519 | form = NewForm(initial={'create_from':2}, | |
515 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
520 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) | |
516 | else: |
|
521 | else: | |
517 | kwargs['button'] = 'Create' |
|
522 | kwargs['button'] = 'Create' | |
518 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
523 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) | |
519 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
524 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] | |
520 | exp=Experiment.objects.get(pk=request.GET['template']) |
|
525 | exp=Experiment.objects.get(pk=request.GET['template']) | |
521 | form = ExperimentForm(instance=exp, |
|
526 | form = ExperimentForm(instance=exp, | |
522 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), |
|
527 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), | |
523 | 'template': False}) |
|
528 | 'template': False}) | |
524 | elif 'blank' in request.GET: |
|
529 | elif 'blank' in request.GET: | |
525 | kwargs['button'] = 'Create' |
|
530 | kwargs['button'] = 'Create' | |
526 | form = ExperimentForm() |
|
531 | form = ExperimentForm() | |
527 | else: |
|
532 | else: | |
528 | form = NewForm() |
|
533 | form = NewForm() | |
529 |
|
534 | |||
530 | if request.method == 'POST': |
|
535 | if request.method == 'POST': | |
531 | form = ExperimentForm(request.POST) |
|
536 | form = ExperimentForm(request.POST) | |
532 | if form.is_valid(): |
|
537 | if form.is_valid(): | |
533 | experiment = form.save() |
|
538 | experiment = form.save() | |
534 |
|
539 | |||
535 | if 'template' in request.GET: |
|
540 | if 'template' in request.GET: | |
536 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
541 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) | |
537 | for conf in configurations: |
|
542 | for conf in configurations: | |
538 | conf.clone(experiment=experiment, template=False) |
|
543 | conf.clone(experiment=experiment, template=False) | |
539 |
|
544 | |||
540 | return redirect('url_experiment', id_exp=experiment.id) |
|
545 | return redirect('url_experiment', id_exp=experiment.id) | |
541 |
|
546 | |||
542 | kwargs['form'] = form |
|
547 | kwargs['form'] = form | |
543 | kwargs['title'] = 'Experiment' |
|
548 | kwargs['title'] = 'Experiment' | |
544 | kwargs['suptitle'] = 'New' |
|
549 | kwargs['suptitle'] = 'New' | |
545 |
|
550 | |||
546 | return render(request, 'experiment_edit.html', kwargs) |
|
551 | return render(request, 'experiment_edit.html', kwargs) | |
547 |
|
552 | |||
548 |
|
553 | |||
549 | def experiment_edit(request, id_exp): |
|
554 | def experiment_edit(request, id_exp): | |
550 |
|
555 | |||
551 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
556 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
552 |
|
557 | |||
553 | if request.method == 'GET': |
|
558 | if request.method == 'GET': | |
554 | form = ExperimentForm(instance=experiment) |
|
559 | form = ExperimentForm(instance=experiment) | |
555 |
|
560 | |||
556 | if request.method=='POST': |
|
561 | if request.method=='POST': | |
557 | form = ExperimentForm(request.POST, instance=experiment) |
|
562 | form = ExperimentForm(request.POST, instance=experiment) | |
558 |
|
563 | |||
559 | if form.is_valid(): |
|
564 | if form.is_valid(): | |
560 | experiment = form.save() |
|
565 | experiment = form.save() | |
561 | return redirect('url_experiment', id_exp=experiment.id) |
|
566 | return redirect('url_experiment', id_exp=experiment.id) | |
562 |
|
567 | |||
563 | kwargs = {} |
|
568 | kwargs = {} | |
564 | kwargs['form'] = form |
|
569 | kwargs['form'] = form | |
565 | kwargs['title'] = 'Experiment' |
|
570 | kwargs['title'] = 'Experiment' | |
566 | kwargs['suptitle'] = 'Edit' |
|
571 | kwargs['suptitle'] = 'Edit' | |
567 | kwargs['button'] = 'Update' |
|
572 | kwargs['button'] = 'Update' | |
568 |
|
573 | |||
569 | return render(request, 'experiment_edit.html', kwargs) |
|
574 | return render(request, 'experiment_edit.html', kwargs) | |
570 |
|
575 | |||
571 |
|
576 | |||
572 | def experiment_delete(request, id_exp): |
|
577 | def experiment_delete(request, id_exp): | |
573 |
|
578 | |||
574 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
579 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
575 |
|
580 | |||
576 | if request.method=='POST': |
|
581 | if request.method=='POST': | |
577 | if request.user.is_staff: |
|
582 | if request.user.is_staff: | |
578 | for conf in Configuration.objects.filter(experiment=experiment): |
|
583 | for conf in Configuration.objects.filter(experiment=experiment): | |
579 | conf.delete() |
|
584 | conf.delete() | |
580 | experiment.delete() |
|
585 | experiment.delete() | |
581 | return redirect('url_experiments') |
|
586 | return redirect('url_experiments') | |
582 |
|
587 | |||
583 | messages.error(request, 'Not enough permission to delete this object') |
|
588 | messages.error(request, 'Not enough permission to delete this object') | |
584 | return redirect(experiment.get_absolute_url()) |
|
589 | return redirect(experiment.get_absolute_url()) | |
585 |
|
590 | |||
586 | kwargs = { |
|
591 | kwargs = { | |
587 | 'title': 'Delete', |
|
592 | 'title': 'Delete', | |
588 | 'suptitle': 'Experiment', |
|
593 | 'suptitle': 'Experiment', | |
589 | 'object': experiment, |
|
594 | 'object': experiment, | |
590 | 'previous': experiment.get_absolute_url(), |
|
595 | 'previous': experiment.get_absolute_url(), | |
591 | 'delete': True |
|
596 | 'delete': True | |
592 | } |
|
597 | } | |
593 |
|
598 | |||
594 | return render(request, 'confirm.html', kwargs) |
|
599 | return render(request, 'confirm.html', kwargs) | |
595 |
|
600 | |||
596 |
|
601 | |||
597 | def experiment_export(request, id_exp): |
|
602 | def experiment_export(request, id_exp): | |
598 |
|
603 | |||
599 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
604 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
600 | content = experiment.parms_to_dict() |
|
605 | content = experiment.parms_to_dict() | |
601 | content_type = 'application/json' |
|
606 | content_type = 'application/json' | |
602 | filename = '%s_%s.json' %(experiment.name, experiment.id) |
|
607 | filename = '%s_%s.json' %(experiment.name, experiment.id) | |
603 |
|
608 | |||
604 | response = HttpResponse(content_type=content_type) |
|
609 | response = HttpResponse(content_type=content_type) | |
605 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
610 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename | |
606 | response.write(content) |
|
611 | response.write(content) | |
607 |
|
612 | |||
608 | return response |
|
613 | return response | |
609 |
|
614 | |||
610 | def experiment_import(request, id_exp): |
|
615 | def experiment_import(request, id_exp): | |
611 |
|
616 | |||
612 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
617 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
613 | configurations = Configuration.objects.filter(experiment=experiment) |
|
618 | configurations = Configuration.objects.filter(experiment=experiment) | |
614 |
|
619 | |||
615 | if request.method == 'GET': |
|
620 | if request.method == 'GET': | |
616 | file_form = UploadFileForm() |
|
621 | file_form = UploadFileForm() | |
617 |
|
622 | |||
618 | if request.method == 'POST': |
|
623 | if request.method == 'POST': | |
619 | file_form = UploadFileForm(request.POST, request.FILES) |
|
624 | file_form = UploadFileForm(request.POST, request.FILES) | |
620 |
|
625 | |||
621 | if file_form.is_valid(): |
|
626 | if file_form.is_valid(): | |
622 |
|
627 | |||
623 | parms = experiment.import_from_file(request.FILES['file']) |
|
628 | parms = experiment.import_from_file(request.FILES['file']) | |
624 |
|
629 | |||
625 | if parms: |
|
630 | if parms: | |
626 |
|
631 | |||
627 | new_exp = experiment.dict_to_parms(parms, CONF_MODELS) |
|
632 | new_exp = experiment.dict_to_parms(parms, CONF_MODELS) | |
628 |
|
633 | |||
629 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
634 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) | |
630 |
|
635 | |||
631 | return redirect(new_exp.get_absolute_url_edit()) |
|
636 | return redirect(new_exp.get_absolute_url_edit()) | |
632 |
|
637 | |||
633 | messages.error(request, "Could not import parameters from file") |
|
638 | messages.error(request, "Could not import parameters from file") | |
634 |
|
639 | |||
635 | kwargs = {} |
|
640 | kwargs = {} | |
636 | kwargs['title'] = 'Experiment' |
|
641 | kwargs['title'] = 'Experiment' | |
637 | kwargs['form'] = file_form |
|
642 | kwargs['form'] = file_form | |
638 | kwargs['suptitle'] = 'Importing file' |
|
643 | kwargs['suptitle'] = 'Importing file' | |
639 | kwargs['button'] = 'Import' |
|
644 | kwargs['button'] = 'Import' | |
640 |
|
645 | |||
641 | kwargs.update(sidebar(experiment=experiment)) |
|
646 | kwargs.update(sidebar(experiment=experiment)) | |
642 |
|
647 | |||
643 | return render(request, 'experiment_import.html', kwargs) |
|
648 | return render(request, 'experiment_import.html', kwargs) | |
644 |
|
649 | |||
645 | def experiment_mix(request, id_exp): |
|
650 | def experiment_mix(request, id_exp): | |
646 |
|
651 | |||
647 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
652 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
648 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, |
|
653 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, | |
649 | mix=False)] |
|
654 | mix=False)] | |
650 |
|
655 | |||
651 | if len(rc_confs)<2: |
|
656 | if len(rc_confs)<2: | |
652 | messages.warning(request, 'You need at least two RC Configurations to make a mix') |
|
657 | messages.warning(request, 'You need at least two RC Configurations to make a mix') | |
653 | return redirect(experiment.get_absolute_url()) |
|
658 | return redirect(experiment.get_absolute_url()) | |
654 |
|
659 | |||
655 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) |
|
660 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) | |
656 |
|
661 | |||
657 | if mix_confs: |
|
662 | if mix_confs: | |
658 | mix = mix_confs[0] |
|
663 | mix = mix_confs[0] | |
659 | else: |
|
664 | else: | |
660 | mix = RCConfiguration(experiment=experiment, |
|
665 | mix = RCConfiguration(experiment=experiment, | |
661 | device=rc_confs[0].device, |
|
666 | device=rc_confs[0].device, | |
662 | ipp=rc_confs[0].ipp, |
|
667 | ipp=rc_confs[0].ipp, | |
663 | clock_in=rc_confs[0].clock_in, |
|
668 | clock_in=rc_confs[0].clock_in, | |
664 | clock_divider=rc_confs[0].clock_divider, |
|
669 | clock_divider=rc_confs[0].clock_divider, | |
665 | mix=True, |
|
670 | mix=True, | |
666 | parameters='') |
|
671 | parameters='') | |
667 | mix.save() |
|
672 | mix.save() | |
668 |
|
673 | |||
669 | line_type = RCLineType.objects.get(name='mix') |
|
674 | line_type = RCLineType.objects.get(name='mix') | |
670 | for i in range(len(rc_confs[0].get_lines())): |
|
675 | for i in range(len(rc_confs[0].get_lines())): | |
671 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
676 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) | |
672 | line.save() |
|
677 | line.save() | |
673 |
|
678 | |||
674 | initial = {'name': mix.name, |
|
679 | initial = {'name': mix.name, | |
675 | 'result': parse_mix_result(mix.parameters), |
|
680 | 'result': parse_mix_result(mix.parameters), | |
676 | 'delay': 0, |
|
681 | 'delay': 0, | |
677 | 'mask': [0,1,2,3,4,5,6,7] |
|
682 | 'mask': [0,1,2,3,4,5,6,7] | |
678 | } |
|
683 | } | |
679 |
|
684 | |||
680 | if request.method=='GET': |
|
685 | if request.method=='GET': | |
681 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
686 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) | |
682 |
|
687 | |||
683 |
if request.method=='POST': |
|
688 | if request.method=='POST': | |
684 |
|
||||
685 | result = mix.parameters |
|
689 | result = mix.parameters | |
686 |
|
690 | |||
687 | if '{}|'.format(request.POST['experiment']) in result: |
|
691 | if '{}|'.format(request.POST['experiment']) in result: | |
688 | messages.error(request, 'Configuration already added') |
|
692 | messages.error(request, 'Configuration already added') | |
689 | else: |
|
693 | else: | |
|
694 | if 'operation' in request.POST: | |||
|
695 | operation = MIX_OPERATIONS[request.POST['operation']] | |||
|
696 | else: | |||
|
697 | operation = '---' | |||
|
698 | ||||
|
699 | mode = MIX_MODES[request.POST['mode']] | |||
|
700 | ||||
690 | if result: |
|
701 | if result: | |
691 | result = '{}-{}|{}|{}|{}'.format(mix.parameters, |
|
702 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, | |
692 |
|
|
703 | request.POST['experiment'], | |
693 |
|
|
704 | mode, | |
694 |
|
|
705 | operation, | |
695 |
|
|
706 | float(request.POST['delay']), | |
696 |
|
|
707 | parse_mask(request.POST.getlist('mask')) | |
|
708 | ) | |||
697 | else: |
|
709 | else: | |
698 | result = '{}|{}|{}|{}'.format(request.POST['experiment'], |
|
710 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], | |
699 |
|
|
711 | mode, | |
700 |
|
|
712 | operation, | |
701 |
|
|
713 | float(request.POST['delay']), | |
702 |
|
|
714 | parse_mask(request.POST.getlist('mask')) | |
|
715 | ) | |||
703 |
|
716 | |||
704 | mix.parameters = result |
|
717 | mix.parameters = result | |
705 | mix.name = request.POST['name'] |
|
718 | mix.name = request.POST['name'] | |
706 | mix.save() |
|
719 | mix.save() | |
707 | mix.update_pulses() |
|
720 | mix.update_pulses() | |
708 |
|
721 | |||
709 |
|
||||
710 | initial['result'] = parse_mix_result(result) |
|
722 | initial['result'] = parse_mix_result(result) | |
711 | initial['name'] = mix.name |
|
723 | initial['name'] = mix.name | |
712 |
|
724 | |||
713 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
725 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) | |
714 |
|
726 | |||
715 |
|
727 | |||
716 | kwargs = { |
|
728 | kwargs = { | |
717 | 'title': 'Experiment', |
|
729 | 'title': 'Experiment', | |
718 | 'suptitle': 'Mix Configurations', |
|
730 | 'suptitle': 'Mix Configurations', | |
719 | 'form' : form, |
|
731 | 'form' : form, | |
720 | 'extra_button': 'Delete', |
|
732 | 'extra_button': 'Delete', | |
721 | 'button': 'Add', |
|
733 | 'button': 'Add', | |
722 | 'cancel': 'Back', |
|
734 | 'cancel': 'Back', | |
723 | 'previous': experiment.get_absolute_url(), |
|
735 | 'previous': experiment.get_absolute_url(), | |
724 | 'id_exp':id_exp, |
|
736 | 'id_exp':id_exp, | |
725 |
|
737 | |||
726 | } |
|
738 | } | |
727 |
|
739 | |||
728 | return render(request, 'experiment_mix.html', kwargs) |
|
740 | return render(request, 'experiment_mix.html', kwargs) | |
729 |
|
741 | |||
730 |
|
742 | |||
731 | def experiment_mix_delete(request, id_exp): |
|
743 | def experiment_mix_delete(request, id_exp): | |
732 |
|
744 | |||
733 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) |
|
745 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) | |
734 | values = conf.parameters.split('-') |
|
746 | values = conf.parameters.split('-') | |
735 | conf.parameters = '-'.join(values[:-1]) |
|
747 | conf.parameters = '-'.join(values[:-1]) | |
736 | conf.save() |
|
748 | conf.save() | |
737 |
|
749 | |||
738 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
750 | return redirect('url_mix_experiment', id_exp=id_exp) | |
739 |
|
751 | |||
740 |
|
752 | |||
741 | def parse_mix_result(s): |
|
753 | def parse_mix_result(s): | |
742 |
|
754 | |||
743 | values = s.split('-') |
|
755 | values = s.split('-') | |
744 | html = '' |
|
756 | html = 'EXP MOD OPE DELAY MASK\r\n' | |
745 |
|
757 | |||
|
758 | if not values or values[0] in ('', ' '): | |||
|
759 | return mark_safe(html) | |||
746 |
|
760 | |||
747 | for i, value in enumerate(values): |
|
761 | for i, value in enumerate(values): | |
748 | if not value: |
|
762 | if not value: | |
749 | continue |
|
763 | continue | |
750 | pk, mode, delay, mask = value.split('|') |
|
764 | pk, mode, operation, delay, mask = value.split('|') | |
751 | conf = RCConfiguration.objects.get(pk=pk) |
|
765 | conf = RCConfiguration.objects.get(pk=pk) | |
752 | if i==0: |
|
766 | if i==0: | |
753 | html += '{:20.18}{:4}{:9}km{:>6}\r\n'.format( |
|
767 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( | |
754 |
conf.name |
|
768 | conf.name, | |
|
769 | mode, | |||
755 | '---', |
|
770 | '---', | |
756 | delay, |
|
771 | delay, | |
757 | mask) |
|
772 | mask) | |
758 | else: |
|
773 | else: | |
759 | html += '{:20.18}{:4}{:9}km{:>6}\r\n'.format( |
|
774 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( | |
760 |
conf.name |
|
775 | conf.name, | |
761 | mode, |
|
776 | mode, | |
|
777 | operation, | |||
762 | delay, |
|
778 | delay, | |
763 | mask) |
|
779 | mask) | |
764 |
|
780 | |||
765 | return mark_safe(html) |
|
781 | return mark_safe(html) | |
766 |
|
782 | |||
767 | def parse_mask(l): |
|
783 | def parse_mask(l): | |
768 |
|
784 | |||
769 | values = [] |
|
785 | values = [] | |
770 |
|
786 | |||
771 | for x in range(8): |
|
787 | for x in range(8): | |
772 | if '{}'.format(x) in l: |
|
788 | if '{}'.format(x) in l: | |
773 | values.append(1) |
|
789 | values.append(1) | |
774 | else: |
|
790 | else: | |
775 | values.append(0) |
|
791 | values.append(0) | |
776 |
|
792 | |||
777 | values.reverse() |
|
793 | values.reverse() | |
778 |
|
794 | |||
779 | return int(''.join([str(x) for x in values]), 2) |
|
795 | return int(''.join([str(x) for x in values]), 2) | |
780 |
|
796 | |||
781 |
|
797 | |||
782 | def dev_confs(request): |
|
798 | def dev_confs(request): | |
783 |
|
799 | |||
784 | configurations = Configuration.objects.all().order_by('type', 'device__device_type', 'experiment') |
|
800 | configurations = Configuration.objects.all().order_by('type', 'device__device_type', 'experiment') | |
785 |
|
801 | |||
786 | kwargs = {} |
|
802 | kwargs = {} | |
787 |
|
803 | |||
788 | kwargs['configuration_keys'] = ['device', 'name', 'experiment', 'type', 'programmed_date'] |
|
804 | kwargs['configuration_keys'] = ['device', 'name', 'experiment', 'type', 'programmed_date'] | |
789 | kwargs['configurations'] = configurations |
|
805 | kwargs['configurations'] = configurations | |
790 |
|
806 | |||
791 | kwargs['title'] = 'Configuration' |
|
807 | kwargs['title'] = 'Configuration' | |
792 | kwargs['suptitle'] = 'List' |
|
808 | kwargs['suptitle'] = 'List' | |
793 |
|
809 | |||
794 | return render(request, 'dev_confs.html', kwargs) |
|
810 | return render(request, 'dev_confs.html', kwargs) | |
795 |
|
811 | |||
796 |
|
812 | |||
797 | def dev_conf(request, id_conf): |
|
813 | def dev_conf(request, id_conf): | |
798 |
|
814 | |||
799 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
815 | conf = get_object_or_404(Configuration, pk=id_conf) | |
800 |
|
816 | |||
801 | return redirect(conf.get_absolute_url()) |
|
817 | return redirect(conf.get_absolute_url()) | |
802 |
|
818 | |||
803 |
|
819 | |||
804 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
820 | def dev_conf_new(request, id_exp=0, id_dev=0): | |
805 |
|
821 | |||
806 | initial = {} |
|
822 | initial = {} | |
807 | kwargs = {} |
|
823 | kwargs = {} | |
808 |
|
824 | |||
809 | if id_exp<>0: |
|
825 | if id_exp<>0: | |
810 | initial['experiment'] = id_exp |
|
826 | initial['experiment'] = id_exp | |
811 |
|
827 | |||
812 | if id_dev<>0: |
|
828 | if id_dev<>0: | |
813 | initial['device'] = id_dev |
|
829 | initial['device'] = id_dev | |
814 |
|
830 | |||
815 | if request.method == 'GET': |
|
831 | if request.method == 'GET': | |
816 |
|
832 | |||
817 | if id_dev: |
|
833 | if id_dev: | |
818 | kwargs['button'] = 'Create' |
|
834 | kwargs['button'] = 'Create' | |
819 | device = Device.objects.get(pk=id_dev) |
|
835 | device = Device.objects.get(pk=id_dev) | |
820 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
836 | DevConfForm = CONF_FORMS[device.device_type.name] | |
821 | initial['name'] = request.GET['name'] |
|
837 | initial['name'] = request.GET['name'] | |
822 | form = DevConfForm(initial=initial) |
|
838 | form = DevConfForm(initial=initial) | |
823 | else: |
|
839 | else: | |
824 | if 'template' in request.GET: |
|
840 | if 'template' in request.GET: | |
825 | if request.GET['template']=='0': |
|
841 | if request.GET['template']=='0': | |
826 | form = NewForm(initial={'create_from':2}, |
|
842 | form = NewForm(initial={'create_from':2}, | |
827 | template_choices=Configuration.objects.filter(template=True).values_list('id', 'name')) |
|
843 | template_choices=Configuration.objects.filter(template=True).values_list('id', 'name')) | |
828 | else: |
|
844 | else: | |
829 | kwargs['button'] = 'Create' |
|
845 | kwargs['button'] = 'Create' | |
830 | conf = Configuration.objects.get(pk=request.GET['template']) |
|
846 | conf = Configuration.objects.get(pk=request.GET['template']) | |
831 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
847 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
832 | form = DevConfForm(instance=conf, |
|
848 | form = DevConfForm(instance=conf, | |
833 | initial={'name': '{} [{:%Y/%m/%d}]'.format(conf.name, datetime.now()), |
|
849 | initial={'name': '{} [{:%Y/%m/%d}]'.format(conf.name, datetime.now()), | |
834 | 'template': False}) |
|
850 | 'template': False}) | |
835 | elif 'blank' in request.GET: |
|
851 | elif 'blank' in request.GET: | |
836 | kwargs['button'] = 'Create' |
|
852 | kwargs['button'] = 'Create' | |
837 | form = ConfigurationForm(initial=initial) |
|
853 | form = ConfigurationForm(initial=initial) | |
838 | else: |
|
854 | else: | |
839 | form = NewForm() |
|
855 | form = NewForm() | |
840 |
|
856 | |||
841 | if request.method == 'POST': |
|
857 | if request.method == 'POST': | |
842 |
|
858 | |||
843 | device = Device.objects.get(pk=request.POST['device']) |
|
859 | device = Device.objects.get(pk=request.POST['device']) | |
844 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
860 | DevConfForm = CONF_FORMS[device.device_type.name] | |
845 |
|
861 | |||
846 | form = DevConfForm(request.POST) |
|
862 | form = DevConfForm(request.POST) | |
847 |
|
863 | |||
848 | if form.is_valid(): |
|
864 | if form.is_valid(): | |
849 | conf = form.save() |
|
865 | conf = form.save() | |
850 |
|
866 | |||
851 | if 'template' in request.GET and conf.device.device_type.name=='rc': |
|
867 | if 'template' in request.GET and conf.device.device_type.name=='rc': | |
852 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) |
|
868 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) | |
853 | for line in lines: |
|
869 | for line in lines: | |
854 | line.clone(rc_configuration=conf) |
|
870 | line.clone(rc_configuration=conf) | |
855 |
|
871 | |||
856 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
872 | return redirect('url_dev_conf', id_conf=conf.pk) | |
857 |
|
873 | |||
858 |
|
874 | |||
859 | kwargs['id_exp'] = id_exp |
|
875 | kwargs['id_exp'] = id_exp | |
860 | kwargs['form'] = form |
|
876 | kwargs['form'] = form | |
861 | kwargs['title'] = 'Configuration' |
|
877 | kwargs['title'] = 'Configuration' | |
862 | kwargs['suptitle'] = 'New' |
|
878 | kwargs['suptitle'] = 'New' | |
863 |
|
879 | |||
864 |
|
880 | |||
865 | if id_dev != 0: |
|
881 | if id_dev != 0: | |
866 | device = Device.objects.get(pk=id_dev) |
|
882 | device = Device.objects.get(pk=id_dev) | |
867 | if 'dds' in device.device_type.name: |
|
883 | if 'dds' in device.device_type.name: | |
868 | kwargs['dds_device'] = True |
|
884 | kwargs['dds_device'] = True | |
869 |
|
885 | |||
870 | return render(request, 'dev_conf_edit.html', kwargs) |
|
886 | return render(request, 'dev_conf_edit.html', kwargs) | |
871 |
|
887 | |||
872 |
|
888 | |||
873 | def dev_conf_edit(request, id_conf): |
|
889 | def dev_conf_edit(request, id_conf): | |
874 |
|
890 | |||
875 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
891 | conf = get_object_or_404(Configuration, pk=id_conf) | |
876 |
|
892 | |||
877 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
893 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
878 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
894 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
879 |
|
895 | |||
880 | dev_conf = DevConfModel.objects.get(pk=id_conf) |
|
896 | dev_conf = DevConfModel.objects.get(pk=id_conf) | |
881 |
|
897 | |||
882 | if request.method=='GET': |
|
898 | if request.method=='GET': | |
883 | form = DevConfForm(instance=dev_conf) |
|
899 | form = DevConfForm(instance=dev_conf) | |
884 |
|
900 | |||
885 | if request.method=='POST': |
|
901 | if request.method=='POST': | |
886 | form = DevConfForm(request.POST, instance=dev_conf) |
|
902 | form = DevConfForm(request.POST, instance=dev_conf) | |
887 |
|
903 | |||
888 | if form.is_valid(): |
|
904 | if form.is_valid(): | |
889 | form.save() |
|
905 | form.save() | |
890 | return redirect('url_dev_conf', id_conf=id_conf) |
|
906 | return redirect('url_dev_conf', id_conf=id_conf) | |
891 |
|
907 | |||
892 | kwargs = {} |
|
908 | kwargs = {} | |
893 | kwargs['form'] = form |
|
909 | kwargs['form'] = form | |
894 | kwargs['title'] = 'Device Configuration' |
|
910 | kwargs['title'] = 'Device Configuration' | |
895 | kwargs['suptitle'] = 'Edit' |
|
911 | kwargs['suptitle'] = 'Edit' | |
896 | kwargs['button'] = 'Update' |
|
912 | kwargs['button'] = 'Update' | |
897 |
|
913 | |||
898 | ###### SIDEBAR ###### |
|
914 | ###### SIDEBAR ###### | |
899 | kwargs.update(sidebar(conf=conf)) |
|
915 | kwargs.update(sidebar(conf=conf)) | |
900 |
|
916 | |||
901 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
917 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
902 |
|
918 | |||
903 |
|
919 | |||
904 | def dev_conf_start(request, id_conf): |
|
920 | def dev_conf_start(request, id_conf): | |
905 |
|
921 | |||
906 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
922 | conf = get_object_or_404(Configuration, pk=id_conf) | |
907 |
|
923 | |||
908 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
924 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
909 |
|
925 | |||
910 | conf = DevConfModel.objects.get(pk=id_conf) |
|
926 | conf = DevConfModel.objects.get(pk=id_conf) | |
911 |
|
927 | |||
912 | if conf.start_device(): |
|
928 | if conf.start_device(): | |
913 | messages.success(request, conf.message) |
|
929 | messages.success(request, conf.message) | |
914 | else: |
|
930 | else: | |
915 | messages.error(request, conf.message) |
|
931 | messages.error(request, conf.message) | |
916 |
|
932 | |||
917 | conf.status_device() |
|
933 | conf.status_device() | |
918 |
|
934 | |||
919 | return redirect(conf.get_absolute_url()) |
|
935 | return redirect(conf.get_absolute_url()) | |
920 |
|
936 | |||
921 |
|
937 | |||
922 | def dev_conf_stop(request, id_conf): |
|
938 | def dev_conf_stop(request, id_conf): | |
923 |
|
939 | |||
924 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
940 | conf = get_object_or_404(Configuration, pk=id_conf) | |
925 |
|
941 | |||
926 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
942 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
927 |
|
943 | |||
928 | conf = DevConfModel.objects.get(pk=id_conf) |
|
944 | conf = DevConfModel.objects.get(pk=id_conf) | |
929 |
|
945 | |||
930 | if conf.stop_device(): |
|
946 | if conf.stop_device(): | |
931 | messages.success(request, conf.message) |
|
947 | messages.success(request, conf.message) | |
932 | else: |
|
948 | else: | |
933 | messages.error(request, conf.message) |
|
949 | messages.error(request, conf.message) | |
934 |
|
950 | |||
935 | conf.status_device() |
|
951 | conf.status_device() | |
936 |
|
952 | |||
937 | return redirect(conf.get_absolute_url()) |
|
953 | return redirect(conf.get_absolute_url()) | |
938 |
|
954 | |||
939 |
|
955 | |||
940 | def dev_conf_status(request, id_conf): |
|
956 | def dev_conf_status(request, id_conf): | |
941 |
|
957 | |||
942 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
958 | conf = get_object_or_404(Configuration, pk=id_conf) | |
943 |
|
959 | |||
944 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
960 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
945 |
|
961 | |||
946 | conf = DevConfModel.objects.get(pk=id_conf) |
|
962 | conf = DevConfModel.objects.get(pk=id_conf) | |
947 |
|
963 | |||
948 | if conf.status_device(): |
|
964 | if conf.status_device(): | |
949 | messages.success(request, conf.message) |
|
965 | messages.success(request, conf.message) | |
950 | else: |
|
966 | else: | |
951 | messages.error(request, conf.message) |
|
967 | messages.error(request, conf.message) | |
952 |
|
968 | |||
953 | return redirect(conf.get_absolute_url()) |
|
969 | return redirect(conf.get_absolute_url()) | |
954 |
|
970 | |||
955 |
|
971 | |||
956 | def dev_conf_write(request, id_conf): |
|
972 | def dev_conf_write(request, id_conf): | |
957 |
|
973 | |||
958 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
974 | conf = get_object_or_404(Configuration, pk=id_conf) | |
959 |
|
975 | |||
960 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
976 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
961 |
|
977 | |||
962 | conf = DevConfModel.objects.get(pk=id_conf) |
|
978 | conf = DevConfModel.objects.get(pk=id_conf) | |
963 |
|
979 | |||
964 | answer = conf.write_device() |
|
980 | answer = conf.write_device() | |
965 | conf.status_device() |
|
981 | conf.status_device() | |
966 |
|
982 | |||
967 | if answer: |
|
983 | if answer: | |
968 | messages.success(request, conf.message) |
|
984 | messages.success(request, conf.message) | |
969 |
|
985 | |||
970 | #Creating a historical configuration |
|
986 | #Creating a historical configuration | |
971 | conf.clone(type=0, template=False) |
|
987 | conf.clone(type=0, template=False) | |
972 |
|
988 | |||
973 | #Original configuration |
|
989 | #Original configuration | |
974 | conf = DevConfModel.objects.get(pk=id_conf) |
|
990 | conf = DevConfModel.objects.get(pk=id_conf) | |
975 | else: |
|
991 | else: | |
976 | messages.error(request, conf.message) |
|
992 | messages.error(request, conf.message) | |
977 |
|
993 | |||
978 | return redirect(conf.get_absolute_url()) |
|
994 | return redirect(conf.get_absolute_url()) | |
979 |
|
995 | |||
980 |
|
996 | |||
981 | def dev_conf_read(request, id_conf): |
|
997 | def dev_conf_read(request, id_conf): | |
982 |
|
998 | |||
983 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
999 | conf = get_object_or_404(Configuration, pk=id_conf) | |
984 |
|
1000 | |||
985 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
1001 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
986 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1002 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
987 |
|
1003 | |||
988 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1004 | conf = DevConfModel.objects.get(pk=id_conf) | |
989 |
|
1005 | |||
990 | if request.method=='GET': |
|
1006 | if request.method=='GET': | |
991 |
|
1007 | |||
992 | parms = conf.read_device() |
|
1008 | parms = conf.read_device() | |
993 | conf.status_device() |
|
1009 | conf.status_device() | |
994 |
|
1010 | |||
995 | if not parms: |
|
1011 | if not parms: | |
996 | messages.error(request, conf.message) |
|
1012 | messages.error(request, conf.message) | |
997 | return redirect(conf.get_absolute_url()) |
|
1013 | return redirect(conf.get_absolute_url()) | |
998 |
|
1014 | |||
999 | form = DevConfForm(initial=parms, instance=conf) |
|
1015 | form = DevConfForm(initial=parms, instance=conf) | |
1000 |
|
1016 | |||
1001 | if request.method=='POST': |
|
1017 | if request.method=='POST': | |
1002 | form = DevConfForm(request.POST, instance=conf) |
|
1018 | form = DevConfForm(request.POST, instance=conf) | |
1003 |
|
1019 | |||
1004 | if form.is_valid(): |
|
1020 | if form.is_valid(): | |
1005 | form.save() |
|
1021 | form.save() | |
1006 | return redirect(conf.get_absolute_url()) |
|
1022 | return redirect(conf.get_absolute_url()) | |
1007 |
|
1023 | |||
1008 | messages.error(request, "Parameters could not be saved") |
|
1024 | messages.error(request, "Parameters could not be saved") | |
1009 |
|
1025 | |||
1010 | kwargs = {} |
|
1026 | kwargs = {} | |
1011 | kwargs['id_dev'] = conf.id |
|
1027 | kwargs['id_dev'] = conf.id | |
1012 | kwargs['form'] = form |
|
1028 | kwargs['form'] = form | |
1013 | kwargs['title'] = 'Device Configuration' |
|
1029 | kwargs['title'] = 'Device Configuration' | |
1014 | kwargs['suptitle'] = 'Parameters read from device' |
|
1030 | kwargs['suptitle'] = 'Parameters read from device' | |
1015 | kwargs['button'] = 'Save' |
|
1031 | kwargs['button'] = 'Save' | |
1016 |
|
1032 | |||
1017 | ###### SIDEBAR ###### |
|
1033 | ###### SIDEBAR ###### | |
1018 | kwargs.update(sidebar(conf=conf)) |
|
1034 | kwargs.update(sidebar(conf=conf)) | |
1019 |
|
1035 | |||
1020 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
1036 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) | |
1021 |
|
1037 | |||
1022 |
|
1038 | |||
1023 | def dev_conf_import(request, id_conf): |
|
1039 | def dev_conf_import(request, id_conf): | |
1024 |
|
1040 | |||
1025 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1041 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1026 |
|
1042 | |||
1027 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
1043 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
1028 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1044 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1029 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1045 | conf = DevConfModel.objects.get(pk=id_conf) | |
1030 |
|
1046 | |||
1031 | if request.method == 'GET': |
|
1047 | if request.method == 'GET': | |
1032 | file_form = UploadFileForm() |
|
1048 | file_form = UploadFileForm() | |
1033 |
|
1049 | |||
1034 | if request.method == 'POST': |
|
1050 | if request.method == 'POST': | |
1035 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1051 | file_form = UploadFileForm(request.POST, request.FILES) | |
1036 |
|
1052 | |||
1037 | if file_form.is_valid(): |
|
1053 | if file_form.is_valid(): | |
1038 |
|
1054 | |||
1039 | parms = conf.import_from_file(request.FILES['file']) |
|
1055 | parms = conf.import_from_file(request.FILES['file']) | |
1040 |
|
1056 | |||
1041 | if parms: |
|
1057 | if parms: | |
1042 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
1058 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) | |
1043 | form = DevConfForm(initial=parms, instance=conf) |
|
1059 | form = DevConfForm(initial=parms, instance=conf) | |
1044 |
|
1060 | |||
1045 | kwargs = {} |
|
1061 | kwargs = {} | |
1046 | kwargs['id_dev'] = conf.id |
|
1062 | kwargs['id_dev'] = conf.id | |
1047 | kwargs['form'] = form |
|
1063 | kwargs['form'] = form | |
1048 | kwargs['title'] = 'Device Configuration' |
|
1064 | kwargs['title'] = 'Device Configuration' | |
1049 | kwargs['suptitle'] = 'Parameters imported' |
|
1065 | kwargs['suptitle'] = 'Parameters imported' | |
1050 | kwargs['button'] = 'Save' |
|
1066 | kwargs['button'] = 'Save' | |
1051 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1067 | kwargs['action'] = conf.get_absolute_url_edit() | |
1052 | kwargs['previous'] = conf.get_absolute_url() |
|
1068 | kwargs['previous'] = conf.get_absolute_url() | |
1053 |
|
1069 | |||
1054 | ###### SIDEBAR ###### |
|
1070 | ###### SIDEBAR ###### | |
1055 | kwargs.update(sidebar(conf=conf)) |
|
1071 | kwargs.update(sidebar(conf=conf)) | |
1056 |
|
1072 | |||
1057 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1073 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
1058 |
|
1074 | |||
1059 | messages.error(request, "Could not import parameters from file") |
|
1075 | messages.error(request, "Could not import parameters from file") | |
1060 |
|
1076 | |||
1061 | kwargs = {} |
|
1077 | kwargs = {} | |
1062 | kwargs['id_dev'] = conf.id |
|
1078 | kwargs['id_dev'] = conf.id | |
1063 | kwargs['title'] = 'Device Configuration' |
|
1079 | kwargs['title'] = 'Device Configuration' | |
1064 | kwargs['form'] = file_form |
|
1080 | kwargs['form'] = file_form | |
1065 | kwargs['suptitle'] = 'Importing file' |
|
1081 | kwargs['suptitle'] = 'Importing file' | |
1066 | kwargs['button'] = 'Import' |
|
1082 | kwargs['button'] = 'Import' | |
1067 |
|
1083 | |||
1068 | kwargs.update(sidebar(conf=conf)) |
|
1084 | kwargs.update(sidebar(conf=conf)) | |
1069 |
|
1085 | |||
1070 | return render(request, 'dev_conf_import.html', kwargs) |
|
1086 | return render(request, 'dev_conf_import.html', kwargs) | |
1071 |
|
1087 | |||
1072 |
|
1088 | |||
1073 | def dev_conf_export(request, id_conf): |
|
1089 | def dev_conf_export(request, id_conf): | |
1074 |
|
1090 | |||
1075 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1091 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1076 |
|
1092 | |||
1077 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
1093 | DevConfModel = CONF_MODELS[conf.device.device_type.name] | |
1078 |
|
1094 | |||
1079 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1095 | conf = DevConfModel.objects.get(pk=id_conf) | |
1080 |
|
1096 | |||
1081 | if request.method == 'GET': |
|
1097 | if request.method == 'GET': | |
1082 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1098 | file_form = DownloadFileForm(conf.device.device_type.name) | |
1083 |
|
1099 | |||
1084 | if request.method == 'POST': |
|
1100 | if request.method == 'POST': | |
1085 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
1101 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) | |
1086 |
|
1102 | |||
1087 | if file_form.is_valid(): |
|
1103 | if file_form.is_valid(): | |
1088 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
1104 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) | |
1089 |
|
1105 | |||
1090 | response = HttpResponse(content_type=fields['content_type']) |
|
1106 | response = HttpResponse(content_type=fields['content_type']) | |
1091 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
1107 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] | |
1092 | response.write(fields['content']) |
|
1108 | response.write(fields['content']) | |
1093 |
|
1109 | |||
1094 | return response |
|
1110 | return response | |
1095 |
|
1111 | |||
1096 | messages.error(request, "Could not export parameters") |
|
1112 | messages.error(request, "Could not export parameters") | |
1097 |
|
1113 | |||
1098 | kwargs = {} |
|
1114 | kwargs = {} | |
1099 | kwargs['id_dev'] = conf.id |
|
1115 | kwargs['id_dev'] = conf.id | |
1100 | kwargs['title'] = 'Device Configuration' |
|
1116 | kwargs['title'] = 'Device Configuration' | |
1101 | kwargs['form'] = file_form |
|
1117 | kwargs['form'] = file_form | |
1102 | kwargs['suptitle'] = 'Exporting file' |
|
1118 | kwargs['suptitle'] = 'Exporting file' | |
1103 | kwargs['button'] = 'Export' |
|
1119 | kwargs['button'] = 'Export' | |
1104 |
|
1120 | |||
1105 | return render(request, 'dev_conf_export.html', kwargs) |
|
1121 | return render(request, 'dev_conf_export.html', kwargs) | |
1106 |
|
1122 | |||
1107 |
|
1123 | |||
1108 | def dev_conf_delete(request, id_conf): |
|
1124 | def dev_conf_delete(request, id_conf): | |
1109 |
|
1125 | |||
1110 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1126 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1111 |
|
1127 | |||
1112 | if request.method=='POST': |
|
1128 | if request.method=='POST': | |
1113 | if request.user.is_staff: |
|
1129 | if request.user.is_staff: | |
1114 | conf.delete() |
|
1130 | conf.delete() | |
1115 | return redirect('url_dev_confs') |
|
1131 | return redirect('url_dev_confs') | |
1116 |
|
1132 | |||
1117 | messages.error(request, 'Not enough permission to delete this object') |
|
1133 | messages.error(request, 'Not enough permission to delete this object') | |
1118 | return redirect(conf.get_absolute_url()) |
|
1134 | return redirect(conf.get_absolute_url()) | |
1119 |
|
1135 | |||
1120 | kwargs = { |
|
1136 | kwargs = { | |
1121 | 'title': 'Delete', |
|
1137 | 'title': 'Delete', | |
1122 | 'suptitle': 'Experiment', |
|
1138 | 'suptitle': 'Experiment', | |
1123 | 'object': conf, |
|
1139 | 'object': conf, | |
1124 | 'previous': conf.get_absolute_url(), |
|
1140 | 'previous': conf.get_absolute_url(), | |
1125 | 'delete': True |
|
1141 | 'delete': True | |
1126 | } |
|
1142 | } | |
1127 |
|
1143 | |||
1128 | return render(request, 'confirm.html', kwargs) |
|
1144 | return render(request, 'confirm.html', kwargs) | |
1129 |
|
1145 | |||
1130 |
|
1146 | |||
1131 | def sidebar(**kwargs): |
|
1147 | def sidebar(**kwargs): | |
1132 |
|
1148 | |||
1133 | side_data = {} |
|
1149 | side_data = {} | |
1134 |
|
1150 | |||
1135 | conf = kwargs.get('conf', None) |
|
1151 | conf = kwargs.get('conf', None) | |
1136 | experiment = kwargs.get('experiment', None) |
|
1152 | experiment = kwargs.get('experiment', None) | |
1137 |
|
1153 | |||
1138 | if not experiment: |
|
1154 | if not experiment: | |
1139 | experiment = conf.experiment |
|
1155 | experiment = conf.experiment | |
1140 |
|
1156 | |||
1141 | if experiment: |
|
1157 | if experiment: | |
1142 | side_data['experiment'] = experiment |
|
1158 | side_data['experiment'] = experiment | |
1143 | campaign = experiment.campaign_set.all() |
|
1159 | campaign = experiment.campaign_set.all() | |
1144 | if campaign: |
|
1160 | if campaign: | |
1145 | side_data['campaign'] = campaign[0] |
|
1161 | side_data['campaign'] = campaign[0] | |
1146 | experiments = campaign[0].experiments.all() |
|
1162 | experiments = campaign[0].experiments.all() | |
1147 | else: |
|
1163 | else: | |
1148 | experiments = [experiment] |
|
1164 | experiments = [experiment] | |
1149 | configurations = experiment.configuration_set.filter(type=0) |
|
1165 | configurations = experiment.configuration_set.filter(type=0) | |
1150 | side_data['side_experiments'] = experiments |
|
1166 | side_data['side_experiments'] = experiments | |
1151 | side_data['side_configurations'] = configurations |
|
1167 | side_data['side_configurations'] = configurations | |
1152 |
|
1168 | |||
1153 | return side_data |
|
1169 | return side_data | |
1154 |
|
1170 | |||
1155 |
|
1171 | |||
1156 | def operation(request, id_camp=None): |
|
1172 | def operation(request, id_camp=None): | |
1157 |
|
1173 | |||
1158 | if not id_camp: |
|
1174 | if not id_camp: | |
1159 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
1175 | campaigns = Campaign.objects.all().order_by('-start_date') | |
1160 |
|
1176 | |||
1161 | if not campaigns: |
|
1177 | if not campaigns: | |
1162 | kwargs = {} |
|
1178 | kwargs = {} | |
1163 | kwargs['title'] = 'No Campaigns' |
|
1179 | kwargs['title'] = 'No Campaigns' | |
1164 | kwargs['suptitle'] = 'Empty' |
|
1180 | kwargs['suptitle'] = 'Empty' | |
1165 | return render(request, 'operation.html', kwargs) |
|
1181 | return render(request, 'operation.html', kwargs) | |
1166 |
|
1182 | |||
1167 | id_camp = campaigns[0].id |
|
1183 | id_camp = campaigns[0].id | |
1168 |
|
1184 | |||
1169 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1185 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1170 |
|
1186 | |||
1171 | if request.method=='GET': |
|
1187 | if request.method=='GET': | |
1172 | form = OperationForm(initial={'campaign': campaign.id}, length = 5) |
|
1188 | form = OperationForm(initial={'campaign': campaign.id}, length = 5) | |
1173 |
|
1189 | |||
1174 | if request.method=='POST': |
|
1190 | if request.method=='POST': | |
1175 | form = OperationForm(request.POST, initial={'campaign':campaign.id}, length = 5) |
|
1191 | form = OperationForm(request.POST, initial={'campaign':campaign.id}, length = 5) | |
1176 |
|
1192 | |||
1177 | if form.is_valid(): |
|
1193 | if form.is_valid(): | |
1178 | return redirect('url_operation', id_camp=campaign.id) |
|
1194 | return redirect('url_operation', id_camp=campaign.id) | |
1179 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
1195 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() | |
1180 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
1196 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) | |
1181 | #for exs in experiments: |
|
1197 | #for exs in experiments: | |
1182 | # exs.get_status() |
|
1198 | # exs.get_status() | |
1183 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
1199 | locations= Location.objects.filter(experiment=experiments).distinct() | |
1184 | #experiments = [Experiment.objects.filter(location__pk=location.id).filter(campaign__pk=campaign.id) for location in locations] |
|
1200 | #experiments = [Experiment.objects.filter(location__pk=location.id).filter(campaign__pk=campaign.id) for location in locations] | |
1185 | kwargs = {} |
|
1201 | kwargs = {} | |
1186 | #---Campaign |
|
1202 | #---Campaign | |
1187 | kwargs['campaign'] = campaign |
|
1203 | kwargs['campaign'] = campaign | |
1188 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
1204 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] | |
1189 | #---Experiment |
|
1205 | #---Experiment | |
1190 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1206 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] | |
1191 | kwargs['experiment_keys'] = keys[1:] |
|
1207 | kwargs['experiment_keys'] = keys[1:] | |
1192 | kwargs['experiments'] = experiments |
|
1208 | kwargs['experiments'] = experiments | |
1193 | #---Radar |
|
1209 | #---Radar | |
1194 | kwargs['locations'] = locations |
|
1210 | kwargs['locations'] = locations | |
1195 | #---Else |
|
1211 | #---Else | |
1196 | kwargs['title'] = 'Campaign' |
|
1212 | kwargs['title'] = 'Campaign' | |
1197 | kwargs['suptitle'] = campaign.name |
|
1213 | kwargs['suptitle'] = campaign.name | |
1198 | kwargs['form'] = form |
|
1214 | kwargs['form'] = form | |
1199 | kwargs['button'] = 'Search' |
|
1215 | kwargs['button'] = 'Search' | |
1200 | kwargs['details'] = True |
|
1216 | kwargs['details'] = True | |
1201 | kwargs['search_button'] = True |
|
1217 | kwargs['search_button'] = True | |
1202 |
|
1218 | |||
1203 | return render(request, 'operation.html', kwargs) |
|
1219 | return render(request, 'operation.html', kwargs) | |
1204 |
|
1220 | |||
1205 |
|
1221 | |||
1206 | def operation_search(request, id_camp=None): |
|
1222 | def operation_search(request, id_camp=None): | |
1207 |
|
1223 | |||
1208 |
|
1224 | |||
1209 | if not id_camp: |
|
1225 | if not id_camp: | |
1210 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
1226 | campaigns = Campaign.objects.all().order_by('-start_date') | |
1211 |
|
1227 | |||
1212 | if not campaigns: |
|
1228 | if not campaigns: | |
1213 | return render(request, 'operation.html', {}) |
|
1229 | return render(request, 'operation.html', {}) | |
1214 |
|
1230 | |||
1215 | id_camp = campaigns[0].id |
|
1231 | id_camp = campaigns[0].id | |
1216 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1232 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1217 |
|
1233 | |||
1218 | if request.method=='GET': |
|
1234 | if request.method=='GET': | |
1219 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
1235 | form = OperationSearchForm(initial={'campaign': campaign.id}) | |
1220 |
|
1236 | |||
1221 | if request.method=='POST': |
|
1237 | if request.method=='POST': | |
1222 | form = OperationSearchForm(request.POST, initial={'campaign':campaign.id}) |
|
1238 | form = OperationSearchForm(request.POST, initial={'campaign':campaign.id}) | |
1223 |
|
1239 | |||
1224 | if form.is_valid(): |
|
1240 | if form.is_valid(): | |
1225 | return redirect('url_operation', id_camp=campaign.id) |
|
1241 | return redirect('url_operation', id_camp=campaign.id) | |
1226 |
|
1242 | |||
1227 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
1243 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() | |
1228 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
1244 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) | |
1229 | #for exs in experiments: |
|
1245 | #for exs in experiments: | |
1230 | # exs.get_status() |
|
1246 | # exs.get_status() | |
1231 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
1247 | locations= Location.objects.filter(experiment=experiments).distinct() | |
1232 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
1248 | form = OperationSearchForm(initial={'campaign': campaign.id}) | |
1233 |
|
1249 | |||
1234 | kwargs = {} |
|
1250 | kwargs = {} | |
1235 | #---Campaign |
|
1251 | #---Campaign | |
1236 | kwargs['campaign'] = campaign |
|
1252 | kwargs['campaign'] = campaign | |
1237 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
1253 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] | |
1238 | #---Experiment |
|
1254 | #---Experiment | |
1239 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1255 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] | |
1240 | kwargs['experiment_keys'] = keys[1:] |
|
1256 | kwargs['experiment_keys'] = keys[1:] | |
1241 | kwargs['experiments'] = experiments |
|
1257 | kwargs['experiments'] = experiments | |
1242 | #---Radar |
|
1258 | #---Radar | |
1243 | kwargs['locations'] = locations |
|
1259 | kwargs['locations'] = locations | |
1244 | #---Else |
|
1260 | #---Else | |
1245 | kwargs['title'] = 'Campaign' |
|
1261 | kwargs['title'] = 'Campaign' | |
1246 | kwargs['suptitle'] = campaign.name |
|
1262 | kwargs['suptitle'] = campaign.name | |
1247 | kwargs['form'] = form |
|
1263 | kwargs['form'] = form | |
1248 | kwargs['button'] = 'Select' |
|
1264 | kwargs['button'] = 'Select' | |
1249 | kwargs['details'] = True |
|
1265 | kwargs['details'] = True | |
1250 | kwargs['search_button'] = False |
|
1266 | kwargs['search_button'] = False | |
1251 |
|
1267 | |||
1252 | return render(request, 'operation.html', kwargs) |
|
1268 | return render(request, 'operation.html', kwargs) | |
1253 |
|
1269 | |||
1254 |
|
1270 | |||
1255 | def radar_play(request, id_camp, id_radar): |
|
1271 | def radar_play(request, id_camp, id_radar): | |
1256 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1272 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1257 | radar = get_object_or_404(Location, pk = id_radar) |
|
1273 | radar = get_object_or_404(Location, pk = id_radar) | |
1258 | today = datetime.today() |
|
1274 | today = datetime.today() | |
1259 | now = today.time() |
|
1275 | now = today.time() | |
1260 |
|
1276 | |||
1261 | #--Clear Old Experiments From RunningExperiment Object |
|
1277 | #--Clear Old Experiments From RunningExperiment Object | |
1262 | running_experiment = RunningExperiment.objects.filter(radar=radar) |
|
1278 | running_experiment = RunningExperiment.objects.filter(radar=radar) | |
1263 | if running_experiment: |
|
1279 | if running_experiment: | |
1264 | running_experiment = running_experiment[0] |
|
1280 | running_experiment = running_experiment[0] | |
1265 | running_experiment.running_experiment.clear() |
|
1281 | running_experiment.running_experiment.clear() | |
1266 | running_experiment.save() |
|
1282 | running_experiment.save() | |
1267 |
|
1283 | |||
1268 | #--If campaign datetime is ok: |
|
1284 | #--If campaign datetime is ok: | |
1269 | if today >= campaign.start_date and today <= campaign.end_date: |
|
1285 | if today >= campaign.start_date and today <= campaign.end_date: | |
1270 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1286 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) | |
1271 | for exp in experiments: |
|
1287 | for exp in experiments: | |
1272 | #--If experiment time is ok: |
|
1288 | #--If experiment time is ok: | |
1273 | if now >= exp.start_time and now <= exp.end_time: |
|
1289 | if now >= exp.start_time and now <= exp.end_time: | |
1274 | configurations = Configuration.objects.filter(experiment = exp) |
|
1290 | configurations = Configuration.objects.filter(experiment = exp) | |
1275 | for conf in configurations: |
|
1291 | for conf in configurations: | |
1276 | if 'cgs' in conf.device.device_type.name: |
|
1292 | if 'cgs' in conf.device.device_type.name: | |
1277 | conf.status_device() |
|
1293 | conf.status_device() | |
1278 | else: |
|
1294 | else: | |
1279 | answer = conf.start_device() |
|
1295 | answer = conf.start_device() | |
1280 | conf.status_device() |
|
1296 | conf.status_device() | |
1281 | #--Running Experiment |
|
1297 | #--Running Experiment | |
1282 | old_running_experiment = RunningExperiment.objects.filter(radar=radar) |
|
1298 | old_running_experiment = RunningExperiment.objects.filter(radar=radar) | |
1283 | #--If RunningExperiment element exists |
|
1299 | #--If RunningExperiment element exists | |
1284 | if old_running_experiment: |
|
1300 | if old_running_experiment: | |
1285 | old_running_experiment = old_running_experiment[0] |
|
1301 | old_running_experiment = old_running_experiment[0] | |
1286 | old_running_experiment.running_experiment.add(exp) |
|
1302 | old_running_experiment.running_experiment.add(exp) | |
1287 | old_running_experiment.status = 3 |
|
1303 | old_running_experiment.status = 3 | |
1288 | old_running_experiment.save() |
|
1304 | old_running_experiment.save() | |
1289 | #--Create a new Running_Experiment Object |
|
1305 | #--Create a new Running_Experiment Object | |
1290 | else: |
|
1306 | else: | |
1291 | new_running_experiment = RunningExperiment( |
|
1307 | new_running_experiment = RunningExperiment( | |
1292 | radar = radar, |
|
1308 | radar = radar, | |
1293 | status = 3, |
|
1309 | status = 3, | |
1294 | ) |
|
1310 | ) | |
1295 | new_running_experiment.save() |
|
1311 | new_running_experiment.save() | |
1296 | new_running_experiment.running_experiment.add(exp) |
|
1312 | new_running_experiment.running_experiment.add(exp) | |
1297 | new_running_experiment.save() |
|
1313 | new_running_experiment.save() | |
1298 |
|
1314 | |||
1299 | if answer: |
|
1315 | if answer: | |
1300 | messages.success(request, conf.message) |
|
1316 | messages.success(request, conf.message) | |
1301 | exp.status=2 |
|
1317 | exp.status=2 | |
1302 | exp.save() |
|
1318 | exp.save() | |
1303 | else: |
|
1319 | else: | |
1304 | messages.error(request, conf.message) |
|
1320 | messages.error(request, conf.message) | |
1305 | else: |
|
1321 | else: | |
1306 | if exp.status == 1 or exp.status == 3: |
|
1322 | if exp.status == 1 or exp.status == 3: | |
1307 | exp.status=3 |
|
1323 | exp.status=3 | |
1308 | exp.save() |
|
1324 | exp.save() | |
1309 |
|
1325 | |||
1310 |
|
1326 | |||
1311 | route = request.META['HTTP_REFERER'] |
|
1327 | route = request.META['HTTP_REFERER'] | |
1312 | route = str(route) |
|
1328 | route = str(route) | |
1313 | if 'search' in route: |
|
1329 | if 'search' in route: | |
1314 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1330 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) | |
1315 | else: |
|
1331 | else: | |
1316 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1332 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
1317 |
|
1333 | |||
1318 |
|
1334 | |||
1319 | def radar_stop(request, id_camp, id_radar): |
|
1335 | def radar_stop(request, id_camp, id_radar): | |
1320 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1336 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1321 | radar = get_object_or_404(Location, pk = id_radar) |
|
1337 | radar = get_object_or_404(Location, pk = id_radar) | |
1322 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1338 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) | |
1323 |
|
1339 | |||
1324 | for exp in experiments: |
|
1340 | for exp in experiments: | |
1325 | configurations = Configuration.objects.filter(experiment = exp) |
|
1341 | configurations = Configuration.objects.filter(experiment = exp) | |
1326 | for conf in configurations: |
|
1342 | for conf in configurations: | |
1327 | if 'cgs' in conf.device.device_type.name: |
|
1343 | if 'cgs' in conf.device.device_type.name: | |
1328 | conf.status_device() |
|
1344 | conf.status_device() | |
1329 | else: |
|
1345 | else: | |
1330 | answer = conf.stop_device() |
|
1346 | answer = conf.stop_device() | |
1331 | conf.status_device() |
|
1347 | conf.status_device() | |
1332 |
|
1348 | |||
1333 | if answer: |
|
1349 | if answer: | |
1334 | messages.success(request, conf.message) |
|
1350 | messages.success(request, conf.message) | |
1335 | exp.status=1 |
|
1351 | exp.status=1 | |
1336 | exp.save() |
|
1352 | exp.save() | |
1337 | else: |
|
1353 | else: | |
1338 | messages.error(request, conf.message) |
|
1354 | messages.error(request, conf.message) | |
1339 |
|
1355 | |||
1340 |
|
1356 | |||
1341 | route = request.META['HTTP_REFERER'] |
|
1357 | route = request.META['HTTP_REFERER'] | |
1342 | route = str(route) |
|
1358 | route = str(route) | |
1343 | if 'search' in route: |
|
1359 | if 'search' in route: | |
1344 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1360 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) | |
1345 | else: |
|
1361 | else: | |
1346 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1362 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
1347 |
|
1363 | |||
1348 |
|
1364 | |||
1349 | def radar_refresh(request, id_camp, id_radar): |
|
1365 | def radar_refresh(request, id_camp, id_radar): | |
1350 |
|
1366 | |||
1351 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1367 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
1352 | radar = get_object_or_404(Location, pk = id_radar) |
|
1368 | radar = get_object_or_404(Location, pk = id_radar) | |
1353 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1369 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) | |
1354 | for exs in experiments: |
|
1370 | for exs in experiments: | |
1355 | exs.get_status() |
|
1371 | exs.get_status() | |
1356 |
|
1372 | |||
1357 | route = request.META['HTTP_REFERER'] |
|
1373 | route = request.META['HTTP_REFERER'] | |
1358 | route = str(route) |
|
1374 | route = str(route) | |
1359 | if 'search' in route: |
|
1375 | if 'search' in route: | |
1360 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1376 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) | |
1361 | else: |
|
1377 | else: | |
1362 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1378 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
1363 |
|
1379 |
@@ -1,370 +1,372 | |||||
1 | import os |
|
1 | import os | |
2 | import ast |
|
2 | import ast | |
3 | import json |
|
3 | import json | |
4 |
|
4 | |||
5 | from django import forms |
|
5 | from django import forms | |
6 | from django.utils.safestring import mark_safe |
|
6 | from django.utils.safestring import mark_safe | |
7 | from apps.main.models import Device |
|
7 | from apps.main.models import Device | |
8 | from apps.main.forms import add_empty_choice |
|
8 | from apps.main.forms import add_empty_choice | |
9 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode |
|
9 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode | |
10 | from .widgets import KmUnitWidget, KmUnitHzWidget, KmUnitDcWidget, UnitKmWidget, DefaultWidget, CodesWidget, HiddenWidget, HCheckboxSelectMultiple |
|
10 | from .widgets import KmUnitWidget, KmUnitHzWidget, KmUnitDcWidget, UnitKmWidget, DefaultWidget, CodesWidget, HiddenWidget, HCheckboxSelectMultiple | |
11 |
|
11 | |||
12 | def create_choices_from_model(model, conf_id, all=False): |
|
12 | def create_choices_from_model(model, conf_id, all=False): | |
13 |
|
13 | |||
14 | if model=='RCLine': |
|
14 | if model=='RCLine': | |
15 | instance = RCConfiguration.objects.get(pk=conf_id) |
|
15 | instance = RCConfiguration.objects.get(pk=conf_id) | |
16 | choices = [(line.pk, line.get_name()) for line in instance.get_lines(line_type__name='tx')] |
|
16 | choices = [(line.pk, line.get_name()) for line in instance.get_lines(line_type__name='tx')] | |
17 | choices = add_empty_choice(choices, label='All') |
|
17 | choices = add_empty_choice(choices, label='All') | |
18 | else: |
|
18 | else: | |
19 | instance = globals()[model] |
|
19 | instance = globals()[model] | |
20 | choices = instance.objects.all().values_list('pk', 'name') |
|
20 | choices = instance.objects.all().values_list('pk', 'name') | |
21 |
|
21 | |||
22 | return choices |
|
22 | return choices | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | class ExtFileField(forms.FileField): |
|
25 | class ExtFileField(forms.FileField): | |
26 | """ |
|
26 | """ | |
27 | Same as forms.FileField, but you can specify a file extension whitelist. |
|
27 | Same as forms.FileField, but you can specify a file extension whitelist. | |
28 |
|
28 | |||
29 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |
|
29 | >>> from django.core.files.uploadedfile import SimpleUploadedFile | |
30 | >>> |
|
30 | >>> | |
31 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) |
|
31 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) | |
32 | >>> |
|
32 | >>> | |
33 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) |
|
33 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) | |
34 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) |
|
34 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) | |
35 | >>> |
|
35 | >>> | |
36 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) |
|
36 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) | |
37 | Traceback (most recent call last): |
|
37 | Traceback (most recent call last): | |
38 | ... |
|
38 | ... | |
39 | ValidationError: [u'Not allowed filetype!'] |
|
39 | ValidationError: [u'Not allowed filetype!'] | |
40 | """ |
|
40 | """ | |
41 | def __init__(self, *args, **kwargs): |
|
41 | def __init__(self, *args, **kwargs): | |
42 | extensions = kwargs.pop("extensions") |
|
42 | extensions = kwargs.pop("extensions") | |
43 | self.extensions = [i.lower() for i in extensions] |
|
43 | self.extensions = [i.lower() for i in extensions] | |
44 |
|
44 | |||
45 | super(ExtFileField, self).__init__(*args, **kwargs) |
|
45 | super(ExtFileField, self).__init__(*args, **kwargs) | |
46 |
|
46 | |||
47 | def clean(self, *args, **kwargs): |
|
47 | def clean(self, *args, **kwargs): | |
48 | data = super(ExtFileField, self).clean(*args, **kwargs) |
|
48 | data = super(ExtFileField, self).clean(*args, **kwargs) | |
49 | filename = data.name |
|
49 | filename = data.name | |
50 | ext = os.path.splitext(filename)[1] |
|
50 | ext = os.path.splitext(filename)[1] | |
51 | ext = ext.lower() |
|
51 | ext = ext.lower() | |
52 | if ext not in self.extensions: |
|
52 | if ext not in self.extensions: | |
53 | raise forms.ValidationError('Not allowed file type: %s' % ext) |
|
53 | raise forms.ValidationError('Not allowed file type: %s' % ext) | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | class RCConfigurationForm(forms.ModelForm): |
|
56 | class RCConfigurationForm(forms.ModelForm): | |
57 |
|
57 | |||
58 | def __init__(self, *args, **kwargs): |
|
58 | def __init__(self, *args, **kwargs): | |
59 | super(RCConfigurationForm, self).__init__(*args, **kwargs) |
|
59 | super(RCConfigurationForm, self).__init__(*args, **kwargs) | |
60 |
|
60 | |||
61 | instance = getattr(self, 'instance', None) |
|
61 | instance = getattr(self, 'instance', None) | |
62 |
|
62 | |||
63 | if instance and instance.pk: |
|
63 | if instance and instance.pk: | |
64 |
|
64 | |||
65 | devices = Device.objects.filter(device_type__name='rc') |
|
65 | devices = Device.objects.filter(device_type__name='rc') | |
66 | if instance.experiment: |
|
66 | if instance.experiment: | |
67 | self.fields['experiment'].widget.attrs['read_only'] = True |
|
67 | self.fields['experiment'].widget.attrs['read_only'] = True | |
68 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] |
|
68 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] | |
69 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
69 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] | |
70 | self.fields['ipp'].widget = KmUnitHzWidget(attrs={'km2unit':instance.km2unit}) |
|
70 | self.fields['ipp'].widget = KmUnitHzWidget(attrs={'km2unit':instance.km2unit}) | |
71 | self.fields['clock'].widget.attrs['readonly'] = True |
|
71 | self.fields['clock'].widget.attrs['readonly'] = True | |
72 |
|
72 | |||
73 | self.fields['time_before'].label = mark_safe(self.fields['time_before'].label) |
|
73 | self.fields['time_before'].label = mark_safe(self.fields['time_before'].label) | |
74 | self.fields['time_after'].label = mark_safe(self.fields['time_after'].label) |
|
74 | self.fields['time_after'].label = mark_safe(self.fields['time_after'].label) | |
75 |
|
75 | |||
76 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
76 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): | |
77 | self.fields['experiment'].widget.attrs['disabled'] = 'disabled' |
|
77 | self.fields['experiment'].widget.attrs['disabled'] = 'disabled' | |
78 |
|
78 | |||
79 | class Meta: |
|
79 | class Meta: | |
80 | model = RCConfiguration |
|
80 | model = RCConfiguration | |
81 | exclude = ('type', 'parameters', 'status', 'mix') |
|
81 | exclude = ('type', 'parameters', 'status', 'total_units', 'mix') | |
82 |
|
82 | |||
83 | def clean(self): |
|
83 | def clean(self): | |
84 | form_data = super(RCConfigurationForm, self).clean() |
|
84 | form_data = super(RCConfigurationForm, self).clean() | |
85 |
|
85 | |||
86 | if 'clock_divider' in form_data: |
|
86 | if 'clock_divider' in form_data: | |
87 | if form_data['clock_divider']<1: |
|
87 | if form_data['clock_divider']<1: | |
88 | self.add_error('clock_divider', 'Invalid Value') |
|
88 | self.add_error('clock_divider', 'Invalid Value') | |
89 | else: |
|
89 | else: | |
90 | if form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider']))%10<>0: |
|
90 | if form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider']))%10<>0: | |
91 | self.add_error('ipp', 'Invalid IPP units={}'.format(form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider'])))) |
|
91 | self.add_error('ipp', 'Invalid IPP units={}'.format(form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider'])))) | |
92 |
|
92 | |||
93 | return form_data |
|
93 | return form_data | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | class RCMixConfigurationForm(forms.Form): |
|
96 | class RCMixConfigurationForm(forms.Form): | |
97 |
|
97 | |||
98 | clock_in = forms.CharField(widget=forms.HiddenInput()) |
|
98 | clock_in = forms.CharField(widget=forms.HiddenInput()) | |
99 | clock_divider = forms.CharField(widget=forms.HiddenInput()) |
|
99 | clock_divider = forms.CharField(widget=forms.HiddenInput()) | |
100 | name = forms.CharField() |
|
100 | name = forms.CharField() | |
101 | experiment = forms.ChoiceField() |
|
101 | experiment = forms.ChoiceField() | |
|
102 | mode = forms.ChoiceField(widget=forms.RadioSelect(), | |||
|
103 | choices=[(0, 'Parallel'), (1, 'Sequence')], | |||
|
104 | initial=0) | |||
102 | operation = forms.ChoiceField(widget=forms.RadioSelect(), |
|
105 | operation = forms.ChoiceField(widget=forms.RadioSelect(), | |
103 | choices=[(0, 'OR'), (1, 'XOR'), (2, 'AND'), (3, 'NAND')], |
|
106 | choices=[(0, 'OR'), (1, 'XOR'), (2, 'AND'), (3, 'NAND')], | |
104 | initial=1) |
|
107 | initial=1) | |
105 | delay = forms.CharField() |
|
108 | delay = forms.CharField() | |
106 | mask = forms.MultipleChoiceField(choices=[(0, 'L1'),(1, 'L2'),(2, 'L3'),(3, 'L4'),(4, 'L5'),(5, 'L6'),(6, 'L7'),(7, 'L8')], |
|
109 | mask = forms.MultipleChoiceField(choices=[(0, 'L1'),(1, 'L2'),(2, 'L3'),(3, 'L4'),(4, 'L5'),(5, 'L6'),(6, 'L7'),(7, 'L8')], | |
107 | widget=HCheckboxSelectMultiple()) |
|
110 | widget=HCheckboxSelectMultiple()) | |
108 | result = forms.CharField(required=False, |
|
111 | result = forms.CharField(required=False, | |
109 | widget=forms.Textarea(attrs={'readonly':True, 'rows':5, 'class':'tabuled'})) |
|
112 | widget=forms.Textarea(attrs={'readonly':True, 'rows':5, 'class':'tabuled'})) | |
110 |
|
113 | |||
111 | def __init__(self, *args, **kwargs): |
|
114 | def __init__(self, *args, **kwargs): | |
112 | confs = kwargs.pop('confs', []) |
|
115 | confs = kwargs.pop('confs', []) | |
113 | if confs: |
|
116 | if confs: | |
114 | km2unit = confs[0].km2unit |
|
117 | km2unit = confs[0].km2unit | |
115 | clock_in = confs[0].clock_in |
|
118 | clock_in = confs[0].clock_in | |
116 | clock_divider = confs[0].clock_divider |
|
119 | clock_divider = confs[0].clock_divider | |
117 | else: |
|
120 | else: | |
118 | km2unit = clock_in = clock_divider = 0 |
|
121 | km2unit = clock_in = clock_divider = 0 | |
119 | super(RCMixConfigurationForm, self).__init__(*args, **kwargs) |
|
122 | super(RCMixConfigurationForm, self).__init__(*args, **kwargs) | |
120 | self.fields['experiment'].choices = [(conf.pk, '{} | {}'.format(conf.pk, conf.name)) for conf in confs] |
|
123 | self.fields['experiment'].choices = [(conf.pk, '{} | {}'.format(conf.pk, conf.name)) for conf in confs] | |
121 | self.fields['delay'].widget = KmUnitWidget(attrs = {'km2unit':km2unit}) |
|
124 | self.fields['delay'].widget = KmUnitWidget(attrs = {'km2unit':km2unit}) | |
122 | self.fields['clock_in'].initial = clock_in |
|
125 | self.fields['clock_in'].initial = clock_in | |
123 | self.fields['clock_divider'].initial = clock_divider |
|
126 | self.fields['clock_divider'].initial = clock_divider | |
124 |
|
127 | |||
125 |
|
||||
126 |
|
128 | |||
127 | class RCLineForm(forms.ModelForm): |
|
129 | class RCLineForm(forms.ModelForm): | |
128 |
|
130 | |||
129 | def __init__(self, *args, **kwargs): |
|
131 | def __init__(self, *args, **kwargs): | |
130 | self.extra_fields = kwargs.pop('extra_fields', []) |
|
132 | self.extra_fields = kwargs.pop('extra_fields', []) | |
131 | super(RCLineForm, self).__init__(*args, **kwargs) |
|
133 | super(RCLineForm, self).__init__(*args, **kwargs) | |
132 |
|
134 | |||
133 | if 'initial' in kwargs and 'line_type' in kwargs['initial']: |
|
135 | if 'initial' in kwargs and 'line_type' in kwargs['initial']: | |
134 | line_type = RCLineType.objects.get(pk=kwargs['initial']['line_type']) |
|
136 | line_type = RCLineType.objects.get(pk=kwargs['initial']['line_type']) | |
135 |
|
137 | |||
136 | if 'code_id' in kwargs['initial']: |
|
138 | if 'code_id' in kwargs['initial']: | |
137 | model_initial = kwargs['initial']['code_id'] |
|
139 | model_initial = kwargs['initial']['code_id'] | |
138 | else: |
|
140 | else: | |
139 | model_initial = 0 |
|
141 | model_initial = 0 | |
140 |
|
142 | |||
141 | params = json.loads(line_type.params) |
|
143 | params = json.loads(line_type.params) | |
142 |
|
144 | |||
143 | for label, value in self.extra_fields.items(): |
|
145 | for label, value in self.extra_fields.items(): | |
144 | if label=='params': |
|
146 | if label=='params': | |
145 | continue |
|
147 | continue | |
146 |
|
148 | |||
147 | if 'model' in params[label]: |
|
149 | if 'model' in params[label]: | |
148 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], |
|
150 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], | |
149 | kwargs['initial']['rc_configuration']), |
|
151 | kwargs['initial']['rc_configuration']), | |
150 | initial=model_initial) |
|
152 | initial=model_initial) | |
151 |
|
153 | |||
152 |
|
154 | |||
153 | else: |
|
155 | else: | |
154 | if label=='codes' and 'code_id' in kwargs['initial']: |
|
156 | if label=='codes' and 'code_id' in kwargs['initial']: | |
155 | self.fields[label] = forms.CharField(initial=RCLineCode.objects.get(pk=kwargs['initial']['code_id']).codes) |
|
157 | self.fields[label] = forms.CharField(initial=RCLineCode.objects.get(pk=kwargs['initial']['code_id']).codes) | |
156 | else: |
|
158 | else: | |
157 | self.fields[label] = forms.CharField(initial=value['value']) |
|
159 | self.fields[label] = forms.CharField(initial=value['value']) | |
158 |
|
160 | |||
159 | if label=='codes': |
|
161 | if label=='codes': | |
160 | self.fields[label].widget = CodesWidget() |
|
162 | self.fields[label].widget = CodesWidget() | |
161 |
|
163 | |||
162 | if self.data: |
|
164 | if self.data: | |
163 | line_type = RCLineType.objects.get(pk=self.data['line_type']) |
|
165 | line_type = RCLineType.objects.get(pk=self.data['line_type']) | |
164 |
|
166 | |||
165 | if 'code_id' in self.data: |
|
167 | if 'code_id' in self.data: | |
166 | model_initial = self.data['code_id'] |
|
168 | model_initial = self.data['code_id'] | |
167 | else: |
|
169 | else: | |
168 | model_initial = 0 |
|
170 | model_initial = 0 | |
169 |
|
171 | |||
170 | params = json.loads(line_type.params) |
|
172 | params = json.loads(line_type.params) | |
171 |
|
173 | |||
172 | for label, value in self.extra_fields.items(): |
|
174 | for label, value in self.extra_fields.items(): | |
173 | if label=='params': |
|
175 | if label=='params': | |
174 | continue |
|
176 | continue | |
175 |
|
177 | |||
176 | if 'model' in params[label]: |
|
178 | if 'model' in params[label]: | |
177 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], |
|
179 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], | |
178 | self.data['rc_configuration']), |
|
180 | self.data['rc_configuration']), | |
179 | initial=model_initial) |
|
181 | initial=model_initial) | |
180 |
|
182 | |||
181 |
|
183 | |||
182 | else: |
|
184 | else: | |
183 | if label=='codes' and 'code' in self.data: |
|
185 | if label=='codes' and 'code' in self.data: | |
184 | self.fields[label] = forms.CharField(initial=self.data['codes']) |
|
186 | self.fields[label] = forms.CharField(initial=self.data['codes']) | |
185 | else: |
|
187 | else: | |
186 | self.fields[label] = forms.CharField(initial=self.data[label]) |
|
188 | self.fields[label] = forms.CharField(initial=self.data[label]) | |
187 |
|
189 | |||
188 | if label=='codes': |
|
190 | if label=='codes': | |
189 | self.fields[label].widget = CodesWidget() |
|
191 | self.fields[label].widget = CodesWidget() | |
190 |
|
192 | |||
191 |
|
193 | |||
192 | class Meta: |
|
194 | class Meta: | |
193 | model = RCLine |
|
195 | model = RCLine | |
194 | fields = ('rc_configuration', 'line_type', 'channel') |
|
196 | fields = ('rc_configuration', 'line_type', 'channel') | |
195 | widgets = { |
|
197 | widgets = { | |
196 | 'channel': forms.HiddenInput(), |
|
198 | 'channel': forms.HiddenInput(), | |
197 | } |
|
199 | } | |
198 |
|
200 | |||
199 |
|
201 | |||
200 | def clean(self): |
|
202 | def clean(self): | |
201 |
|
203 | |||
202 | form_data = self.cleaned_data |
|
204 | form_data = self.cleaned_data | |
203 | if 'code' in self.data and self.data['TX_ref']=="0": |
|
205 | if 'code' in self.data and self.data['TX_ref']=="0": | |
204 | self.add_error('TX_ref', 'Choose a valid TX reference') |
|
206 | self.add_error('TX_ref', 'Choose a valid TX reference') | |
205 |
|
207 | |||
206 | return form_data |
|
208 | return form_data | |
207 |
|
209 | |||
208 |
|
210 | |||
209 | def save(self): |
|
211 | def save(self): | |
210 | line = super(RCLineForm, self).save() |
|
212 | line = super(RCLineForm, self).save() | |
211 |
|
213 | |||
212 | #auto add channel |
|
214 | #auto add channel | |
213 | line.channel = RCLine.objects.filter(rc_configuration=line.rc_configuration).count()-1 |
|
215 | line.channel = RCLine.objects.filter(rc_configuration=line.rc_configuration).count()-1 | |
214 |
|
216 | |||
215 | #auto add position for TX, TR & CODE |
|
217 | #auto add position for TX, TR & CODE | |
216 | if line.line_type.name in ('tx', ): |
|
218 | if line.line_type.name in ('tx', ): | |
217 | line.position = RCLine.objects.filter(rc_configuration=line.rc_configuration, line_type=line.line_type).count()-1 |
|
219 | line.position = RCLine.objects.filter(rc_configuration=line.rc_configuration, line_type=line.line_type).count()-1 | |
218 |
|
220 | |||
219 | #save extra fields in params |
|
221 | #save extra fields in params | |
220 | params = {} |
|
222 | params = {} | |
221 | for label, value in self.extra_fields.items(): |
|
223 | for label, value in self.extra_fields.items(): | |
222 | if label=='params': |
|
224 | if label=='params': | |
223 | params['params'] = [] |
|
225 | params['params'] = [] | |
224 | elif label=='codes': |
|
226 | elif label=='codes': | |
225 | params[label] = [s for s in self.data[label].split('\r\n') if s] |
|
227 | params[label] = [s for s in self.data[label].split('\r\n') if s] | |
226 | else: |
|
228 | else: | |
227 | params[label] = self.data[label] |
|
229 | params[label] = self.data[label] | |
228 | line.params = json.dumps(params) |
|
230 | line.params = json.dumps(params) | |
229 | line.save() |
|
231 | line.save() | |
230 | return |
|
232 | return | |
231 |
|
233 | |||
232 |
|
234 | |||
233 | class RCLineViewForm(forms.Form): |
|
235 | class RCLineViewForm(forms.Form): | |
234 |
|
236 | |||
235 | def __init__(self, *args, **kwargs): |
|
237 | def __init__(self, *args, **kwargs): | |
236 |
|
238 | |||
237 | extra_fields = kwargs.pop('extra_fields') |
|
239 | extra_fields = kwargs.pop('extra_fields') | |
238 | line = kwargs.pop('line') |
|
240 | line = kwargs.pop('line') | |
239 | subform = kwargs.pop('subform', False) |
|
241 | subform = kwargs.pop('subform', False) | |
240 | super(RCLineViewForm, self).__init__(*args, **kwargs) |
|
242 | super(RCLineViewForm, self).__init__(*args, **kwargs) | |
241 |
|
243 | |||
242 | if subform: |
|
244 | if subform: | |
243 | params = json.loads(line.line_type.params)['params'] |
|
245 | params = json.loads(line.line_type.params)['params'] | |
244 | else: |
|
246 | else: | |
245 | params = json.loads(line.line_type.params) |
|
247 | params = json.loads(line.line_type.params) | |
246 |
|
248 | |||
247 | for label, value in extra_fields.items(): |
|
249 | for label, value in extra_fields.items(): | |
248 |
|
250 | |||
249 | if label=='params': |
|
251 | if label=='params': | |
250 | continue |
|
252 | continue | |
251 | if 'ref' in label: |
|
253 | if 'ref' in label: | |
252 | if value in (0, '0'): |
|
254 | if value in (0, '0'): | |
253 | value = 'All' |
|
255 | value = 'All' | |
254 | else: |
|
256 | else: | |
255 | value = RCLine.objects.get(pk=value).get_name() |
|
257 | value = RCLine.objects.get(pk=value).get_name() | |
256 | elif label=='code': |
|
258 | elif label=='code': | |
257 | value = RCLineCode.objects.get(pk=value).name |
|
259 | value = RCLineCode.objects.get(pk=value).name | |
258 |
|
260 | |||
259 | self.fields[label] = forms.CharField(initial=value) |
|
261 | self.fields[label] = forms.CharField(initial=value) | |
260 |
|
262 | |||
261 | if 'widget' in params[label]: |
|
263 | if 'widget' in params[label]: | |
262 | km2unit = line.rc_configuration.km2unit |
|
264 | km2unit = line.rc_configuration.km2unit | |
263 | if params[label]['widget']=='km': |
|
265 | if params[label]['widget']=='km': | |
264 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
266 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
265 | elif params[label]['widget']=='unit': |
|
267 | elif params[label]['widget']=='unit': | |
266 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
268 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
267 | elif params[label]['widget']=='dc': |
|
269 | elif params[label]['widget']=='dc': | |
268 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
270 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
269 | elif params[label]['widget']=='codes': |
|
271 | elif params[label]['widget']=='codes': | |
270 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
272 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
271 | else: |
|
273 | else: | |
272 | self.fields[label].widget = DefaultWidget(attrs={'disabled':True}) |
|
274 | self.fields[label].widget = DefaultWidget(attrs={'disabled':True}) | |
273 |
|
275 | |||
274 |
|
276 | |||
275 | class RCLineEditForm(forms.ModelForm): |
|
277 | class RCLineEditForm(forms.ModelForm): | |
276 |
|
278 | |||
277 | def __init__(self, *args, **kwargs): |
|
279 | def __init__(self, *args, **kwargs): | |
278 |
|
280 | |||
279 | extra_fields = kwargs.pop('extra_fields', []) |
|
281 | extra_fields = kwargs.pop('extra_fields', []) | |
280 | conf = kwargs.pop('conf', False) |
|
282 | conf = kwargs.pop('conf', False) | |
281 | line = kwargs.pop('line') |
|
283 | line = kwargs.pop('line') | |
282 | subform = kwargs.pop('subform', False) |
|
284 | subform = kwargs.pop('subform', False) | |
283 |
|
285 | |||
284 | super(RCLineEditForm, self).__init__(*args, **kwargs) |
|
286 | super(RCLineEditForm, self).__init__(*args, **kwargs) | |
285 |
|
287 | |||
286 | if subform is not False: |
|
288 | if subform is not False: | |
287 | params = json.loads(line.line_type.params)['params'] |
|
289 | params = json.loads(line.line_type.params)['params'] | |
288 | count = subform |
|
290 | count = subform | |
289 | else: |
|
291 | else: | |
290 | params = json.loads(line.line_type.params) |
|
292 | params = json.loads(line.line_type.params) | |
291 | count = -1 |
|
293 | count = -1 | |
292 |
|
294 | |||
293 | for label, value in extra_fields.items(): |
|
295 | for label, value in extra_fields.items(): | |
294 |
|
296 | |||
295 | if label in ('params',): |
|
297 | if label in ('params',): | |
296 | continue |
|
298 | continue | |
297 | if 'help' in params[label]: |
|
299 | if 'help' in params[label]: | |
298 | help_text = params[label]['help'] |
|
300 | help_text = params[label]['help'] | |
299 | else: |
|
301 | else: | |
300 | help_text = '' |
|
302 | help_text = '' | |
301 |
|
303 | |||
302 | if 'model' in params[label]: |
|
304 | if 'model' in params[label]: | |
303 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], conf.id), |
|
305 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], conf.id), | |
304 | initial=value, |
|
306 | initial=value, | |
305 | widget=forms.Select(attrs={'name':'%s|%s|%s' % (count, line.id, label)}), |
|
307 | widget=forms.Select(attrs={'name':'%s|%s|%s' % (count, line.id, label)}), | |
306 | help_text=help_text) |
|
308 | help_text=help_text) | |
307 |
|
309 | |||
308 | else: |
|
310 | else: | |
309 |
|
311 | |||
310 | self.fields[label] = forms.CharField(initial=value, help_text=help_text) |
|
312 | self.fields[label] = forms.CharField(initial=value, help_text=help_text) | |
311 |
|
313 | |||
312 | if label in ('code', ): |
|
314 | if label in ('code', ): | |
313 | self.fields[label].widget = HiddenWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) |
|
315 | self.fields[label].widget = HiddenWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) | |
314 |
|
316 | |||
315 | elif 'widget' in params[label]: |
|
317 | elif 'widget' in params[label]: | |
316 | km2unit = line.rc_configuration.km2unit |
|
318 | km2unit = line.rc_configuration.km2unit | |
317 | if params[label]['widget']=='km': |
|
319 | if params[label]['widget']=='km': | |
318 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
320 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
319 | elif params[label]['widget']=='unit': |
|
321 | elif params[label]['widget']=='unit': | |
320 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
322 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
321 | elif params[label]['widget']=='dc': |
|
323 | elif params[label]['widget']=='dc': | |
322 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
324 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
323 | elif params[label]['widget']=='codes': |
|
325 | elif params[label]['widget']=='codes': | |
324 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
326 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
325 | else: |
|
327 | else: | |
326 | self.fields[label].widget = DefaultWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) |
|
328 | self.fields[label].widget = DefaultWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) | |
327 |
|
329 | |||
328 |
|
330 | |||
329 | class Meta: |
|
331 | class Meta: | |
330 | model = RCLine |
|
332 | model = RCLine | |
331 | exclude = ('rc_configuration', 'line_type', 'channel', 'position', 'params', 'pulses') |
|
333 | exclude = ('rc_configuration', 'line_type', 'channel', 'position', 'params', 'pulses') | |
332 |
|
334 | |||
333 |
|
335 | |||
334 | class RCSubLineEditForm(forms.Form): |
|
336 | class RCSubLineEditForm(forms.Form): | |
335 |
|
337 | |||
336 | def __init__(self, *args, **kwargs): |
|
338 | def __init__(self, *args, **kwargs): | |
337 | extra_fields = kwargs.pop('extra_fields') |
|
339 | extra_fields = kwargs.pop('extra_fields') | |
338 | count = kwargs.pop('count') |
|
340 | count = kwargs.pop('count') | |
339 | line = kwargs.pop('line') |
|
341 | line = kwargs.pop('line') | |
340 | super(RCSubLineEditForm, self).__init__(*args, **kwargs) |
|
342 | super(RCSubLineEditForm, self).__init__(*args, **kwargs) | |
341 | for label, value in extra_fields.items(): |
|
343 | for label, value in extra_fields.items(): | |
342 | self.fields[label] = forms.CharField(initial=value, |
|
344 | self.fields[label] = forms.CharField(initial=value, | |
343 | widget=forms.TextInput(attrs={'name':'%s|%s|%s' % (count, line, label)})) |
|
345 | widget=forms.TextInput(attrs={'name':'%s|%s|%s' % (count, line, label)})) | |
344 |
|
346 | |||
345 |
|
347 | |||
346 | class RCImportForm(forms.Form): |
|
348 | class RCImportForm(forms.Form): | |
347 |
|
349 | |||
348 | file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) |
|
350 | file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) | |
349 |
|
351 | |||
350 |
|
352 | |||
351 | class RCLineCodesForm(forms.ModelForm): |
|
353 | class RCLineCodesForm(forms.ModelForm): | |
352 |
|
354 | |||
353 | def __init__(self, *args, **kwargs): |
|
355 | def __init__(self, *args, **kwargs): | |
354 | super(RCLineCodesForm, self).__init__(*args, **kwargs) |
|
356 | super(RCLineCodesForm, self).__init__(*args, **kwargs) | |
355 |
|
357 | |||
356 | if 'initial' in kwargs: |
|
358 | if 'initial' in kwargs: | |
357 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), |
|
359 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), | |
358 | initial=kwargs['initial']['code']) |
|
360 | initial=kwargs['initial']['code']) | |
359 | if 'instance' in kwargs: |
|
361 | if 'instance' in kwargs: | |
360 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), |
|
362 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), | |
361 | initial=kwargs['instance'].pk) |
|
363 | initial=kwargs['instance'].pk) | |
362 |
|
364 | |||
363 | self.fields['codes'].widget = CodesWidget() |
|
365 | self.fields['codes'].widget = CodesWidget() | |
364 |
|
366 | |||
365 |
|
367 | |||
366 | class Meta: |
|
368 | class Meta: | |
367 | model = RCLineCode |
|
369 | model = RCLineCode | |
368 | exclude = ('name',) |
|
370 | exclude = ('name',) | |
369 |
|
371 | |||
370 | No newline at end of file |
|
372 |
@@ -1,737 +1,761 | |||||
1 |
|
1 | |||
2 | import ast |
|
2 | import ast | |
3 | import json |
|
3 | import json | |
4 | import numpy as np |
|
4 | import numpy as np | |
5 |
|
5 | |||
6 | from polymorphic import PolymorphicModel |
|
6 | from polymorphic import PolymorphicModel | |
7 |
|
7 | |||
8 | from django.db import models |
|
8 | from django.db import models | |
9 | from django.core.urlresolvers import reverse |
|
9 | from django.core.urlresolvers import reverse | |
10 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
10 | from django.core.validators import MinValueValidator, MaxValueValidator | |
11 |
|
11 | |||
12 | from apps.main.models import Configuration |
|
12 | from apps.main.models import Configuration | |
13 | from devices.rc import api |
|
13 | from devices.rc import api | |
14 | from .utils import RCFile |
|
14 | from .utils import RCFile | |
15 |
|
15 | |||
16 | # Create your models here. |
|
16 | # Create your models here. | |
17 |
|
17 | |||
18 | LINE_TYPES = ( |
|
18 | LINE_TYPES = ( | |
19 | ('none', 'Not used'), |
|
19 | ('none', 'Not used'), | |
20 | ('tr', 'Transmission/reception selector signal'), |
|
20 | ('tr', 'Transmission/reception selector signal'), | |
21 | ('tx', 'A modulating signal (Transmission pulse)'), |
|
21 | ('tx', 'A modulating signal (Transmission pulse)'), | |
22 | ('codes', 'BPSK modulating signal'), |
|
22 | ('codes', 'BPSK modulating signal'), | |
23 | ('windows', 'Sample window signal'), |
|
23 | ('windows', 'Sample window signal'), | |
24 | ('sync', 'Synchronizing signal'), |
|
24 | ('sync', 'Synchronizing signal'), | |
25 | ('flip', 'IPP related periodic signal'), |
|
25 | ('flip', 'IPP related periodic signal'), | |
26 | ('prog_pulses', 'Programmable pulse'), |
|
26 | ('prog_pulses', 'Programmable pulse'), | |
27 | ('mix', 'Mixed line'), |
|
27 | ('mix', 'Mixed line'), | |
28 | ) |
|
28 | ) | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | SAMPLING_REFS = ( |
|
31 | SAMPLING_REFS = ( | |
32 | ('none', 'No Reference'), |
|
32 | ('none', 'No Reference'), | |
33 | ('first_baud', 'Middle of the first baud'), |
|
33 | ('first_baud', 'Middle of the first baud'), | |
34 | ('sub_baud', 'Middle of the sub-baud') |
|
34 | ('sub_baud', 'Middle of the sub-baud') | |
35 | ) |
|
35 | ) | |
36 |
|
36 | |||
37 | DAT_CMDS = { |
|
37 | DAT_CMDS = { | |
38 | # Pulse Design commands |
|
38 | # Pulse Design commands | |
39 | 'DISABLE' : 0, # Disables pulse generation |
|
39 | 'DISABLE' : 0, # Disables pulse generation | |
40 | 'ENABLE' : 24, # Enables pulse generation |
|
40 | 'ENABLE' : 24, # Enables pulse generation | |
41 | 'DELAY_START' : 40, # Write delay status to memory |
|
41 | 'DELAY_START' : 40, # Write delay status to memory | |
42 | 'FLIP_START' : 48, # Write flip status to memory |
|
42 | 'FLIP_START' : 48, # Write flip status to memory | |
43 | 'SAMPLING_PERIOD' : 64, # Establish Sampling Period |
|
43 | 'SAMPLING_PERIOD' : 64, # Establish Sampling Period | |
44 | 'TX_ONE' : 72, # Output '0' in line TX |
|
44 | 'TX_ONE' : 72, # Output '0' in line TX | |
45 | 'TX_ZERO' : 88, # Output '0' in line TX |
|
45 | 'TX_ZERO' : 88, # Output '0' in line TX | |
46 | 'SW_ONE' : 104, # Output '0' in line SW |
|
46 | 'SW_ONE' : 104, # Output '0' in line SW | |
47 | 'SW_ZERO' : 112, # Output '1' in line SW |
|
47 | 'SW_ZERO' : 112, # Output '1' in line SW | |
48 | 'RESTART': 120, # Restarts CR8 Firmware |
|
48 | 'RESTART': 120, # Restarts CR8 Firmware | |
49 | 'CONTINUE' : 253, # Function Unknown |
|
49 | 'CONTINUE' : 253, # Function Unknown | |
50 | # Commands available to new controllers |
|
50 | # Commands available to new controllers | |
51 | # In Pulse Design Executable, the clock divisor code is written as 12 at the start, but it should be written as code 22(below) just before the final enable. |
|
51 | # In Pulse Design Executable, the clock divisor code is written as 12 at the start, but it should be written as code 22(below) just before the final enable. | |
52 | 'CLOCK_DIVISOR_INIT' : 12, # Specifies Clock Divisor. Legacy command, ignored in the actual .dat conversion |
|
52 | 'CLOCK_DIVISOR_INIT' : 12, # Specifies Clock Divisor. Legacy command, ignored in the actual .dat conversion | |
53 | 'CLOCK_DIVISOR_LAST' : 22, # Specifies Clock Divisor (default 60 if not included) syntax: 255,22 254,N-1. |
|
53 | 'CLOCK_DIVISOR_LAST' : 22, # Specifies Clock Divisor (default 60 if not included) syntax: 255,22 254,N-1. | |
54 | 'CLOCK_DIVIDER' : 8, |
|
54 | 'CLOCK_DIVIDER' : 8, | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | class RCConfiguration(Configuration): |
|
58 | class RCConfiguration(Configuration): | |
59 |
|
59 | |||
60 |
ipp = models.FloatField(verbose_name='Inter pulse period |
|
60 | ipp = models.FloatField(verbose_name='Inter pulse period [Km]', validators=[MinValueValidator(1), MaxValueValidator(9000)], default=300) | |
61 | ntx = models.PositiveIntegerField(verbose_name='Number of TX', validators=[MinValueValidator(1), MaxValueValidator(300)], default=1) |
|
61 | ntx = models.PositiveIntegerField(verbose_name='Number of TX', validators=[MinValueValidator(1), MaxValueValidator(300)], default=1) | |
62 |
clock_in = models.FloatField(verbose_name='Clock in |
|
62 | clock_in = models.FloatField(verbose_name='Clock in [MHz]', validators=[MinValueValidator(1), MaxValueValidator(80)], default=1) | |
63 | clock_divider = models.PositiveIntegerField(verbose_name='Clock divider', validators=[MinValueValidator(1), MaxValueValidator(256)], default=1) |
|
63 | clock_divider = models.PositiveIntegerField(verbose_name='Clock divider', validators=[MinValueValidator(1), MaxValueValidator(256)], default=1) | |
64 |
clock = models.FloatField(verbose_name='Clock Master |
|
64 | clock = models.FloatField(verbose_name='Clock Master [MHz]', blank=True, default=1) | |
65 |
time_before = models.PositiveIntegerField(verbose_name='Time before |
|
65 | time_before = models.PositiveIntegerField(verbose_name='Time before [μS]', default=12) | |
66 |
time_after = models.PositiveIntegerField(verbose_name='Time after |
|
66 | time_after = models.PositiveIntegerField(verbose_name='Time after [μS]', default=1) | |
67 | sync = models.PositiveIntegerField(verbose_name='Synchro delay', default=0) |
|
67 | sync = models.PositiveIntegerField(verbose_name='Synchro delay', default=0) | |
68 | sampling_reference = models.CharField(verbose_name='Sampling Reference', choices=SAMPLING_REFS, default='none', max_length=40) |
|
68 | sampling_reference = models.CharField(verbose_name='Sampling Reference', choices=SAMPLING_REFS, default='none', max_length=40) | |
69 | control_tx = models.BooleanField(verbose_name='Control Switch TX', default=False) |
|
69 | control_tx = models.BooleanField(verbose_name='Control Switch TX', default=False) | |
70 | control_sw = models.BooleanField(verbose_name='Control Switch SW', default=False) |
|
70 | control_sw = models.BooleanField(verbose_name='Control Switch SW', default=False) | |
|
71 | total_units = models.PositiveIntegerField(default=0) | |||
71 | mix = models.BooleanField(default=False) |
|
72 | mix = models.BooleanField(default=False) | |
72 |
|
73 | |||
73 | class Meta: |
|
74 | class Meta: | |
74 | db_table = 'rc_configurations' |
|
75 | db_table = 'rc_configurations' | |
75 |
|
76 | |||
76 |
|
77 | |||
77 | def __unicode__(self): |
|
78 | def __unicode__(self): | |
78 |
|
79 | |||
79 | if self.mix: |
|
80 | if self.mix: | |
80 | return u'[MIXED]: %s' % self.name |
|
81 | return u'[MIXED]: %s' % self.name | |
81 | else: |
|
82 | else: | |
82 | return u'[%s]: %s' % (self.device.name, self.name) |
|
83 | return u'[%s]: %s' % (self.device.name, self.name) | |
83 |
|
84 | |||
84 | def get_absolute_url_plot(self): |
|
85 | def get_absolute_url_plot(self): | |
85 | return reverse('url_plot_rc_pulses', args=[str(self.id)]) |
|
86 | return reverse('url_plot_rc_pulses', args=[str(self.id)]) | |
86 |
|
87 | |||
87 | def get_absolute_url_import(self): |
|
88 | def get_absolute_url_import(self): | |
88 | return reverse('url_import_rc_conf', args=[str(self.id)]) |
|
89 | return reverse('url_import_rc_conf', args=[str(self.id)]) | |
89 |
|
90 | |||
90 | @property |
|
91 | @property | |
|
92 | def ipp_unit(self): | |||
|
93 | ||||
|
94 | return '{} ({})'.format(self.ipp, int(self.ipp*self.km2unit)) | |||
|
95 | ||||
|
96 | @property | |||
91 | def us2unit(self): |
|
97 | def us2unit(self): | |
92 |
|
98 | |||
93 | return self.clock_in/self.clock_divider |
|
99 | return self.clock_in/self.clock_divider | |
94 |
|
100 | |||
95 | @property |
|
101 | @property | |
96 | def km2unit(self): |
|
102 | def km2unit(self): | |
97 |
|
103 | |||
98 | return 20./3*(self.clock_in/self.clock_divider) |
|
104 | return 20./3*(self.clock_in/self.clock_divider) | |
99 |
|
105 | |||
100 | def clone(self, **kwargs): |
|
106 | def clone(self, **kwargs): | |
101 |
|
107 | |||
102 | lines = self.get_lines() |
|
108 | lines = self.get_lines() | |
103 | self.pk = None |
|
109 | self.pk = None | |
104 | self.id = None |
|
110 | self.id = None | |
105 | for attr, value in kwargs.items(): |
|
111 | for attr, value in kwargs.items(): | |
106 | setattr(self, attr, value) |
|
112 | setattr(self, attr, value) | |
107 | self.save() |
|
113 | self.save() | |
108 |
|
114 | |||
109 | for line in lines: |
|
115 | for line in lines: | |
110 | line.clone(rc_configuration=self) |
|
116 | line.clone(rc_configuration=self) | |
111 |
|
117 | |||
112 | return self |
|
118 | return self | |
113 |
|
119 | |||
114 | def get_lines(self, **kwargs): |
|
120 | def get_lines(self, **kwargs): | |
115 | ''' |
|
121 | ''' | |
116 | Retrieve configuration lines |
|
122 | Retrieve configuration lines | |
117 | ''' |
|
123 | ''' | |
118 |
|
124 | |||
119 | return RCLine.objects.filter(rc_configuration=self.pk, **kwargs) |
|
125 | return RCLine.objects.filter(rc_configuration=self.pk, **kwargs) | |
120 |
|
126 | |||
121 |
|
127 | |||
122 | def clean_lines(self): |
|
128 | def clean_lines(self): | |
123 | ''' |
|
129 | ''' | |
124 | ''' |
|
130 | ''' | |
125 |
|
131 | |||
126 | empty_line = RCLineType.objects.get(name='none') |
|
132 | empty_line = RCLineType.objects.get(name='none') | |
127 |
|
133 | |||
128 | for line in self.get_lines(): |
|
134 | for line in self.get_lines(): | |
129 | line.line_type = empty_line |
|
135 | line.line_type = empty_line | |
130 | line.params = '{}' |
|
136 | line.params = '{}' | |
131 | line.save() |
|
137 | line.save() | |
132 |
|
138 | |||
133 | def parms_to_dict(self): |
|
139 | def parms_to_dict(self): | |
134 | ''' |
|
140 | ''' | |
135 | ''' |
|
141 | ''' | |
136 |
|
142 | |||
137 | ignored = ('parameters', 'type', 'polymorphic_ctype', 'configuration_ptr', |
|
143 | ignored = ('parameters', 'type', 'polymorphic_ctype', 'configuration_ptr', | |
138 | 'created_date', 'programmed_date') |
|
144 | 'created_date', 'programmed_date') | |
139 |
|
145 | |||
140 | data = {} |
|
146 | data = {} | |
141 | for field in self._meta.fields: |
|
147 | for field in self._meta.fields: | |
142 | if field.name in ignored: |
|
148 | if field.name in ignored: | |
143 | continue |
|
149 | continue | |
144 | data[field.name] = '{}'.format(field.value_from_object(self)) |
|
150 | data[field.name] = '{}'.format(field.value_from_object(self)) | |
145 |
|
151 | |||
146 | data['lines'] = [] |
|
152 | data['lines'] = [] | |
147 |
|
153 | |||
148 | for line in self.get_lines(): |
|
154 | for line in self.get_lines(): | |
149 | line_data = json.loads(line.params) |
|
155 | line_data = json.loads(line.params) | |
150 | if 'TX_ref' in line_data and line_data['TX_ref'] not in (0, '0'): |
|
156 | if 'TX_ref' in line_data and line_data['TX_ref'] not in (0, '0'): | |
151 | line_data['TX_ref'] = RCLine.objects.get(pk=line_data['TX_ref']).get_name() |
|
157 | line_data['TX_ref'] = RCLine.objects.get(pk=line_data['TX_ref']).get_name() | |
152 | if 'code' in line_data: |
|
158 | if 'code' in line_data: | |
153 | line_data['code'] = RCLineCode.objects.get(pk=line_data['code']).name |
|
159 | line_data['code'] = RCLineCode.objects.get(pk=line_data['code']).name | |
154 | line_data['type'] = line.line_type.name |
|
160 | line_data['type'] = line.line_type.name | |
155 | data['lines'].append(line_data) |
|
161 | data['lines'].append(line_data) | |
156 |
|
162 | |||
157 | data['delays'] = self.get_delays() |
|
163 | data['delays'] = self.get_delays() | |
158 | data['pulses'] = self.get_pulses() |
|
164 | data['pulses'] = self.get_pulses() | |
159 |
|
165 | |||
160 | return data |
|
166 | return data | |
161 |
|
167 | |||
162 | def dict_to_parms(self, data): |
|
168 | def dict_to_parms(self, data): | |
163 | ''' |
|
169 | ''' | |
164 | ''' |
|
170 | ''' | |
165 |
|
171 | |||
166 | self.name = data['name'] |
|
172 | self.name = data['name'] | |
167 | self.ipp = data['ipp'] |
|
173 | self.ipp = data['ipp'] | |
168 | self.ntx = data['ntx'] |
|
174 | self.ntx = data['ntx'] | |
169 | self.clock_in = data['clock_in'] |
|
175 | self.clock_in = data['clock_in'] | |
170 | self.clock_divider = data['clock_divider'] |
|
176 | self.clock_divider = data['clock_divider'] | |
171 | self.clock = data['clock'] |
|
177 | self.clock = data['clock'] | |
172 | self.time_before = data['time_before'] |
|
178 | self.time_before = data['time_before'] | |
173 | self.time_after = data['time_after'] |
|
179 | self.time_after = data['time_after'] | |
174 | self.sync = data['sync'] |
|
180 | self.sync = data['sync'] | |
175 | self.sampling_reference = data['sampling_reference'] |
|
181 | self.sampling_reference = data['sampling_reference'] | |
176 | self.clean_lines() |
|
182 | self.clean_lines() | |
177 |
|
183 | |||
178 | lines = [] |
|
184 | lines = [] | |
179 | positions = {'tx':0, 'tr':0} |
|
185 | positions = {'tx':0, 'tr':0} | |
180 |
|
186 | |||
181 | for i, line_data in enumerate(data['lines']): |
|
187 | for i, line_data in enumerate(data['lines']): | |
182 | line_type = RCLineType.objects.get(name=line_data.pop('type')) |
|
188 | line_type = RCLineType.objects.get(name=line_data.pop('type')) | |
183 | if line_type.name=='codes': |
|
189 | if line_type.name=='codes': | |
184 | code = RCLineCode.objects.get(name=line_data['code']) |
|
190 | code = RCLineCode.objects.get(name=line_data['code']) | |
185 | line_data['code'] = code.pk |
|
191 | line_data['code'] = code.pk | |
186 | line = RCLine.objects.filter(rc_configuration=self, channel=i) |
|
192 | line = RCLine.objects.filter(rc_configuration=self, channel=i) | |
187 | if line: |
|
193 | if line: | |
188 | line = line[0] |
|
194 | line = line[0] | |
189 | line.line_type = line_type |
|
195 | line.line_type = line_type | |
190 | line.params = json.dumps(line_data) |
|
196 | line.params = json.dumps(line_data) | |
191 | else: |
|
197 | else: | |
192 | line = RCLine(rc_configuration=self, line_type=line_type, |
|
198 | line = RCLine(rc_configuration=self, line_type=line_type, | |
193 | params=json.dumps(line_data), |
|
199 | params=json.dumps(line_data), | |
194 | channel=i) |
|
200 | channel=i) | |
195 |
|
201 | |||
196 | if line_type.name=='tx': |
|
202 | if line_type.name=='tx': | |
197 | line.position = positions['tx'] |
|
203 | line.position = positions['tx'] | |
198 | positions['tx'] += 1 |
|
204 | positions['tx'] += 1 | |
199 |
|
205 | |||
200 | if line_type.name=='tr': |
|
206 | if line_type.name=='tr': | |
201 | line.position = positions['tr'] |
|
207 | line.position = positions['tr'] | |
202 | positions['tr'] += 1 |
|
208 | positions['tr'] += 1 | |
203 |
|
209 | |||
204 | line.save() |
|
210 | line.save() | |
205 | lines.append(line) |
|
211 | lines.append(line) | |
206 |
|
212 | |||
207 | for line, line_data in zip(lines, data['lines']): |
|
213 | for line, line_data in zip(lines, data['lines']): | |
208 | if 'TX_ref' in line_data: |
|
214 | if 'TX_ref' in line_data: | |
209 | params = json.loads(line.params) |
|
215 | params = json.loads(line.params) | |
210 | if line_data['TX_ref'] in (0, '0'): |
|
216 | if line_data['TX_ref'] in (0, '0'): | |
211 | params['TX_ref'] = '0' |
|
217 | params['TX_ref'] = '0' | |
212 | else: |
|
218 | else: | |
213 | params['TX_ref'] = [l.pk for l in lines if l.line_type.name=='tx' and l.get_name()==line_data['TX_ref']][0] |
|
219 | params['TX_ref'] = [l.pk for l in lines if l.line_type.name=='tx' and l.get_name()==line_data['TX_ref']][0] | |
214 | line.params = json.dumps(params) |
|
220 | line.params = json.dumps(params) | |
215 | line.save() |
|
221 | line.save() | |
216 |
|
222 | |||
217 |
|
223 | |||
218 | def get_delays(self): |
|
224 | def get_delays(self): | |
219 |
|
225 | |||
220 | pulses = [line.pulses_as_points() for line in self.get_lines()] |
|
226 | pulses = [line.pulses_as_points() for line in self.get_lines()] | |
221 | points = [tup for tups in pulses for tup in tups] |
|
227 | points = [tup for tups in pulses for tup in tups] | |
222 | points = set([x for tup in points for x in tup]) |
|
228 | points = set([x for tup in points for x in tup]) | |
223 | points = list(points) |
|
229 | points = list(points) | |
224 | points.sort() |
|
230 | points.sort() | |
225 |
|
231 | |||
226 | if points[0]<>0: |
|
232 | if points[0]<>0: | |
227 | points.insert(0, 0) |
|
233 | points.insert(0, 0) | |
228 |
|
234 | |||
229 | return [points[i+1]-points[i] for i in range(len(points)-1)] |
|
235 | return [points[i+1]-points[i] for i in range(len(points)-1)] | |
230 |
|
236 | |||
231 |
|
237 | |||
232 | def get_pulses(self, binary=True): |
|
238 | def get_pulses(self, binary=True): | |
233 |
|
239 | |||
234 | pulses = [line.pulses_as_points() for line in self.get_lines()] |
|
240 | pulses = [line.pulses_as_points() for line in self.get_lines()] | |
235 | points = [tup for tups in pulses for tup in tups] |
|
241 | points = [tup for tups in pulses for tup in tups] | |
236 | points = set([x for tup in points for x in tup]) |
|
242 | points = set([x for tup in points for x in tup]) | |
237 | points = list(points) |
|
243 | points = list(points) | |
238 | points.sort() |
|
244 | points.sort() | |
239 |
|
245 | |||
240 | line_points = [line.pulses_as_points() for line in self.get_lines()] |
|
246 | line_points = [line.pulses_as_points() for line in self.get_lines()] | |
241 | line_points = [[(x, x+y) for x,y in tups] for tups in line_points] |
|
247 | line_points = [[(x, x+y) for x,y in tups] for tups in line_points] | |
242 | line_points = [[t for x in tups for t in x] for tups in line_points] |
|
248 | line_points = [[t for x in tups for t in x] for tups in line_points] | |
243 | states = [[1 if x in tups else 0 for tups in line_points] for x in points] |
|
249 | states = [[1 if x in tups else 0 for tups in line_points] for x in points] | |
244 |
|
250 | |||
245 | if binary: |
|
251 | if binary: | |
246 | states.reverse() |
|
252 | states.reverse() | |
247 | states = [int(''.join([str(x) for x in flips]), 2) for flips in states] |
|
253 | states = [int(''.join([str(x) for x in flips]), 2) for flips in states] | |
248 |
|
254 | |||
249 | return states[:-1] |
|
255 | return states[:-1] | |
250 |
|
256 | |||
251 | def add_cmd(self, cmd): |
|
257 | def add_cmd(self, cmd): | |
252 |
|
258 | |||
253 | if cmd in DAT_CMDS: |
|
259 | if cmd in DAT_CMDS: | |
254 | return (255, DAT_CMDS[cmd]) |
|
260 | return (255, DAT_CMDS[cmd]) | |
255 |
|
261 | |||
256 | def add_data(self, value): |
|
262 | def add_data(self, value): | |
257 |
|
263 | |||
258 | return (254, value-1) |
|
264 | return (254, value-1) | |
259 |
|
265 | |||
260 | def parms_to_binary(self): |
|
266 | def parms_to_binary(self): | |
261 | ''' |
|
267 | ''' | |
262 | Create "dat" stream to be send to CR |
|
268 | Create "dat" stream to be send to CR | |
263 | ''' |
|
269 | ''' | |
264 |
|
270 | |||
265 | data = [] |
|
271 | data = [] | |
266 | # create header |
|
272 | # create header | |
267 | data.append(self.add_cmd('DISABLE')) |
|
273 | data.append(self.add_cmd('DISABLE')) | |
268 | data.append(self.add_cmd('CONTINUE')) |
|
274 | data.append(self.add_cmd('CONTINUE')) | |
269 | data.append(self.add_cmd('RESTART')) |
|
275 | data.append(self.add_cmd('RESTART')) | |
270 |
|
276 | |||
271 | if self.control_sw: |
|
277 | if self.control_sw: | |
272 | data.append(self.add_cmd('SW_ONE')) |
|
278 | data.append(self.add_cmd('SW_ONE')) | |
273 | else: |
|
279 | else: | |
274 | data.append(self.add_cmd('SW_ZERO')) |
|
280 | data.append(self.add_cmd('SW_ZERO')) | |
275 |
|
281 | |||
276 | if self.control_tx: |
|
282 | if self.control_tx: | |
277 | data.append(self.add_cmd('TX_ONE')) |
|
283 | data.append(self.add_cmd('TX_ONE')) | |
278 | else: |
|
284 | else: | |
279 | data.append(self.add_cmd('TX_ZERO')) |
|
285 | data.append(self.add_cmd('TX_ZERO')) | |
280 |
|
286 | |||
281 | # write divider |
|
287 | # write divider | |
282 | data.append(self.add_cmd('CLOCK_DIVIDER')) |
|
288 | data.append(self.add_cmd('CLOCK_DIVIDER')) | |
283 | data.append(self.add_data(self.clock_divider)) |
|
289 | data.append(self.add_data(self.clock_divider)) | |
284 |
|
290 | |||
285 | # write delays |
|
291 | # write delays | |
286 | data.append(self.add_cmd('DELAY_START')) |
|
292 | data.append(self.add_cmd('DELAY_START')) | |
287 | # first delay is always zero |
|
293 | # first delay is always zero | |
288 | data.append(self.add_data(1)) |
|
294 | data.append(self.add_data(1)) | |
289 |
|
295 | |||
290 | delays = self.get_delays() |
|
296 | delays = self.get_delays() | |
291 |
|
297 | |||
292 | for delay in delays: |
|
298 | for delay in delays: | |
293 | while delay>252: |
|
299 | while delay>252: | |
294 | data.append(self.add_data(253)) |
|
300 | data.append(self.add_data(253)) | |
295 | delay -= 253 |
|
301 | delay -= 253 | |
296 | data.append(self.add_data(delay)) |
|
302 | data.append(self.add_data(delay)) | |
297 |
|
303 | |||
298 | # write flips |
|
304 | # write flips | |
299 | data.append(self.add_cmd('FLIP_START')) |
|
305 | data.append(self.add_cmd('FLIP_START')) | |
300 |
|
306 | |||
301 | states = self.get_pulses(binary=False) |
|
307 | states = self.get_pulses(binary=False) | |
302 |
|
308 | |||
303 | for flips, delay in zip(states, delays): |
|
309 | for flips, delay in zip(states, delays): | |
304 | flips.reverse() |
|
310 | flips.reverse() | |
305 | flip = int(''.join([str(x) for x in flips]), 2) |
|
311 | flip = int(''.join([str(x) for x in flips]), 2) | |
306 | data.append(self.add_data(flip+1)) |
|
312 | data.append(self.add_data(flip+1)) | |
307 | while delay>252: |
|
313 | while delay>252: | |
308 | data.append(self.add_data(1)) |
|
314 | data.append(self.add_data(1)) | |
309 | delay -= 253 |
|
315 | delay -= 253 | |
310 |
|
316 | |||
311 | # write sampling period |
|
317 | # write sampling period | |
312 | data.append(self.add_cmd('SAMPLING_PERIOD')) |
|
318 | data.append(self.add_cmd('SAMPLING_PERIOD')) | |
313 | wins = self.get_lines(line_type__name='windows') |
|
319 | wins = self.get_lines(line_type__name='windows') | |
314 | if wins: |
|
320 | if wins: | |
315 | win_params = json.loads(wins[0].params)['params'] |
|
321 | win_params = json.loads(wins[0].params)['params'] | |
316 | if win_params: |
|
322 | if win_params: | |
317 | dh = int(win_params[0]['resolution']*self.km2unit) |
|
323 | dh = int(win_params[0]['resolution']*self.km2unit) | |
318 | else: |
|
324 | else: | |
319 | dh = 1 |
|
325 | dh = 1 | |
320 | else: |
|
326 | else: | |
321 | dh = 1 |
|
327 | dh = 1 | |
322 | data.append(self.add_data(dh)) |
|
328 | data.append(self.add_data(dh)) | |
323 |
|
329 | |||
324 | # write enable |
|
330 | # write enable | |
325 | data.append(self.add_cmd('ENABLE')) |
|
331 | data.append(self.add_cmd('ENABLE')) | |
326 |
|
332 | |||
327 | return '\n'.join(['{}'.format(x) for tup in data for x in tup]) |
|
333 | return '\n'.join(['{}'.format(x) for tup in data for x in tup]) | |
328 |
|
334 | |||
329 | def update_from_file(self, filename): |
|
335 | def update_from_file(self, filename): | |
330 | ''' |
|
336 | ''' | |
331 | Update instance from file |
|
337 | Update instance from file | |
332 | ''' |
|
338 | ''' | |
333 |
|
339 | |||
334 | f = RCFile(filename) |
|
340 | f = RCFile(filename) | |
335 | self.dict_to_parms(f.data) |
|
341 | self.dict_to_parms(f.data) | |
336 | self.update_pulses() |
|
342 | self.update_pulses() | |
337 | self.save() |
|
|||
338 |
|
343 | |||
339 | def update_pulses(self): |
|
344 | def update_pulses(self): | |
340 |
|
345 | |||
341 | for line in self.get_lines(): |
|
346 | for line in self.get_lines(): | |
342 | line.update_pulses() |
|
347 | line.update_pulses() | |
343 |
|
348 | |||
344 | def plot_pulses(self): |
|
349 | def plot_pulses(self): | |
345 |
|
350 | |||
346 | import matplotlib.pyplot as plt |
|
351 | import matplotlib.pyplot as plt | |
347 | from bokeh.resources import CDN |
|
352 | from bokeh.resources import CDN | |
348 | from bokeh.embed import components |
|
353 | from bokeh.embed import components | |
349 | from bokeh.mpl import to_bokeh |
|
354 | from bokeh.mpl import to_bokeh | |
350 | from bokeh.models.tools import WheelZoomTool, ResetTool, PanTool, PreviewSaveTool |
|
355 | from bokeh.models.tools import WheelZoomTool, ResetTool, PanTool, PreviewSaveTool | |
351 |
|
356 | |||
352 | lines = self.get_lines() |
|
357 | lines = self.get_lines() | |
353 |
|
||||
354 | max_value = self.ipp*self.km2unit*self.ntx |
|
|||
355 |
|
358 | |||
356 | N = len(lines) |
|
359 | N = len(lines) | |
357 | fig = plt.figure(figsize=(10, 2+N*0.5)) |
|
360 | fig = plt.figure(figsize=(10, 2+N*0.5)) | |
358 | ax = fig.add_subplot(111) |
|
361 | ax = fig.add_subplot(111) | |
359 | labels = [] |
|
362 | labels = [] | |
360 |
|
363 | |||
361 | for i, line in enumerate(lines): |
|
364 | for i, line in enumerate(lines): | |
362 | labels.append(line.get_name()) |
|
365 | labels.append(line.get_name()) | |
363 |
l = ax.plot((0, |
|
366 | l = ax.plot((0, self.total_units),(N-i-1, N-i-1)) | |
364 | points = [(tup[0], tup[1]-tup[0]) for tup in line.pulses_as_points() if tup<>(0,0)] |
|
367 | points = [(tup[0], tup[1]-tup[0]) for tup in line.pulses_as_points() if tup<>(0,0)] | |
365 | ax.broken_barh(points, (N-i-1, 0.5), |
|
368 | ax.broken_barh(points, (N-i-1, 0.5), | |
366 | edgecolor=l[0].get_color(), facecolor='none') |
|
369 | edgecolor=l[0].get_color(), facecolor='none') | |
367 |
|
370 | |||
368 | labels.reverse() |
|
371 | labels.reverse() | |
369 | ax.set_yticklabels(labels) |
|
372 | ax.set_yticklabels(labels) | |
370 | plot = to_bokeh(fig, use_pandas=False) |
|
373 | plot = to_bokeh(fig, use_pandas=False) | |
371 | plot.tools = [PanTool(dimensions=['width']), WheelZoomTool(dimensions=['width']), ResetTool(), PreviewSaveTool()] |
|
374 | plot.tools = [PanTool(dimensions=['width']), WheelZoomTool(dimensions=['width']), ResetTool(), PreviewSaveTool()] | |
372 |
|
375 | |||
373 | return components(plot, CDN) |
|
376 | return components(plot, CDN) | |
374 |
|
377 | |||
375 | def status_device(self): |
|
378 | def status_device(self): | |
376 |
|
379 | |||
377 | return 0 |
|
380 | return 0 | |
378 |
|
381 | |||
379 | def stop_device(self): |
|
382 | def stop_device(self): | |
380 |
|
383 | |||
381 | answer = api.disable(ip = self.device.ip_address, |
|
384 | answer = api.disable(ip = self.device.ip_address, | |
382 | port = self.device.port_address) |
|
385 | port = self.device.port_address) | |
383 |
|
386 | |||
384 | if answer[0] != "1": |
|
387 | if answer[0] != "1": | |
385 | self.message = answer[0:] |
|
388 | self.message = answer[0:] | |
386 | return 0 |
|
389 | return 0 | |
387 |
|
390 | |||
388 | self.message = answer[2:] |
|
391 | self.message = answer[2:] | |
389 | return 1 |
|
392 | return 1 | |
390 |
|
393 | |||
391 | def start_device(self): |
|
394 | def start_device(self): | |
392 |
|
395 | |||
393 | answer = api.enable(ip = self.device.ip_address, |
|
396 | answer = api.enable(ip = self.device.ip_address, | |
394 | port = self.device.port_address) |
|
397 | port = self.device.port_address) | |
395 |
|
398 | |||
396 | if answer[0] != "1": |
|
399 | if answer[0] != "1": | |
397 | self.message = answer[0:] |
|
400 | self.message = answer[0:] | |
398 | return 0 |
|
401 | return 0 | |
399 |
|
402 | |||
400 | self.message = answer[2:] |
|
403 | self.message = answer[2:] | |
401 | return 1 |
|
404 | return 1 | |
402 |
|
405 | |||
403 | def write_device(self): |
|
406 | def write_device(self): | |
404 | answer = api.write_config(ip = self.device.ip_address, |
|
407 | answer = api.write_config(ip = self.device.ip_address, | |
405 | port = self.device.port_address, |
|
408 | port = self.device.port_address, | |
406 | parms = self.parms_to_dict()) |
|
409 | parms = self.parms_to_dict()) | |
407 |
|
410 | |||
408 | if answer[0] != "1": |
|
411 | if answer[0] != "1": | |
409 | self.message = answer[0:] |
|
412 | self.message = answer[0:] | |
410 | return 0 |
|
413 | return 0 | |
411 |
|
414 | |||
412 | self.message = answer[2:] |
|
415 | self.message = answer[2:] | |
413 | return 1 |
|
416 | return 1 | |
414 |
|
417 | |||
415 |
|
418 | |||
416 | class RCLineCode(models.Model): |
|
419 | class RCLineCode(models.Model): | |
417 |
|
420 | |||
418 | name = models.CharField(max_length=40) |
|
421 | name = models.CharField(max_length=40) | |
419 | bits_per_code = models.PositiveIntegerField(default=0) |
|
422 | bits_per_code = models.PositiveIntegerField(default=0) | |
420 | number_of_codes = models.PositiveIntegerField(default=0) |
|
423 | number_of_codes = models.PositiveIntegerField(default=0) | |
421 | codes = models.TextField(blank=True, null=True) |
|
424 | codes = models.TextField(blank=True, null=True) | |
422 |
|
425 | |||
423 | class Meta: |
|
426 | class Meta: | |
424 | db_table = 'rc_line_codes' |
|
427 | db_table = 'rc_line_codes' | |
425 | ordering = ('name',) |
|
428 | ordering = ('name',) | |
426 |
|
429 | |||
427 | def __unicode__(self): |
|
430 | def __unicode__(self): | |
428 | return u'%s' % self.name |
|
431 | return u'%s' % self.name | |
429 |
|
432 | |||
430 |
|
433 | |||
431 | class RCLineType(models.Model): |
|
434 | class RCLineType(models.Model): | |
432 |
|
435 | |||
433 | name = models.CharField(choices=LINE_TYPES, max_length=40) |
|
436 | name = models.CharField(choices=LINE_TYPES, max_length=40) | |
434 | description = models.TextField(blank=True, null=True) |
|
437 | description = models.TextField(blank=True, null=True) | |
435 | params = models.TextField(default='[]') |
|
438 | params = models.TextField(default='[]') | |
436 |
|
439 | |||
437 | class Meta: |
|
440 | class Meta: | |
438 | db_table = 'rc_line_types' |
|
441 | db_table = 'rc_line_types' | |
439 |
|
442 | |||
440 | def __unicode__(self): |
|
443 | def __unicode__(self): | |
441 | return u'%s - %s' % (self.name.upper(), self.get_name_display()) |
|
444 | return u'%s - %s' % (self.name.upper(), self.get_name_display()) | |
442 |
|
445 | |||
443 |
|
446 | |||
444 | class RCLine(models.Model): |
|
447 | class RCLine(models.Model): | |
445 |
|
448 | |||
446 | rc_configuration = models.ForeignKey(RCConfiguration, on_delete=models.CASCADE) |
|
449 | rc_configuration = models.ForeignKey(RCConfiguration, on_delete=models.CASCADE) | |
447 | line_type = models.ForeignKey(RCLineType) |
|
450 | line_type = models.ForeignKey(RCLineType) | |
448 | channel = models.PositiveIntegerField(default=0) |
|
451 | channel = models.PositiveIntegerField(default=0) | |
449 | position = models.PositiveIntegerField(default=0) |
|
452 | position = models.PositiveIntegerField(default=0) | |
450 | params = models.TextField(default='{}') |
|
453 | params = models.TextField(default='{}') | |
451 | pulses = models.TextField(default='') |
|
454 | pulses = models.TextField(default='') | |
452 |
|
455 | |||
453 | class Meta: |
|
456 | class Meta: | |
454 | db_table = 'rc_lines' |
|
457 | db_table = 'rc_lines' | |
455 | ordering = ['channel'] |
|
458 | ordering = ['channel'] | |
456 |
|
459 | |||
457 | def __unicode__(self): |
|
460 | def __unicode__(self): | |
458 | if self.rc_configuration: |
|
461 | if self.rc_configuration: | |
459 | return u'%s - %s' % (self.rc_configuration, self.get_name()) |
|
462 | return u'%s - %s' % (self.rc_configuration, self.get_name()) | |
460 |
|
463 | |||
461 | def clone(self, **kwargs): |
|
464 | def clone(self, **kwargs): | |
462 |
|
465 | |||
463 | self.pk = None |
|
466 | self.pk = None | |
464 |
|
467 | |||
465 | for attr, value in kwargs.items(): |
|
468 | for attr, value in kwargs.items(): | |
466 | setattr(self, attr, value) |
|
469 | setattr(self, attr, value) | |
467 |
|
470 | |||
468 | self.save() |
|
471 | self.save() | |
469 |
|
472 | |||
470 | return self |
|
473 | return self | |
471 |
|
474 | |||
472 | def get_name(self): |
|
475 | def get_name(self): | |
473 |
|
476 | |||
474 | chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
477 | chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
475 | s = '' |
|
478 | s = '' | |
476 |
|
479 | |||
477 | if self.line_type.name in ('tx',): |
|
480 | if self.line_type.name in ('tx',): | |
478 | s = chars[self.position] |
|
481 | s = chars[self.position] | |
479 | elif self.line_type.name in ('codes', 'windows', 'tr'): |
|
482 | elif self.line_type.name in ('codes', 'windows', 'tr'): | |
480 | if 'TX_ref' in json.loads(self.params): |
|
483 | if 'TX_ref' in json.loads(self.params): | |
481 | pk = json.loads(self.params)['TX_ref'] |
|
484 | pk = json.loads(self.params)['TX_ref'] | |
482 | if pk in (0, '0'): |
|
485 | if pk in (0, '0'): | |
483 | s = ','.join(chars[l.position] for l in self.rc_configuration.get_lines(line_type__name='tx')) |
|
486 | s = ','.join(chars[l.position] for l in self.rc_configuration.get_lines(line_type__name='tx')) | |
484 | else: |
|
487 | else: | |
485 | ref = RCLine.objects.get(pk=pk) |
|
488 | ref = RCLine.objects.get(pk=pk) | |
486 | s = chars[ref.position] |
|
489 | s = chars[ref.position] | |
487 |
|
490 | |||
488 | if s: |
|
491 | if s: | |
489 | return '{}({}) {}'.format(self.line_type.name.upper(), s, self.channel) |
|
492 | return '{}({}) {}'.format(self.line_type.name.upper(), s, self.channel) | |
490 | else: |
|
493 | else: | |
491 | return '{} {}'.format(self.line_type.name.upper(), self.channel) |
|
494 | return '{} {}'.format(self.line_type.name.upper(), self.channel) | |
492 |
|
495 | |||
493 | def get_lines(self, **kwargs): |
|
496 | def get_lines(self, **kwargs): | |
494 |
|
497 | |||
495 | return RCLine.objects.filter(rc_configuration=self.rc_configuration, **kwargs) |
|
498 | return RCLine.objects.filter(rc_configuration=self.rc_configuration, **kwargs) | |
496 |
|
499 | |||
497 | def pulses_as_array(self): |
|
500 | def pulses_as_array(self): | |
498 |
|
501 | |||
499 |
y = np.zeros(self.rc_configuration. |
|
502 | y = np.zeros(self.rc_configuration.total_units) | |
500 |
|
503 | |||
501 | for tup in ast.literal_eval(self.pulses): |
|
504 | for tup in ast.literal_eval(self.pulses): | |
502 | y[tup[0]:tup[1]] = 1 |
|
505 | y[tup[0]:tup[1]] = 1 | |
503 |
|
506 | |||
504 | return y.astype(np.int8) |
|
507 | return y.astype(np.int8) | |
505 |
|
508 | |||
506 | def pulses_as_points(self): |
|
509 | def pulses_as_points(self): | |
507 |
|
510 | |||
508 | return ast.literal_eval(self.pulses) |
|
511 | return ast.literal_eval(self.pulses) | |
509 |
|
512 | |||
510 | def get_win_ref(self, params, tx_id, km2unit): |
|
513 | def get_win_ref(self, params, tx_id, km2unit): | |
511 |
|
514 | |||
512 | ref = self.rc_configuration.sampling_reference |
|
515 | ref = self.rc_configuration.sampling_reference | |
513 | codes = [line for line in self.get_lines(line_type__name='codes') if int(json.loads(line.params)['TX_ref'])==int(tx_id)] |
|
516 | codes = [line for line in self.get_lines(line_type__name='codes') if int(json.loads(line.params)['TX_ref'])==int(tx_id)] | |
514 |
|
517 | |||
515 | if codes: |
|
518 | if codes: | |
516 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit/len(json.loads(codes[0].params)['codes'][0]) |
|
519 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit/len(json.loads(codes[0].params)['codes'][0]) | |
517 | else: |
|
520 | else: | |
518 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit |
|
521 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit | |
519 |
|
522 | |||
520 | if ref=='first_baud': |
|
523 | if ref=='first_baud': | |
521 | return int(1 + (tx_width + 1)/2 + params['first_height']*km2unit - params['resolution']*km2unit) |
|
524 | return int(1 + (tx_width + 1)/2 + params['first_height']*km2unit - params['resolution']*km2unit) | |
522 | elif ref=='sub_baud': |
|
525 | elif ref=='sub_baud': | |
523 | return int(1 + params['first_height']*km2unit - params['resolution']*km2unit/2) |
|
526 | return int(1 + params['first_height']*km2unit - params['resolution']*km2unit/2) | |
524 | else: |
|
527 | else: | |
525 | return 0 |
|
528 | return 0 | |
526 |
|
529 | |||
527 | def update_pulses(self): |
|
530 | def update_pulses(self): | |
528 | ''' |
|
531 | ''' | |
529 | Update pulses field |
|
532 | Update pulses field | |
530 | ''' |
|
533 | ''' | |
531 |
|
534 | |||
532 | km2unit = self.rc_configuration.km2unit |
|
535 | km2unit = self.rc_configuration.km2unit | |
533 | us2unit = self.rc_configuration.us2unit |
|
536 | us2unit = self.rc_configuration.us2unit | |
534 | ipp = self.rc_configuration.ipp |
|
537 | ipp = self.rc_configuration.ipp | |
535 | ntx = self.rc_configuration.ntx |
|
538 | ntx = self.rc_configuration.ntx | |
536 | ipp_u = int(ipp*km2unit) |
|
539 | ipp_u = int(ipp*km2unit) | |
537 |
|
540 | total = ipp_u*ntx | ||
538 | y = [] |
|
541 | y = [] | |
539 |
|
542 | |||
540 | if self.line_type.name=='tr': |
|
543 | if self.line_type.name=='tr': | |
541 | tr_params = json.loads(self.params) |
|
544 | tr_params = json.loads(self.params) | |
542 |
|
545 | |||
543 | if tr_params['TX_ref'] in ('0', 0): |
|
546 | if tr_params['TX_ref'] in ('0', 0): | |
544 | txs = self.get_lines(line_type__name='tx') |
|
547 | txs = self.get_lines(line_type__name='tx') | |
545 | else: |
|
548 | else: | |
546 | txs = [RCLine.objects.filter(pk=tr_params['TX_ref'])] |
|
549 | txs = [RCLine.objects.filter(pk=tr_params['TX_ref'])] | |
547 |
|
550 | |||
548 | for tx in txs: |
|
551 | for tx in txs: | |
549 | params = json.loads(tx.params) |
|
552 | params = json.loads(tx.params) | |
550 | if float(params['pulse_width'])==0: |
|
553 | if float(params['pulse_width'])==0: | |
551 | continue |
|
554 | continue | |
552 | delays = [float(d)*km2unit for d in params['delays'].split(',') if d] |
|
555 | delays = [float(d)*km2unit for d in params['delays'].split(',') if d] | |
553 | width = float(params['pulse_width'])*km2unit+int(self.rc_configuration.time_before*us2unit) |
|
556 | width = float(params['pulse_width'])*km2unit+int(self.rc_configuration.time_before*us2unit) | |
554 | before = 0 |
|
557 | before = 0 | |
555 | after = int(self.rc_configuration.time_after*us2unit) |
|
558 | after = int(self.rc_configuration.time_after*us2unit) | |
556 |
|
559 | |||
557 | y_tx = self.points(ntx, ipp_u, width, |
|
560 | y_tx = self.points(ntx, ipp_u, width, | |
558 | delay=delays, |
|
561 | delay=delays, | |
559 | before=before, |
|
562 | before=before, | |
560 | after=after, |
|
563 | after=after, | |
561 | sync=self.rc_configuration.sync) |
|
564 | sync=self.rc_configuration.sync) | |
562 |
|
565 | |||
563 | ranges = params['range'].split(',') |
|
566 | ranges = params['range'].split(',') | |
564 |
|
567 | |||
565 | if len(ranges)>0 and ranges[0]<>'0': |
|
568 | if len(ranges)>0 and ranges[0]<>'0': | |
566 | y_tx = self.mask_ranges(y_tx, ranges) |
|
569 | y_tx = self.mask_ranges(y_tx, ranges) | |
567 |
|
570 | |||
568 | tr_ranges = tr_params['range'].split(',') |
|
571 | tr_ranges = tr_params['range'].split(',') | |
569 |
|
572 | |||
570 | if len(tr_ranges)>0 and tr_ranges[0]<>'0': |
|
573 | if len(tr_ranges)>0 and tr_ranges[0]<>'0': | |
571 | y_tx = self.mask_ranges(y_tx, tr_ranges) |
|
574 | y_tx = self.mask_ranges(y_tx, tr_ranges) | |
572 |
|
575 | |||
573 | y.extend(y_tx) |
|
576 | y.extend(y_tx) | |
574 |
|
577 | |||
575 | elif self.line_type.name=='tx': |
|
578 | elif self.line_type.name=='tx': | |
576 | params = json.loads(self.params) |
|
579 | params = json.loads(self.params) | |
577 | delays = [float(d)*km2unit for d in params['delays'].split(',') if d] |
|
580 | delays = [float(d)*km2unit for d in params['delays'].split(',') if d] | |
578 | width = float(params['pulse_width'])*km2unit |
|
581 | width = float(params['pulse_width'])*km2unit | |
579 |
|
582 | |||
580 | if width>0: |
|
583 | if width>0: | |
581 | before = int(self.rc_configuration.time_before*us2unit) |
|
584 | before = int(self.rc_configuration.time_before*us2unit) | |
582 | after = 0 |
|
585 | after = 0 | |
583 |
|
586 | |||
584 | y = self.points(ntx, ipp_u, width, |
|
587 | y = self.points(ntx, ipp_u, width, | |
585 | delay=delays, |
|
588 | delay=delays, | |
586 | before=before, |
|
589 | before=before, | |
587 | after=after, |
|
590 | after=after, | |
588 | sync=self.rc_configuration.sync) |
|
591 | sync=self.rc_configuration.sync) | |
589 |
|
592 | |||
590 | ranges = params['range'].split(',') |
|
593 | ranges = params['range'].split(',') | |
591 |
|
594 | |||
592 | if len(ranges)>0 and ranges[0]<>'0': |
|
595 | if len(ranges)>0 and ranges[0]<>'0': | |
593 | y = self.mask_ranges(y, ranges) |
|
596 | y = self.mask_ranges(y, ranges) | |
594 |
|
597 | |||
595 | elif self.line_type.name=='flip': |
|
598 | elif self.line_type.name=='flip': | |
596 | n = float(json.loads(self.params)['number_of_flips']) |
|
599 | n = float(json.loads(self.params)['number_of_flips']) | |
597 | width = n*ipp*km2unit |
|
600 | width = n*ipp*km2unit | |
598 | y = self.points(int((ntx+1)/(2*n)), ipp_u*n*2, width) |
|
601 | y = self.points(int((ntx+1)/(2*n)), ipp_u*n*2, width) | |
599 |
|
602 | |||
600 | elif self.line_type.name=='codes': |
|
603 | elif self.line_type.name=='codes': | |
601 | params = json.loads(self.params) |
|
604 | params = json.loads(self.params) | |
602 | tx = RCLine.objects.get(pk=params['TX_ref']) |
|
605 | tx = RCLine.objects.get(pk=params['TX_ref']) | |
603 | tx_params = json.loads(tx.params) |
|
606 | tx_params = json.loads(tx.params) | |
604 | delays = [float(d)*km2unit for d in tx_params['delays'].split(',') if d] |
|
607 | delays = [float(d)*km2unit for d in tx_params['delays'].split(',') if d] | |
605 | f = int(float(tx_params['pulse_width'])*km2unit)/len(params['codes'][0]) |
|
608 | f = int(float(tx_params['pulse_width'])*km2unit)/len(params['codes'][0]) | |
606 | codes = [(np.fromstring(''.join([s*f for s in code]), dtype=np.uint8)-48).astype(np.int8) for code in params['codes']] |
|
609 | codes = [(np.fromstring(''.join([s*f for s in code]), dtype=np.uint8)-48).astype(np.int8) for code in params['codes']] | |
607 | codes = [self.array_to_points(code) for code in codes] |
|
610 | codes = [self.array_to_points(code) for code in codes] | |
608 | n = len(codes) |
|
611 | n = len(codes) | |
609 |
|
612 | |||
610 | for i, tup in enumerate(tx.pulses_as_points()): |
|
613 | for i, tup in enumerate(tx.pulses_as_points()): | |
611 | code = codes[i%n] |
|
614 | code = codes[i%n] | |
612 | y.extend([(c[0]+tup[0], c[1]+tup[0]) for c in code]) |
|
615 | y.extend([(c[0]+tup[0], c[1]+tup[0]) for c in code]) | |
613 |
|
616 | |||
614 | ranges = tx_params['range'].split(',') |
|
617 | ranges = tx_params['range'].split(',') | |
615 | if len(ranges)>0 and ranges[0]<>'0': |
|
618 | if len(ranges)>0 and ranges[0]<>'0': | |
616 | y = self.mask_ranges(y, ranges) |
|
619 | y = self.mask_ranges(y, ranges) | |
617 |
|
620 | |||
618 | elif self.line_type.name=='sync': |
|
621 | elif self.line_type.name=='sync': | |
619 | params = json.loads(self.params) |
|
622 | params = json.loads(self.params) | |
620 | n = ipp_u*ntx |
|
623 | n = ipp_u*ntx | |
621 | if params['invert'] in ('1', 1): |
|
624 | if params['invert'] in ('1', 1): | |
622 | y = [(n-1, n)] |
|
625 | y = [(n-1, n)] | |
623 | else: |
|
626 | else: | |
624 | y = [(0, 1)] |
|
627 | y = [(0, 1)] | |
625 |
|
628 | |||
626 | elif self.line_type.name=='prog_pulses': |
|
629 | elif self.line_type.name=='prog_pulses': | |
627 | params = json.loads(self.params) |
|
630 | params = json.loads(self.params) | |
628 | if int(params['periodic'])==0: |
|
631 | if int(params['periodic'])==0: | |
629 | nntx = 1 |
|
632 | nntx = 1 | |
630 | nipp = ipp_u*ntx |
|
633 | nipp = ipp_u*ntx | |
631 | else: |
|
634 | else: | |
632 | nntx = ntx |
|
635 | nntx = ntx | |
633 | nipp = ipp_u |
|
636 | nipp = ipp_u | |
634 |
|
637 | |||
635 | if 'params' in params and len(params['params'])>0: |
|
638 | if 'params' in params and len(params['params'])>0: | |
636 | for p in params['params']: |
|
639 | for p in params['params']: | |
637 | y_pp = self.points(nntx, nipp, |
|
640 | y_pp = self.points(nntx, nipp, | |
638 | p['end']-p['begin'], |
|
641 | p['end']-p['begin'], | |
639 | before=p['begin']) |
|
642 | before=p['begin']) | |
640 |
|
643 | |||
641 | y.extend(y_pp) |
|
644 | y.extend(y_pp) | |
642 |
|
645 | |||
643 | elif self.line_type.name=='windows': |
|
646 | elif self.line_type.name=='windows': | |
644 | params = json.loads(self.params) |
|
647 | params = json.loads(self.params) | |
645 |
|
648 | |||
646 | if 'params' in params and len(params['params'])>0: |
|
649 | if 'params' in params and len(params['params'])>0: | |
647 | tr_params = json.loads(self.get_lines(line_type__name='tr')[0].params) |
|
650 | tr_params = json.loads(self.get_lines(line_type__name='tr')[0].params) | |
648 | tr_ranges = tr_params['range'].split(',') |
|
651 | tr_ranges = tr_params['range'].split(',') | |
649 | for p in params['params']: |
|
652 | for p in params['params']: | |
650 | y_win = self.points(ntx, ipp_u, |
|
653 | y_win = self.points(ntx, ipp_u, | |
651 | p['resolution']*p['number_of_samples']*km2unit, |
|
654 | p['resolution']*p['number_of_samples']*km2unit, | |
652 | before=int(self.rc_configuration.time_before*us2unit)+self.get_win_ref(p, params['TX_ref'], km2unit), |
|
655 | before=int(self.rc_configuration.time_before*us2unit)+self.get_win_ref(p, params['TX_ref'], km2unit), | |
653 | sync=self.rc_configuration.sync) |
|
656 | sync=self.rc_configuration.sync) | |
654 |
|
657 | |||
655 | if len(tr_ranges)>0 and tr_ranges[0]<>'0': |
|
658 | if len(tr_ranges)>0 and tr_ranges[0]<>'0': | |
656 | y_win = self.mask_ranges(y_win, tr_ranges) |
|
659 | y_win = self.mask_ranges(y_win, tr_ranges) | |
657 |
|
660 | |||
658 | y.extend(y_win) |
|
661 | y.extend(y_win) | |
659 |
|
662 | |||
660 | elif self.line_type.name=='mix': |
|
663 | elif self.line_type.name=='mix': | |
661 | values = self.rc_configuration.parameters.split('-') |
|
664 | values = self.rc_configuration.parameters.split('-') | |
662 | confs = RCConfiguration.objects.filter(pk__in=[value.split('|')[0] for value in values]) |
|
665 | confs = RCConfiguration.objects.filter(pk__in=[value.split('|')[0] for value in values]) | |
663 | modes = [value.split('|')[1] for value in values] |
|
666 | modes = [value.split('|')[1] for value in values] | |
664 |
|
|
667 | ops = [value.split('|')[2] for value in values] | |
665 |
|
|
668 | delays = [value.split('|')[3] for value in values] | |
|
669 | masks = [value.split('|')[4] for value in values] | |||
|
670 | mask = list('{:8b}'.format(int(masks[0]))) | |||
|
671 | mask.reverse() | |||
|
672 | if mask[self.channel] in ('0', '', ' '): | |||
|
673 | y = np.zeros(total, dtype=np.int8) | |||
|
674 | else: | |||
|
675 | y = confs[0].get_lines(channel=self.channel)[0].pulses_as_array() | |||
666 |
|
676 | |||
667 | y = confs[0].get_lines(channel=self.channel)[0].pulses_as_array() |
|
|||
668 | ysize = len(y) |
|
|||
669 | for i in range(1, len(values)): |
|
677 | for i in range(1, len(values)): | |
670 | mask = list('{:8b}'.format(int(masks[i]))) |
|
678 | mask = list('{:8b}'.format(int(masks[i]))) | |
671 | mask.reverse() |
|
679 | mask.reverse() | |
672 |
|
680 | |||
673 | if mask[self.channel] in ('0', '', ' '): |
|
681 | if mask[self.channel] in ('0', '', ' '): | |
674 | continue |
|
682 | continue | |
675 | Y = confs[i].get_lines(channel=self.channel)[0].pulses_as_array() |
|
683 | Y = confs[i].get_lines(channel=self.channel)[0].pulses_as_array() | |
676 | delay = float(delays[i])*km2unit |
|
684 | delay = float(delays[i])*km2unit | |
677 | if delay>0: |
|
685 | ||
678 | y_temp = np.empty_like(Y) |
|
686 | if delay>0: | |
679 | y_temp[:delay] = 0 |
|
687 | if delay<self.rc_configuration.ipp*km2unit and len(Y)==len(y): | |
680 |
y_temp |
|
688 | y_temp = np.empty_like(Y) | |
681 |
y_temp |
|
689 | y_temp[:delay] = 0 | |
682 | if modes[i]=='OR': |
|
690 | y_temp[delay:] = Y[:-delay] | |
|
691 | elif delay+len(Y)>len(y): | |||
|
692 | y_new = np.zeros(delay+len(Y), dtype=np.int8) | |||
|
693 | y_new[:len(y)] = y | |||
|
694 | y = y_new | |||
|
695 | y_temp = np.zeros(delay+len(Y), dtype=np.int8) | |||
|
696 | y_temp[-len(Y):] = Y | |||
|
697 | elif delay+len(Y)==len(y): | |||
|
698 | y_temp = np.zeros(delay+len(Y)) | |||
|
699 | y_temp[-len(Y):] = Y | |||
|
700 | ||||
|
701 | if ops[i]=='OR': | |||
683 | y = y | y_temp |
|
702 | y = y | y_temp | |
684 |
elif |
|
703 | elif ops[i]=='XOR': | |
685 | y = y ^ y_temp |
|
704 | y = y ^ y_temp | |
686 |
elif |
|
705 | elif ops[i]=='AND': | |
687 | y = y & y_temp |
|
706 | y = y & y_temp | |
688 |
elif |
|
707 | elif ops[i]=='NAND': | |
689 | y = y & ~y_temp |
|
708 | y = y & ~y_temp | |
690 |
|
|
709 | ||
|
710 | total = len(y) | |||
691 | y = self.array_to_points(y) |
|
711 | y = self.array_to_points(y) | |
692 |
|
712 | |||
693 | else: |
|
713 | else: | |
694 | y = [] |
|
714 | y = [] | |
695 |
|
715 | |||
|
716 | if self.rc_configuration.total_units <> total: | |||
|
717 | self.rc_configuration.total_units = total | |||
|
718 | self.rc_configuration.save() | |||
|
719 | ||||
696 | self.pulses = y |
|
720 | self.pulses = y | |
697 | self.save() |
|
721 | self.save() | |
698 |
|
722 | |||
699 | @staticmethod |
|
723 | @staticmethod | |
700 | def array_to_points(X): |
|
724 | def array_to_points(X): | |
701 |
|
725 | |||
702 | d = X[1:]-X[:-1] |
|
726 | d = X[1:]-X[:-1] | |
703 |
|
727 | |||
704 | up = np.where(d==1)[0] |
|
728 | up = np.where(d==1)[0] | |
705 | if X[0]==1: |
|
729 | if X[0]==1: | |
706 | up = np.concatenate((np.array([-1]), up)) |
|
730 | up = np.concatenate((np.array([-1]), up)) | |
707 | up += 1 |
|
731 | up += 1 | |
708 |
|
732 | |||
709 | dw = np.where(d==-1)[0] |
|
733 | dw = np.where(d==-1)[0] | |
710 | if X[-1]==1: |
|
734 | if X[-1]==1: | |
711 | dw = np.concatenate((dw, np.array([len(X)-1]))) |
|
735 | dw = np.concatenate((dw, np.array([len(X)-1]))) | |
712 | dw += 1 |
|
736 | dw += 1 | |
713 |
|
737 | |||
714 | return [(tup[0], tup[1]) for tup in zip(up, dw)] |
|
738 | return [(tup[0], tup[1]) for tup in zip(up, dw)] | |
715 |
|
739 | |||
716 | @staticmethod |
|
740 | @staticmethod | |
717 | def mask_ranges(Y, ranges): |
|
741 | def mask_ranges(Y, ranges): | |
718 |
|
742 | |||
719 | y = [(0, 0) for __ in Y] |
|
743 | y = [(0, 0) for __ in Y] | |
720 |
|
744 | |||
721 | for index in ranges: |
|
745 | for index in ranges: | |
722 | if '-' in index: |
|
746 | if '-' in index: | |
723 | args = [int(a) for a in index.split('-')] |
|
747 | args = [int(a) for a in index.split('-')] | |
724 | y[args[0]-1:args[1]] = Y[args[0]-1:args[1]] |
|
748 | y[args[0]-1:args[1]] = Y[args[0]-1:args[1]] | |
725 | else: |
|
749 | else: | |
726 | y[int(index-1)] = Y[int(index-1)] |
|
750 | y[int(index-1)] = Y[int(index-1)] | |
727 |
|
751 | |||
728 | return y |
|
752 | return y | |
729 |
|
753 | |||
730 | @staticmethod |
|
754 | @staticmethod | |
731 | def points(ntx, ipp, width, delay=[0], before=0, after=0, sync=0): |
|
755 | def points(ntx, ipp, width, delay=[0], before=0, after=0, sync=0): | |
732 |
|
756 | |||
733 | delays = len(delay) |
|
757 | delays = len(delay) | |
734 |
|
758 | |||
735 | Y = [(ipp*x+before+delay[x%delays], ipp*x+width+before+delay[x%delays]+after) for x in range(ntx)] |
|
759 | Y = [(ipp*x+before+delay[x%delays], ipp*x+width+before+delay[x%delays]+after) for x in range(ntx)] | |
736 |
|
760 | |||
737 | return Y No newline at end of file |
|
761 | return Y |
@@ -1,370 +1,370 | |||||
1 |
|
1 | |||
2 | import json |
|
2 | import json | |
3 |
|
3 | |||
4 | from django.contrib import messages |
|
4 | from django.contrib import messages | |
5 | from django.utils.safestring import mark_safe |
|
5 | from django.utils.safestring import mark_safe | |
6 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
6 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse | |
7 |
|
7 | |||
8 | from apps.main.models import Configuration, Experiment, Device |
|
8 | from apps.main.models import Configuration, Experiment, Device | |
9 | from apps.main.views import sidebar |
|
9 | from apps.main.views import sidebar | |
10 |
|
10 | |||
11 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode |
|
11 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode | |
12 | from .forms import RCConfigurationForm, RCLineForm, RCLineViewForm, RCLineEditForm, RCImportForm, RCLineCodesForm |
|
12 | from .forms import RCConfigurationForm, RCLineForm, RCLineViewForm, RCLineEditForm, RCImportForm, RCLineCodesForm | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | def conf(request, conf_id): |
|
15 | def conf(request, conf_id): | |
16 |
|
16 | |||
17 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
17 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
18 |
|
18 | |||
19 | lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel') |
|
19 | lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel') | |
20 |
|
20 | |||
21 | for line in lines: |
|
21 | for line in lines: | |
22 | params = json.loads(line.params) |
|
22 | params = json.loads(line.params) | |
23 | line.form = RCLineViewForm(extra_fields=params, line=line) |
|
23 | line.form = RCLineViewForm(extra_fields=params, line=line) | |
24 | if 'params' in params: |
|
24 | if 'params' in params: | |
25 | line.subforms = [RCLineViewForm(extra_fields=fields, line=line, subform=True) for fields in params['params']] |
|
25 | line.subforms = [RCLineViewForm(extra_fields=fields, line=line, subform=True) for fields in params['params']] | |
26 |
|
26 | |||
27 | kwargs = {} |
|
27 | kwargs = {} | |
28 | kwargs['dev_conf'] = conf |
|
28 | kwargs['dev_conf'] = conf | |
29 | kwargs['rc_lines'] = lines |
|
29 | kwargs['rc_lines'] = lines | |
30 | kwargs['dev_conf_keys'] = ['name', 'ipp', 'ntx', 'clock_in', 'clock_divider', 'clock', |
|
30 | kwargs['dev_conf_keys'] = ['name', 'ipp_unit', 'ntx', 'clock_in', 'clock_divider', 'clock', | |
31 | 'time_before', 'time_after', 'sync', 'sampling_reference', 'control_tx', 'control_sw'] |
|
31 | 'time_before', 'time_after', 'sync', 'sampling_reference', 'control_tx', 'control_sw'] | |
32 |
|
32 | |||
33 | kwargs['title'] = 'RC Configuration' |
|
33 | kwargs['title'] = 'RC Configuration' | |
34 | kwargs['suptitle'] = 'Details' |
|
34 | kwargs['suptitle'] = 'Details' | |
35 |
|
35 | |||
36 | kwargs['button'] = 'Edit Configuration' |
|
36 | kwargs['button'] = 'Edit Configuration' | |
37 | ###### SIDEBAR ###### |
|
37 | ###### SIDEBAR ###### | |
38 | kwargs.update(sidebar(conf=conf)) |
|
38 | kwargs.update(sidebar(conf=conf)) | |
39 |
|
39 | |||
40 | return render(request, 'rc_conf.html', kwargs) |
|
40 | return render(request, 'rc_conf.html', kwargs) | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | def conf_edit(request, conf_id): |
|
43 | def conf_edit(request, conf_id): | |
44 |
|
44 | |||
45 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
45 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
46 |
|
46 | |||
47 | lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel') |
|
47 | lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel') | |
48 |
|
48 | |||
49 | for line in lines: |
|
49 | for line in lines: | |
50 | params = json.loads(line.params) |
|
50 | params = json.loads(line.params) | |
51 | line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params) |
|
51 | line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params) | |
52 | line.subform = False |
|
52 | line.subform = False | |
53 |
|
53 | |||
54 | if 'params' in params: |
|
54 | if 'params' in params: | |
55 | line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])] |
|
55 | line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])] | |
56 | line.subform = True |
|
56 | line.subform = True | |
57 |
|
57 | |||
58 | if request.method=='GET': |
|
58 | if request.method=='GET': | |
59 |
|
59 | |||
60 | form = RCConfigurationForm(instance=conf) |
|
60 | form = RCConfigurationForm(instance=conf) | |
61 |
|
61 | |||
62 | elif request.method=='POST': |
|
62 | elif request.method=='POST': | |
63 |
|
63 | |||
64 | line_data = {} |
|
64 | line_data = {} | |
65 | conf_data = {} |
|
65 | conf_data = {} | |
66 | extras = [] |
|
66 | extras = [] | |
67 |
|
67 | |||
68 | #classified post fields |
|
68 | #classified post fields | |
69 | for label,value in request.POST.items(): |
|
69 | for label,value in request.POST.items(): | |
70 | if label=='csrfmiddlewaretoken': |
|
70 | if label=='csrfmiddlewaretoken': | |
71 | continue |
|
71 | continue | |
72 |
|
72 | |||
73 | if label.count('|')==0: |
|
73 | if label.count('|')==0: | |
74 | conf_data[label] = value |
|
74 | conf_data[label] = value | |
75 | continue |
|
75 | continue | |
76 |
|
76 | |||
77 | elif label.split('|')[0]<>'-1': |
|
77 | elif label.split('|')[0]<>'-1': | |
78 | extras.append(label) |
|
78 | extras.append(label) | |
79 | continue |
|
79 | continue | |
80 |
|
80 | |||
81 | x, pk, name = label.split('|') |
|
81 | x, pk, name = label.split('|') | |
82 |
|
82 | |||
83 | if name=='codes': |
|
83 | if name=='codes': | |
84 | value = [s for s in value.split('\r\n') if s] |
|
84 | value = [s for s in value.split('\r\n') if s] | |
85 |
|
85 | |||
86 | if pk in line_data: |
|
86 | if pk in line_data: | |
87 | line_data[pk][name] = value |
|
87 | line_data[pk][name] = value | |
88 | else: |
|
88 | else: | |
89 | line_data[pk] = {name:value} |
|
89 | line_data[pk] = {name:value} | |
90 |
|
90 | |||
91 | #update conf |
|
91 | #update conf | |
92 | form = RCConfigurationForm(conf_data, instance=conf) |
|
92 | form = RCConfigurationForm(conf_data, instance=conf) | |
93 |
|
93 | |||
94 | if form.is_valid(): |
|
94 | if form.is_valid(): | |
95 |
|
95 | |||
96 | form.save() |
|
96 | form.save() | |
97 |
|
97 | |||
98 | #update lines fields |
|
98 | #update lines fields | |
99 | extras.sort() |
|
99 | extras.sort() | |
100 | for label in extras: |
|
100 | for label in extras: | |
101 | x, pk, name = label.split('|') |
|
101 | x, pk, name = label.split('|') | |
102 | if pk not in line_data: |
|
102 | if pk not in line_data: | |
103 | line_data[pk] = {} |
|
103 | line_data[pk] = {} | |
104 | if 'params' not in line_data[pk]: |
|
104 | if 'params' not in line_data[pk]: | |
105 | line_data[pk]['params'] = [] |
|
105 | line_data[pk]['params'] = [] | |
106 | if len(line_data[pk]['params'])<int(x)+1: |
|
106 | if len(line_data[pk]['params'])<int(x)+1: | |
107 | line_data[pk]['params'].append({}) |
|
107 | line_data[pk]['params'].append({}) | |
108 | line_data[pk]['params'][int(x)][name] = float(request.POST[label]) |
|
108 | line_data[pk]['params'][int(x)][name] = float(request.POST[label]) | |
109 |
|
109 | |||
110 | for pk, params in line_data.items(): |
|
110 | for pk, params in line_data.items(): | |
111 | line = RCLine.objects.get(pk=pk) |
|
111 | line = RCLine.objects.get(pk=pk) | |
112 | if line.line_type.name in ('windows', 'prog_pulses'): |
|
112 | if line.line_type.name in ('windows', 'prog_pulses'): | |
113 | if 'params' not in params: |
|
113 | if 'params' not in params: | |
114 | params['params'] = [] |
|
114 | params['params'] = [] | |
115 | line.params = json.dumps(params) |
|
115 | line.params = json.dumps(params) | |
116 | line.save() |
|
116 | line.save() | |
117 |
|
117 | |||
118 | #update pulses field |
|
118 | #update pulses field | |
119 |
conf.update_pulses() |
|
119 | conf.update_pulses() | |
120 |
|
120 | |||
121 | messages.success(request, 'RC Configuration successfully updated') |
|
121 | messages.success(request, 'RC Configuration successfully updated') | |
122 |
|
122 | |||
123 | return redirect(conf.get_absolute_url()) |
|
123 | return redirect(conf.get_absolute_url()) | |
124 |
|
124 | |||
125 | kwargs = {} |
|
125 | kwargs = {} | |
126 | kwargs['dev_conf'] = conf |
|
126 | kwargs['dev_conf'] = conf | |
127 | kwargs['form'] = form |
|
127 | kwargs['form'] = form | |
128 | kwargs['rc_lines'] = lines |
|
128 | kwargs['rc_lines'] = lines | |
129 | kwargs['edit'] = True |
|
129 | kwargs['edit'] = True | |
130 |
|
130 | |||
131 | kwargs['title'] = 'RC Configuration' |
|
131 | kwargs['title'] = 'RC Configuration' | |
132 | kwargs['suptitle'] = 'Edit' |
|
132 | kwargs['suptitle'] = 'Edit' | |
133 | kwargs['button'] = 'Update' |
|
133 | kwargs['button'] = 'Update' | |
134 | kwargs['previous'] = conf.get_absolute_url() |
|
134 | kwargs['previous'] = conf.get_absolute_url() | |
135 |
|
135 | |||
136 | return render(request, 'rc_conf_edit.html', kwargs) |
|
136 | return render(request, 'rc_conf_edit.html', kwargs) | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | def add_line(request, conf_id, line_type_id=None, code_id=None): |
|
139 | def add_line(request, conf_id, line_type_id=None, code_id=None): | |
140 |
|
140 | |||
141 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
141 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
142 |
|
142 | |||
143 | if request.method=='GET': |
|
143 | if request.method=='GET': | |
144 | if line_type_id: |
|
144 | if line_type_id: | |
145 | line_type = get_object_or_404(RCLineType, pk=line_type_id) |
|
145 | line_type = get_object_or_404(RCLineType, pk=line_type_id) | |
146 |
|
146 | |||
147 | if code_id: |
|
147 | if code_id: | |
148 | form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id, 'code_id': code_id}, |
|
148 | form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id, 'code_id': code_id}, | |
149 | extra_fields=json.loads(line_type.params)) |
|
149 | extra_fields=json.loads(line_type.params)) | |
150 | else: |
|
150 | else: | |
151 | form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id}, |
|
151 | form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id}, | |
152 | extra_fields=json.loads(line_type.params)) |
|
152 | extra_fields=json.loads(line_type.params)) | |
153 | else: |
|
153 | else: | |
154 | line_type = {'id':0} |
|
154 | line_type = {'id':0} | |
155 | form = RCLineForm(initial={'rc_configuration':conf_id}) |
|
155 | form = RCLineForm(initial={'rc_configuration':conf_id}) | |
156 |
|
156 | |||
157 | if request.method=='POST': |
|
157 | if request.method=='POST': | |
158 |
|
158 | |||
159 | line_type = get_object_or_404(RCLineType, pk=line_type_id) |
|
159 | line_type = get_object_or_404(RCLineType, pk=line_type_id) | |
160 | form = RCLineForm(request.POST, |
|
160 | form = RCLineForm(request.POST, | |
161 | extra_fields=json.loads(line_type.params)) |
|
161 | extra_fields=json.loads(line_type.params)) | |
162 |
|
162 | |||
163 | if form.is_valid(): |
|
163 | if form.is_valid(): | |
164 | form.save() |
|
164 | form.save() | |
165 | form.instance.update_pulses() |
|
165 | form.instance.update_pulses() | |
166 | return redirect('url_edit_rc_conf', conf.id) |
|
166 | return redirect('url_edit_rc_conf', conf.id) | |
167 |
|
167 | |||
168 | kwargs = {} |
|
168 | kwargs = {} | |
169 | kwargs['form'] = form |
|
169 | kwargs['form'] = form | |
170 | kwargs['title'] = 'RC Configuration' |
|
170 | kwargs['title'] = 'RC Configuration' | |
171 | kwargs['suptitle'] = 'Add Line' |
|
171 | kwargs['suptitle'] = 'Add Line' | |
172 | kwargs['button'] = 'Add' |
|
172 | kwargs['button'] = 'Add' | |
173 | kwargs['previous'] = conf.get_absolute_url_edit() |
|
173 | kwargs['previous'] = conf.get_absolute_url_edit() | |
174 | kwargs['dev_conf'] = conf |
|
174 | kwargs['dev_conf'] = conf | |
175 | kwargs['line_type'] = line_type |
|
175 | kwargs['line_type'] = line_type | |
176 |
|
176 | |||
177 | return render(request, 'rc_add_line.html', kwargs) |
|
177 | return render(request, 'rc_add_line.html', kwargs) | |
178 |
|
178 | |||
179 | def edit_codes(request, conf_id, line_id, code_id=None): |
|
179 | def edit_codes(request, conf_id, line_id, code_id=None): | |
180 |
|
180 | |||
181 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
181 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
182 | line = get_object_or_404(RCLine, pk=line_id) |
|
182 | line = get_object_or_404(RCLine, pk=line_id) | |
183 | params = json.loads(line.params) |
|
183 | params = json.loads(line.params) | |
184 |
|
184 | |||
185 | if request.method=='GET': |
|
185 | if request.method=='GET': | |
186 | if code_id: |
|
186 | if code_id: | |
187 | code = get_object_or_404(RCLineCode, pk=code_id) |
|
187 | code = get_object_or_404(RCLineCode, pk=code_id) | |
188 | form = RCLineCodesForm(instance=code) |
|
188 | form = RCLineCodesForm(instance=code) | |
189 | else: |
|
189 | else: | |
190 | initial = {'code': params['code'], |
|
190 | initial = {'code': params['code'], | |
191 | 'codes': params['codes'] if 'codes' in params else [], |
|
191 | 'codes': params['codes'] if 'codes' in params else [], | |
192 | 'number_of_codes': len(params['codes']) if 'codes' in params else 0, |
|
192 | 'number_of_codes': len(params['codes']) if 'codes' in params else 0, | |
193 | 'bits_per_code': len(params['codes'][0]) if 'codes' in params else 0, |
|
193 | 'bits_per_code': len(params['codes'][0]) if 'codes' in params else 0, | |
194 | } |
|
194 | } | |
195 | form = RCLineCodesForm(initial=initial) |
|
195 | form = RCLineCodesForm(initial=initial) | |
196 |
|
196 | |||
197 | if request.method=='POST': |
|
197 | if request.method=='POST': | |
198 | form = RCLineCodesForm(request.POST) |
|
198 | form = RCLineCodesForm(request.POST) | |
199 | if form.is_valid(): |
|
199 | if form.is_valid(): | |
200 | params['code'] = request.POST['code'] |
|
200 | params['code'] = request.POST['code'] | |
201 | params['codes'] = [s for s in request.POST['codes'].split('\r\n') if s] |
|
201 | params['codes'] = [s for s in request.POST['codes'].split('\r\n') if s] | |
202 | line.params = json.dumps(params) |
|
202 | line.params = json.dumps(params) | |
203 | line.save() |
|
203 | line.save() | |
204 | messages.success(request, 'Line: "%s" has been updated.' % line) |
|
204 | messages.success(request, 'Line: "%s" has been updated.' % line) | |
205 | return redirect('url_edit_rc_conf', conf.id) |
|
205 | return redirect('url_edit_rc_conf', conf.id) | |
206 |
|
206 | |||
207 | kwargs = {} |
|
207 | kwargs = {} | |
208 | kwargs['form'] = form |
|
208 | kwargs['form'] = form | |
209 | kwargs['title'] = line |
|
209 | kwargs['title'] = line | |
210 | kwargs['suptitle'] = 'Edit' |
|
210 | kwargs['suptitle'] = 'Edit' | |
211 | kwargs['button'] = 'Update' |
|
211 | kwargs['button'] = 'Update' | |
212 | kwargs['dev_conf'] = conf |
|
212 | kwargs['dev_conf'] = conf | |
213 | kwargs['previous'] = conf.get_absolute_url_edit() |
|
213 | kwargs['previous'] = conf.get_absolute_url_edit() | |
214 | kwargs['line'] = line |
|
214 | kwargs['line'] = line | |
215 |
|
215 | |||
216 | return render(request, 'rc_edit_codes.html', kwargs) |
|
216 | return render(request, 'rc_edit_codes.html', kwargs) | |
217 |
|
217 | |||
218 | def add_subline(request, conf_id, line_id): |
|
218 | def add_subline(request, conf_id, line_id): | |
219 |
|
219 | |||
220 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
220 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
221 | line = get_object_or_404(RCLine, pk=line_id) |
|
221 | line = get_object_or_404(RCLine, pk=line_id) | |
222 |
|
222 | |||
223 | if request.method == 'POST': |
|
223 | if request.method == 'POST': | |
224 | if line: |
|
224 | if line: | |
225 | params = json.loads(line.params) |
|
225 | params = json.loads(line.params) | |
226 | subparams = json.loads(line.line_type.params) |
|
226 | subparams = json.loads(line.line_type.params) | |
227 | if 'params' in subparams: |
|
227 | if 'params' in subparams: | |
228 | dum = {} |
|
228 | dum = {} | |
229 | for key, value in subparams['params'].items(): |
|
229 | for key, value in subparams['params'].items(): | |
230 | dum[key] = value['value'] |
|
230 | dum[key] = value['value'] | |
231 | params['params'].append(dum) |
|
231 | params['params'].append(dum) | |
232 | line.params = json.dumps(params) |
|
232 | line.params = json.dumps(params) | |
233 | line.save() |
|
233 | line.save() | |
234 | return redirect('url_edit_rc_conf', conf.id) |
|
234 | return redirect('url_edit_rc_conf', conf.id) | |
235 |
|
235 | |||
236 | kwargs = {} |
|
236 | kwargs = {} | |
237 |
|
237 | |||
238 | kwargs['title'] = 'Add new' |
|
238 | kwargs['title'] = 'Add new' | |
239 | kwargs['suptitle'] = '%s to %s' % (line.line_type.name, line) |
|
239 | kwargs['suptitle'] = '%s to %s' % (line.line_type.name, line) | |
240 |
|
240 | |||
241 | return render(request, 'confirm.html', kwargs) |
|
241 | return render(request, 'confirm.html', kwargs) | |
242 |
|
242 | |||
243 | def remove_line(request, conf_id, line_id): |
|
243 | def remove_line(request, conf_id, line_id): | |
244 |
|
244 | |||
245 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
245 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
246 | line = get_object_or_404(RCLine, pk=line_id) |
|
246 | line = get_object_or_404(RCLine, pk=line_id) | |
247 |
|
247 | |||
248 | if request.method == 'POST': |
|
248 | if request.method == 'POST': | |
249 | if line: |
|
249 | if line: | |
250 | try: |
|
250 | try: | |
251 | channel = line.channel |
|
251 | channel = line.channel | |
252 | line.delete() |
|
252 | line.delete() | |
253 | for ch in range(channel+1, RCLine.objects.filter(rc_configuration=conf).count()+1): |
|
253 | for ch in range(channel+1, RCLine.objects.filter(rc_configuration=conf).count()+1): | |
254 | l = RCLine.objects.get(rc_configuration=conf, channel=ch) |
|
254 | l = RCLine.objects.get(rc_configuration=conf, channel=ch) | |
255 | l.channel = l.channel-1 |
|
255 | l.channel = l.channel-1 | |
256 | l.save() |
|
256 | l.save() | |
257 | messages.success(request, 'Line: "%s" has been deleted.' % line) |
|
257 | messages.success(request, 'Line: "%s" has been deleted.' % line) | |
258 | except: |
|
258 | except: | |
259 | messages.error(request, 'Unable to delete line: "%s".' % line) |
|
259 | messages.error(request, 'Unable to delete line: "%s".' % line) | |
260 |
|
260 | |||
261 | return redirect('url_edit_rc_conf', conf.id) |
|
261 | return redirect('url_edit_rc_conf', conf.id) | |
262 |
|
262 | |||
263 | kwargs = {} |
|
263 | kwargs = {} | |
264 |
|
264 | |||
265 | kwargs['object'] = line |
|
265 | kwargs['object'] = line | |
266 | kwargs['delete_view'] = True |
|
266 | kwargs['delete_view'] = True | |
267 | kwargs['title'] = 'Confirm delete' |
|
267 | kwargs['title'] = 'Confirm delete' | |
268 | kwargs['previous'] = conf.get_absolute_url_edit() |
|
268 | kwargs['previous'] = conf.get_absolute_url_edit() | |
269 | return render(request, 'confirm.html', kwargs) |
|
269 | return render(request, 'confirm.html', kwargs) | |
270 |
|
270 | |||
271 |
|
271 | |||
272 | def remove_subline(request, conf_id, line_id, subline_id): |
|
272 | def remove_subline(request, conf_id, line_id, subline_id): | |
273 |
|
273 | |||
274 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
274 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
275 | line = get_object_or_404(RCLine, pk=line_id) |
|
275 | line = get_object_or_404(RCLine, pk=line_id) | |
276 |
|
276 | |||
277 | if request.method == 'POST': |
|
277 | if request.method == 'POST': | |
278 | if line: |
|
278 | if line: | |
279 | params = json.loads(line.params) |
|
279 | params = json.loads(line.params) | |
280 | params['params'].remove(params['params'][int(subline_id)-1]) |
|
280 | params['params'].remove(params['params'][int(subline_id)-1]) | |
281 | line.params = json.dumps(params) |
|
281 | line.params = json.dumps(params) | |
282 | line.save() |
|
282 | line.save() | |
283 |
|
283 | |||
284 | return redirect('url_edit_rc_conf', conf.id) |
|
284 | return redirect('url_edit_rc_conf', conf.id) | |
285 |
|
285 | |||
286 | kwargs = {} |
|
286 | kwargs = {} | |
287 |
|
287 | |||
288 | kwargs['object'] = line |
|
288 | kwargs['object'] = line | |
289 | kwargs['object_name'] = line.line_type.name |
|
289 | kwargs['object_name'] = line.line_type.name | |
290 | kwargs['delete_view'] = True |
|
290 | kwargs['delete_view'] = True | |
291 | kwargs['title'] = 'Confirm delete' |
|
291 | kwargs['title'] = 'Confirm delete' | |
292 |
|
292 | |||
293 | return render(request, 'confirm.html', kwargs) |
|
293 | return render(request, 'confirm.html', kwargs) | |
294 |
|
294 | |||
295 |
|
295 | |||
296 | def update_lines_position(request, conf_id): |
|
296 | def update_lines_position(request, conf_id): | |
297 |
|
297 | |||
298 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
298 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
299 |
|
299 | |||
300 | if request.method=='POST': |
|
300 | if request.method=='POST': | |
301 | ch = 0 |
|
301 | ch = 0 | |
302 | for item in request.POST['items'].split('&'): |
|
302 | for item in request.POST['items'].split('&'): | |
303 | line = RCLine.objects.get(pk=item.split('=')[-1]) |
|
303 | line = RCLine.objects.get(pk=item.split('=')[-1]) | |
304 | line.channel = ch |
|
304 | line.channel = ch | |
305 | line.save() |
|
305 | line.save() | |
306 | ch += 1 |
|
306 | ch += 1 | |
307 |
|
307 | |||
308 | lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel') |
|
308 | lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel') | |
309 |
|
309 | |||
310 | for line in lines: |
|
310 | for line in lines: | |
311 | params = json.loads(line.params) |
|
311 | params = json.loads(line.params) | |
312 | line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params) |
|
312 | line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params) | |
313 |
|
313 | |||
314 | if 'params' in params: |
|
314 | if 'params' in params: | |
315 | line.subform = True |
|
315 | line.subform = True | |
316 | line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])] |
|
316 | line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])] | |
317 |
|
317 | |||
318 | html = render(request, 'rc_lines.html', {'dev_conf':conf, 'rc_lines':lines, 'edit':True}) |
|
318 | html = render(request, 'rc_lines.html', {'dev_conf':conf, 'rc_lines':lines, 'edit':True}) | |
319 | data = {'html': html.content} |
|
319 | data = {'html': html.content} | |
320 |
|
320 | |||
321 | return HttpResponse(json.dumps(data), content_type="application/json") |
|
321 | return HttpResponse(json.dumps(data), content_type="application/json") | |
322 | return redirect('url_edit_rc_conf', conf.id) |
|
322 | return redirect('url_edit_rc_conf', conf.id) | |
323 |
|
323 | |||
324 |
|
324 | |||
325 | def import_file(request, conf_id): |
|
325 | def import_file(request, conf_id): | |
326 |
|
326 | |||
327 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
327 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
328 | if request.method=='POST': |
|
328 | if request.method=='POST': | |
329 | form = RCImportForm(request.POST, request.FILES) |
|
329 | form = RCImportForm(request.POST, request.FILES) | |
330 | if form.is_valid(): |
|
330 | if form.is_valid(): | |
331 | try: |
|
331 | try: | |
332 | conf.update_from_file(request.FILES['file_name']) |
|
332 | conf.update_from_file(request.FILES['file_name']) | |
333 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) |
|
333 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) | |
334 | return redirect(conf.get_absolute_url_edit()) |
|
334 | return redirect(conf.get_absolute_url_edit()) | |
335 |
|
335 | |||
336 | except Exception as e: |
|
336 | except Exception as e: | |
337 | messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) |
|
337 | messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) | |
338 |
|
338 | |||
339 | else: |
|
339 | else: | |
340 | messages.warning(request, 'Your current configuration will be replaced') |
|
340 | messages.warning(request, 'Your current configuration will be replaced') | |
341 | form = RCImportForm() |
|
341 | form = RCImportForm() | |
342 |
|
342 | |||
343 | kwargs = {} |
|
343 | kwargs = {} | |
344 | kwargs['form'] = form |
|
344 | kwargs['form'] = form | |
345 | kwargs['title'] = 'RC Configuration' |
|
345 | kwargs['title'] = 'RC Configuration' | |
346 | kwargs['suptitle'] = 'Import file' |
|
346 | kwargs['suptitle'] = 'Import file' | |
347 | kwargs['button'] = 'Upload' |
|
347 | kwargs['button'] = 'Upload' | |
348 | kwargs['previous'] = conf.get_absolute_url() |
|
348 | kwargs['previous'] = conf.get_absolute_url() | |
349 |
|
349 | |||
350 | return render(request, 'rc_import.html', kwargs) |
|
350 | return render(request, 'rc_import.html', kwargs) | |
351 |
|
351 | |||
352 |
|
352 | |||
353 | def plot_pulses(request, conf_id): |
|
353 | def plot_pulses(request, conf_id): | |
354 |
|
354 | |||
355 | conf = get_object_or_404(RCConfiguration, pk=conf_id) |
|
355 | conf = get_object_or_404(RCConfiguration, pk=conf_id) | |
356 |
|
356 | |||
357 | script, div = conf.plot_pulses() |
|
357 | script, div = conf.plot_pulses() | |
358 |
|
358 | |||
359 | kwargs = {} |
|
359 | kwargs = {} | |
360 |
|
360 | |||
361 | kwargs['title'] = 'RC Pulses' |
|
361 | kwargs['title'] = 'RC Pulses' | |
362 | kwargs['suptitle'] = conf.name |
|
362 | kwargs['suptitle'] = conf.name | |
363 | kwargs['div'] = mark_safe(div) |
|
363 | kwargs['div'] = mark_safe(div) | |
364 | kwargs['script'] = mark_safe(script) |
|
364 | kwargs['script'] = mark_safe(script) | |
365 |
|
365 | |||
366 | if 'json' in request.GET: |
|
366 | if 'json' in request.GET: | |
367 | return HttpResponse(json.dumps({'div':mark_safe(div), 'script':mark_safe(script)}), content_type="application/json") |
|
367 | return HttpResponse(json.dumps({'div':mark_safe(div), 'script':mark_safe(script)}), content_type="application/json") | |
368 | else: |
|
368 | else: | |
369 | return render(request, 'rc_pulses.html', kwargs) |
|
369 | return render(request, 'rc_pulses.html', kwargs) | |
370 |
|
370 |
General Comments 0
You need to be logged in to leave comments.
Login now