Skip to content

Custom users preferences and settings

deby edited this page Oct 23, 2018 · 8 revisions

↑ Parent: Advanced tutorials

← Previous: Allow your staff team to change configurations

Settings page

Users can configure their preferences and settings under the settings page, which is part of the Default pages.

You may want to provide more settings available to users.

To do so, you can add more EXTRA_PREFERENCES in your Website settings ${PROJECT}/settings.py:

from magi.default_settings import DEFAULT_EXTRA_PREFERENCES

EXTRA_PREFERENCES = DEFAULT_EXTRA_PREFERENCES + [
    ('pronouns', _('Pronouns')),
]

EXTRA_PREFERENCES works like choices of dictionary fields.


If you need to customize the form fields, you can override the UserPreferencesForm like so:

${PROJECT}/settings.py:

CUSTOM_PREFERENCES_FORM = True

${PROJECT}/forms.py:

from magi.forms import UserPreferencesForm as _UserPreferencesForm

class UserPreferencesForm(_UserPreferencesForm):
    def __init__(self, *args, **kwargs):
        super(UserPreferencesForm, self).__init__(*args, **kwargs)
        if 'd_pronouns' in self.fields:
            self.fields['d_extra-pronouns'] = forms.ChoiceField(
                required=False,
		choices=BLANK_CHOICE_DASH + [('she', _('She/Her')), ('they', _('They/Them')), ...]),
                label=self.fields['d_extra-pronouns'].label,
                initial=self.fields['d_extra-pronouns'].initial,
            )

If you need to add more python logic before the settings page is rendered, you can overwrite the settings view function.

For example, to add cute forms:

${PROJECT}/settings.py

ENABLED_PAGES['settings']['custom'] = True

${PROJECT}/views.py

from magi.views import settingsContext

def settings(request):
    context = settingsContext(request)
    cuteFormFieldsForContext({
        'd_extra-pronouns': {
            'to_cuteform': 'value',
        },
    }, context, context['forms']['preferences'])
    return render(request, 'pages/settings.html', context)

If you want to add something to the list of links that show up under the profile's jumbotron:

${PROJECT}/magicollections.py

from magi.utils import AttrDict
from magi.magicollections importUserCollection as _UserCollection

class UserCollection(_UserCollection):
    class ItemView(_UserCollection.ItemView):
        def get_meta_links(self, user, context):
            first_links, meta_links, links = super(UserCollection.ItemView, self).get_meta_links(user, context)
            if user.preferences.extra.get('pronouns', None):
                meta_links.append(AttrDict({
                    't_type': _('Pronouns'),
                    'value': user.preferences.extra['pronouns'],
                }))
            return (first_links, meta_links, links)

I. Introduction

II. Tutorials

  1. Collections
    1. MagiModel
    2. MagiCollection
    3. MagiForm
    4. MagiFiltersForm
    5. MagiFields
  2. Single pages
  3. Configuring the navbar

III. References

IV. Utils

V. Advanced tutorials

VI. More

Clone this wiki locally