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