mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Added JS for SlugField autofill
This commit is contained in:
@ -4,6 +4,7 @@ from django.db.models import Count
|
||||
from dcim.models import Site, Device, Interface, Rack, IFACE_FF_VIRTUAL
|
||||
from utilities.forms import (
|
||||
APISelect, BootstrapMixin, BulkImportForm, CommentField, ConfirmationForm, CSVDataField, Livesearch, SmallTextarea,
|
||||
SlugField,
|
||||
)
|
||||
|
||||
from .models import PORT_SPEED_CHOICES, Circuit, CircuitType, Provider
|
||||
@ -14,6 +15,7 @@ from .models import PORT_SPEED_CHOICES, Circuit, CircuitType, Provider
|
||||
#
|
||||
|
||||
class ProviderForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
comments = CommentField()
|
||||
|
||||
class Meta:
|
||||
@ -25,7 +27,6 @@ class ProviderForm(forms.ModelForm, BootstrapMixin):
|
||||
}
|
||||
help_texts = {
|
||||
'name': "Full name of the provider",
|
||||
'slug': "URL-friendly unique shorthand (e.g. 'decix' for DE-CIX)",
|
||||
'asn': "BGP autonomous system number (if applicable)",
|
||||
'portal_url': "URL of the provider's customer support portal",
|
||||
'noc_contact': "NOC email address and phone number",
|
||||
@ -63,6 +64,7 @@ class ProviderBulkDeleteForm(ConfirmationForm):
|
||||
#
|
||||
|
||||
class CircuitTypeForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = CircuitType
|
||||
|
@ -6,7 +6,7 @@ from django.db.models import Count, Q
|
||||
from ipam.models import IPAddress
|
||||
from utilities.forms import (
|
||||
APISelect, BootstrapMixin, BulkImportForm, CommentField, ConfirmationForm, CSVDataField, ExpandableNameField,
|
||||
FlexibleModelChoiceField, Livesearch, SelectWithDisabled, SmallTextarea,
|
||||
FlexibleModelChoiceField, Livesearch, SelectWithDisabled, SmallTextarea, SlugField,
|
||||
)
|
||||
|
||||
from .models import (
|
||||
@ -43,6 +43,7 @@ def get_device_by_name_or_pk(name):
|
||||
#
|
||||
|
||||
class SiteForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
comments = CommentField()
|
||||
|
||||
class Meta:
|
||||
@ -54,7 +55,6 @@ class SiteForm(forms.ModelForm, BootstrapMixin):
|
||||
}
|
||||
help_texts = {
|
||||
'name': "Full name of the site",
|
||||
'slug': "URL-friendly unique shorthand (e.g. 'nyc3' for NYC3)",
|
||||
'facility': "Data center provider and facility (e.g. Equinix NY7)",
|
||||
'asn': "BGP autonomous system number",
|
||||
'physical_address': "Physical location of the building (e.g. for GPS)",
|
||||
@ -78,6 +78,7 @@ class SiteImportForm(BulkImportForm, BootstrapMixin):
|
||||
#
|
||||
|
||||
class RackGroupForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = RackGroup
|
||||
@ -195,6 +196,7 @@ class RackFilterForm(forms.Form, BootstrapMixin):
|
||||
#
|
||||
|
||||
class ManufacturerForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = Manufacturer
|
||||
@ -210,6 +212,7 @@ class ManufacturerBulkDeleteForm(ConfirmationForm):
|
||||
#
|
||||
|
||||
class DeviceTypeForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField(slug_source='model')
|
||||
|
||||
class Meta:
|
||||
model = DeviceType
|
||||
@ -286,6 +289,7 @@ class InterfaceTemplateForm(forms.ModelForm, BootstrapMixin):
|
||||
#
|
||||
|
||||
class DeviceRoleForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = DeviceRole
|
||||
@ -301,6 +305,7 @@ class DeviceRoleBulkDeleteForm(ConfirmationForm):
|
||||
#
|
||||
|
||||
class PlatformForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = Platform
|
||||
|
@ -4,7 +4,9 @@ from django import forms
|
||||
from django.db.models import Count
|
||||
|
||||
from dcim.models import Site, Device, Interface
|
||||
from utilities.forms import BootstrapMixin, ConfirmationForm, APISelect, Livesearch, CSVDataField, BulkImportForm
|
||||
from utilities.forms import (
|
||||
BootstrapMixin, ConfirmationForm, APISelect, Livesearch, CSVDataField, BulkImportForm, SlugField,
|
||||
)
|
||||
|
||||
from .models import (
|
||||
Aggregate, IPAddress, Prefix, PREFIX_STATUS_CHOICES, RIR, Role, VLAN, VLAN_STATUS_CHOICES, VRF,
|
||||
@ -53,6 +55,7 @@ class VRFBulkDeleteForm(ConfirmationForm):
|
||||
#
|
||||
|
||||
class RIRForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = RIR
|
||||
@ -118,6 +121,7 @@ class AggregateFilterForm(forms.Form, BootstrapMixin):
|
||||
#
|
||||
|
||||
class RoleForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = Role
|
||||
|
@ -5,6 +5,27 @@ $(document).ready(function() {
|
||||
$(this).parents('table').find('td input:checkbox').prop('checked', $(this).prop('checked'));
|
||||
});
|
||||
|
||||
// Slugify
|
||||
function slugify(s, num_chars) {
|
||||
s = s.replace(/[^-\.\+\w\s]/g, ''); // Remove unneeded chars
|
||||
s = s.replace(/^\s+|\s+$/g, ''); // Trim leading/trailing spaces
|
||||
s = s.replace(/[-\s]+/g, '-'); // Convert spaces to hyphens
|
||||
s = s.toLowerCase(); // Convert to lowercase
|
||||
return s.substring(0, num_chars); // Trim to first num_chars chars
|
||||
}
|
||||
var slug_field = $('#id_slug');
|
||||
slug_field.change(function() {
|
||||
$(this).attr('_changed', true);
|
||||
});
|
||||
if (slug_field) {
|
||||
var slug_source = $('#id_' + slug_field.attr('slug-source'));
|
||||
slug_source.keyup(function() {
|
||||
if (slug_field && !slug_field.attr('_changed')) {
|
||||
slug_field.val(slugify($(this).val(), 50));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Helper select fields
|
||||
$('select.helper-parent').change(function () {
|
||||
|
||||
|
@ -5,7 +5,7 @@ from django import forms
|
||||
from django.db.models import Count
|
||||
|
||||
from dcim.models import Device
|
||||
from utilities.forms import BootstrapMixin, BulkImportForm, ConfirmationForm, CSVDataField
|
||||
from utilities.forms import BootstrapMixin, BulkImportForm, ConfirmationForm, CSVDataField, SlugField
|
||||
|
||||
from .models import Secret, SecretRole, UserKey
|
||||
|
||||
@ -35,6 +35,7 @@ def validate_rsa_key(key, is_secret=True):
|
||||
#
|
||||
|
||||
class SecretRoleForm(forms.ModelForm, BootstrapMixin):
|
||||
slug = SlugField()
|
||||
|
||||
class Meta:
|
||||
model = SecretRole
|
||||
|
@ -7,7 +7,6 @@
|
||||
<div class="panel-body">
|
||||
{% render_field form.name %}
|
||||
{% render_field form.slug %}
|
||||
{% render_field form.category %}
|
||||
{% render_field form.asn %}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -203,6 +203,15 @@ class FlexibleModelChoiceField(forms.ModelChoiceField):
|
||||
return value
|
||||
|
||||
|
||||
class SlugField(forms.SlugField):
|
||||
|
||||
def __init__(self, slug_source='name', *args, **kwargs):
|
||||
label = kwargs.pop('label', "Slug")
|
||||
help_text = kwargs.pop('help_text', "URL-friendly unique shorthand")
|
||||
super(SlugField, self).__init__(label=label, help_text=help_text, *args, **kwargs)
|
||||
self.widget.attrs['slug-source'] = slug_source
|
||||
|
||||
|
||||
#
|
||||
# Forms
|
||||
#
|
||||
|
Reference in New Issue
Block a user