Table des matières

Sujet précédent

6.1. Badges

Sujet suivant

6.1.2. badges.views — views for badges

Cette page

Autres langues

6.1.1. badges.utils

6.1.1.1. MetaBadge

All badges are subclassed from MetaBadge .

class openPLM.apps.badges.utils.MetaBadge[source]
get_user(instance)[source]

This function should be overriden if the user can not be reached using instance.user

get_progress_percentage(progress=None, user=None)[source]

Return the percentage of progress for a user.

check_user(instance)[source]

checks if the user exists or not

check_can_win_badge(instance)[source]

Checks if a user can win a badge. Some badge can only be won by contributor. If a badge can be won by user who are not contributor this function should be overridden

6.1.1.2. How to add a new badge

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.

6.1.1.2.1. Fields

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.

Example :
To point to http://wiki.openplm.org/docs/dev/en/user/tuto_3_user.html#delegation set link_to_doc to “tuto_3_user.html#delegation”

6.1.1.2.2. Functions

6.1.1.2.2.1. get_progress()

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

6.1.1.2.2.2. Optional functions

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.

6.1.1.2.3. The complete Autobiographer class

 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