def create(cls, key, source_badges, variant_count): entry = cls(key, source_badges, variant_count) for i in range(variant_count): UserBadge.create(title=u'treatment-%s-%s' % (key, i), color=u'', visible=False, description=u'') meta.Session.add(entry) meta.Session.flush() return entry
def _update_userbadges(self, user): mappings = config.get_json("adhocracy.shibboleth.userbadge_mapping") is_modified = False for m in mappings: badge_title = m["title"] function_name = m["function"] kwargs = m["args"] badge = UserBadge.find(badge_title) if badge is None: raise Exception('configuration expects badge "%s"' % badge_title) want_badge = USERBADGE_MAPPERS[function_name](request, **kwargs) has_badge = badge in user.badges if want_badge and not has_badge: # assign badge badge.assign(user=user, creator=user) is_modified = True elif has_badge and not want_badge: # unassign badge user.badges.remove(badge) is_modified = True if is_modified: meta.Session.commit()
def _update_userbadges(self, user): tuples = get_userbadge_mapping() is_modified = False for t in tuples: badge_name = t[0] function_name = t[1] args = t[2:] badge = UserBadge.find(badge_name) want_badge = USERBADGE_MAPPERS[function_name](request, *args) has_badge = badge in user.badges if want_badge and not has_badge: # assign badge badge.assign(user=user, creator=user) is_modified = True elif has_badge and not want_badge: # unassign badge user.badges.remove(badge) is_modified = True if is_modified: meta.Session.commit()
def get_variant_badge(self, variant_id): return UserBadge.find('treatment-%s-%s' % (self.key, variant_id))