Hide keyboard shortcuts

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 

2 

3from django.apps import apps as django_apps 

4from django.core.exceptions import ImproperlyConfigured 

5from superdjango.conf import SUPERDJANGO 

6 

7# Exports 

8 

9__all__ = ( 

10 "get_profile_model", 

11 "get_user_profile", 

12) 

13 

14# Functions 

15 

16 

17def get_profile_model(): 

18 """Get the user profile model, if any. 

19 

20 .. note:: 

21 This utility is intentionally located outside of the ``profiles`` to reduce dependency on that app. 

22 

23 """ 

24 if SUPERDJANGO.USER_PROFILE_MODEL is None: 

25 return None 

26 

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) 

35 

36 

37def get_user_profile(user): 

38 """Get a user profile. 

39 

40 :param user: The user instance. 

41 :type user: AUTH_USER_MODEL 

42 

43 :rtype: SUPERDJANGO_USER_PROFILE_MODEL | None 

44 

45 .. tip:: 

46 A ``user`` field *must* exist on the profile model. 

47 

48 .. note:: 

49 This utility is intentionally located outside of the ``profiles`` to reduce dependency on that app. 

50 

51 """ 

52 profile_model = get_profile_model() 

53 if profile_model is None: 

54 return None 

55 

56 try: 

57 return profile_model.objects.get(user=user) 

58 except profile_model.DoesNotExist: 

59 return None