All badges are subclassed from MetaBadge .
To add a new badge, create the class corresponding to its in the file meta_badges.py.
Here’s how was added the Autobiographer badge.
Some fields in your badge class are required see below. This fields need to be set so the badge can be identified and displayed.
1 2 3 4 5 6 7 8 9 | class Autobiographer(MetaBadge):
id = "autobiographer"
model = UserProfile
one_time_only = True
title = "Autobiographer"
description = "Completed the User Profile"
link_to_doc = ""
level = "1"
|
If you want to point to a section in the user documentation set the field link_to_doc with it, otherwise don’t define this field.
The value returned by get_progress() will be used to calculate the percentage of progress (see openPLM.apps.badges.utils.MetaBadge).
1 2 3 4 | def get_progress(self, user):
has_email = 1 if user.email else 0
has_bio = 1 if user.profile.bio else 0
return has_email + has_bio
|
1 2 3 4 5 6 7 8 | def get_user(self, instance):
return instance.user
def check_email(self, instance):
return instance.user.email
def check_bio(self, instance):
return instance.bio
|
The get_user() function may need to be overriden for some badges.
The functions which name begins by check are used to check if the user (of the instance) can win the badge. You may not need to define more check functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Autobiographer(MetaBadge):
id = "autobiographer"
model = UserProfile
one_time_only = True
title = "Autobiographer"
description = "Completed the User Profile"
link_to_doc = ""
level = "1"
# not required
progress_start = 0
progress_finish = 2
# optional functions
def get_user(self, instance):
return instance.user
def check_email(self, instance):
return instance.user.email
def check_bio(self, instance):
return instance.bio
# required functions
def get_progress(self, user):
has_email = 1 if user.email else 0
has_bio = 1 if user.profile.bio else 0
return has_email + has_bio
|