Compare commits

..

1 Commits

Author SHA1 Message Date
Martin Hauser
6560aba3ee chore(ruff): Expand configuration for linting and formatting
Update `ruff.toml` with additional exclusions, linting rules, and
formatting preferences. Includes support for respecting `.gitignore`
and a consistent coding style.

Fixes #21410
2026-02-15 20:06:19 +01:00
11 changed files with 107 additions and 117 deletions

View File

@@ -91,13 +91,13 @@ class ProviderNetworkForm(PrimaryModelForm):
class CircuitTypeForm(OrganizationalModelForm):
fieldsets = (
FieldSet('name', 'slug', 'color', 'description', 'tags'),
FieldSet('name', 'slug', 'color', 'description', 'owner', 'tags'),
)
class Meta:
model = CircuitType
fields = [
'name', 'slug', 'color', 'description', 'owner', 'comments', 'tags',
'name', 'slug', 'color', 'description', 'comments', 'tags',
]

View File

@@ -126,7 +126,7 @@ class DeviceDeviceTypePanel(panels.ObjectAttributesPanel):
manufacturer = attrs.RelatedObjectAttr('device_type.manufacturer', linkify=True)
model = attrs.RelatedObjectAttr('device_type', linkify=True)
height = attrs.TemplatedAttr('device_type.u_height', template_name='dcim/devicetype/attrs/height.html')
height = attrs.TextAttr('device_type.u_height', format_string='{}U')
front_image = attrs.ImageAttr('device_type.front_image')
rear_image = attrs.ImageAttr('device_type.rear_image')
@@ -143,7 +143,7 @@ class DeviceTypePanel(panels.ObjectAttributesPanel):
part_number = attrs.TextAttr('part_number')
default_platform = attrs.RelatedObjectAttr('default_platform', linkify=True)
description = attrs.TextAttr('description')
height = attrs.TemplatedAttr('u_height', template_name='dcim/devicetype/attrs/height.html')
height = attrs.TextAttr('u_height', format_string='{}U', label=_('Height'))
exclude_from_utilization = attrs.BooleanAttr('exclude_from_utilization')
full_depth = attrs.BooleanAttr('is_full_depth')
weight = attrs.NumericAttr('weight', unit_accessor='get_weight_unit_display')

View File

@@ -113,17 +113,6 @@ def enqueue_event(queue, instance, request, event_type):
def process_event_rules(event_rules, object_type, event):
"""
Process a list of EventRules against an event.
Notes on event sources:
- Object change events (created/updated/deleted) are enqueued via
enqueue_event() during an HTTP request.
These events include a request object and legacy request
attributes (e.g. username, request_id) for backward compatibility.
- Job lifecycle events (JOB_STARTED/JOB_COMPLETED) are emitted by
job_start/job_end signal handlers and may not include a request
context.
Consumers must not assume that fields like `username` are always
present.
"""
for event_rule in event_rules:
@@ -143,22 +132,16 @@ def process_event_rules(event_rules, object_type, event):
queue_name = get_config().QUEUE_MAPPINGS.get('webhook', RQ_QUEUE_DEFAULT)
rq_queue = get_queue(queue_name)
# For job lifecycle events, `username` may be absent because
# there is no request context.
# Prefer the associated user object when present, falling
# back to the legacy username attribute.
username = getattr(event.get('user'), 'username', None) or event.get('username')
# Compile the task parameters
params = {
'event_rule': event_rule,
'object_type': object_type,
'event_type': event['event_type'],
'data': event_data,
'snapshots': event.get('snapshots'),
'timestamp': timezone.now().isoformat(),
'username': username,
'retry': get_rq_retry(),
"event_rule": event_rule,
"object_type": object_type,
"event_type": event['event_type'],
"data": event_data,
"snapshots": event.get('snapshots'),
"timestamp": timezone.now().isoformat(),
"username": event['username'],
"retry": get_rq_retry()
}
if 'request' in event:
# Exclude FILES - webhooks don't need uploaded files,
@@ -175,12 +158,11 @@ def process_event_rules(event_rules, object_type, event):
# Enqueue a Job to record the script's execution
from extras.jobs import ScriptJob
params = {
'instance': event_rule.action_object,
'name': script.name,
'user': event['user'],
'data': event_data,
"instance": event_rule.action_object,
"name": script.name,
"user": event['user'],
"data": event_data
}
if 'snapshots' in event:
params['snapshots'] = event['snapshots']
@@ -197,7 +179,7 @@ def process_event_rules(event_rules, object_type, event):
object_type=object_type,
object_id=event_data['id'],
object_repr=event_data.get('display'),
event_type=event['event_type'],
event_type=event['event_type']
)
else:

View File

