Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12794 change User ref to get_user_model #12905

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions netbox/core/forms/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -105,7 +105,7 @@ class JobFilterForm(SavedFiltersMixin, FilterForm):
widget=DateTimePicker()
)
user = DynamicModelMultipleChoiceField(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
required=False,
label=_('User'),
widget=APISelectMultiple(
Expand Down
4 changes: 2 additions & 2 deletions netbox/core/management/commands/nbshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django import get_version
from django.apps import apps
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand

Expand Down Expand Up @@ -60,7 +60,7 @@ def get_namespace(self):

# Additional objects to include
namespace['ContentType'] = ContentType
namespace['User'] = User
namespace['User'] = get_user_model()

# Load convenience commands
namespace.update({
Expand Down
4 changes: 2 additions & 2 deletions netbox/core/models/jobs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid

import django_rq
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.validators import MinValueValidator
Expand Down Expand Up @@ -69,7 +69,7 @@ class Job(models.Model):
blank=True
)
user = models.ForeignKey(
to=User,
to=settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
related_name='+',
blank=True,
Expand Down
6 changes: 3 additions & 3 deletions netbox/dcim/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import django_filters
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.utils.translation import gettext as _

from extras.filtersets import LocalConfigContextFilterSet
Expand Down Expand Up @@ -395,12 +395,12 @@ class RackReservationFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
label=_('Location (slug)'),
)
user_id = django_filters.ModelMultipleChoiceFilter(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
label=_('User (ID)'),
)
user = django_filters.ModelMultipleChoiceFilter(
field_name='user__username',
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
to_field_name='username',
label=_('User (name)'),
)
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/forms/bulk_edit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.utils.translation import gettext as _
from timezone_field import TimeZoneFormField

Expand Down Expand Up @@ -322,7 +322,7 @@ class RackBulkEditForm(NetBoxModelBulkEditForm):

class RackReservationBulkEditForm(NetBoxModelBulkEditForm):
user = forms.ModelChoiceField(
queryset=User.objects.order_by(
queryset=get_user_model().objects.order_by(
'username'
),
required=False
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/forms/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.utils.translation import gettext as _

from dcim.choices import *
Expand Down Expand Up @@ -376,7 +376,7 @@ class RackReservationFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm):
label=_('Rack')
)
user_id = DynamicModelMultipleChoiceField(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
required=False,
label=_('User'),
widget=APISelectMultiple(
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/forms/model_forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext as _
from timezone_field import TimeZoneFormField
Expand Down Expand Up @@ -236,7 +236,7 @@ class RackReservationForm(TenancyForm, NetBoxModelForm):
help_text=_("Comma-separated list of numeric unit IDs. A range may be specified using a hyphen.")
)
user = forms.ModelChoiceField(
queryset=User.objects.order_by(
queryset=get_user_model().objects.order_by(
'username'
)
)
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/models/racks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import decimal
from functools import cached_property

from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -505,7 +505,7 @@ class RackReservation(PrimaryModel):
null=True
)
user = models.ForeignKey(
to=User,
to=settings.AUTH_USER_MODEL,
on_delete=models.PROTECT
)
description = models.CharField(
Expand Down
5 changes: 4 additions & 1 deletion netbox/dcim/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.test import override_settings
from django.urls import reverse
from rest_framework import status
Expand All @@ -14,6 +14,9 @@
from wireless.models import WirelessLAN


User = get_user_model()


class AppTest(APITestCase):

def test_root(self):
Expand Down
5 changes: 4 additions & 1 deletion netbox/dcim/tests/test_filtersets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.test import TestCase

from dcim.choices import *
Expand All @@ -12,6 +12,9 @@
from wireless.choices import WirelessChannelChoices, WirelessRoleChoices


User = get_user_model()


class DeviceComponentFilterSetTests:

def test_device_type(self):
Expand Down
5 changes: 4 additions & 1 deletion netbox/dcim/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from backports.zoneinfo import ZoneInfo

import yaml
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.test import override_settings
from django.urls import reverse
Expand All @@ -22,6 +22,9 @@
from wireless.models import WirelessLAN


User = get_user_model()


class RegionTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
model = Region

Expand Down
4 changes: 2 additions & 2 deletions netbox/extras/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import serializers
Expand Down Expand Up @@ -256,7 +256,7 @@ class JournalEntrySerializer(NetBoxModelSerializer):
assigned_object = serializers.SerializerMethodField(read_only=True)
created_by = serializers.PrimaryKeyRelatedField(
allow_null=True,
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
required=False,
default=serializers.CurrentUserDefault()
)
Expand Down
14 changes: 7 additions & 7 deletions netbox/extras/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import django_filters
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -159,12 +159,12 @@ class SavedFilterFilterSet(BaseFilterSet):
)
content_types = ContentTypeFilter()
user_id = django_filters.ModelMultipleChoiceFilter(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
label=_('User (ID)'),
)
user = django_filters.ModelMultipleChoiceFilter(
field_name='user__username',
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
to_field_name='username',
label=_('User (name)'),
)
Expand Down Expand Up @@ -223,12 +223,12 @@ class JournalEntryFilterSet(NetBoxModelFilterSet):
queryset=ContentType.objects.all()
)
created_by_id = django_filters.ModelMultipleChoiceFilter(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
label=_('User (ID)'),
)
created_by = django_filters.ModelMultipleChoiceFilter(
field_name='created_by__username',
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
to_field_name='username',
label=_('User (name)'),
)
Expand Down Expand Up @@ -510,12 +510,12 @@ class ObjectChangeFilterSet(BaseFilterSet):
queryset=ContentType.objects.all()
)
user_id = django_filters.ModelMultipleChoiceFilter(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
label=_('User (ID)'),
)
user = django_filters.ModelMultipleChoiceFilter(
field_name='user__username',
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
to_field_name='username',
label=_('User name'),
)
Expand Down
6 changes: 3 additions & 3 deletions netbox/extras/forms/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -385,7 +385,7 @@ class JournalEntryFilterForm(NetBoxModelFilterSetForm):
widget=DateTimePicker()
)
created_by_id = DynamicModelMultipleChoiceField(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
required=False,
label=_('User'),
widget=APISelectMultiple(
Expand Down Expand Up @@ -429,7 +429,7 @@ class ObjectChangeFilterForm(SavedFiltersMixin, FilterForm):
required=False
)
user_id = DynamicModelMultipleChoiceField(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
required=False,
label=_('User'),
widget=APISelectMultiple(
Expand Down
4 changes: 3 additions & 1 deletion netbox/extras/management/commands/runscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import traceback
import uuid

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction

Expand Down Expand Up @@ -63,6 +63,8 @@ def _run_script():

logger.info(f"Script completed in {job.duration}")

User = get_user_model()

# Params
script = options['script']
loglevel = options['loglevel']
Expand Down
4 changes: 2 additions & 2 deletions netbox/extras/models/change_logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
Expand All @@ -24,7 +24,7 @@ class ObjectChange(models.Model):
db_index=True
)
user = models.ForeignKey(
to=User,
to=settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
related_name='changes',
blank=True,
Expand Down
6 changes: 3 additions & 3 deletions netbox/extras/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from django.conf import settings
from django.contrib import admin
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
Expand Down Expand Up @@ -419,7 +419,7 @@ class SavedFilter(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
blank=True
)
user = models.ForeignKey(
to=User,
to=settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
blank=True,
null=True
Expand Down Expand Up @@ -560,7 +560,7 @@ class JournalEntry(CustomFieldsMixin, CustomLinksMixin, TagsMixin, ExportTemplat
fk_field='assigned_object_id'
)
created_by = models.ForeignKey(
to=User,
to=settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
blank=True,
null=True
Expand Down
5 changes: 4 additions & 1 deletion netbox/extras/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils.timezone import make_aware
Expand All @@ -15,6 +15,9 @@
from utilities.testing import APITestCase, APIViewTestCases


User = get_user_model()


class AppTest(APITestCase):

def test_root(self):
Expand Down
5 changes: 4 additions & 1 deletion netbox/extras/tests/test_filtersets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from datetime import datetime, timezone

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase

Expand All @@ -18,6 +18,9 @@
from virtualization.models import Cluster, ClusterGroup, ClusterType


User = get_user_model()


class CustomFieldTestCase(TestCase, BaseFilterSetTests):
queryset = CustomField.objects.all()
filterset = CustomFieldFilterSet
Expand Down
5 changes: 4 additions & 1 deletion netbox/extras/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import urllib.parse
import uuid

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse

Expand All @@ -11,6 +11,9 @@
from utilities.testing import ViewTestCases, TestCase


User = get_user_model()


class CustomFieldTestCase(ViewTestCases.PrimaryObjectViewTestCase):
model = CustomField

Expand Down
Loading