def test_get_all_badgets(self): #setup from adhocracy.model import Badge, CategoryBadge, DelegateableBadge, \ InstanceBadge from adhocracy.model import UserBadge, Instance instance = Instance.find(u'test') # create for each type a global scope and an instance scope badge InstanceBadge.create(u'badge ü', u'#ccc', u'description ü') InstanceBadge.create(u'badge ü', u'#ccc', u'description ü', instance=instance) UserBadge.create(u'badge ü', u'#ccc', u'description ü') UserBadge.create(u'ü', u'#ccc', u'ü', instance=instance) DelegateableBadge.create(u'badge ü', u'#ccc', u'description ü') DelegateableBadge.create(u'badge ü', u'#ccc', u'description ü', instance=instance) CategoryBadge.create(u'badge ü', u'#ccc', u"desc") CategoryBadge.create(u'badge ü', u'#ccc', u"desc", instance=instance) # all instance badges self.assert_(len(InstanceBadge.all()) == 1) self.assert_(len(InstanceBadge.all(instance=instance)) == 1) # all delegateable badges self.assert_(len(DelegateableBadge.all()) == 1) self.assert_(len(DelegateableBadge.all(instance=instance)) == 1) # all delegateable category badges self.assert_(len(CategoryBadge.all()) == 1) self.assert_(len(CategoryBadge.all(instance=instance)) == 1) # all user badgets self.assert_(len(UserBadge.all()) == 1) self.assert_(len(UserBadge.all(instance=instance)) == 1) # We can get all Badges by using `Badge` self.assert_(len(Badge.all()) == 4) self.assert_(len(Badge.all(instance=instance)) == 4) self.assert_(len(Badge.all_q().all()) == 8)
def _to_python(self, value, state): from adhocracy.model import Badge badge = Badge.by_id(value) if not badge: raise formencode.Invalid( _("No Badge ID '%s' exists") % value, value, state) return badge
def badge_styles(): ''' Render a <style>-block with dyamic badge styles ''' from adhocracy.lib.templating import render_def from adhocracy.model import Badge badges = Badge.all_q().all() return render_def('/badge/tiles.html', 'badge_styles', badges=badges, cached=True)
def badge_styles(instance): ''' Render a <style>-block with dyamic badge styles ''' from adhocracy.lib.templating import render_def from adhocracy.model import Badge badges = Badge.all(instance=instance, include_global=True) return render_def('/badge/tiles.html', 'badge_styles', badges=badges, cached=True)
def test_get_all_badges(self): # setup from adhocracy.model import Badge, CategoryBadge, DelegateableBadge, \ InstanceBadge, ThumbnailBadge from adhocracy.model import UserBadge, Instance instance = Instance.find(u'test') # create for each type a global scope and an instance scope badge InstanceBadge.create(u'badge ü', u'#ccc', True, u'description ü') InstanceBadge.create(u'badge ü', u'#ccc', True, u'description ü', instance=instance) UserBadge.create(u'badge ü', u'#ccc', True, u'description ü') UserBadge.create(u'ü', u'#ccc', True, u'ü', instance=instance) DelegateableBadge.create(u'badge ü', u'#ccc', True, u'description ü') DelegateableBadge.create(u'badge ü', u'#ccc', True, u'description ü', instance=instance) CategoryBadge.create(u'badge ü', u'#ccc', True, u"desc") CategoryBadge.create(u'badge ü', u'#ccc', True, u"desc", instance=instance) ThumbnailBadge.create(u'badge ü', u'#ccc', True, u"desc", thumbnail=b'binary') ThumbnailBadge.create(u'badge ü', u'#ccc', True, u"desc", thumbnail=b'binary', instance=instance) # all instance badges self.assertEqual(len(InstanceBadge.all()), 1) self.assertEqual(len(InstanceBadge.all(instance=instance)), 1) # all delegateable badges self.assertEqual(len(DelegateableBadge.all()), 1) self.assertEqual(len(DelegateableBadge.all(instance=instance)), 1) # all delegateable category badges self.assertEqual(len(CategoryBadge.all()), 1) self.assertEqual(len(CategoryBadge.all(instance=instance)), 1) # all delegateable thumbnail badges self.assertEqual(len(ThumbnailBadge.all()), 1) self.assertEqual(len(ThumbnailBadge.all(instance=instance)), 1) # all user badges self.assertEqual(len(UserBadge.all()), 3) self.assertEqual(len(UserBadge.all(instance=instance)), 1) # We can get all Badges by using `Badge` self.assertEqual(len(Badge.all()), 7) self.assertEqual(len(Badge.all(instance=instance)), 5) self.assertEqual(len(Badge.all_q().all()), 12)
def test_valid_badge(self): from adhocracy.forms import ValidBadge from adhocracy.model import Badge, UserBadge creator = tt_make_user('creator') badged_user = tt_make_user('badged_user') badge = Badge.create('testbadge', '#ccc', 'description') UserBadge.create(badged_user, badge, creator) value = ValidBadge.to_python(badge.id, None) self.assertEqual(value, badge)
def test_add_badge(self): from adhocracy.model import Badge #add badge badge = Badge.create(u'badge ü', u'#ccc', u'description ü') self.assert_(str(badge) == '<Badge(1,badge ?)>') #We can set a flag if this badge should be uses with users (default) #or with delegateables self.assert_(badge.badge_delegateable == False) badge.badge_delegateable = True self.assert_(badge.badge_delegateable == True)
def _make_one(self): """Returns creator, badged user and badge""" from adhocracy.model import Badge, UserBadge creator = tt_make_user('creator') badged_user = tt_make_user('badged_user') badge = Badge.create(u'testbadge', u'#ccc', u'description') UserBadge.create(badged_user, badge, creator) return creator, badged_user, badge
def _make_content(self): """Returns creator, delegateable and badge""" from adhocracy.model import Badge, Proposal, Instance instance = Instance.find('test') creator = tt_make_user('creator') delegateable = Proposal.create(instance, u"labeld", creator) badge = Badge.create(u'testbadge', u'#ccc', 'description') return creator, delegateable, badge
def index(self, format='html'): #require.user.manage() badges = Badge.all() if format == 'json': return render_json([badge.to_dict() for badge in badges]) badges_users = filter(lambda x: not x.badge_delegateable, badges) c.badges_users = sorted(badges_users, key=attrgetter('title')) badges_delegateables = filter(lambda x: x.badge_delegateable, badges) c.badges_delegateables = sorted(badges_delegateables, key=attrgetter('title')) return render("/badge/index.html")
def create(self): title = self.form_result.get('title').strip() description = self.form_result.get('description').strip() color = self.form_result.get('color').strip() group = self.form_result.get('group') display_group = self.form_result.get('display_group') badge_delegateable = bool(self.form_result.get('badge_delegateable')) badge = Badge.create(title, color, description, group, display_group, badge_delegateable) meta.Session.add(badge) meta.Session.commit() redirect(self.base_url)
def test_add_badge(self): from adhocracy.model import Badge # add badge badge = Badge.create(u"badge ü", u"#ccc", u"description ü") self.assert_(str(badge) == "<Badge(1,badge ?)>") # We can set a flag if this badge should be uses with users (default) # or with delegateables self.assert_(badge.badge_delegateable == False) badge.badge_delegateable = True self.assert_(badge.badge_delegateable == True)
def test_invalid_badge(self): from formencode import Invalid from adhocracy.forms import ValidBadge from adhocracy.model import Badge, UserBadge creator = tt_make_user('creator') badged_user = tt_make_user('badged_user') badge = Badge.create('testbadge', '#ccc', 'description') UserBadge.create(badged_user, badge, creator) self.assertRaises(Invalid, ValidBadge.to_python, badge.id + 1, state=None)
def _make_content(self): """Returns creator, delegateable and badge""" from adhocracy.model import Badge, Proposal, Instance instance = Instance.find("test") creator = tt_make_user("creator") delegateable = Proposal.create(instance, u"labeld", creator) badge = Badge.create(u"testbadge", u"#ccc", "description") return creator, delegateable, badge
def _make_one(self): """Returns creator, badged user and badge""" from adhocracy.model import Badge, UserBadge creator = tt_make_user("creator") badged_user = tt_make_user("badged_user") badge = Badge.create(u"testbadge", u"#ccc", u"description") UserBadge.create(badged_user, badge, creator) return creator, badged_user, badge
def _get_badge_or_redirect(self, id): ''' Get a badge. Redirect if it does not exist. Redirect if the badge is not from the current instance, but the user is only an instance admin, not a global admin ''' badge = Badge.by_id(id, instance_filter=False) if badge is None: self._redirect_not_found(id) if badge.instance != c.instance and not has('global.admin'): self._redirect_not_found(id) return badge
def test_to_dict(self): from adhocracy.model import Badge badge = Badge.create(u'badge', u'#ccc', u'description') result = badge.to_dict() self.assertEqual( result, { 'color': u'#ccc', 'title': u'badge', 'id': 1, 'users': [], 'display_group': False, 'group': None, 'badge_delegateable': False })
def test_to_dict(self): from adhocracy.model import Badge badge = Badge.create(u"badge", u"#ccc", u"description") result = badge.to_dict() self.assertEqual( result, { "color": u"#ccc", "title": u"badge", "id": 1, "users": [], "display_group": False, "group": None, "badge_delegateable": False, }, )
def edit(self, id, errors=None): c.form_title = c.save_button = _("Edit Badge") c.action_url = self.base_url + '/edit/%s' % id c.groups = meta.Session.query(Group).order_by(Group.group_name).all() badge = Badge.by_id(id) if badge is None: self._redirect_not_found(id) group_default = badge.group and badge.group.code or '' defaults = dict(title=badge.title, description=badge.description, color=badge.color, group=group_default, display_group=badge.display_group, badge_delegateable=badge.badge_delegateable, ) return htmlfill.render(render("/badge/form.html"), errors=errors, defaults=defaults)
def edit(self, id, errors=None): c.form_title = c.save_button = _("Edit Badge") c.action_url = self.base_url + '/edit/%s' % id c.groups = meta.Session.query(Group).order_by(Group.group_name).all() badge = Badge.by_id(id) if badge is None: self._redirect_not_found(id) group_default = badge.group and badge.group.code or '' defaults = dict( title=badge.title, description=badge.description, color=badge.color, group=group_default, display_group=badge.display_group, badge_delegateable=badge.badge_delegateable, ) return htmlfill.render(render("/badge/form.html"), errors=errors, defaults=defaults)
def update(self, id): badge = Badge.by_id(id) if badge is None: self._redirect_not_found(id) title = self.form_result.get('title').strip() description = self.form_result.get('description').strip() color = self.form_result.get('color').strip() group = self.form_result.get('group') display_group = self.form_result.get('display_group') if group: badge.group = group else: badge.group = None badge.title = title badge.color = color badge.description = description badge.display_group = display_group meta.Session.commit() h.flash(_("Badge changed successfully"), 'success') redirect(self.base_url)