Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/configurable profile inline #1572

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ Dotted package path and class name of profile form to use for users signing up a

Default: ``'mezzanine.accounts.forms.ProfileForm'``

``ACCOUNTS_PROFILE_INLINE_CLASS``
-------------------------------

Dotted package path of the profile inline class to use, when ``mezzanine.accounts`` is installed.

Default: ``'mezzanine.accounts.admin.ProfileInline'``

.. _ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS:

``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS``
Expand Down
12 changes: 12 additions & 0 deletions docs/user-accounts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ update profile forms, as well as in the user's public profile page.
For more information consult the `Django docs for profiles
<https://docs.djangoproject.com/en/1.4/topics/auth/#storing-additional-information-about-users>`_.

ProfileAdmin
============

By default, in the Mezzanine admin, Mezzanine will render all
relevant user profile fields on a User object via the
``ProfileInline(admin.StackedInline)`` class. However, if you need
to use a custom inline class you can create the custom class in the
admin.py file of your app and add a reference to it in
settings.py with the following setting:
:ref:`ACCOUNTS_PROFILE_INLINE_CLASS`.


Restricting Account Fields
==========================

Expand Down
14 changes: 14 additions & 0 deletions mezzanine/accounts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ def get_profile_form():
settings.ACCOUNTS_PROFILE_FORM_CLASS)


def get_profile_inline_admin():
"""
Returns the profile inline class defined by
``settings.ACCOUNTS_PROFILE_INLINE_CLASS``.
"""
from mezzanine.conf import settings
try:
return import_dotted_path(settings.ACCOUNTS_PROFILE_INLINE_CLASS)
except ImportError:
raise ImproperlyConfigured("Value for ACCOUNTS_PROFILE_INLINE_CLASS "
"could not be imported: %s" %
settings.ACCOUNTS_PROFILE_INLINE_CLASS)


def get_profile_user_fieldname(profile_model=None, user_model=None):
"""
Returns the name of the first field on the profile model that
Expand Down
24 changes: 8 additions & 16 deletions mezzanine/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from django.contrib import admin
from django.contrib.auth import get_user_model
from mezzanine.accounts import get_profile_model, ProfileNotConfigured
from mezzanine.accounts import get_profile_model, ProfileNotConfigured, \
get_profile_inline_admin

from mezzanine.core.admin import SitePermissionUserAdmin
from mezzanine.conf import settings
Expand Down Expand Up @@ -45,23 +46,14 @@ def save_model(self, request, obj, form, change):
send_verification_mail(request, user, "signup_verify")


try:
class ProfileInline(admin.StackedInline):
model = get_profile_model()
can_delete = False
template = "admin/profile_inline.html"
extra = 0
class ProfileInline(admin.StackedInline):
model = get_profile_model()
can_delete = False
template = "admin/profile_inline.html"
extra = 0

def get_min_num(self, request, obj=None, **kwargs):
"""This causes profile forms to be shown when editing but hidden
when creating. If min_num is fixed at 1, Django's initial user
creation form fails if the profile model has a required field."""
return 0 if obj is None else 1

UserProfileAdmin.inlines += (ProfileInline,)
except ProfileNotConfigured:
pass

UserProfileAdmin.inlines += (get_profile_inline_admin(),)

if User in admin.site._registry:
admin.site.unregister(User)
Expand Down
9 changes: 9 additions & 0 deletions mezzanine/accounts/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
default=False,
)

register_setting(
name="ACCOUNTS_PROFILE_INLINE_CLASS",
description=_("Dotted package path and class name of the profile inline "
"admin that will be added to the User admin, when ``mezzanine.accounts`` "
"is installed and ``settings.AUTH_PROFILE_MODULE`` is defined."),
editable=False,
default="mezzanine.accounts.admin.ProfileInline",
)

register_setting(
name="ACCOUNTS_VERIFICATION_REQUIRED",
description=_("If ``True``, when users create an account, they will be "
Expand Down