ubernostrum / django-profiles

A user-profile application for Django.

Changed (Δ900 bytes):

raw changeset »

profiles/utils.py (31 lines added, 3 lines removed)

Up to file-list profiles/utils.py:

1
"""
2
Utility functions for retrieving and generating forms for the
3
site-specific user profile model specified in the
4
``AUTH_PROFILE_MODULE`` setting.
5
6
"""
7
1
8
from django.conf import settings
2
9
from django.db.models import get_model
10
from django import newforms as forms
3
11
4
12
from django.contrib.auth.models import SiteProfileNotAvailable
5
13
@@ -7,12 +15,32 @@ from django.contrib.auth.models import S
7
15
def get_profile_model():
8
16
    """
9
17
    Returns the model class for the currently-active user profile
10
    model, as defined by the ``AUTH_PROFILE_MODULE`` setting.
18
    model, as defined by the ``AUTH_PROFILE_MODULE`` setting. If that
19
    setting is missing, raises
20
    ``django.contrib.auth.models.SiteProfileNotAvailable``.
11
21
    
12
22
    """
13
    if not settings.AUTH_PROFILE_MODULE:
23
    if (not hasattr(settings, 'AUTH_PROFILE_MODULE')) or \
24
           (not settings.AUTH_PROFILE_MODULE):
14
25
        raise SiteProfileNotAvailable
15
26
    profile_mod = get_model(*settings.AUTH_PROFILE_MODULE.split('.'))
16
    if not profile_mod:
27
    if profile_mod is None:
17
28
        raise SiteProfileNotAvailable
18
29
    return profile_mod
30
31
32
def get_profile_form():
33
    """
34
    Returns a form class (a subclass of the default ``ModelForm``)
35
    suitable for creating/editing instances of the site-specific user
36
    profile model, as defined by the ``AUTH_PROFILE_MODULE``
37
    setting. If that setting is missing, raises
38
    ``django.contrib.auth.models.SiteProfileNotAvailable``.
39
    
40
    """
41
    profile_mod = get_profile_model()
42
    class _ProfileForm(forms.ModelForm):
43
        class Meta:
44
            model = profile_mod
45
            exclude = ('user',)
46
    return _ProfileForm