def karma(request): """Admin view that displays karma related data.""" if request.POST.get('init'): init_karma.delay() messages.add_message(request, messages.SUCCESS, 'init_karma task queued!') return HttpResponseRedirect(request.path) if request.POST.get('update-top'): update_top_contributors.delay() messages.add_message(request, messages.SUCCESS, 'update_top_contributors task queued!') return HttpResponseRedirect(request.path) kmgr = KarmaManager() top_alltime = [_user_karma_alltime(u, kmgr) for u in kmgr.top_alltime()] top_week = [_user_karma_week(u, kmgr) for u in kmgr.top_week()] username = request.GET.get('username') user_karma = None if username: try: user = User.objects.get(username=username) d = kmgr.user_data(user) user_karma = [{'key': k, 'value': d[k]} for k in sorted(d.keys())] except User.DoesNotExist: pass return render_to_response('karma/admin/karma.html', {'title': 'Karma', 'top_alltime': top_alltime, 'top_week': top_week, 'username': username, 'user_karma': user_karma}, RequestContext(request, {}))
def update_top_contributors(): """"Update the top contributor lists""" if not waffle.switch_is_active('karma'): return kmgr = KarmaManager() kmgr.update_top_alltime() kmgr.update_top_week()
def setUp(self): super(KarmaActionTests, self).setUp() self.user = user(save=True) try: self.mgr = KarmaManager() redis_client('karma').flushdb() except (KeyError, AttributeError): raise SkipTest
class KarmaActionTests(TestCase): def setUp(self): super(KarmaActionTests, self).setUp() self.user = user(save=True) try: self.mgr = KarmaManager() redis_client('karma').flushdb() except (KeyError, AttributeError): raise SkipTest @mock.patch.object(waffle, 'switch_is_active') def test_action(self, switch_is_active): """Save an action and verify.""" switch_is_active.return_value = True TestAction1(user=self.user).save() eq_(3, self.mgr.total_points(self.user)) eq_(1, self.mgr.total_count(TestAction1, self.user)) today = date.today() eq_(1, self.mgr.day_count(TestAction1, self.user, today)) eq_(1, self.mgr.month_count(TestAction1, self.user, today.year, today.month)) eq_(1, self.mgr.year_count(TestAction1, self.user, today.year)) @mock.patch.object(waffle, 'switch_is_active') def test_two_actions(self, switch_is_active): """Save two actions, one twice, and verify.""" switch_is_active.return_value = True TestAction1(user=self.user).save() TestAction2(user=self.user).save() TestAction2(user=self.user).save() eq_(17, self.mgr.total_points(self.user)) eq_(1, self.mgr.total_count(TestAction1, self.user)) eq_(2, self.mgr.total_count(TestAction2, self.user)) today = date.today() eq_(1, self.mgr.day_count(TestAction1, self.user, today)) eq_(1, self.mgr.month_count(TestAction1, self.user, today.year, today.month)) eq_(1, self.mgr.year_count(TestAction1, self.user, today.year)) eq_(2, self.mgr.day_count(TestAction2, self.user, today)) eq_(2, self.mgr.month_count(TestAction2, self.user, today.year, today.month)) eq_(2, self.mgr.year_count(TestAction2, self.user, today.year))