Profile fields
Misago allows your users to fill in additional details about themselves for other users to see on their profiles, such as their real name, gender, short bio or contact options such as homepage address or Twitter/Skype handles.
In addition to those, Misago also uses "read only" profile fields to displays users IP addresses on their profiles for forum moderators.
Writing custom profile field
Under the hood, profile fields are Python classes inheriting from either the misago.users.profilefields.ProfileField
class or one of higher-level base fields.
To write your own profile field, you'll need to create custom class that specifies following properties and methods:
fieldname
fieldname
The fieldname
attribute specifies field's unique identifier in Misago. This identifier is used as key on user.profile_fields
model field to store field's value as well as source form inputs name
and id
attributes.
Example "Website" field that specifies website
as it's fieldname:
label
and get_label
label
and get_label
Your field will have to define either label
attribute, or get_label(user)
method if you wish to customize the label content's depending on user's field contents.
Below field will always display "Skype ID" as its label:
And example field that will look up user's gender before displaying label:
help_text
and get_help_text
help_text
and get_help_text
If you wish to, your field may profile help text for display in forms that will provide additional help to users filling in their details.
Unlike label, the help text is displayed only in forms.
Below fields have no help text:
This field specifies help text via help_text
property:
And this field builds help text using user's slug:
readonly
readonly
Defining readonly = True
on field will make it never editable. This is useful when you are defining profile fields that display different information about user but are never editable on their own.
Example field that displays to moderators an IP address that user used to register account:
is_editable
is_editable
The is_editable
method allows you make field conditionally read-only.
Below profile field will be only editable by profile field's moderators:
get_form_field
get_form_field
The get_form_field
method is called to obtain form field that is then added to user edit form, plugging the field to Django's form handling mechanics.
This method is used in two places:
to add field to edit user form in admin control panel.
to add field to edit user details form used behind the courtain by Misago to clean and validate user details sent to API from "edit details" form in user options and on user profile pages.
This field uses get_form_field
to specify URLField
as field that should be used to edit its value:
Notice that the fact that this field is hidden from users outside of admin control panel means that you may use it to specify admin-oriented label
and help_text
within it, and use label
and help_text
props documented ealier for user-facing UI.
Also take a note that while its not necessary, its good practice for all profile fields to be optional, and thus specify required=False
on field returned by get_form_field
.
get_form_field_json
get_form_field_json
This method is called to obtain the JSON describing how to build edit field in JavaScript UI. Because default implementation provided by the ProfileField
is versalite enough for great majority of use cases, its unlikely that you'll ever want to write custom implementation for your fields, instead limiting yourself to editing the get_input_json
exclusively.
Very much alike the get_form_field
documented ealier, because this method is origin of truth for details edit forms in user-facing UI, you may modify it to customize field's label and help text.
get_input_json
get_input_json
This method is called to obtain the JSON describing field input to create in end-user facing forms.
It supports either of those inputs:
Misago comes with convenience base classes for popular input types like text, textarea or select, that are documented further into this document.
clean
clean
In addition to cleaning and validation logic implemented by the form field returned in get_form_field
, you may include custom cleaning and validation logic on your field that gets run as part of form's clean and validate process.
Below example shows field that performs additional validation and cleanup on user-entered twitter handle:
Field's clean
method is called only when user filled it in edit form. It should return cleaned value, or raise ValidationError on any errors. Those errors will be associated with and displayed by their fields in form.
get_display_data
get_display_data
Just like the get_input_json
returns JSON describing how form field should be displayed in user-facing edit interface, the get_display_data
should return JSON describing how its value should be displayed on user's "details" tab in profile. This field is also well covered by the default implementation provided by the ProfileField
class, which does following:
If user's profile doesn't have value for field that we are trying to display, and field is not always readonly, return
None
, telling interface to don't display it.Call the
get_value_display_data
method to obtain JSON describing how field's value should be displayed on the page. If this method returnsNone
, don't display field on user's profile.Add field's name and label to returned JSON.
Because of such generic implementation, you'll likely limit yourself to customizing the get_value_display_data
when writing your fields, and only ever use this method if you want to customize displayed label.
get_value_display_data
get_value_display_data
This method returns JSON describing how field's value should be displayed on user's profile page. Misago supports quite a few display methods for profile fields:
In default implementation provided by the ProfileField
, this field is called by the get_display_data
. As such its prefferable in most cases for field to provide custom get_value_display_data
instead of get_display_data
.
As documented in the get_display_data
, returning None
from this methid will prevent this field from beind displayed on user's profile.
search_users
search_users
This method is called by search function available on users lists in admin control panel, to search users that have profile field with specified value. Its called with single argument, criteria
, that contains search string entered in search form, and should return None
or Q
object that lets Misago search field's value.
The ProfileField
class implements generic search_users
implementation that assumes that field's value is text and performs case sensitive search on users profile_fields
value as well as excludes readonly fields from search:
In addition to this ChoiceProfileField
base field provides custom implementation that searches users by label and value of their choice.
Base fields
To make it easier to implement common profile fields, Misago defines few custom classes importable from the misago.users.profilefields
:
ChoiceProfileField
ChoiceProfileField
This field lets user select single choice from list of available options. Example of such fields in Misago is GenderField
that allows user to select gender information to be displayed on his or her profile.
choices
and get_choices
choices
and get_choices
Fields inheriting from the ChoiceProfileField
require you to specify either choices
attribute, or get_choices
method if you wish for choices to be dependant on individual user.
If no user is provided to get_choices
, all possible choices should be returned - however this only affects the behaviour of searching users in admin control panel.
Example of field that lets user select gender information:
Field that associates available choices with user's posts count:
TextProfileField
TextProfileField
Default text input that takes no more than 250 characters and displays them as paragraph of text.
TextareaProfileField
TextareaProfileField
Textarea that takes no more than 500 characters and displays them as list paragraphs.
UrlifiedTextareaProfileField
UrlifiedTextareaProfileField
This field behaves same as UrlifiedTextareaProfileField
, but if user enters urls in the field, those will be turned into clickable anchor links.
UrlProfileField
UrlProfileField
Text input that takes no more than 250 characters, validates if input is valid URL and displays it as anchor link with rel="nofollow"
attribute on user's profile.
Default fields
Misago defines few default profile fields that can be imported from the misago.users.profilefields.default
:
BioField
BioField
Field inheriting from UrlifiedTextareaProfileField
that allows user to enter small text bit about themselves.
RealNameField
RealNameField
Field inheriting from TextProfileField
that allows user to enter real name.
LocationField
LocationField
Field inheriting from TextProfileField
that allows user to enter their location or origin.
GenderField
GenderField
Field inheriting from ChoiceProfileField
that allows user to disclose his/her gender to other members, or make their wish to keep it secret explictly known via "Not telling" option.
WebsiteField
WebsiteField
Field inheriting from UrlProfileField
that allows user to enter their website's address.
SkypeIdField
SkypeIdField
Field inheriting from TextProfileField
that allows user to enter their Skype ID.
TwitterHandleField
TwitterHandleField
Field inheriting from TextProfileField
that allows user to enter their Twitter handle. This field validates valid handles, strips optional leading "@" and displays handle as rel="nofollow"
url to user's Twitter profile.
Obtaining list of profile fields keys existing in database
Misago comes with special listusedprofilefields
command that lists fields existing in database, by their fieldname
:
Above result means that 5 users profile_fields
has bio
and real_name
keys and two users have location
key. This tool doesn't filter off empty values, meaning that those key exist, but may be empty strings.
Deleting profile field from database
If you want to delete key from users profile_fields
, you may do it with deleteprofilefield
commant. For example, to delete all information set in website
field, you may do this:
Likewise, this field deletes keys and does no checking if those are user entered values or empty strings.
Using logging for controling profile fields usage
Whenever user's profile_fields
value is edited either in admin control panel or via "edit details" form, such event, new values and old values are logged by misago.users.ProfileFields
logger.
Last updated