Enable dynamic edit form for DataSource

This commit is contained in:
jeremystretch 2023-01-31 11:55:55 -05:00
parent 1000fe5a3d
commit 6f2786da0a
4 changed files with 38 additions and 24 deletions

View File

@ -6,6 +6,7 @@ from urllib.parse import quote, urlunparse, urlparse
from django import forms
from django.conf import settings
from django.utils.translation import gettext as _
from .exceptions import SyncError
@ -46,13 +47,19 @@ class LocalBackend(DataBackend):
class GitBackend(DataBackend):
parameters = {
'username': forms.CharField(
required=False
required=False,
label=_('Username'),
widget=forms.TextInput(attrs={'class': 'form-control'})
),
'password': forms.CharField(
required=False
required=False,
label=_('Password'),
widget=forms.TextInput(attrs={'class': 'form-control'})
),
'branch': forms.CharField(
required=False
required=False,
label=_('Branch'),
widget=forms.TextInput(attrs={'class': 'form-control'})
)
}

View File

@ -1,5 +1,8 @@
import copy
from core.models import *
from netbox.forms import NetBoxModelForm, StaticSelect
from ..models.data import BACKEND_CLASSES
__all__ = (
'DataSourceForm',
@ -19,22 +22,28 @@ class DataSourceForm(NetBoxModelForm):
]
widgets = {
'type': StaticSelect(
# attrs={
# 'hx-get': '.',
# 'hx-include': '#form_fields input',
# 'hx-target': '#form_fields',
# }
attrs={
'hx-get': '.',
'hx-include': '#form_fields input',
'hx-target': '#form_fields',
}
),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance:
backend = self.instance.get_backend()
for name, form_field in backend.parameters.items():
field_name = f'backend_{name}'
self.fields[field_name] = form_field
if self.is_bound and self.data.get('type') in BACKEND_CLASSES:
backend_type = self.data['type']
elif self.initial and self.initial.get('type') in BACKEND_CLASSES:
backend_type = self.initial['type']
else:
backend_type = self.fields['type'].initial
backend = BACKEND_CLASSES.get(backend_type)
for name, form_field in backend.parameters.items():
field_name = f'backend_{name}'
self.fields[field_name] = copy.copy(form_field)
if self.instance and self.instance.parameters:
self.fields[field_name].initial = self.instance.parameters.get(name)
def save(self, *args, **kwargs):

View File

@ -27,6 +27,11 @@ __all__ = (
logger = logging.getLogger('netbox.core.data')
BACKEND_CLASSES = {
DataSourceTypeChoices.LOCAL: LocalBackend,
DataSourceTypeChoices.GIT: GitBackend,
}
class DataSource(ChangeLoggedModel):
"""
@ -124,10 +129,7 @@ class DataSource(ChangeLoggedModel):
return job_result
def get_backend(self):
backend_cls = {
DataSourceTypeChoices.LOCAL: LocalBackend,
DataSourceTypeChoices.GIT: GitBackend,
}.get(self.type)
backend_cls = BACKEND_CLASSES.get(self.type)
backend_params = self.parameters or {}
return backend_cls(self.url, **backend_params)

View File

@ -67,10 +67,6 @@
{{ ''|placeholder }}
{% endif %}</td>
</tr>
<tr>
<th scope="row">File count</th>
<td>{{ object.datafiles.count }}</td>
</tr>
</table>
</div>
</div>
@ -81,10 +77,10 @@
<h5 class="card-header">Backend</h5>
<div class="card-body">
<table class="table table-hover attr-table">
{% for name, value in object.parameters.items %}
{% for name, field in object.get_backend.parameters.items %}
<tr>
<th scope="row">{{ name }}</th>
<td>{{ value|placeholder }}</td>
<th scope="row">{{ field.label }}</th>
<td>{{ object.parameters|get_key:name|placeholder }}</td>
</tr>
{% empty %}
<tr>