From d00d7019c5054e9fe49b299807451ddb37f6a5ca Mon Sep 17 00:00:00 2001 From: chirac Date: Mon, 28 Dec 2020 16:19:29 +0100 Subject: [PATCH] Add autocomplete on machine form fields --- machines/forms.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++- machines/urls.py | 9 +++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/machines/forms.py b/machines/forms.py index 03f1ecc7..7141bcfc 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -40,7 +40,7 @@ from django.forms import ModelForm, Form from django.utils.translation import ugettext_lazy as _ from re2o.field_permissions import FieldPermissionFormMixin -from re2o.mixins import FormRevMixin +from re2o.mixins import FormRevMixin, AutocompleteModelMixin, AutocompleteMultipleModelMixin from .models import ( Domain, Machine, @@ -71,6 +71,11 @@ class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): class Meta: model = Machine fields = "__all__" + widgets = { + "user": AutocompleteModelMixin( + url="/users/user-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -91,6 +96,17 @@ class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): class Meta: model = Interface fields = ["machine", "machine_type", "ipv4", "mac_address", "details"] + widgets = { + "machine": AutocompleteModelMixin( + url="/machines/machine-autocomplete", + ), + "machine_type": AutocompleteModelMixin( + url="/machines/machinetype-autocomplete", + ), + "ipv4": AutocompleteModelMixin( + url="/machines/ipv4-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -139,6 +155,11 @@ class AliasForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): class Meta: model = Domain fields = ["name", "extension", "ttl"] + widgets = { + "extension": AutocompleteModelMixin( + url="/machines/extension-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -222,6 +243,14 @@ class IpTypeForm(FormRevMixin, ModelForm): class Meta: model = IpType fields = "__all__" + widgets = { + "vlan": AutocompleteModelMixin( + url="/machines/vlan-autocomplete", + ), + "extension": AutocompleteModelMixin( + url="/machines/extension-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -351,6 +380,14 @@ class MxForm(FormRevMixin, ModelForm): class Meta: model = Mx fields = ["zone", "priority", "name", "ttl"] + widgets = { + "zone": AutocompleteModelMixin( + url="/machines/extension-autocomplete", + ), + "name": AutocompleteModelMixin( + url="/machines/domain-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -386,6 +423,14 @@ class NsForm(FormRevMixin, ModelForm): class Meta: model = Ns fields = ["zone", "ns", "ttl"] + widgets = { + "zone": AutocompleteModelMixin( + url="/machines/extension-autocomplete", + ), + "ns": AutocompleteModelMixin( + url="/machines/domain-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -419,6 +464,11 @@ class TxtForm(FormRevMixin, ModelForm): class Meta: model = Txt fields = "__all__" + widgets = { + "zone": AutocompleteModelMixin( + url="/machines/extension-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -449,6 +499,11 @@ class DNameForm(FormRevMixin, ModelForm): class Meta: model = DName fields = "__all__" + widgets = { + "zone": AutocompleteModelMixin( + url="/machines/extension-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -479,6 +534,14 @@ class SrvForm(FormRevMixin, ModelForm): class Meta: model = Srv fields = "__all__" + widgets = { + "extension": AutocompleteModelMixin( + url="/machines/extension-autocomplete", + ), + "target": AutocompleteModelMixin( + url="/machines/domain-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -509,6 +572,14 @@ class NasForm(FormRevMixin, ModelForm): class Meta: model = Nas fields = "__all__" + widgets = { + "nas_type": AutocompleteModelMixin( + url="/machines/machinetype-autocomplete", + ), + "machine_type": AutocompleteModelMixin( + url="/machines/machinetype-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -539,6 +610,11 @@ class RoleForm(FormRevMixin, ModelForm): class Meta: model = Role fields = "__all__" + widgets = { + "servers": AutocompleteMultipleModelMixin( + url="/machines/interface-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -572,6 +648,11 @@ class ServiceForm(FormRevMixin, ModelForm): class Meta: model = Service fields = "__all__" + widgets = { + "servers": AutocompleteMultipleModelMixin( + url="/machines/interface-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) @@ -656,6 +737,11 @@ class EditOuverturePortConfigForm(FormRevMixin, ModelForm): class Meta: model = Interface fields = ["port_lists"] + widgets = { + "port_lists": AutocompleteMultipleModelMixin( + url="/machines/ouvertureportlist-autocomplete", + ), + } def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) diff --git a/machines/urls.py b/machines/urls.py index a8e1dbea..2967f7fd 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -29,6 +29,7 @@ from __future__ import unicode_literals from django.conf.urls import url from . import views +from . import views_autocomplete urlpatterns = [ url(r"^new_machine/(?P[0-9]+)$", views.new_machine, name="new-machine"), @@ -153,4 +154,12 @@ urlpatterns = [ views.configure_ports, name="port-config", ), + ### Autocomplete Views + url(r'^vlan-autocomplete/$', views_autocomplete.VlanAutocomplete.as_view(), name='vlan-autocomplete',), + url(r'^interface-autocomplete/$', views_autocomplete.InterfaceAutocomplete.as_view(), name='interface-autocomplete',), + url(r'^machine-autocomplete/$', views_autocomplete.MachineAutocomplete.as_view(), name='machine-autocomplete',), + url(r'^machinetype-autocomplete/$', views_autocomplete.MachineTypeAutocomplete.as_view(), name='machinetype-autocomplete',), + url(r'^extension-autocomplete/$', views_autocomplete.ExtensionAutocomplete.as_view(), name='extension-autocomplete',), + url(r'^domain-autocomplete/$', views_autocomplete.DomainAutocomplete.as_view(), name='domain-autocomplete',), + url(r'^ouvertureportlist-autocomplete/$', views_autocomplete.OuverturePortListAutocomplete.as_view(), name='ouvertureportlist-autocomplete',), ]