Skip to content

Commit

Permalink
Remove imports for builtin django User model (#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysyngsun authored Feb 5, 2025
1 parent d185a7a commit f59e570
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ storybook-static/

/**/.yarn/cache
.swc

# ignore local ssl certs
certs/
15 changes: 11 additions & 4 deletions channels/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
"""API for channels"""

from django.contrib.auth.models import Group, User
from typing import TYPE_CHECKING

from django.contrib.auth.models import Group
from django.db import transaction

from channels.constants import CHANNEL_ROLE_CHOICES, CHANNEL_ROLE_MODERATORS
from channels.models import Channel, ChannelGroupRole

if TYPE_CHECKING:
from django.contrib.auth import get_user_model

User = get_user_model()


def create_channel_groups_and_roles(
channel: Channel,
Expand All @@ -31,14 +38,14 @@ def get_role_model(channel: Channel, role: str) -> ChannelGroupRole:
return ChannelGroupRole.objects.get(channel=channel, role=role)


def add_user_role(channel: Channel, role: str, user: User):
def add_user_role(channel: Channel, role: str, user: "User"):
"""
Add a user to a channel role's group
"""
get_role_model(channel, role).group.user_set.add(user)


def remove_user_role(channel: Channel, role: str, user: User):
def remove_user_role(channel: Channel, role: str, user: "User"):
"""
Remove a user from a channel role's group
"""
Expand All @@ -51,7 +58,7 @@ def get_group_role_name(channel_id: int, role: str) -> str:
return f"channel_{channel_name}_{role}"


def is_moderator(user: User, channel_id: int) -> bool:
def is_moderator(user: "User", channel_id: int) -> bool:
"""
Determine if the user is a moderator for a channel (or a staff user)
"""
Expand Down
6 changes: 5 additions & 1 deletion channels/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.db.models import Prefetch
from django.utils.decorators import method_decorator
from django_filters.rest_framework import DjangoFilterBackend
Expand Down Expand Up @@ -169,6 +169,8 @@ def get_queryset(self):
"""
Build a queryset of relevant users with moderator permissions for this channel
"""
User = get_user_model()

channel_group_name = get_group_role_name(
self.kwargs["id"],
CHANNEL_ROLE_MODERATORS,
Expand All @@ -190,6 +192,8 @@ class ChannelModeratorDetailView(APIView):

def delete(self, request, *args, **kwargs): # noqa: ARG002
"""Remove the user from the moderator groups for this website"""
User = get_user_model()

user = User.objects.get(username=self.kwargs["moderator_name"])
remove_user_role(
Channel.objects.get(id=self.kwargs["id"]), CHANNEL_ROLE_MODERATORS, user
Expand Down
5 changes: 4 additions & 1 deletion channels/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from math import ceil

import pytest
from django.contrib.auth.models import Group, User
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.urls import reverse

from channels.api import add_user_role
Expand All @@ -26,6 +27,8 @@

pytestmark = pytest.mark.django_db

User = get_user_model()


def test_list_channels(user_client):
"""Test that all channels are returned"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Management command to create user Favorites lists"""

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.core.management import BaseCommand

from learning_resources.constants import FAVORITES_TITLE
Expand All @@ -24,6 +24,8 @@ def add_arguments(self, parser):

def handle(self, *args, **options): # noqa: ARG002
"""Create a Favorites userlist for every active user"""
User = get_user_model()

if options["delete"]:
self.stdout.write("Deleting all existing Favorites userlists")
UserList.objects.filter(title=FAVORITES_TITLE).delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.core.management import BaseCommand

from channels.models import Channel
Expand All @@ -18,6 +18,8 @@
)
from main.utils import clear_search_cache, now_in_utc

User = get_user_model()


class Command(BaseCommand):
"""create dev-only featured lists for offeror channels"""
Expand Down
19 changes: 14 additions & 5 deletions learning_resources/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import uuid
from abc import abstractmethod
from functools import cached_property
from typing import Optional
from typing import TYPE_CHECKING, Optional

from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.db.models import CharField, Count, JSONField, OuterRef, Prefetch, Q
Expand All @@ -25,6 +25,11 @@
)
from main.models import TimestampedModel, TimestampedModelQuerySet

if TYPE_CHECKING:
from django.contrib.auth import get_user_model

User = get_user_model()


def default_delivery():
"""Return the default delivery as a list"""
Expand Down Expand Up @@ -328,7 +333,7 @@ def __str__(self):
class LearningResourceQuerySet(TimestampedModelQuerySet):
"""QuerySet for LearningResource"""

def for_serialization(self, *, user: User | None = None):
def for_serialization(self, *, user: Optional["User"] = None):
"""Return the list of prefetches"""
return (
self.prefetch_related(
Expand Down Expand Up @@ -792,7 +797,9 @@ class LearningPath(LearningResourceDetailModel):
on_delete=models.CASCADE,
)
author = models.ForeignKey(
User, related_name="learning_paths", on_delete=models.PROTECT
settings.AUTH_USER_MODEL,
related_name="learning_paths",
on_delete=models.PROTECT,
)

def __str__(self):
Expand Down Expand Up @@ -896,7 +903,9 @@ class UserList(TimestampedModel):
"""

author = models.ForeignKey(
User, on_delete=models.deletion.CASCADE, related_name="user_lists"
settings.AUTH_USER_MODEL,
on_delete=models.deletion.CASCADE,
related_name="user_lists",
)
title = models.CharField(max_length=256)
description = models.TextField(default="", blank=True)
Expand Down
4 changes: 3 additions & 1 deletion learning_resources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from decimal import Decimal
from uuid import uuid4

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import F, Max
from drf_spectacular.utils import extend_schema_field
Expand Down Expand Up @@ -845,6 +845,8 @@ def get_item_count(self, instance) -> int:

def create(self, validated_data):
"""Create a new user list"""
User = get_user_model()

request = self.context.get("request")
if request and hasattr(request, "user") and isinstance(request.user, User):
validated_data["author"] = request.user
Expand Down
10 changes: 8 additions & 2 deletions learning_resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import logging
import re
from typing import TYPE_CHECKING

import rapidjson
import requests
import yaml
from botocore.exceptions import ClientError
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.contrib.auth.models import Group
from django.db import transaction
from django.db.models import Q
from retry import retry
Expand All @@ -33,6 +34,11 @@

log = logging.getLogger()

if TYPE_CHECKING:
from django.contrib.auth import get_user_model

User = get_user_model()


def user_list_image_upload_uri(instance, filename):
"""
Expand Down Expand Up @@ -229,7 +235,7 @@ def parse_instructors(staff):
return instructors


def update_editor_group(user: User, is_editor: False):
def update_editor_group(user: "User", is_editor: False):
"""Assign or unassign user to staff list editors group"""
group, _ = Group.objects.get_or_create(name=GROUP_STAFF_LISTS_EDITORS)
if is_editor:
Expand Down
6 changes: 3 additions & 3 deletions main/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import ulid
from django.contrib.auth.models import Group, User
from django.conf import settings
from factory import LazyFunction, RelatedFactory, SubFactory, Trait
from factory.django import DjangoModelFactory
from factory.fuzzy import FuzzyText
Expand All @@ -21,7 +21,7 @@ class UserFactory(DjangoModelFactory):
profile = RelatedFactory("profiles.factories.ProfileFactory", "user")

class Meta:
model = User
model = settings.AUTH_USER_MODEL
skip_postgeneration_save = True

class Params:
Expand All @@ -34,7 +34,7 @@ class GroupFactory(DjangoModelFactory):
name = FuzzyText()

class Meta:
model = Group
model = "auth.Group"


class UserSocialAuthFactory(DjangoModelFactory):
Expand Down
2 changes: 2 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@

ALLOWED_HOSTS = ["*"]

AUTH_USER_MODEL = "auth.User"

SECURE_SSL_REDIRECT = get_bool("MITOL_SECURE_SSL_REDIRECT", True) # noqa: FBT003

SITE_ID = 1
Expand Down
3 changes: 2 additions & 1 deletion profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from cairosvg import svg2png # pylint:disable=no-name-in-module
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
Expand Down Expand Up @@ -137,6 +136,8 @@ def name_initials_avatar_view(
bgcolor,
): # pylint:disable=unused-argument
"""View for initial avatar"""
User = get_user_model()

user = User.objects.filter(username=username).first()
if not user:
return redirect(DEFAULT_PROFILE_IMAGE)
Expand Down
4 changes: 3 additions & 1 deletion profiles/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework import status

Expand All @@ -25,6 +25,8 @@

pytestmark = [pytest.mark.django_db]

User = get_user_model()


def test_list_users(staff_client, staff_user):
"""
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ convention = "pep257"
[tool.ruff.lint.flake8-quotes]
inline-quotes = "double"

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"django.contrib.auth.models.User".msg = "use get_user_model() or settings.AUTH_USER_MODEL"

[tool.ruff.lint.per-file-ignores]
"*_test.py" = ["ARG001", "E501", "S101", "PLR2004"]
"test_*.py" = ["ARG001", "E501", "S101", "PLR2004"]
Expand Down

0 comments on commit f59e570

Please sign in to comment.