Skip to content

Commit 3dc1dca

Browse files
authored
Model import fix (ansible#646)
This fixes issue [443](ansible#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!
1 parent 366e624 commit 3dc1dca

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ansible_base/lib/utils/models.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
from __future__ import annotations
2+
13
import logging
24
from dataclasses import asdict, dataclass
35
from itertools import chain
4-
from typing import Optional
6+
from typing import TYPE_CHECKING, Optional
57

68
from crum import get_current_user
79
from django.conf import settings
810
from django.contrib.auth import get_user_model
9-
from django.contrib.auth.models import AbstractUser
1011
from django.db import models
1112
from django.utils.translation import gettext_lazy as _
1213
from inflection import underscore
@@ -17,6 +18,10 @@
1718

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

21+
# Handle type hints of AbstractUser during linting
22+
if TYPE_CHECKING:
23+
from django.contrib.auth.models import AbstractUser
24+
2025

2126
def get_all_field_names(model, concrete_only=False, include_attnames=True):
2227
# Implements compatibility with _meta.get_all_field_names
@@ -83,6 +88,8 @@ def is_system_user(user: Optional[models.Model]) -> bool:
8388
"""
8489
Takes a model and returns a boolean if its a user whose username is the same as the SYSTEM_USERNAME setting
8590
"""
91+
from django.contrib.auth.models import AbstractUser
92+
8693
system_username = get_system_username()[0]
8794
if user is None or not isinstance(user, AbstractUser) or system_username is None:
8895
# 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
@@ -95,6 +102,7 @@ class NotARealException(Exception):
95102

96103

97104
def get_system_user() -> Optional[AbstractUser]:
105+
98106
from ansible_base.lib.abstract_models.user import AbstractDABUser
99107

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

143151

144152
def is_encrypted_field(model, field_name):
153+
from django.contrib.auth.models import AbstractUser
154+
145155
if model is None:
146156
return False
147157

0 commit comments

Comments
 (0)