Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.apps import apps as django_apps
4from django.core.exceptions import ImproperlyConfigured
5from superdjango.conf import SUPERDJANGO
7# Exports
9__all__ = (
10 "get_profile_model",
11 "get_user_profile",
12)
14# Functions
17def get_profile_model():
18 """Get the user profile model, if any.
20 .. note::
21 This utility is intentionally located outside of the ``profiles`` to reduce dependency on that app.
23 """
24 if SUPERDJANGO.USER_PROFILE_MODEL is None:
25 return None
27 # TODO: Determine the implications of require_ready when getting the profile model.
28 try:
29 return django_apps.get_model(SUPERDJANGO.USER_PROFILE_MODEL, require_ready=False)
30 except ValueError:
31 raise ImproperlyConfigured('SUPERDJANGO_USER_PROFILE_MODEL must be in the form of "app_label.ModelName".')
32 except LookupError:
33 e = "SUPERDJANGO_USER_PROFILE_MODEL refers to a model that has not been installed: %s"
34 raise ImproperlyConfigured(e % SUPERDJANGO.USER_PROFILE_MODEL)
37def get_user_profile(user):
38 """Get a user profile.
40 :param user: The user instance.
41 :type user: AUTH_USER_MODEL
43 :rtype: SUPERDJANGO_USER_PROFILE_MODEL | None
45 .. tip::
46 A ``user`` field *must* exist on the profile model.
48 .. note::
49 This utility is intentionally located outside of the ``profiles`` to reduce dependency on that app.
51 """
52 profile_model = get_profile_model()
53 if profile_model is None:
54 return None
56 try:
57 return profile_model.objects.get(user=user)
58 except profile_model.DoesNotExist:
59 return None