Skip to content

Commit

Permalink
Model import fix (#646)
Browse files Browse the repository at this point in the history
This fixes issue
[443](#443).
Imports of AbstractUser are now inline in the functions that use it,
also added new import "annotations" in order to handle the functions
which had AbstractUser in type hints.

Please let me know if I need to add some additional information or if
something needs fixing, this is my first PR here!
  • Loading branch information
Kuppjaerk authored Nov 19, 2024
1 parent 366e624 commit 3dc1dca
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ansible_base/lib/utils/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

import logging
from dataclasses import asdict, dataclass
from itertools import chain
from typing import Optional
from typing import TYPE_CHECKING, Optional

from crum import get_current_user
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from inflection import underscore
Expand All @@ -17,6 +18,10 @@

logger = logging.getLogger('ansible_base.lib.utils.models')

# Handle type hints of AbstractUser during linting
if TYPE_CHECKING:
from django.contrib.auth.models import AbstractUser


def get_all_field_names(model, concrete_only=False, include_attnames=True):
# Implements compatibility with _meta.get_all_field_names
Expand Down Expand Up @@ -83,6 +88,8 @@ def is_system_user(user: Optional[models.Model]) -> bool:
"""
Takes a model and returns a boolean if its a user whose username is the same as the SYSTEM_USERNAME setting
"""
from django.contrib.auth.models import AbstractUser

system_username = get_system_username()[0]
if user is None or not isinstance(user, AbstractUser) or system_username is None:
# If we didn't get anything or that thing isn't an AbstractUser or system_username is not set set than what we have can't be the system user
Expand All @@ -95,6 +102,7 @@ class NotARealException(Exception):


def get_system_user() -> Optional[AbstractUser]:

from ansible_base.lib.abstract_models.user import AbstractDABUser

system_username, setting_name = get_system_username()
Expand Down Expand Up @@ -142,6 +150,8 @@ def current_user_or_system_user() -> Optional[AbstractUser]:


def is_encrypted_field(model, field_name):
from django.contrib.auth.models import AbstractUser

if model is None:
return False

Expand Down

0 comments on commit 3dc1dca

Please sign in to comment.