forms.py
86 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
|
r23 | import json | |
|
r13 | from django import forms | |
|
r25 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode | |
def create_choices_from_model(model, conf_id): | |||
if model=='RCLine': | |||
instance = RCConfiguration.objects.get(pk=conf_id) | |||
return instance.get_refs_lines() | |||
else: | |||
instance = globals()[model] | |||
return instance.objects.all().values_list('pk', 'name') | |||
|
r13 | ||
class RCConfigurationForm(forms.ModelForm): | |||
class Meta: | |||
model = RCConfiguration | |||
|
r23 | exclude = ('clock', 'ipp', 'ntx', 'clock_divider') | |
class RCLineForm(forms.ModelForm): | |||
|
r25 | def __init__(self, *args, **kwargs): | |
self.extra_fields = kwargs.pop('extra_fields', []) | |||
super(RCLineForm, self).__init__(*args, **kwargs) | |||
if 'initial'in kwargs: | |||
for item in self.extra_fields: | |||
if 'model' in item: | |||
self.fields[item['name']] = forms.ChoiceField(choices=create_choices_from_model(item['model'], | |||
kwargs['initial']['rc_configuration'])) | |||
else: | |||
self.fields[item['name']] = forms.CharField(initial=item['value']) | |||
|
r23 | class Meta: | |
model = RCLine | |||
fields = ('rc_configuration', 'line_type', 'channel') | |||
widgets = { | |||
'channel': forms.HiddenInput(), | |||
} | |||
def save(self): | |||
line = super(RCLineForm, self).save() | |||
|
r25 | ||
|
r23 | #auto add channel | |
line.channel = RCLine.objects.filter(rc_configuration=line.rc_configuration).count()-1 | |||
|
r25 | ||
|
r23 | #auto add position for TX, TR & CODE | |
|
r25 | if line.line_type.name in ('tx', 'tr', 'codes', 'windows'): | |
|
r23 | line.position = RCLine.objects.filter(rc_configuration=line.rc_configuration, line_type=line.line_type).count()-1 | |
|
r25 | ||
#save extra fields in params | |||
|
r23 | params = {} | |
|
r25 | for item in self.extra_fields: | |
params[item['name']] = self.data[item['name']] | |||
|
r23 | line.params = json.dumps(params) | |
line.save() | |||
return | |||
class RCLineViewForm(forms.Form): | |||
def __init__(self, *args, **kwargs): | |||
extra_fields = kwargs.pop('extra_fields') | |||
super(RCLineViewForm, self).__init__(*args, **kwargs) | |||
for label, value in extra_fields.items(): | |||
|
r25 | if 'ref' in label: | |
value = RCLine.objects.get(pk=value).get_name() | |||
elif 'code' in label: | |||
value = RCLineCode.objects.get(pk=value).name | |||
self.fields[label] = forms.CharField(initial=value) | |||
class RCLineEditForm(forms.Form): | |||
def __init__(self, *args, **kwargs): | |||
self.extra_fields = kwargs.pop('extra_fields', []) | |||
super(RCLineEditForm, self).__init__(*args, **kwargs) | |||
if 'initial'in kwargs: | |||
for item in self.extra_fields: | |||
if 'model' in item: | |||
self.fields[item['name']] = forms.ChoiceField(choices=create_choices_from_model(item['model'], | |||
kwargs['initial']['rc_configuration']), | |||
initial=item['value'], | |||
widget=forms.Select(attrs={'name':'%s|%s' % (kwargs['initial']['line'], item['name'])})) | |||
else: | |||
self.fields[item['name']] = forms.CharField(initial=item['value'], | |||
widget=forms.TextInput(attrs={'name':'%s|%s' % (kwargs['initial']['line'], item['name'])})) | |||