@classmethod
    @badges_service
    @secure_post
    # add a badge to a user (gets passed
    # a badge and a current level over POST,
    # the username is taken from the environment)
    def badges_user_add(cls, slug, params=None):
        try:
            session = cls._get_gamesession(params)
            game = session.game
            if game is None:
                raise ApiException('No game with that slug')

            badge_key = params['badge_key']
            if not badge_key:
                raise ApiException('Must specify a badge_key to add.')

            # we have a badge_key now try to see if that badge exists
            badges_obj = Badges.get_singleton(game)
            badge = badges_obj.get_badge(badge_key)
            if not badge:
                raise ApiException('Badge name %s was not found.' % badge_key)
            if not ('image' in badge) or not badge['image']:
                badge['image'] = '/static/img/badge-46x46.png'

            # Use the badge['key'] property because badge_key is unicode
            ub = {'username': session.user.username,
                  'badge_key': badge['key']}

            badge_total = badge.get('total')
            total = badge_total or 1.0

            current = 0
            if 'current' in params:
                try:
                    current = float(int(params['current']))
                except (__HOLE__, TypeError):
                    response.status_int = 400
                    return {'ok': False, 'msg': '\'current\' must be a integer'}
            if not current:
                current = total
            ub['current'] = current

            userbadge = badges_obj.get_userbadge(session.user.username, badge_key)
            Badges.get_singleton(game).upsert_badge(ub)

            if current >= total and (not userbadge or userbadge.get('current', 0) < total):
                achieved = True
            else:
                achieved = False

            response.status_int = 200
            return {'ok': True, 'data': {
                'current': current,
                'total': badge_total,
                'badge_key': badge_key,
                'achieved': achieved
            }}

        except BadgesUnsupportedException:
            response.status_int = 404
            return {'ok': False, 'msg': 'Badges are unsupported for this game'}
        except ApiException as message:
            response.status_int = 404
            return {'ok': False, 'msg': str(message)}
dataset/ETHPy150Open turbulenz/turbulenz_local/turbulenz_local/controllers/apiv1/badges.py/BadgesController.badges_user_add