# Imports
from django.apps import apps as django_apps
from django.core.exceptions import ImproperlyConfigured
from superdjango.conf import SUPERDJANGO
# Exports
__all__ = (
"get_profile_model",
"get_user_profile",
)
# Functions
[docs]def get_profile_model():
"""Get the user profile model, if any.
:returns: The profile class or ``None``.
:raises: ImproperlyConfigured, ValueError
.. note::
This utility is intentionally located outside of the ``profiles`` app to reduce dependency on that app.
"""
if SUPERDJANGO.USER_PROFILE_MODEL is None:
return None
# TODO: Determine the implications of require_ready when getting the profile model.
try:
return django_apps.get_model(SUPERDJANGO.USER_PROFILE_MODEL, require_ready=False)
except ValueError:
raise ImproperlyConfigured('SUPERDJANGO_USER_PROFILE_MODEL must be in the form of "app_label.ModelName".')
except LookupError:
e = "SUPERDJANGO_USER_PROFILE_MODEL refers to a model that has not been installed: %s"
raise ImproperlyConfigured(e % SUPERDJANGO.USER_PROFILE_MODEL)
[docs]def get_user_profile(user):
"""Get a user profile.
:param user: The user instance.
:type user: AUTH_USER_MODEL
:rtype: SUPERDJANGO_USER_PROFILE_MODEL | None
.. tip::
A ``user`` field *must* exist on the profile model.
.. note::
This utility is intentionally located outside of the ``profiles`` app to reduce dependency on that app.
"""
profile_model = get_profile_model()
if profile_model is None:
return None
try:
return profile_model.objects.get(user=user)
except profile_model.DoesNotExist:
return None