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 5 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_form():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the name of this function misleading. It currently implies that a form class is returned, not an admin class. I suggest we rename it to get_profile_inline_admin().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Done

"""
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
22 changes: 9 additions & 13 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_form

from mezzanine.core.admin import SitePermissionUserAdmin
from mezzanine.conf import settings
Expand Down Expand Up @@ -45,20 +46,15 @@ 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,)
try:
UserProfileAdmin.inlines += (get_profile_inline_form(),)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we even need to create a wrapper function for this. import_dotted_path() already handles exceptions with sufficient output to debug if something goes wrong. Maybe we can call it here directly and drop this function altogether?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me. I don't see a reason we need trap and log for ProfileNotConfigured anymore. But I could easily be overlooking something. I will defer to you all on that one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, let's see what @stephenmcd has to say.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @jerivas, function seems unnecessary, especially since the default should never fail since the setting will default to something that always works

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it only gets called in one spot, so there's no notion of reusability

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks. Removed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's still there.

except ProfileNotConfigured:
pass

Expand Down
8 changes: 8 additions & 0 deletions mezzanine/accounts/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
default=False,
)

register_setting(
name="ACCOUNTS_PROFILE_INLINE_CLASS",
description=_("Dotted package path of the profile inline "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this could explain a bit better that this appears in the User admin. Something like "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."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"class to use when ``mezzanine.accounts`` is installed."),
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