@@ -1,6 +1,6 @@
import json
import uuid
from unittest.mock import Mock, patch
from unittest.mock import patch
import django_rq
from django.http import HttpResponse
@@ -15,8 +15,7 @@ from dcim.choices import SiteStatusChoices
from dcim.models import Site
from extras.choices import EventRuleActionChoices
from extras.events import enqueue_event, flush_events, serialize_for_event
from extras.models import EventRule, Script, Tag, Webhook
from extras.signals import process_job_end_event_rules
from extras.models import EventRule, Tag, Webhook
from extras.webhooks import generate_signature, send_webhook
from netbox.context_managers import event_tracking
from utilities.testing import APITestCase
@@ -396,36 +395,6 @@ class EventRuleTest(APITestCase):
with patch.object(Session, 'send', dummy_send):
send_webhook(**job.kwargs)
def test_job_completed_webhook_username_fallback(self):
"""
Ensure job_end event processing can enqueue a webhook even when the EventContext
lacks legacy request attributes (e.g. `username`).
The job_start/job_end signal receivers only populate `user` and `data`, so webhook
processing must derive the username from the user object (or tolerate it being unset).
"""
script_type = ObjectType.objects.get_for_model(Script)
webhook_type = ObjectType.objects.get_for_model(Webhook)
webhook = Webhook.objects.get(name='Webhook 1')
event_rule = EventRule.objects.create(
name='Event Rule Job Completed',
event_types=[JOB_COMPLETED],
action_type=EventRuleActionChoices.WEBHOOK,
action_object_type=webhook_type,
action_object_id=webhook.pk,
)
event_rule.object_types.set([script_type])
# Mimic the `core.job_end` signal sender expected by extras.signals.process_job_end_event_rules
# (notably: no request, and thus no legacy `username`)
sender = Mock(object_type=script_type, data={}, user=self.user)
process_job_end_event_rules(sender)
self.assertEqual(self.queue.count, 1)
job = self.queue.jobs[0]
self.assertEqual(job.kwargs['event_rule'], event_rule)
self.assertEqual(job.kwargs['event_type'], JOB_COMPLETED)
self.assertEqual(job.kwargs['object_type'], script_type)
self.assertEqual(job.kwargs['username'], self.user.username)
def test_duplicate_triggers(self):
"""
Test for erroneous duplicate event triggers resulting from saving an object multiple times

View File

@@ -13,11 +13,10 @@ def set_vid_ranges(apps, schema_editor):
VLANGroup = apps.get_model('ipam', 'VLANGroup')
db_alias = schema_editor.connection.alias
vlan_groups = VLANGroup.objects.using(db_alias).only('id', 'min_vid', 'max_vid')
for group in vlan_groups:
for group in VLANGroup.objects.using(db_alias).all():
group.vid_ranges = [NumericRange(group.min_vid, group.max_vid, bounds='[]')]
group._total_vlan_ids = group.max_vid - group.min_vid + 1
VLANGroup.objects.using(db_alias).bulk_update(vlan_groups, ['vid_ranges', '_total_vlan_ids'], batch_size=100)
group.save()
class Migration(migrations.Migration):

View File

@@ -11,10 +11,14 @@ from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import URLValidator
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
from rest_framework.utils import field_mapping
from strawberry_django import pagination
from strawberry_django.fields.field import StrawberryDjangoField
from core.exceptions import IncompatiblePluginError
from netbox.config import PARAMS as CONFIG_PARAMS
from netbox.constants import RQ_QUEUE_DEFAULT, RQ_QUEUE_HIGH, RQ_QUEUE_LOW
from netbox.graphql.pagination import OffsetPaginationInput, apply_pagination
from netbox.plugins import PluginConfig
from netbox.registry import registry
import storages.utils # type: ignore
@@ -24,6 +28,21 @@ from utilities.string import trailing_slash
from .monkey import get_unique_validators
#
# Monkey-patching
#
# TODO: Remove this once #20547 has been implemented
# Override DRF's get_unique_validators() function with our own (see bug #19302)
field_mapping.get_unique_validators = get_unique_validators
# Override strawberry-django's OffsetPaginationInput class to add the `start` parameter
pagination.OffsetPaginationInput = OffsetPaginationInput
# Patch StrawberryDjangoField to use our custom `apply_pagination()` method with support for cursor-based pagination
StrawberryDjangoField.apply_pagination = apply_pagination
#
# Environment setup
#
@@ -950,26 +969,6 @@ for plugin_name in PLUGINS:
raise ImproperlyConfigured(f"events_pipline in plugin: {plugin_name} must be a list or tuple")
#
# Monkey-patching
#
from rest_framework.utils import field_mapping # noqa: E402
from strawberry_django import pagination # noqa: E402
from strawberry_django.fields.field import StrawberryDjangoField # noqa: E402
from netbox.graphql.pagination import OffsetPaginationInput, apply_pagination # noqa: E402
# TODO: Remove this once #20547 has been implemented
# Override DRF's get_unique_validators() function with our own (see bug #19302)
field_mapping.get_unique_validators = get_unique_validators
# Override strawberry-django's OffsetPaginationInput class to add the `start` parameter
pagination.OffsetPaginationInput = OffsetPaginationInput
# Patch StrawberryDjangoField to use our custom `apply_pagination()` method with support for cursor-based pagination
StrawberryDjangoField.apply_pagination = apply_pagination
# UNSUPPORTED FUNCTIONALITY: Import any local overrides.
try:
from .local_settings import *

View File

@@ -103,7 +103,7 @@ class TextAttr(ObjectAttribute):
def get_value(self, obj):
value = resolve_attr_path(obj, self.accessor)
# Apply format string (if any)
if value is not None and value != '' and self.format_string:
if value and self.format_string:
return self.format_string.format(value)
return value

View File

@@ -1 +0,0 @@
{{ value|floatformat }}U

View File

@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-02-17 05:26+0000\n"
"POT-Creation-Date: 2026-02-14 05:17+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -8037,7 +8037,7 @@ msgid "Racks"
msgstr ""
#: netbox/dcim/tables/racks.py:55 netbox/dcim/tables/racks.py:130
#: netbox/dcim/ui/panels.py:30
#: netbox/dcim/ui/panels.py:30 netbox/dcim/ui/panels.py:146
#: netbox/templates/dcim/inc/panels/racktype_dimensions.html:14
msgid "Height"
msgstr ""
@@ -12716,67 +12716,67 @@ msgstr ""
msgid "Cannot delete stores from registry"
msgstr ""
#: netbox/netbox/settings.py:828
#: netbox/netbox/settings.py:847
msgid "Czech"
msgstr ""
#: netbox/netbox/settings.py:829
#: netbox/netbox/settings.py:848
msgid "Danish"
msgstr ""
#: netbox/netbox/settings.py:830
#: netbox/netbox/settings.py:849
msgid "German"
msgstr ""
#: netbox/netbox/settings.py:831
#: netbox/netbox/settings.py:850
msgid "English"
msgstr ""
#: netbox/netbox/settings.py:832
#: netbox/netbox/settings.py:851
msgid "Spanish"
msgstr ""
#: netbox/netbox/settings.py:833
#: netbox/netbox/settings.py:852
msgid "French"
msgstr ""
#: netbox/netbox/settings.py:834
#: netbox/netbox/settings.py:853
msgid "Italian"
msgstr ""
#: netbox/netbox/settings.py:835
#: netbox/netbox/settings.py:854
msgid "Japanese"
msgstr ""
#: netbox/netbox/settings.py:836
#: netbox/netbox/settings.py:855
msgid "Latvian"
msgstr ""
#: netbox/netbox/settings.py:837
#: netbox/netbox/settings.py:856
msgid "Dutch"
msgstr ""
#: netbox/netbox/settings.py:838
#: netbox/netbox/settings.py:857
msgid "Polish"
msgstr ""
#: netbox/netbox/settings.py:839
#: netbox/netbox/settings.py:858
msgid "Portuguese"
msgstr ""
#: netbox/netbox/settings.py:840
#: netbox/netbox/settings.py:859
msgid "Russian"
msgstr ""
#: netbox/netbox/settings.py:841
#: netbox/netbox/settings.py:860
msgid "Turkish"
msgstr ""
#: netbox/netbox/settings.py:842
#: netbox/netbox/settings.py:861
msgid "Ukrainian"
msgstr ""
#: netbox/netbox/settings.py:843
#: netbox/netbox/settings.py:862
msgid "Chinese"
msgstr ""

View File

@@ -165,12 +165,12 @@ class ContentTypeFilter(django_filters.CharFilter):
try:
app_label, model = value.lower().split('.')
content_type = ContentType.objects.get_by_natural_key(app_label, model)
except (ValueError, ContentType.DoesNotExist):
except ValueError:
return qs.none()
return qs.filter(
**{
f'{self.field_name}': content_type,
f'{self.field_name}__app_label': app_label,
f'{self.field_name}__model': model
}
)

View File

@@ -1,16 +1,58 @@
# Ruff configuration
####################
exclude = [
"netbox/project-static/**"
".eggs",
".git",
".pyenv",
".pytest_cache",
".ruff_cache",
".venv",
".vscode",
"__pypackages__",
"_build",
"build",
"dist",
"netbox/project-static/**",
"node_modules",
"site-packages",
"venv",
]
# Enforce line length and indent-width
line-length = 120
indent-width = 4
# Ignores anything in .gitignore
respect-gitignore = true
# Always generate Python 3.12-compatible code
target-version = "py312"
[lint]
extend-select = ["E1", "E2", "E3", "E501", "W"]
ignore = ["F403", "F405"]
extend-select = [
"E1", # pycodestyle errors: indentation-related (e.g., unexpected/missing indent)
"E2", # pycodestyle errors: whitespace-related (e.g., missing whitespace, extra spaces)
"E3", # pycodestyle errors: blank lines / spacing around definitions
"E501", # pycodestyle: line too long (enforced with `line-length` above)
"W", # pycodestyle warnings (various style warnings, often whitespace/newlines)
]
ignore = [
"F403", # pyflakes: `from ... import *` used; unable to detect undefined names
"F405", # pyflakes: name may be undefined or defined from star imports
"UP032", # pyupgrade: prefer f-strings over `str.format(...)`
]
preview = true
[lint.per-file-ignores]
"template_code.py" = ["E501"]
[format]
# Use single quotes for strings.
quote-style = "single"
# Indent with spaces, rather than tabs.
indent-style = "space"
# Enforce UNIX line ending
line-ending = "lf"