-
Notifications
You must be signed in to change notification settings - Fork 11
Custom users preferences and settings
↑ Parent: Advanced tutorials
← Previous: Allow your staff team to change configurations
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)
→ Next: Background illustrations
I. Introduction
II. Tutorials
III. References
- Files tree
- Default collections
- Default pages
- Website settings
- MagiCollection settings
- Abstract models
- Abstract collections
- MagiForm settings
- MagiFiltersForm settings
- Single pages settings
IV. Utils
-
MagiModel utils
- MagiModel images and files
- DateTime fields
- BaseAccount model
- Save choices values as integer rather than strings
- Store comma separated values
- Store dictionaries
- Store Markdown texts
- Translate fields values in multiple languages
- Store JSON
- More model field types
- Transform images before saving them
- Check choices at form level instead of model level
- Use an internal cache for foreign keys in models
- Customize views with MagiModel properties
- Events
- Form utils
- Python utils
- Templates utils
- Javascript utils
- Enable and disable existing collections and pages
- Translations
- Page titles and descriptions
- Characters
- Seasons
- Roles and permissions
- Allow your staff team to change configurations
- Custom users preferences and settings
- Background illustrations
- Activities tabs
- Corner popups
VI. More