@@ -1,381 +1,386 | |||
|
1 | 1 | import os |
|
2 | 2 | import ast |
|
3 | 3 | import json |
|
4 | 4 | |
|
5 | 5 | from django import forms |
|
6 | 6 | from django.utils.safestring import mark_safe |
|
7 | 7 | from apps.main.models import Device |
|
8 | 8 | from apps.main.forms import add_empty_choice |
|
9 | 9 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode |
|
10 | 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_choice=False): | |
|
13 | 13 | |
|
14 | 14 | if model=='RCLine': |
|
15 | 15 | instance = RCConfiguration.objects.get(pk=conf_id) |
|
16 | 16 | choices = [(line.pk, line.get_name()) for line in instance.get_lines(line_type__name='tx')] |
|
17 | if all_choice: | |
|
17 | 18 | choices = add_empty_choice(choices, label='All') |
|
18 | 19 | else: |
|
19 | 20 | instance = globals()[model] |
|
20 | 21 | choices = instance.objects.all().values_list('pk', 'name') |
|
21 | 22 | |
|
22 | 23 | return choices |
|
23 | 24 | |
|
24 | 25 | |
|
25 | 26 | class ExtFileField(forms.FileField): |
|
26 | 27 | """ |
|
27 | 28 | Same as forms.FileField, but you can specify a file extension whitelist. |
|
28 | 29 | |
|
29 | 30 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |
|
30 | 31 | >>> |
|
31 | 32 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) |
|
32 | 33 | >>> |
|
33 | 34 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) |
|
34 | 35 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) |
|
35 | 36 | >>> |
|
36 | 37 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) |
|
37 | 38 | Traceback (most recent call last): |
|
38 | 39 | ... |
|
39 | 40 | ValidationError: [u'Not allowed filetype!'] |
|
40 | 41 | """ |
|
41 | 42 | def __init__(self, *args, **kwargs): |
|
42 | 43 | extensions = kwargs.pop("extensions") |
|
43 | 44 | self.extensions = [i.lower() for i in extensions] |
|
44 | 45 | |
|
45 | 46 | super(ExtFileField, self).__init__(*args, **kwargs) |
|
46 | 47 | |
|
47 | 48 | def clean(self, *args, **kwargs): |
|
48 | 49 | data = super(ExtFileField, self).clean(*args, **kwargs) |
|
49 | 50 | filename = data.name |
|
50 | 51 | ext = os.path.splitext(filename)[1] |
|
51 | 52 | ext = ext.lower() |
|
52 | 53 | if ext not in self.extensions: |
|
53 | 54 | raise forms.ValidationError('Not allowed file type: %s' % ext) |
|
54 | 55 | |
|
55 | 56 | |
|
56 | 57 | class RCConfigurationForm(forms.ModelForm): |
|
57 | 58 | |
|
58 | 59 | def __init__(self, *args, **kwargs): |
|
59 | 60 | super(RCConfigurationForm, self).__init__(*args, **kwargs) |
|
60 | 61 | |
|
61 | 62 | instance = getattr(self, 'instance', None) |
|
62 | 63 | |
|
63 | 64 | if instance and instance.pk: |
|
64 | 65 | |
|
65 | 66 | devices = Device.objects.filter(device_type__name='rc') |
|
66 | 67 | if instance.experiment: |
|
67 | 68 | self.fields['experiment'].widget.attrs['read_only'] = True |
|
68 | 69 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] |
|
69 | 70 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
70 | 71 | self.fields['ipp'].widget = KmUnitHzWidget(attrs={'km2unit':instance.km2unit}) |
|
71 | 72 | self.fields['clock'].widget.attrs['readonly'] = True |
|
72 | 73 | |
|
73 | 74 | self.fields['time_before'].label = mark_safe(self.fields['time_before'].label) |
|
74 | 75 | self.fields['time_after'].label = mark_safe(self.fields['time_after'].label) |
|
75 | 76 | |
|
76 | 77 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
77 | 78 | self.fields['experiment'].widget.attrs['readonly'] = True |
|
78 | 79 | |
|
79 | 80 | class Meta: |
|
80 | 81 | model = RCConfiguration |
|
81 | 82 | exclude = ('type', 'parameters', 'status', 'total_units', 'mix') |
|
82 | 83 | |
|
83 | 84 | def clean(self): |
|
84 | 85 | form_data = super(RCConfigurationForm, self).clean() |
|
85 | 86 | |
|
86 | 87 | if 'clock_divider' in form_data: |
|
87 | 88 | if form_data['clock_divider']<1: |
|
88 | 89 | self.add_error('clock_divider', 'Invalid Value') |
|
89 | 90 | else: |
|
90 | 91 | if form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider']))%10<>0: |
|
91 | 92 | self.add_error('ipp', 'Invalid IPP units={}'.format(form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider'])))) |
|
92 | 93 | |
|
93 | 94 | return form_data |
|
94 | 95 | |
|
95 | 96 | def save(self): |
|
96 | 97 | conf = super(RCConfigurationForm, self).save() |
|
97 | 98 | conf.total_units = conf.ipp*conf.ntx*conf.km2unit |
|
98 | 99 | conf.save() |
|
99 | 100 | return conf |
|
100 | 101 | |
|
101 | 102 | |
|
102 | 103 | class RCMixConfigurationForm(forms.Form): |
|
103 | 104 | |
|
104 | 105 | clock_in = forms.CharField(widget=forms.HiddenInput()) |
|
105 | 106 | clock_divider = forms.CharField(widget=forms.HiddenInput()) |
|
106 | 107 | name = forms.CharField() |
|
107 | 108 | experiment = forms.ChoiceField() |
|
108 | 109 | mode = forms.ChoiceField(widget=forms.RadioSelect(), |
|
109 | 110 | choices=[(0, 'Parallel'), (1, 'Sequence')], |
|
110 | 111 | initial=0) |
|
111 | 112 | operation = forms.ChoiceField(widget=forms.RadioSelect(), |
|
112 | 113 | choices=[(0, 'OR'), (1, 'XOR'), (2, 'AND'), (3, 'NAND')], |
|
113 | 114 | initial=1) |
|
114 | 115 | delay = forms.CharField() |
|
115 | 116 | mask = forms.MultipleChoiceField(choices=[(0, 'L1'),(1, 'L2'),(2, 'L3'),(3, 'L4'),(4, 'L5'),(5, 'L6'),(6, 'L7'),(7, 'L8')], |
|
116 | 117 | widget=HCheckboxSelectMultiple()) |
|
117 | 118 | result = forms.CharField(required=False, |
|
118 | 119 | widget=forms.Textarea(attrs={'readonly':True, 'rows':5, 'class':'tabuled'})) |
|
119 | 120 | |
|
120 | 121 | def __init__(self, *args, **kwargs): |
|
121 | 122 | confs = kwargs.pop('confs', []) |
|
122 | 123 | if confs: |
|
123 | 124 | km2unit = confs[0].km2unit |
|
124 | 125 | clock_in = confs[0].clock_in |
|
125 | 126 | clock_divider = confs[0].clock_divider |
|
126 | 127 | else: |
|
127 | 128 | km2unit = clock_in = clock_divider = 0 |
|
128 | 129 | super(RCMixConfigurationForm, self).__init__(*args, **kwargs) |
|
129 | 130 | self.fields['experiment'].choices = [(conf.pk, '{} | {}'.format(conf.pk, conf.name)) for conf in confs] |
|
130 | 131 | self.fields['delay'].widget = KmUnitWidget(attrs = {'km2unit':km2unit}) |
|
131 | 132 | self.fields['clock_in'].initial = clock_in |
|
132 | 133 | self.fields['clock_divider'].initial = clock_divider |
|
133 | 134 | |
|
134 | 135 | |
|
135 | 136 | class RCLineForm(forms.ModelForm): |
|
136 | 137 | |
|
137 | 138 | def __init__(self, *args, **kwargs): |
|
138 | 139 | self.extra_fields = kwargs.pop('extra_fields', []) |
|
139 | 140 | super(RCLineForm, self).__init__(*args, **kwargs) |
|
140 | 141 | |
|
141 | 142 | if 'initial' in kwargs and 'line_type' in kwargs['initial']: |
|
142 | 143 | line_type = RCLineType.objects.get(pk=kwargs['initial']['line_type']) |
|
143 | 144 | |
|
144 | 145 | if 'code_id' in kwargs['initial']: |
|
145 | 146 | model_initial = kwargs['initial']['code_id'] |
|
146 | 147 | else: |
|
147 | 148 | model_initial = 0 |
|
148 | 149 | |
|
149 | 150 | params = json.loads(line_type.params) |
|
150 | 151 | |
|
151 | 152 | for label, value in self.extra_fields.items(): |
|
152 | 153 | if label=='params': |
|
153 | 154 | continue |
|
154 | 155 | |
|
155 | 156 | if 'model' in params[label]: |
|
156 | 157 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], |
|
157 | 158 | kwargs['initial']['rc_configuration']), |
|
158 | 159 | initial=model_initial) |
|
159 | 160 | |
|
160 | 161 | |
|
161 | 162 | else: |
|
162 | 163 | if label=='codes' and 'code_id' in kwargs['initial']: |
|
163 | 164 | self.fields[label] = forms.CharField(initial=RCLineCode.objects.get(pk=kwargs['initial']['code_id']).codes) |
|
164 | 165 | else: |
|
165 | 166 | self.fields[label] = forms.CharField(initial=value['value']) |
|
166 | 167 | |
|
167 | 168 | if label=='codes': |
|
168 | 169 | self.fields[label].widget = CodesWidget() |
|
169 | 170 | |
|
170 | 171 | if self.data: |
|
171 | 172 | line_type = RCLineType.objects.get(pk=self.data['line_type']) |
|
172 | 173 | |
|
173 | 174 | if 'code_id' in self.data: |
|
174 | 175 | model_initial = self.data['code_id'] |
|
175 | 176 | else: |
|
176 | 177 | model_initial = 0 |
|
177 | 178 | |
|
178 | 179 | params = json.loads(line_type.params) |
|
179 | 180 | |
|
180 | 181 | for label, value in self.extra_fields.items(): |
|
181 | 182 | if label=='params': |
|
182 | 183 | continue |
|
183 | 184 | |
|
184 | 185 | if 'model' in params[label]: |
|
185 | 186 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], |
|
186 | 187 | self.data['rc_configuration']), |
|
187 | 188 | initial=model_initial) |
|
188 | 189 | |
|
189 | 190 | |
|
190 | 191 | else: |
|
191 | 192 | if label=='codes' and 'code' in self.data: |
|
192 | 193 | self.fields[label] = forms.CharField(initial=self.data['codes']) |
|
193 | 194 | else: |
|
194 | 195 | self.fields[label] = forms.CharField(initial=self.data[label]) |
|
195 | 196 | |
|
196 | 197 | if label=='codes': |
|
197 | 198 | self.fields[label].widget = CodesWidget() |
|
198 | 199 | |
|
199 | 200 | |
|
200 | 201 | class Meta: |
|
201 | 202 | model = RCLine |
|
202 | 203 | fields = ('rc_configuration', 'line_type', 'channel') |
|
203 | 204 | widgets = { |
|
204 | 205 | 'channel': forms.HiddenInput(), |
|
205 | 206 | } |
|
206 | 207 | |
|
207 | 208 | |
|
208 | 209 | def clean(self): |
|
209 | 210 | |
|
210 | 211 | form_data = self.cleaned_data |
|
211 | 212 | if 'code' in self.data and self.data['TX_ref']=="0": |
|
212 | 213 | self.add_error('TX_ref', 'Choose a valid TX reference') |
|
213 | 214 | |
|
214 | 215 | if RCLineType.objects.get(pk=self.data['line_type']).name=='mix': |
|
215 | 216 | self.add_error('line_type', 'Invalid Line type') |
|
216 | 217 | |
|
217 | 218 | return form_data |
|
218 | 219 | |
|
219 | 220 | |
|
220 | 221 | def save(self): |
|
221 | 222 | line = super(RCLineForm, self).save() |
|
222 | 223 | |
|
223 | 224 | #auto add channel |
|
224 | 225 | line.channel = RCLine.objects.filter(rc_configuration=line.rc_configuration).count()-1 |
|
225 | 226 | |
|
226 | 227 | #auto add position for TX, TR & CODE |
|
227 | 228 | if line.line_type.name in ('tx', ): |
|
228 | 229 | line.position = RCLine.objects.filter(rc_configuration=line.rc_configuration, line_type=line.line_type).count()-1 |
|
229 | 230 | |
|
230 | 231 | #save extra fields in params |
|
231 | 232 | params = {} |
|
232 | 233 | for label, value in self.extra_fields.items(): |
|
233 | 234 | if label=='params': |
|
234 | 235 | params['params'] = [] |
|
235 | 236 | elif label=='codes': |
|
236 | 237 | params[label] = [s for s in self.data[label].split('\r\n') if s] |
|
237 | 238 | else: |
|
238 | 239 | params[label] = self.data[label] |
|
239 | 240 | line.params = json.dumps(params) |
|
240 | 241 | line.save() |
|
241 | 242 | return |
|
242 | 243 | |
|
243 | 244 | |
|
244 | 245 | class RCLineViewForm(forms.Form): |
|
245 | 246 | |
|
246 | 247 | def __init__(self, *args, **kwargs): |
|
247 | 248 | |
|
248 | 249 | extra_fields = kwargs.pop('extra_fields') |
|
249 | 250 | line = kwargs.pop('line') |
|
250 | 251 | subform = kwargs.pop('subform', False) |
|
251 | 252 | super(RCLineViewForm, self).__init__(*args, **kwargs) |
|
252 | 253 | |
|
253 | 254 | if subform: |
|
254 | 255 | params = json.loads(line.line_type.params)['params'] |
|
255 | 256 | else: |
|
256 | 257 | params = json.loads(line.line_type.params) |
|
257 | 258 | |
|
258 | 259 | for label, value in extra_fields.items(): |
|
259 | 260 | |
|
260 | 261 | if label=='params': |
|
261 | 262 | continue |
|
262 | 263 | if 'ref' in label: |
|
263 | 264 | if value in (0, '0'): |
|
264 | 265 | value = 'All' |
|
265 | 266 | else: |
|
266 | 267 | value = RCLine.objects.get(pk=value).get_name() |
|
267 | 268 | elif label=='code': |
|
268 | 269 | value = RCLineCode.objects.get(pk=value).name |
|
269 | 270 | |
|
270 | 271 | self.fields[label] = forms.CharField(initial=value) |
|
271 | 272 | |
|
272 | 273 | if 'widget' in params[label]: |
|
273 | 274 | km2unit = line.rc_configuration.km2unit |
|
274 | 275 | if params[label]['widget']=='km': |
|
275 | 276 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
276 | 277 | elif params[label]['widget']=='unit': |
|
277 | 278 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
278 | 279 | elif params[label]['widget']=='dc': |
|
279 | 280 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
280 | 281 | elif params[label]['widget']=='codes': |
|
281 | 282 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
282 | 283 | else: |
|
283 | 284 | self.fields[label].widget = DefaultWidget(attrs={'disabled':True}) |
|
284 | 285 | |
|
285 | 286 | |
|
286 | 287 | class RCLineEditForm(forms.ModelForm): |
|
287 | 288 | |
|
288 | 289 | def __init__(self, *args, **kwargs): |
|
289 | 290 | |
|
290 | 291 | extra_fields = kwargs.pop('extra_fields', []) |
|
291 | 292 | conf = kwargs.pop('conf', False) |
|
292 | 293 | line = kwargs.pop('line') |
|
293 | 294 | subform = kwargs.pop('subform', False) |
|
294 | 295 | |
|
295 | 296 | super(RCLineEditForm, self).__init__(*args, **kwargs) |
|
296 | 297 | |
|
297 | 298 | if subform is not False: |
|
298 | 299 | params = json.loads(line.line_type.params)['params'] |
|
299 | 300 | count = subform |
|
300 | 301 | else: |
|
301 | 302 | params = json.loads(line.line_type.params) |
|
302 | 303 | count = -1 |
|
303 | 304 | |
|
304 | 305 | for label, value in extra_fields.items(): |
|
305 | 306 | |
|
306 | 307 | if label in ('params',): |
|
307 | 308 | continue |
|
308 | 309 | if 'help' in params[label]: |
|
309 | 310 | help_text = params[label]['help'] |
|
310 | 311 | else: |
|
311 | 312 | help_text = '' |
|
312 | 313 | |
|
313 | 314 |
if 'model' in params[label]: |
|
314 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], conf.id), | |
|
315 | if line.line_type.name=='tr': | |
|
316 | all_choice = True | |
|
317 | else: | |
|
318 | all_choice = False | |
|
319 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], conf.id, all_choice=all_choice), | |
|
315 | 320 | initial=value, |
|
316 | 321 | widget=forms.Select(attrs={'name':'%s|%s|%s' % (count, line.id, label)}), |
|
317 | 322 | help_text=help_text) |
|
318 | 323 | |
|
319 | 324 | else: |
|
320 | 325 | |
|
321 | 326 | self.fields[label] = forms.CharField(initial=value, help_text=help_text) |
|
322 | 327 | |
|
323 | 328 | if label in ('code', ): |
|
324 | 329 | self.fields[label].widget = HiddenWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) |
|
325 | 330 | |
|
326 | 331 | elif 'widget' in params[label]: |
|
327 | 332 | km2unit = line.rc_configuration.km2unit |
|
328 | 333 | if params[label]['widget']=='km': |
|
329 | 334 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
330 | 335 | elif params[label]['widget']=='unit': |
|
331 | 336 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
332 | 337 | elif params[label]['widget']=='dc': |
|
333 | 338 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
334 | 339 | elif params[label]['widget']=='codes': |
|
335 | 340 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
336 | 341 | else: |
|
337 | 342 | self.fields[label].widget = DefaultWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) |
|
338 | 343 | |
|
339 | 344 | |
|
340 | 345 | class Meta: |
|
341 | 346 | model = RCLine |
|
342 | 347 | exclude = ('rc_configuration', 'line_type', 'channel', 'position', 'params', 'pulses') |
|
343 | 348 | |
|
344 | 349 | |
|
345 | 350 | class RCSubLineEditForm(forms.Form): |
|
346 | 351 | |
|
347 | 352 | def __init__(self, *args, **kwargs): |
|
348 | 353 | extra_fields = kwargs.pop('extra_fields') |
|
349 | 354 | count = kwargs.pop('count') |
|
350 | 355 | line = kwargs.pop('line') |
|
351 | 356 | super(RCSubLineEditForm, self).__init__(*args, **kwargs) |
|
352 | 357 | for label, value in extra_fields.items(): |
|
353 | 358 | self.fields[label] = forms.CharField(initial=value, |
|
354 | 359 | widget=forms.TextInput(attrs={'name':'%s|%s|%s' % (count, line, label)})) |
|
355 | 360 | |
|
356 | 361 | |
|
357 | 362 | class RCImportForm(forms.Form): |
|
358 | 363 | |
|
359 | 364 | file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) |
|
360 | 365 | |
|
361 | 366 | |
|
362 | 367 | class RCLineCodesForm(forms.ModelForm): |
|
363 | 368 | |
|
364 | 369 | def __init__(self, *args, **kwargs): |
|
365 | 370 | super(RCLineCodesForm, self).__init__(*args, **kwargs) |
|
366 | 371 | |
|
367 | 372 | if 'initial' in kwargs: |
|
368 | 373 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), |
|
369 | 374 | initial=kwargs['initial']['code']) |
|
370 | 375 | if 'instance' in kwargs: |
|
371 | 376 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), |
|
372 | 377 | initial=kwargs['instance'].pk) |
|
373 | 378 | |
|
374 | 379 | self.fields['codes'].widget = CodesWidget() |
|
375 | 380 | |
|
376 | 381 | |
|
377 | 382 | class Meta: |
|
378 | 383 | model = RCLineCode |
|
379 | 384 | exclude = ('name',) |
|
380 | 385 | |
|
381 | 386 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now