mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-15 16:22:18 -06:00
Compare commits
11 Commits
v4.4.7
...
d6df5d0fc8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6df5d0fc8 | ||
|
|
9ae53fc232 | ||
|
|
6efb258b9f | ||
|
|
da1e0f4b53 | ||
|
|
7f39f75d3d | ||
|
|
ebf8f7fa1b | ||
|
|
922b08c0ff | ||
|
|
84864fa5e1 | ||
|
|
767dfccd8f | ||
|
|
dc4bab7477 | ||
|
|
60aa952eb1 |
@@ -12,7 +12,7 @@ Depending on its classification, each NetBox model may support various features
|
|||||||
|
|
||||||
| Feature | Feature Mixin | Registry Key | Description |
|
| Feature | Feature Mixin | Registry Key | Description |
|
||||||
|------------------------------------------------------------|-------------------------|---------------------|-----------------------------------------------------------------------------------------|
|
|------------------------------------------------------------|-------------------------|---------------------|-----------------------------------------------------------------------------------------|
|
||||||
| [Bookmarks](../features/customization.md#bookmarks) | `BookmarksMixin` | `bookmarks` | These models can be bookmarked natively in the user interface |
|
| [Bookmarks](../features/user-preferences.md#bookmarks) | `BookmarksMixin` | `bookmarks` | These models can be bookmarked natively in the user interface |
|
||||||
| [Change logging](../features/change-logging.md) | `ChangeLoggingMixin` | `change_logging` | Changes to these objects are automatically recorded in the change log |
|
| [Change logging](../features/change-logging.md) | `ChangeLoggingMixin` | `change_logging` | Changes to these objects are automatically recorded in the change log |
|
||||||
| Cloning | `CloningMixin` | `cloning` | Provides the `clone()` method to prepare a copy |
|
| Cloning | `CloningMixin` | `cloning` | Provides the `clone()` method to prepare a copy |
|
||||||
| [Contacts](../features/contacts.md) | `ContactsMixin` | `contacts` | Contacts can be associated with these models |
|
| [Contacts](../features/contacts.md) | `ContactsMixin` | `contacts` | Contacts can be associated with these models |
|
||||||
|
|||||||
@@ -472,14 +472,30 @@ class ModuleTypeImportForm(NetBoxModelImportForm):
|
|||||||
required=False,
|
required=False,
|
||||||
help_text=_('Unit for module weight')
|
help_text=_('Unit for module weight')
|
||||||
)
|
)
|
||||||
|
attribute_data = forms.JSONField(
|
||||||
|
label=_('Attributes'),
|
||||||
|
required=False,
|
||||||
|
help_text=_('Attribute values for the assigned profile, passed as a dictionary')
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ModuleType
|
model = ModuleType
|
||||||
fields = [
|
fields = [
|
||||||
'manufacturer', 'model', 'part_number', 'description', 'airflow', 'weight', 'weight_unit', 'profile',
|
'manufacturer', 'model', 'part_number', 'description', 'airflow', 'weight', 'weight_unit', 'profile',
|
||||||
'comments', 'tags'
|
'attribute_data', 'comments', 'tags',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
super().clean()
|
||||||
|
|
||||||
|
# Attribute data may be included only if a profile is specified
|
||||||
|
if self.cleaned_data.get('attribute_data') and not self.cleaned_data.get('profile'):
|
||||||
|
raise forms.ValidationError(_("Profile must be specified if attribute data is provided."))
|
||||||
|
|
||||||
|
# Default attribute_data to an empty dictionary if a profile is specified (to enforce schema validation)
|
||||||
|
if self.cleaned_data.get('profile') and not self.cleaned_data.get('attribute_data'):
|
||||||
|
self.cleaned_data['attribute_data'] = {}
|
||||||
|
|
||||||
|
|
||||||
class DeviceRoleImportForm(NetBoxModelImportForm):
|
class DeviceRoleImportForm(NetBoxModelImportForm):
|
||||||
parent = CSVModelChoiceField(
|
parent = CSVModelChoiceField(
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import decimal
|
||||||
|
|
||||||
import django.core.validators
|
import django.core.validators
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
@@ -17,8 +19,8 @@ class Migration(migrations.Migration):
|
|||||||
max_digits=8,
|
max_digits=8,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[
|
validators=[
|
||||||
django.core.validators.MinValueValidator(-90.0),
|
django.core.validators.MinValueValidator(decimal.Decimal('-90.0')),
|
||||||
django.core.validators.MaxValueValidator(90.0),
|
django.core.validators.MaxValueValidator(decimal.Decimal('90.0'))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -31,8 +33,8 @@ class Migration(migrations.Migration):
|
|||||||
max_digits=9,
|
max_digits=9,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[
|
validators=[
|
||||||
django.core.validators.MinValueValidator(-180.0),
|
django.core.validators.MinValueValidator(decimal.Decimal('-180.0')),
|
||||||
django.core.validators.MaxValueValidator(180.0),
|
django.core.validators.MaxValueValidator(decimal.Decimal('180.0'))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -45,8 +47,8 @@ class Migration(migrations.Migration):
|
|||||||
max_digits=8,
|
max_digits=8,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[
|
validators=[
|
||||||
django.core.validators.MinValueValidator(-90.0),
|
django.core.validators.MinValueValidator(decimal.Decimal('-90.0')),
|
||||||
django.core.validators.MaxValueValidator(90.0),
|
django.core.validators.MaxValueValidator(decimal.Decimal('90.0'))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -59,8 +61,8 @@ class Migration(migrations.Migration):
|
|||||||
max_digits=9,
|
max_digits=9,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[
|
validators=[
|
||||||
django.core.validators.MinValueValidator(-180.0),
|
django.core.validators.MinValueValidator(decimal.Decimal('-180.0')),
|
||||||
django.core.validators.MaxValueValidator(180.0),
|
django.core.validators.MaxValueValidator(decimal.Decimal('180.0'))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -646,7 +646,10 @@ class Device(
|
|||||||
decimal_places=6,
|
decimal_places=6,
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[MinValueValidator(-90.0), MaxValueValidator(90.0)],
|
validators=[
|
||||||
|
MinValueValidator(decimal.Decimal('-90.0')),
|
||||||
|
MaxValueValidator(decimal.Decimal('90.0'))
|
||||||
|
],
|
||||||
help_text=_("GPS coordinate in decimal format (xx.yyyyyy)")
|
help_text=_("GPS coordinate in decimal format (xx.yyyyyy)")
|
||||||
)
|
)
|
||||||
longitude = models.DecimalField(
|
longitude = models.DecimalField(
|
||||||
@@ -655,7 +658,10 @@ class Device(
|
|||||||
decimal_places=6,
|
decimal_places=6,
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[MinValueValidator(-180.0), MaxValueValidator(180.0)],
|
validators=[
|
||||||
|
MinValueValidator(decimal.Decimal('-180.0')),
|
||||||
|
MaxValueValidator(decimal.Decimal('180.0'))
|
||||||
|
],
|
||||||
help_text=_("GPS coordinate in decimal format (xx.yyyyyy)")
|
help_text=_("GPS coordinate in decimal format (xx.yyyyyy)")
|
||||||
)
|
)
|
||||||
services = GenericRelation(
|
services = GenericRelation(
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import decimal
|
||||||
|
|
||||||
from django.contrib.contenttypes.fields import GenericRelation
|
from django.contrib.contenttypes.fields import GenericRelation
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.validators import MaxValueValidator, MinValueValidator
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
||||||
@@ -211,7 +213,10 @@ class Site(ContactsMixin, ImageAttachmentsMixin, PrimaryModel):
|
|||||||
decimal_places=6,
|
decimal_places=6,
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[MinValueValidator(-90.0), MaxValueValidator(90.0)],
|
validators=[
|
||||||
|
MinValueValidator(decimal.Decimal('-90.0')),
|
||||||
|
MaxValueValidator(decimal.Decimal('90.0'))
|
||||||
|
],
|
||||||
help_text=_('GPS coordinate in decimal format (xx.yyyyyy)')
|
help_text=_('GPS coordinate in decimal format (xx.yyyyyy)')
|
||||||
)
|
)
|
||||||
longitude = models.DecimalField(
|
longitude = models.DecimalField(
|
||||||
@@ -220,7 +225,10 @@ class Site(ContactsMixin, ImageAttachmentsMixin, PrimaryModel):
|
|||||||
decimal_places=6,
|
decimal_places=6,
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True,
|
null=True,
|
||||||
validators=[MinValueValidator(-180.0), MaxValueValidator(180.0)],
|
validators=[
|
||||||
|
MinValueValidator(decimal.Decimal('-180.0')),
|
||||||
|
MaxValueValidator(decimal.Decimal('180.0'))
|
||||||
|
],
|
||||||
help_text=_('GPS coordinate in decimal format (xx.yyyyyy)')
|
help_text=_('GPS coordinate in decimal format (xx.yyyyyy)')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import logging
|
|||||||
import traceback
|
import traceback
|
||||||
from contextlib import ExitStack
|
from contextlib import ExitStack
|
||||||
|
|
||||||
from django.db import transaction
|
from django.db import router, transaction
|
||||||
|
from django.db import DEFAULT_DB_ALIAS
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
from core.signals import clear_events
|
from core.signals import clear_events
|
||||||
|
from dcim.models import Device
|
||||||
from extras.models import Script as ScriptModel
|
from extras.models import Script as ScriptModel
|
||||||
|
from netbox.context_managers import event_tracking
|
||||||
from netbox.jobs import JobRunner
|
from netbox.jobs import JobRunner
|
||||||
from netbox.registry import registry
|
from netbox.registry import registry
|
||||||
from utilities.exceptions import AbortScript, AbortTransaction
|
from utilities.exceptions import AbortScript, AbortTransaction
|
||||||
@@ -42,10 +45,21 @@ class ScriptJob(JobRunner):
|
|||||||
# A script can modify multiple models so need to do an atomic lock on
|
# A script can modify multiple models so need to do an atomic lock on
|
||||||
# both the default database (for non ChangeLogged models) and potentially
|
# both the default database (for non ChangeLogged models) and potentially
|
||||||
# any other database (for ChangeLogged models)
|
# any other database (for ChangeLogged models)
|
||||||
with transaction.atomic():
|
changeloged_db = router.db_for_write(Device)
|
||||||
script.output = script.run(data, commit)
|
with transaction.atomic(using=DEFAULT_DB_ALIAS):
|
||||||
if not commit:
|
# If branch database is different from default, wrap in a second atomic transaction
|
||||||
raise AbortTransaction()
|
# Note: Don't add any extra code between the two atomic transactions,
|
||||||
|
# otherwise the changes might get committed to the default database
|
||||||
|
# if there are any raised exceptions.
|
||||||
|
if changeloged_db != DEFAULT_DB_ALIAS:
|
||||||
|
with transaction.atomic(using=changeloged_db):
|
||||||
|
script.output = script.run(data, commit)
|
||||||
|
if not commit:
|
||||||
|
raise AbortTransaction()
|
||||||
|
else:
|
||||||
|
script.output = script.run(data, commit)
|
||||||
|
if not commit:
|
||||||
|
raise AbortTransaction()
|
||||||
except AbortTransaction:
|
except AbortTransaction:
|
||||||
script.log_info(message=_("Database changes have been reverted automatically."))
|
script.log_info(message=_("Database changes have been reverted automatically."))
|
||||||
if script.failed:
|
if script.failed:
|
||||||
@@ -108,14 +122,14 @@ class ScriptJob(JobRunner):
|
|||||||
script.request = request
|
script.request = request
|
||||||
self.logger.debug(f"Request ID: {request.id if request else None}")
|
self.logger.debug(f"Request ID: {request.id if request else None}")
|
||||||
|
|
||||||
# Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process
|
|
||||||
# change logging, event rules, etc.
|
|
||||||
if commit:
|
if commit:
|
||||||
self.logger.info("Executing script (commit enabled)")
|
self.logger.info("Executing script (commit enabled)")
|
||||||
with ExitStack() as stack:
|
|
||||||
for request_processor in registry['request_processors']:
|
|
||||||
stack.enter_context(request_processor(request))
|
|
||||||
self.run_script(script, request, data, commit)
|
|
||||||
else:
|
else:
|
||||||
self.logger.warning("Executing script (commit disabled)")
|
self.logger.warning("Executing script (commit disabled)")
|
||||||
|
|
||||||
|
with ExitStack() as stack:
|
||||||
|
for request_processor in registry['request_processors']:
|
||||||
|
if not commit and request_processor is event_tracking:
|
||||||
|
continue
|
||||||
|
stack.enter_context(request_processor(request))
|
||||||
self.run_script(script, request, data, commit)
|
self.run_script(script, request, data, commit)
|
||||||
|
|||||||
@@ -230,10 +230,6 @@ class PrefixImportForm(ScopedImportForm, NetBoxModelImportForm):
|
|||||||
query |= Q(**{
|
query |= Q(**{
|
||||||
f"site__{self.fields['vlan_site'].to_field_name}": vlan_site
|
f"site__{self.fields['vlan_site'].to_field_name}": vlan_site
|
||||||
})
|
})
|
||||||
# Don't Forget to include VLANs without a site in the filter
|
|
||||||
query |= Q(**{
|
|
||||||
f"site__{self.fields['vlan_site'].to_field_name}__isnull": True
|
|
||||||
})
|
|
||||||
|
|
||||||
if vlan_group:
|
if vlan_group:
|
||||||
query &= Q(**{
|
query &= Q(**{
|
||||||
|
|||||||
@@ -564,6 +564,82 @@ vlan: 102
|
|||||||
self.assertEqual(prefix.vlan.vid, 102)
|
self.assertEqual(prefix.vlan.vid, 102)
|
||||||
self.assertEqual(prefix.scope, site)
|
self.assertEqual(prefix.scope, site)
|
||||||
|
|
||||||
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||||
|
def test_prefix_import_with_vlan_site_multiple_vlans_same_vid(self):
|
||||||
|
"""
|
||||||
|
Test import when multiple VLANs exist with the same vid but different sites.
|
||||||
|
Ref: #20560
|
||||||
|
"""
|
||||||
|
site1 = Site.objects.get(name='Site 1')
|
||||||
|
site2 = Site.objects.get(name='Site 2')
|
||||||
|
|
||||||
|
# Create VLANs with the same vid but different sites
|
||||||
|
vlan1 = VLAN.objects.create(vid=1, name='VLAN1-Site1', site=site1)
|
||||||
|
VLAN.objects.create(vid=1, name='VLAN1-Site2', site=site2) # Create ambiguity
|
||||||
|
|
||||||
|
# Import prefix with vlan_site specified
|
||||||
|
IMPORT_DATA = f"""
|
||||||
|
prefix: 10.11.0.0/22
|
||||||
|
status: active
|
||||||
|
scope_type: dcim.site
|
||||||
|
scope_id: {site1.pk}
|
||||||
|
vlan_site: {site1.name}
|
||||||
|
vlan: 1
|
||||||
|
description: LOC02-MGMT
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Add all required permissions to the test user
|
||||||
|
self.add_permissions('ipam.view_prefix', 'ipam.add_prefix')
|
||||||
|
|
||||||
|
form_data = {
|
||||||
|
'data': IMPORT_DATA,
|
||||||
|
'format': 'yaml'
|
||||||
|
}
|
||||||
|
response = self.client.post(reverse('ipam:prefix_bulk_import'), data=form_data, follow=True)
|
||||||
|
self.assertHttpStatus(response, 200)
|
||||||
|
|
||||||
|
# Verify the prefix was created with the correct VLAN
|
||||||
|
prefix = Prefix.objects.get(prefix='10.11.0.0/22')
|
||||||
|
self.assertEqual(prefix.vlan, vlan1)
|
||||||
|
|
||||||
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||||
|
def test_prefix_import_with_vlan_site_and_global_vlan(self):
|
||||||
|
"""
|
||||||
|
Test import when a global VLAN (no site) and site-specific VLAN exist with same vid.
|
||||||
|
When vlan_site is specified, should prefer the site-specific VLAN.
|
||||||
|
Ref: #20560
|
||||||
|
"""
|
||||||
|
site1 = Site.objects.get(name='Site 1')
|
||||||
|
|
||||||
|
# Create a global VLAN (no site) and a site-specific VLAN with the same vid
|
||||||
|
VLAN.objects.create(vid=10, name='VLAN10-Global', site=None) # Create ambiguity
|
||||||
|
vlan_site = VLAN.objects.create(vid=10, name='VLAN10-Site1', site=site1)
|
||||||
|
|
||||||
|
# Import prefix with vlan_site specified
|
||||||
|
IMPORT_DATA = f"""
|
||||||
|
prefix: 10.12.0.0/22
|
||||||
|
status: active
|
||||||
|
scope_type: dcim.site
|
||||||
|
scope_id: {site1.pk}
|
||||||
|
vlan_site: {site1.name}
|
||||||
|
vlan: 10
|
||||||
|
description: Test Site-Specific VLAN
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Add all required permissions to the test user
|
||||||
|
self.add_permissions('ipam.view_prefix', 'ipam.add_prefix')
|
||||||
|
|
||||||
|
form_data = {
|
||||||
|
'data': IMPORT_DATA,
|
||||||
|
'format': 'yaml'
|
||||||
|
}
|
||||||
|
response = self.client.post(reverse('ipam:prefix_bulk_import'), data=form_data, follow=True)
|
||||||
|
self.assertHttpStatus(response, 200)
|
||||||
|
|
||||||
|
# Verify the prefix was created with the site-specific VLAN (not the global one)
|
||||||
|
prefix = Prefix.objects.get(prefix='10.12.0.0/22')
|
||||||
|
self.assertEqual(prefix.vlan, vlan_site)
|
||||||
|
|
||||||
|
|
||||||
class IPRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
class IPRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
||||||
model = IPRange
|
model = IPRange
|
||||||
|
|||||||
@@ -559,6 +559,7 @@ class ComponentCreateView(GetReturnURLMixin, BaseObjectView):
|
|||||||
form.instance._replicated_base = hasattr(self.form, "replication_fields")
|
form.instance._replicated_base = hasattr(self.form, "replication_fields")
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
|
changelog_message = form.cleaned_data.pop('changelog_message', '')
|
||||||
new_components = []
|
new_components = []
|
||||||
data = deepcopy(request.POST)
|
data = deepcopy(request.POST)
|
||||||
pattern_count = len(form.cleaned_data[self.form.replication_fields[0]])
|
pattern_count = len(form.cleaned_data[self.form.replication_fields[0]])
|
||||||
@@ -585,6 +586,9 @@ class ComponentCreateView(GetReturnURLMixin, BaseObjectView):
|
|||||||
# Create the new components
|
# Create the new components
|
||||||
new_objs = []
|
new_objs = []
|
||||||
for component_form in new_components:
|
for component_form in new_components:
|
||||||
|
# Record changelog message (if any)
|
||||||
|
if changelog_message:
|
||||||
|
component_form.instance._changelog_message = changelog_message
|
||||||
obj = component_form.save()
|
obj = component_form.save()
|
||||||
new_objs.append(obj)
|
new_objs.append(obj)
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user