1# -*- coding: utf-8 -*-
2import importlib
3
4from django.apps import apps
5from django.conf import settings
6
7
8# overide with custom classes if they exist
9if settings.AUTH_USER_MODEL != 'auth.User':  # pragma: no cover
10    # UserAdmin class
11    user_app_name = settings.AUTH_USER_MODEL.split('.')[0]
12    app = apps.get_app_config(user_app_name).models_module
13
14    try:
15        custom_admin = importlib.import_module(app.__name__[:-6] + "admin")
16
17        if hasattr(custom_admin, 'UserAdmin'):
18            UserAdmin = custom_admin.UserAdmin
19        else:
20            from django.contrib.auth.admin import UserAdmin
21    except ImportError:
22        from django.contrib.auth.admin import UserAdmin  # nopyflakes
23
24    # user form classes
25    try:
26        custom_forms = importlib.import_module(app.__name__[:-6] + "forms")
27
28        if hasattr(custom_forms, 'UserCreationForm'):
29            UserCreationForm = custom_forms.UserCreationForm
30        else:
31            from django.contrib.auth.forms import UserCreationForm
32
33        if hasattr(custom_forms, 'UserChangeForm'):
34            UserChangeForm = custom_forms.UserChangeForm
35        else:
36            from django.contrib.auth.forms import UserChangeForm
37    except ImportError:
38        from django.contrib.auth.forms import UserCreationForm  # nopyflakes
39        from django.contrib.auth.forms import UserChangeForm  # nopyflakes
40else:
41    from django.contrib.auth.admin import UserAdmin  # nopyflakes
42    from django.contrib.auth.forms import UserCreationForm  # nopyflakes
43    from django.contrib.auth.forms import UserChangeForm  # nopyflakes
44