Currently i am doing this:
[
{
"name": "hostname",
"type": "string",
"description": "Hostname router",
"required": "Yes" (No if it is not required)
},
{
"name": "is_dhcp",
"type": "choice",
"description": "DHCP?",
"choices": [
[
"yes",
"Yes"
],
[
"no",
"No"
]
],
"required": "No"
},
{
"name": "gateway",
"type": "ip",
"description": "Gateway DHCP",
"required": "Yes"
},
{
"name": "sub_int_bck",
"type": "integer",
"description": "In case of existing! - Sub interface",
"required": "No"
},
{
"name": "exIpList",
"type": "list",
"description": "List excluded IPs, seperated by ;",
"required": "No"
}
]
Then i use a big amount of If’s to check one by one:
if var_type.lower() == 'string':
self.fields[var_name] = forms.CharField(
label=var_description,
required=var_req_bool,
widget=forms.TextInput(attrs={"class": "form-control"})
)
elif var_type.lower() == 'integer':
self.fields[var_name] = forms.IntegerField(
label=var_description,
required=var_req_bool,
widget=forms.TextInput(attrs={"class": "form-control"})
)
elif var_type.lower() == 'choice':
self.fields[var_name] = forms.ChoiceField(
label=var_description,
choices=var.get('choices', []), # Default to empty list if not provided
widget=forms.Select(attrs={"class": "form-control"}),
required=var_req_bool
)
elif var_type.lower() == 'ip' and 'mask' in var_description.lower():
self.fields[var_name] = forms.GenericIPAddressField(
label=var_description,
required=var_req_bool,
widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "255.255.255.0 OU 0.0.0.255 OU 24"}),
)
elif var_type.lower() == 'ip':
self.fields[var_name] = forms.GenericIPAddressField(
label=var_description,
required=var_req_bool,
widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "192.168.1.1"}),
)
elif var_type.lower() == 'list':
self.fields[var_name] = forms.CharField(
label=var_description,
required=var_req_bool,
widget=forms.Textarea(attrs={"class": "form-control"}),
)
elif var_type.lower() == 'bool':
self.fields[var_name] = forms.BooleanField(
label=var_description,
required=var_req_bool,
widget=forms.CheckboxInput(attrs={"class": "form-control"}),
)
The objective is to gather all the variables needed to generate a jinja2 template with configurations for routers and switches.
Currently some stuff that is scaring me is:
1 – There are groups of configurations that are optional, but currently i need to display everything so let’s assume the total variables are 30, but there are 3 groups of 10 variables. If the user says no to one group he will still see the other 9 questions even if they are not needed, which causes a lot of bloat.
2 – I think there will be a lot of alternative tags and that will make things confusing (i think), currently i need to add at least 2 more tags, because i need to identify if it’s a normal subnet mask, reverse or prefix. This is worrying me because even with everything documented it will probably cause confusion when creating the json (It needs to be done by hand)
Is there a way to simplify this?
Am i doing this correctly or is there a better way / smarter way to do it?
I created this JSON logic from scratch since i do not know any django package that could help me creating dynamic forms.
But i feel like it is getting too complex to be maintained when the configurations need 20+ variables to be generated.