本文整理汇总了Python中karma.manager.KarmaManager类的典型用法代码示例。如果您正苦于以下问题:Python KarmaManager类的具体用法?Python KarmaManager怎么用?Python KarmaManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了KarmaManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: overview
def overview(request):
"""Returns the overview for a daterange.
GET paramaters:
* daterange - 7d, 1m, 3m, 6m or 1y (default: 1y)
Returns an overview dict with a count for all action types.
"""
form = OverviewAPIForm(request.GET)
if not form.is_valid():
return {'success': False, 'errors': form.errors}
daterange = form.cleaned_data.get('daterange') or '1y'
mgr = KarmaManager()
overview = {}
for t in KarmaManager.action_types.keys():
overview[t] = mgr.count(daterange=daterange, type=t)
# TODO: Maybe have a karma action not assigned to a user for this?
num_days = KarmaManager.date_ranges[daterange]
start_day = date.today() - timedelta(days=num_days)
overview['question'] = Question.objects.filter(
created__gt=start_day).count()
return {
'success': True,
'overview': overview}
示例2: _process_recalculate_chunk
def _process_recalculate_chunk(data, **kwargs):
"""Recalculate karma points for a chunk of user ids."""
mgr = KarmaManager()
actions = [AnswerAction, AnswerMarkedHelpfulAction, AnswerMarkedNotHelpfulAction, FirstAnswerAction, SolutionAction]
actions_dict = dict((a.action_type, a.points) for a in actions)
for userid in data:
mgr.recalculate_points(userid, actions_dict)
示例3: setUp
def setUp(self, switch_is_active):
switch_is_active.return_value = True
super(KarmaAPITests, self).setUp()
try:
self.mgr = KarmaManager()
redis_client('karma').flushdb()
except RedisError:
raise SkipTest
self.user1 = user(save=True)
self.user2 = user(save=True)
self.user3 = user(save=True)
TestAction1(user=self.user1).save()
TestAction2(user=self.user2).save()
TestAction2(user=self.user2).save()
TestAction1(user=self.user3).save()
TestAction1(user=self.user3).save()
TestAction1(user=self.user3).save()
self.mgr.update_top()
self.client.login(username=self.user1.username, password='testpass')
add_permission(self.user1, models.Title, 'view_dashboard')
示例4: setUp
def setUp(self, switch_is_active):
switch_is_active.return_value = True
super(KarmaManagerTests, self).setUp()
try:
self.mgr = KarmaManager()
redis_client('karma').flushdb()
except RedisError:
raise SkipTest
self.user1 = user(save=True)
self.user2 = user(save=True)
self.user3 = user(save=True)
today = date.today()
# user1 actions (3 + 3 + 7):
TestAction1(user=self.user1, day=today).save()
TestAction1(user=self.user1, day=today).save()
TestAction2(user=self.user1, day=today).save()
# user2 actions (3 + 7 + 7):
TestAction1(user=self.user2, day=today - timedelta(days=8)).save()
TestAction2(user=self.user2, day=today - timedelta(days=32)).save()
TestAction2(user=self.user2, day=today - timedelta(days=360)).save()
# user3 actions (3 + 3 + 3 + 7):
TestAction1(user=self.user3, day=today - timedelta(days=10)).save()
TestAction1(user=self.user3, day=today - timedelta(days=40)).save()
TestAction1(user=self.user3, day=today - timedelta(days=190)).save()
TestAction2(user=self.user3, day=today - timedelta(days=3)).save()
示例5: karma
def karma(request):
"""Admin view that displays karma related data."""
if not request.user.has_perm('users.view_karma_points'):
raise PermissionDenied
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('recalculate'):
recalculate_karma_points.delay()
messages.add_message(request, messages.SUCCESS,
'recalculate_karma_points 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_users('all') or []]
top_week = [_user_karma_week(u, kmgr) for
u in kmgr.top_users(daterange='1w') or []]
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(
request,
'admin/karma.html',
{'title': 'Karma',
'top_alltime': top_alltime,
'top_week': top_week,
'username': username,
'user_karma': user_karma})
示例6: setUp
def setUp(self):
super(KarmaActionTests, self).setUp()
self.user = user(save=True)
try:
self.mgr = KarmaManager()
redis_client('karma').flushdb()
except RedisError:
raise SkipTest
示例7: user_num_answers
def user_num_answers(user):
"""Count the number of answers a user has.
If karma is enabled, and redis is working, this will query that (much
faster), otherwise it will just count objects in the database.
"""
if waffle.switch_is_active('karma'):
try:
km = KarmaManager()
count = km.count(user=user, type=AnswerAction.action_type)
if count is not None:
return count
except RedisError as e:
statsd.incr('redis.errror')
log.error('Redis connection error: %s' % e)
return Answer.objects.filter(creator=user).count()
示例8: users
def users(request):
"""Returns list of user karma information.
GET paramaters:
* daterange - 7d, 1m, 3m, 6m or 1y (default: 1y)
* sort - field to sort on (default: points). Order is always descending.
* page - starts at 1 (default: 1)
* pagesize - (default: 100)
Returns list of objects with the following fields:
userid, username, points, <action_types>
"""
form = UserAPIForm(request.GET)
if not form.is_valid():
return {'success': False, 'errors': form.errors}
daterange = form.cleaned_data.get('daterange') or '1y'
sort = form.cleaned_data.get('sort') or 'points'
page = form.cleaned_data.get('page') or 1
pagesize = form.cleaned_data.get('pagesize') or 100
mgr = KarmaManager()
users = mgr.top_users(daterange=daterange, type=sort, count=pagesize,
offset=(page - 1) * pagesize) or []
now = datetime.now()
action_types = KarmaManager.action_types.keys()
schema = ['id', 'username', 'lastactivity', 'points'] + action_types
user_list = []
for u in users:
user = [u.id, u.username]
last_activity = Answer.last_activity_for(u)
user.append((now - last_activity).days if last_activity else None)
user.append(mgr.count(u, daterange=daterange, type='points'))
for t in action_types:
user.append(mgr.count(u, daterange=daterange, type=t))
user_list.append(user)
return {
'success': True,
'results': user_list,
'schema': schema}
示例9: user_num_solutions
def user_num_solutions(user):
"""Count the number of solutions a user has.
This means the number of answers the user has submitted that are then
marked as the solution to the question they belong to.
If karma is enabled, and redis is working, this will query that (much
faster), otherwise it will just count objects in the database.
"""
if waffle.switch_is_active('karma'):
try:
km = KarmaManager()
count = km.count(user=user, type=SolutionAction.action_type)
if count is not None:
return count
except RedisError as e:
statsd.incr('redis.errror')
log.error('Redis connection error: %s' % e)
return Question.objects.filter(solution__in=Answer.objects
.filter(creator=user)).count()
示例10: users
def users(request):
"""Returns list of user karma information.
GET paramaters:
* daterange - 7d, 1m, 3m, 6m or 1y (default: 1y)
* sort - field to sort on (default: points). Order is always descending.
* page - starts at 1 (default: 1)
* pagesize - (default: 100)
Returns list of objects with the following fields:
userid, username, points, <action_types>
"""
form = UserAPIForm(request.GET)
if not form.is_valid():
return {"success": False, "errors": form.errors}
daterange = form.cleaned_data.get("daterange") or "1y"
sort = form.cleaned_data.get("sort") or "points"
page = form.cleaned_data.get("page") or 1
pagesize = form.cleaned_data.get("pagesize") or 100
mgr = KarmaManager()
users = mgr.top_users(daterange, type=sort, count=pagesize, offset=(page - 1) * pagesize) or []
now = datetime.now()
action_types = KarmaManager.action_types.keys()
schema = ["id", "username", "lastactivity", "points"] + action_types
user_list = []
for u in users:
user = [u.id, u.username]
last_activity = Answer.last_activity_for(u)
user.append((now - last_activity).days if last_activity else None)
user.append(mgr.count(daterange, u, type="points"))
for t in action_types:
user.append(mgr.count(daterange, u, type=t))
user_list.append(user)
return {"success": True, "results": user_list, "schema": schema}
示例11: KarmaManagerTests
class KarmaManagerTests(TestCase):
def setUp(self):
super(KarmaManagerTests, self).setUp()
self.user = user(save=True)
try:
self.mgr = KarmaManager()
redis_client('karma').flushdb()
except RedisError:
raise SkipTest
@mock.patch.object(waffle, 'switch_is_active')
def test_recalculate_points(self, switch_is_active):
"""Test the recalculate_points method."""
switch_is_active.return_value = True
TestAction1(user=self.user).save() # 3pts
TestAction1(user=self.user).save() # 3pts
TestAction2(user=self.user).save() # 7pts
eq_(13, self.mgr.total_points(self.user))
new_pts = {
'test-action-1': 15,
'test-action-2': 12
}
self.mgr.recalculate_points(self.user, new_pts)
eq_(42, self.mgr.total_points(self.user))
示例12: KarmaAPITests
class KarmaAPITests(TestCase):
client_class = LocalizingClient
@mock.patch.object(waffle, 'switch_is_active')
def setUp(self, switch_is_active):
switch_is_active.return_value = True
super(KarmaAPITests, self).setUp()
try:
self.mgr = KarmaManager()
redis_client('karma').flushdb()
except RedisError:
raise SkipTest
self.user1 = user(save=True)
self.user2 = user(save=True)
self.user3 = user(save=True)
TestAction1(user=self.user1).save()
TestAction2(user=self.user2).save()
TestAction2(user=self.user2).save()
TestAction1(user=self.user3).save()
TestAction1(user=self.user3).save()
TestAction1(user=self.user3).save()
self.mgr.update_top()
self.client.login(username=self.user1.username, password='testpass')
add_permission(self.user1, models.Title, 'view_dashboard')
@mock.patch.object(waffle, 'switch_is_active')
def test_user_api_no_permission(self, switch_is_active):
"""No view_dashboard permission? No API for you."""
switch_is_active.return_value = True
self.client.login(username=self.user2.username, password='testpass')
url = reverse('karma.api.users')
response = self.client.get(url)
eq_(403, response.status_code)
@mock.patch.object(waffle, 'switch_is_active')
def test_user_api_default(self, switch_is_active):
"""Test user API with all defaults."""
switch_is_active.return_value = True
url = reverse('karma.api.users')
response = self.client.get(url)
eq_(200, response.status_code)
r = json.loads(response.content)
user_ids = [u[0] for u in r['results']]
eq_([self.user2.id, self.user3.id, self.user1.id], user_ids)
@mock.patch.object(waffle, 'switch_is_active')
def test_user_api_sort_testaction1(self, switch_is_active):
"""Test user API with sort = TestAction1."""
switch_is_active.return_value = True
url = reverse('karma.api.users')
url = urlparams(url, sort=TestAction1.action_type)
response = self.client.get(url)
eq_(200, response.status_code)
r = json.loads(response.content)
user_ids = [u[0] for u in r['results']]
eq_([self.user3.id, self.user1.id], user_ids)
@mock.patch.object(waffle, 'switch_is_active')
def test_user_api_sort_testaction2(self, switch_is_active):
"""Test user API with sort = TestAction2."""
switch_is_active.return_value = True
url = reverse('karma.api.users')
url = urlparams(url, sort=TestAction2.action_type)
response = self.client.get(url)
eq_(200, response.status_code)
r = json.loads(response.content)
user_ids = [u[0] for u in r['results']]
eq_([self.user2.id], user_ids)
@mock.patch.object(waffle, 'switch_is_active')
def test_user_api_last_activity(self, switch_is_active):
"""Verify the last activity field."""
switch_is_active.return_value = True
now = datetime.now()
one_day = now - timedelta(days=1)
two_days = now - timedelta(days=2)
answer(creator=self.user1, created=now, save=True)
answer(creator=self.user2, created=one_day, save=True)
answer(creator=self.user3, created=two_days, save=True)
url = reverse('karma.api.users')
response = self.client.get(url)
eq_(200, response.status_code)
r = json.loads(response.content)
days_since_last_activity = [u[2] for u in r['results']]
eq_([1, 2, 0], days_since_last_activity)
@mock.patch.object(waffle, 'switch_is_active')
def test_overview_api(self, switch_is_active):
"""Test overview API."""
switch_is_active.return_value = True
url = reverse('karma.api.overview')
url = urlparams(url, daterange='6m')
#.........这里部分代码省略.........
示例13: KarmaAPITests
class KarmaAPITests(TestCase):
client_class = LocalizingClient
@mock.patch.object(waffle, "switch_is_active")
def setUp(self, switch_is_active):
switch_is_active.return_value = True
super(KarmaAPITests, self).setUp()
try:
self.mgr = KarmaManager()
redis_client("karma").flushdb()
except RedisError:
raise SkipTest
self.user1 = user(save=True)
self.user2 = user(save=True)
self.user3 = user(save=True)
TestAction1(user=self.user1).save()
TestAction2(user=self.user2).save()
TestAction2(user=self.user2).save()
TestAction1(user=self.user3).save()
TestAction1(user=self.user3).save()
TestAction1(user=self.user3).save()
self.mgr.update_top()
self.client.login(username=self.user1.username, password="testpass")
add_permission(self.user1, models.Title, "view_dashboard")
@mock.patch.object(waffle, "switch_is_active")
def test_user_api_no_permission(self, switch_is_active):
"""No view_dashboard permission? No API for you."""
switch_is_active.return_value = True
self.client.login(username=self.user2.username, password="testpass")
url = reverse("karma.api.users")
response = self.client.get(url)
eq_(403, response.status_code)
@mock.patch.object(waffle, "switch_is_active")
def test_user_api_default(self, switch_is_active):
"""Test user API with all defaults."""
switch_is_active.return_value = True
url = reverse("karma.api.users")
response = self.client.get(url)
eq_(200, response.status_code)
r = json.loads(response.content)
user_ids = [u[0] for u in r["results"]]
eq_([self.user2.id, self.user3.id, self.user1.id], user_ids)
@mock.patch.object(waffle, "switch_is_active")
def test_user_api_sort_testaction1(self, switch_is_active):
"""Test user API with sort = TestAction1."""
switch_is_active.return_value = True
url = reverse("karma.api.users")
url = urlparams(url, sort=TestAction1.action_type)
response = self.client.get(url)
eq_(200, response.status_code)
r = json.loads(response.content)
user_ids = [u[0] for u in r["results"]]
eq_([self.user3.id, self.user1.id], user_ids)
@mock.patch.object(waffle, "switch_is_active")
def test_user_api_sort_testaction2(self, switch_is_active):
"""Test user API with sort = TestAction2."""
switch_is_active.return_value = True
url = reverse("karma.api.users")
url = urlparams(url, sort=TestAction2.action_type)
response = self.client.get(url)
eq_(200, response.status_code)
r = json.loads(response.content)
user_ids = [u[0] for u in r["results"]]
eq_([self.user2.id], user_ids)
示例14: questions
#.........这里部分代码省略.........
"questions_questionvote.question_id = "
"questions_question.id"
}
)
question_qs = question_qs.filter(creator__is_active=1)
if filter_ == "no-replies":
question_qs = question_qs.filter(num_answers=0, is_locked=False)
elif filter_ == "replies":
question_qs = question_qs.filter(num_answers__gt=0)
elif filter_ == "solved":
question_qs = question_qs.exclude(solution=None)
elif filter_ == "unsolved":
question_qs = question_qs.filter(solution=None)
elif filter_ == "my-contributions" and request.user.is_authenticated():
criteria = Q(answers__creator=request.user) | Q(creator=request.user)
question_qs = question_qs.filter(criteria).distinct()
elif filter_ == "recent-unanswered":
# Only unanswered questions from the last 72 hours.
start = datetime.now() - timedelta(hours=72)
question_qs = question_qs.filter(num_answers=0, created__gt=start, is_locked=False)
else:
filter_ = None
feed_urls = ((reverse("questions.feed"), QuestionsFeed().title()),)
if tagged:
tag_slugs = tagged.split(",")
tags = Tag.objects.filter(slug__in=tag_slugs)
if tags:
for t in tags:
question_qs = question_qs.filter(tags__name__in=[t.name])
if len(tags) == 1:
feed_urls += (
(reverse("questions.tagged_feed", args=[tags[0].slug]), TaggedQuestionsFeed().title(tags[0])),
)
else:
question_qs = Question.objects.get_empty_query_set()
# Exclude questions over 90 days old without an answer.
oldest_date = date.today() - timedelta(days=90)
question_qs = question_qs.exclude(created__lt=oldest_date, num_answers=0)
# Filter by product.
if product:
# This filter will match if any of the products on a question have the
# correct id.
question_qs = question_qs.filter(products__id__exact=product.id)
# Set the order.
question_qs = question_qs.order_by(*order)
try:
with statsd.timer("questions.view.paginate.%s" % filter_):
questions_page = simple_paginate(request, question_qs, per_page=constants.QUESTIONS_PER_PAGE)
except (PageNotAnInteger, EmptyPage):
# If we aren't on page 1, redirect there.
# TODO: Is 404 more appropriate?
if request.GET.get("page", "1") != "1":
url = build_paged_url(request)
return HttpResponseRedirect(urlparams(url, page=1))
# Recent answered stats
recent_asked_count = Question.recent_asked_count()
recent_unanswered_count = Question.recent_unanswered_count()
if recent_asked_count:
recent_answered_percent = int((float(recent_asked_count - recent_unanswered_count) / recent_asked_count) * 100)
else:
recent_answered_percent = 0
# List of products to fill the selector.
product_list = Product.objects.filter(visible=True)
data = {
"questions": questions_page,
"feeds": feed_urls,
"filter": filter_,
"sort": sort_,
"tags": tags,
"tagged": tagged,
"recent_asked_count": recent_asked_count,
"recent_unanswered_count": recent_unanswered_count,
"recent_answered_percent": recent_answered_percent,
"product_list": product_list,
"product": product,
}
if waffle.flag_is_active(request, "karma") and waffle.switch_is_active("karma"):
kmgr = KarmaManager()
data.update(karma_top=kmgr.top_users("3m"))
if request.user.is_authenticated():
ranking = kmgr.ranking("3m", request.user)
if ranking <= constants.HIGHEST_RANKING:
data.update(karma_ranking=ranking)
else:
data.update(top_contributors=_get_top_contributors())
with statsd.timer("questions.view.render"):
return jingo.render(request, template, data)
示例15: KarmaManagerTests
class KarmaManagerTests(TestCase):
@mock.patch.object(waffle, 'switch_is_active')
def setUp(self, switch_is_active):
switch_is_active.return_value = True
super(KarmaManagerTests, self).setUp()
try:
self.mgr = KarmaManager()
redis_client('karma').flushdb()
except RedisError:
raise SkipTest
self.user1 = user(save=True)
self.user2 = user(save=True)
self.user3 = user(save=True)
today = date.today()
# user1 actions (3 + 3 + 7):
TestAction1(user=self.user1, day=today).save()
TestAction1(user=self.user1, day=today).save()
TestAction2(user=self.user1, day=today).save()
# user2 actions (3 + 7 + 7):
TestAction1(user=self.user2, day=today - timedelta(days=8)).save()
TestAction2(user=self.user2, day=today - timedelta(days=32)).save()
TestAction2(user=self.user2, day=today - timedelta(days=360)).save()
# user3 actions (3 + 3 + 3 + 7):
TestAction1(user=self.user3, day=today - timedelta(days=10)).save()
TestAction1(user=self.user3, day=today - timedelta(days=40)).save()
TestAction1(user=self.user3, day=today - timedelta(days=190)).save()
TestAction2(user=self.user3, day=today - timedelta(days=3)).save()
@mock.patch.object(waffle, 'switch_is_active')
def test_count(self, switch_is_active):
"""Test count method."""
switch_is_active.return_value = True
self.mgr.update_top()
eq_(13, self.mgr.count(self.user1, type='points'))
eq_(2, self.mgr.count(self.user1, type=TestAction1.action_type))
eq_(1, self.mgr.count(self.user1, type=TestAction2.action_type))
eq_(0, self.mgr.count(self.user2, type='points', daterange='1w'))
eq_(3, self.mgr.count(self.user2, type='points', daterange='1m'))
eq_(2, self.mgr.count(self.user2, type=TestAction2.action_type,
daterange='1y'))
eq_(2, self.mgr.count(self.user3, type=TestAction1.action_type,
daterange='6m'))
@mock.patch.object(waffle, 'switch_is_active')
def test_top_users(self, switch_is_active):
"""Test top_users method."""
switch_is_active.return_value = True
self.mgr.update_top()
u1, u2, u3 = self.user1, self.user2, self.user3
eq_([u2, u3, u1], self.mgr.top_users())
eq_([u3, u1, u2], self.mgr.top_users(type=TestAction1.action_type))
eq_([u3, u1, u2], self.mgr.top_users(type=TestAction1.action_type))
eq_([u1], self.mgr.top_users(type=TestAction1.action_type,
daterange='1w'))
eq_([u1, u3], self.mgr.top_users(daterange='1w'))
@mock.patch.object(waffle, 'switch_is_active')
def test_ranking(self, switch_is_active):
"""Test ranking method."""
switch_is_active.return_value = True
self.mgr.update_top()
eq_(1, self.mgr.ranking(self.user2))
eq_(3, self.mgr.ranking(self.user1))
eq_(1, self.mgr.ranking(self.user1, daterange='1w'))
eq_(1, self.mgr.ranking(self.user3, type=TestAction1.action_type))
@mock.patch.object(waffle, 'switch_is_active')
def test_recalculate_points(self, switch_is_active):
"""Test the recalculate_points method."""
switch_is_active.return_value = True
KarmaManager.action_types = {
'test-action-1': 15,
'test-action-2': 12
}
self.mgr.recalculate_points(self.user1)
eq_(42, self.mgr.count(self.user1, type='points'))