def form_from_xml(xml_parser, req=None, init=False, usr=None, post=None, files=None, path=None): input_array = xml_parser.getroot().find('inputArray') def gen_file_format_string(file_format): if file_format == 'undefined': return '' else: file_format_list = file_format.split(',') new_list = list() for each in file_format_list: if each and each != '.': new_list.append(each if each.startswith('.') else '.' + each) return ','.join(new_list) if req: custom_form = CustomForm(req.POST, req.FILES) elif init and post: custom_form = CustomForm(post, files) else: custom_form = CustomForm() if input_array is not None: for input_item in input_array: if input_item.tag == "inputItem": try: if input_item.attrib["optional"] == '1': optional_prop = False else: optional_prop = True except: optional_prop = True try: help_line = input_item.attrib["help"] or '' if help_line == 'undefined': help_line = '' except: help_line = '' field = None if input_item.attrib["type"] == "NUM": # numeric input # protect empty MAX and MIN limits if input_item.attrib["max"]: max_decimal = Decimal(input_item.attrib["max"]) else: max_decimal = None if input_item.attrib["min"]: min_decimal = Decimal(input_item.attrib["min"]) else: min_decimal = None field = forms.DecimalField( initial=input_item.attrib["val"], max_value=max_decimal, min_value=min_decimal, required=optional_prop, help_text=help_line ) elif input_item.attrib["type"] == "TEX": # text box field = forms.CharField( initial=input_item.attrib["val"], max_length=100, required=optional_prop, help_text=help_line, widget=forms.TextInput(attrs={'type': 'text', }) ) elif input_item.attrib["type"] == "TAR": # text area field = forms.CharField( initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.Textarea( attrs={ 'cols': 15, 'rows': 3, } ) ) elif input_item.attrib["type"] == "CHB": # check box checked = False if input_item.attrib["val"] == "true": checked = True field = forms.BooleanField( required=False, initial=checked, help_text=help_line ) elif input_item.attrib["type"] == "DRP": # drop down list drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text),) field = forms.ChoiceField( choices=drop_options, initial=input_item.attrib["val"], help_text=help_line ) elif input_item.attrib["type"] == "DTS": # custom dataset (drop down list control) drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text),) field = forms.ChoiceField( choices=drop_options, initial=input_item.attrib["val"], help_text=help_line ) elif input_item.attrib["type"] == "RAD": # radio buttons radio_options = tuple() for alt in input_item.find('altArray').findall('altItem'): radio_options = radio_options + ((alt.text, alt.text),) field = forms.ChoiceField( initial=input_item.attrib["val"], widget=forms.RadioSelect(attrs={'value': input_item.attrib["default"]}), choices=radio_options, help_text=help_line ) elif input_item.attrib["type"] == "FIL" or input_item.attrib["type"] == "TPL": # file upload field # print path, files field = forms.FileField( # initial=loc required=optional_prop, help_text=help_line, widget=forms.ClearableFileInput( attrs={ 'class': input_item.attrib["type"], 'which': input_item.attrib["default"], 'accept': gen_file_format_string(input_item.attrib.get("file_format", '')), } ) ) elif input_item.attrib["type"] == "MLT": # multiple select mult_options = tuple() for alt in input_item.find('altArray').findall('altItem'): mult_options = mult_options + ((alt.text, alt.text),) field = forms.MultipleChoiceField( initial=re.split(',', input_item.attrib["val"]), help_text=help_line, choices=mult_options ) # Dotmatix samples control elif input_item.attrib["type"] == "DTM_SAMPLES": # custom dataset (multiple select) # group_list_of_tuples = list() # sample_list_of_tuples = list() # push r-code here to populate dtm_samples group_list_of_tuples = rora.get_dtm_screen_groups() sample_list_of_tuples = rora.get_dtm_screens() dtm_options = list() dtm_options.append(tuple(( 'Groups', tuple(group_list_of_tuples) ))) dtm_options.append(tuple(( 'Individual Screens', tuple(sample_list_of_tuples) ))) field = forms.MultipleChoiceField( choices=dtm_options, initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.SelectMultiple(attrs={'class': 'dotmatix_samples'}) ) elif input_item.attrib["type"] == "SCREEN_GROUPS": # group_list_of_tuples = list() group_list_of_tuples = rora.get_dtm_screen_groups() dtm_options = list() dtm_options.append(tuple(( 'Groups', tuple(group_list_of_tuples) ))) field = forms.MultipleChoiceField( choices=dtm_options, initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.SelectMultiple(attrs={'class': 'dotmatix_samples'}) ) elif input_item.attrib["type"] == "HED": # section header pass else: pass if field: custom_form.fields[input_item.attrib["comment"]] = field elif input_item.tag == "subSection": print "section" else: pass return custom_form
def form_from_xml(xml, req=None, init=False, usr=None): input_array = xml.getroot().find('inputArray') if req: custom_form = CustomForm(req.POST, req.FILES) elif init: custom_form = CustomForm(req.POST, req.FILES) else: custom_form = CustomForm() if input_array != None: for input_item in input_array: if input_item.tag == "inputItem": try: if input_item.attrib["optional"] == '1': optional_prop = False else: optional_prop = True except: optional_prop = True try: help_line = input_item.attrib["help"] except: help_line = '' if input_item.attrib["type"] == "NUM": # numeric input # protect empty MAX and MIN limits if input_item.attrib["max"]: max_decimal = Decimal(input_item.attrib["max"]) else: max_decimal = None if input_item.attrib["min"]: min_decimal = Decimal(input_item.attrib["min"]) else: min_decimal = None custom_form.fields[input_item.attrib["comment"]] = forms.DecimalField( initial=input_item.attrib["val"], max_value=max_decimal, min_value=min_decimal, required=optional_prop, help_text=help_line ) elif input_item.attrib["type"] == "TEX": # text box custom_form.fields[input_item.attrib["comment"]] = forms.CharField( initial=input_item.attrib["val"], max_length=100, required=optional_prop, help_text=help_line, widget=forms.TextInput(attrs={'type': 'text', }) ) elif input_item.attrib["type"] == "TAR": # text area custom_form.fields[input_item.attrib["comment"]] = forms.CharField( initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.Textarea( attrs={ 'cols': 15, 'rows': 3, } ) ) elif input_item.attrib["type"] == "CHB": # check box checked = False if input_item.attrib["val"] == "true": checked = True custom_form.fields[input_item.attrib["comment"]] = forms.BooleanField( required=False, initial=checked, help_text=help_line ) elif input_item.attrib["type"] == "DRP": # drop down list drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.ChoiceField( choices=drop_options, initial=input_item.attrib["val"], help_text=help_line ) elif input_item.attrib["type"] == "DTS": # custom dataset (drop down list control) drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.ChoiceField( choices=drop_options, initial=input_item.attrib["val"], help_text=help_line ) elif input_item.attrib["type"] == "RAD": # radio buttons radio_options = tuple() for alt in input_item.find('altArray').findall('altItem'): radio_options = radio_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.ChoiceField( initial=input_item.attrib["val"], widget=forms.RadioSelect(attrs={'value': input_item.attrib["default"]}), choices=radio_options, help_text=help_line ) elif input_item.attrib["type"] == "FIL" or input_item.attrib["type"] == "TPL": # file upload field custom_form.fields[input_item.attrib["comment"]] = forms.FileField( # initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.ClearableFileInput( attrs={ 'class': input_item.attrib["type"], 'which': input_item.attrib["default"], } ) ) elif input_item.attrib["type"] == "MLT": # multiple select mult_options = tuple() for alt in input_item.find('altArray').findall('altItem'): mult_options = mult_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.MultipleChoiceField( initial=re.split(',', input_item.attrib["val"]), help_text=help_line, choices=mult_options ) # Dotmatix samples control elif input_item.attrib["type"] == "DTM_SAMPLES": # custom dataset (multiple select) group_list_of_tuples = list() sample_list_of_tuples = list() # push r-code here to populate dtm_samples group_list_of_tuples = rora.get_dtm_screen_groups() sample_list_of_tuples = rora.get_dtm_screens() dtm_options = list() dtm_options.append( tuple(( 'Groups', tuple(group_list_of_tuples) )) ) dtm_options.append( tuple(( 'Individual Screens', tuple(sample_list_of_tuples) )) ) custom_form.fields[input_item.attrib["comment"]] = forms.MultipleChoiceField( choices=dtm_options, initial=input_item.attrib["val"], help_text=help_line, widget=forms.SelectMultiple(attrs={'class':'dotmatix_samples'}) ) elif input_item.attrib["type"] == "HED": # section header pass else: pass elif input_item.tag == "subSection": print "section" else: pass return custom_form
def form_from_xml(xml, req=None, init=False, usr=None): input_array = xml.getroot().find('inputArray') if req: custom_form = CustomForm(req.POST, req.FILES) elif init: custom_form = CustomForm(req.POST, req.FILES) else: custom_form = CustomForm() if input_array != None: for input_item in input_array: if input_item.tag == "inputItem": try: if input_item.attrib["optional"] == '1': optional_prop = False else: optional_prop = True except: optional_prop = True try: help_line = input_item.attrib["help"] except: help_line = '' if input_item.attrib["type"] == "NUM": # numeric input # protect empty MAX and MIN limits if input_item.attrib["max"]: max_decimal = Decimal(input_item.attrib["max"]) else: max_decimal = None if input_item.attrib["min"]: min_decimal = Decimal(input_item.attrib["min"]) else: min_decimal = None custom_form.fields[input_item.attrib["comment"]] = forms.DecimalField( initial=input_item.attrib["val"], max_value=max_decimal, min_value=min_decimal, required=optional_prop, help_text=help_line ) elif input_item.attrib["type"] == "TEX": # text box custom_form.fields[input_item.attrib["comment"]] = forms.CharField( initial=input_item.attrib["val"], max_length=100, required=optional_prop, help_text=help_line, widget=forms.TextInput(attrs={'type': 'text', }) ) elif input_item.attrib["type"] == "TAR": # text area custom_form.fields[input_item.attrib["comment"]] = forms.CharField( initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.Textarea( attrs={ 'cols': 15, 'rows': 3, } ) ) elif input_item.attrib["type"] == "CHB": # check box checked = False if input_item.attrib["val"] == "true": checked = True custom_form.fields[input_item.attrib["comment"]] = forms.BooleanField( required=False, initial=checked, help_text=help_line ) elif input_item.attrib["type"] == "DRP": # drop down list drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.ChoiceField( choices=drop_options, initial=input_item.attrib["val"], help_text=help_line ) elif input_item.attrib["type"] == "DTS": # custom dataset (drop down list control) drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.ChoiceField( choices=drop_options, initial=input_item.attrib["val"], help_text=help_line ) elif input_item.attrib["type"] == "RAD": # radio buttons radio_options = tuple() for alt in input_item.find('altArray').findall('altItem'): radio_options = radio_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.ChoiceField( initial=input_item.attrib["val"], widget=forms.RadioSelect(attrs={'value': input_item.attrib["default"]}), choices=radio_options, help_text=help_line ) elif input_item.attrib["type"] == "FIL" or input_item.attrib["type"] == "TPL": # file upload field custom_form.fields[input_item.attrib["comment"]] = forms.FileField( # initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.ClearableFileInput( attrs={ 'class': input_item.attrib["type"], 'which': input_item.attrib["default"], } ) ) elif input_item.attrib["type"] == "MLT": # multiple select mult_options = tuple() for alt in input_item.find('altArray').findall('altItem'): mult_options = mult_options + ((alt.text, alt.text),) custom_form.fields[input_item.attrib["comment"]] = forms.MultipleChoiceField( initial=re.split(',', input_item.attrib["val"]), help_text=help_line, choices=mult_options ) # Dotmatix samples control elif input_item.attrib["type"] == "DTM_SAMPLES": # custom dataset (multiple select) group_list_of_tuples = list() sample_list_of_tuples = list() # push r-code here to populate dtm_samples group_list_of_tuples = rora.get_dtm_screen_groups() sample_list_of_tuples = rora.get_dtm_screens() dtm_options = list() dtm_options.append( tuple(( 'Groups', tuple(group_list_of_tuples) )) ) dtm_options.append( tuple(( 'Individual Screens', tuple(sample_list_of_tuples) )) ) custom_form.fields[input_item.attrib["comment"]] = forms.MultipleChoiceField( choices=dtm_options, initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.SelectMultiple(attrs={'class':'dotmatix_samples'}) ) elif input_item.attrib["type"] == "HED": # section header pass else: pass elif input_item.tag == "subSection": print "section" else: pass return custom_form
def form_from_xml(xml_parser, req=None, init=False, usr=None, post=None, files=None, path=None): input_array = xml_parser.getroot().find('inputArray') def gen_file_format_string(file_format): if file_format == 'undefined': return '' else: file_format_list = file_format.split(',') new_list = list() for each in file_format_list: if each and each != '.': new_list.append(each if each.startswith('.') else '.' + each) return ','.join(new_list) if req: custom_form = CustomForm(req.POST, req.FILES) elif init and post: custom_form = CustomForm(post, files) else: custom_form = CustomForm() if input_array is not None: for input_item in input_array: if input_item.tag == "inputItem": try: if input_item.attrib["optional"] == '1': optional_prop = False else: optional_prop = True except: optional_prop = True try: help_line = input_item.attrib["help"] or '' if help_line == 'undefined': help_line = '' except: help_line = '' field = None if input_item.attrib["type"] == "NUM": # numeric input # protect empty MAX and MIN limits if input_item.attrib["max"]: max_decimal = Decimal(input_item.attrib["max"]) else: max_decimal = None if input_item.attrib["min"]: min_decimal = Decimal(input_item.attrib["min"]) else: min_decimal = None field = forms.DecimalField( initial=input_item.attrib["val"], max_value=max_decimal, min_value=min_decimal, required=optional_prop, help_text=help_line) elif input_item.attrib["type"] == "TEX": # text box field = forms.CharField( initial=input_item.attrib["val"], max_length=100, required=optional_prop, help_text=help_line, widget=forms.TextInput(attrs={ 'type': 'text', })) elif input_item.attrib["type"] == "TAR": # text area field = forms.CharField(initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.Textarea(attrs={ 'cols': 15, 'rows': 3, })) elif input_item.attrib["type"] == "CHB": # check box checked = False if input_item.attrib["val"] == "true": checked = True field = forms.BooleanField(required=False, initial=checked, help_text=help_line) elif input_item.attrib["type"] == "DRP": # drop down list drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text), ) field = forms.ChoiceField(choices=drop_options, initial=input_item.attrib["val"], help_text=help_line) elif input_item.attrib[ "type"] == "DTS": # custom dataset (drop down list control) drop_options = tuple() for alt in input_item.find('altArray').findall('altItem'): drop_options = drop_options + ((alt.text, alt.text), ) field = forms.ChoiceField(choices=drop_options, initial=input_item.attrib["val"], help_text=help_line) elif input_item.attrib["type"] == "RAD": # radio buttons radio_options = tuple() for alt in input_item.find('altArray').findall('altItem'): radio_options = radio_options + ( (alt.text, alt.text), ) field = forms.ChoiceField( initial=input_item.attrib["val"], widget=forms.RadioSelect( attrs={'value': input_item.attrib["default"]}), choices=radio_options, help_text=help_line) elif input_item.attrib["type"] == "FIL" or input_item.attrib[ "type"] == "TPL": # file upload field # print path, files field = forms.FileField( # initial=loc required=optional_prop, help_text=help_line, widget=forms.ClearableFileInput( attrs={ 'class': input_item.attrib["type"], 'which': input_item.attrib["default"], 'accept': gen_file_format_string( input_item.attrib.get("file_format", '')), })) elif input_item.attrib["type"] == "MLT": # multiple select mult_options = tuple() for alt in input_item.find('altArray').findall('altItem'): mult_options = mult_options + ((alt.text, alt.text), ) field = forms.MultipleChoiceField(initial=re.split( ',', input_item.attrib["val"]), help_text=help_line, choices=mult_options) # Dotmatix samples control elif input_item.attrib[ "type"] == "DTM_SAMPLES": # custom dataset (multiple select) # group_list_of_tuples = list() # sample_list_of_tuples = list() # push r-code here to populate dtm_samples group_list_of_tuples = rora.get_dtm_screen_groups() sample_list_of_tuples = rora.get_dtm_screens() dtm_options = list() dtm_options.append( tuple(('Groups', tuple(group_list_of_tuples)))) dtm_options.append( tuple(('Individual Screens', tuple(sample_list_of_tuples)))) field = forms.MultipleChoiceField( choices=dtm_options, initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.SelectMultiple( attrs={'class': 'dotmatix_samples'})) elif input_item.attrib["type"] == "SCREEN_GROUPS": # group_list_of_tuples = list() group_list_of_tuples = rora.get_dtm_screen_groups() dtm_options = list() dtm_options.append( tuple(('Groups', tuple(group_list_of_tuples)))) field = forms.MultipleChoiceField( choices=dtm_options, initial=input_item.attrib["val"], required=optional_prop, help_text=help_line, widget=forms.SelectMultiple( attrs={'class': 'dotmatix_samples'})) elif input_item.attrib["type"] == "HED": # section header pass else: pass if field: custom_form.fields[input_item.attrib["comment"]] = field elif input_item.tag == "subSection": print "section" else: pass return custom_form