本文整理汇总了Python中karma.manager.KarmaManager.ranking方法的典型用法代码示例。如果您正苦于以下问题:Python KarmaManager.ranking方法的具体用法?Python KarmaManager.ranking怎么用?Python KarmaManager.ranking使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类karma.manager.KarmaManager
的用法示例。
在下文中一共展示了KarmaManager.ranking方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: questions
# 需要导入模块: from karma.manager import KarmaManager [as 别名]
# 或者: from karma.manager.KarmaManager import ranking [as 别名]
#.........这里部分代码省略.........
else:
order = '-updated'
question_qs = Question.objects.select_related(
'creator', 'last_answer', 'last_answer__creator')
if not waffle.switch_is_active('hide-total-question-votes'):
question_qs = question_qs.extra(
{'_num_votes': 'SELECT COUNT(*) FROM questions_questionvote WHERE '
'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)
# 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
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}
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)
示例2: KarmaManagerTests
# 需要导入模块: from karma.manager import KarmaManager [as 别名]
# 或者: from karma.manager.KarmaManager import ranking [as 别名]
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
# Create Points with new point values.
p1 = Points.objects.create(action='test-action-1', points=15)
Points.objects.create(action='test-action-2', points=12)
self.mgr.recalculate_points(self.user1)
eq_(42, self.mgr.count(self.user1, type='points'))
# Update one of the Point values.
p1.points = 30
p1.save()
self.mgr.recalculate_points(self.user1)
eq_(72, self.mgr.count(self.user1, type='points'))
@mock.patch.object(waffle, 'switch_is_active')
def test_overview_counts(self, switch_is_active):
"""Verify the overview counts are correct."""
switch_is_active.return_value = True
self.mgr.update_top()
eq_(46, self.mgr.count(type='points'))
eq_(6, self.mgr.count(type=TestAction1.action_type))
eq_(4, self.mgr.count(type=TestAction2.action_type))
#.........这里部分代码省略.........
示例3: KarmaManagerTests
# 需要导入模块: from karma.manager import KarmaManager [as 别名]
# 或者: from karma.manager.KarmaManager import ranking [as 别名]
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'))
示例4: questions
# 需要导入模块: from karma.manager import KarmaManager [as 别名]
# 或者: from karma.manager.KarmaManager import ranking [as 别名]
def questions(request):
"""View the questions."""
filter_ = request.GET.get('filter')
tagged = request.GET.get('tagged')
tags = None
sort_ = request.GET.get('sort')
cache_count = True # Some counts are too esoteric to cache right now.
if sort_ == 'requested':
order = '-num_votes_past_week'
else:
sort_ = None
order = '-updated'
question_qs = Question.objects.select_related(
'creator', 'last_answer', 'last_answer__creator')
question_qs = question_qs.extra(
{'_num_votes': 'SELECT COUNT(*) FROM questions_questionvote WHERE '
'questions_questionvote.question_id = '
'questions_question.id'})
question_qs = question_qs.filter(creator__is_active=1, is_locked=False)
if filter_ == 'no-replies':
question_qs = question_qs.filter(num_answers=0)
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()
cache_count = False
else:
filter_ = None
feed_urls = ((reverse('questions.feed'),
QuestionsFeed().title()),)
if tagged:
cache_count = False
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()
question_qs = question_qs.order_by(order)
if cache_count:
cache_key = u'questions:count:%s' % filter_
count = cache.get(cache_key)
if not count:
count = question_qs.count()
cache.add(cache_key, count, settings.QUESTIONS_COUNT_TTL)
else:
count = question_qs.count()
questions_ = paginate(request, question_qs, count=count,
per_page=constants.QUESTIONS_PER_PAGE)
data = {'questions': questions_, 'feeds': feed_urls, 'filter': filter_,
'sort': sort_, 'tags': tags, 'tagged': tagged}
if (waffle.flag_is_active(request, 'karma') and
waffle.switch_is_active('karma')):
kmgr = KarmaManager()
data.update(karma_top=kmgr.top_users())
if request.user.is_authenticated():
ranking = kmgr.ranking(request.user)
if ranking <= constants.HIGHEST_RANKING:
data.update(karma_ranking=ranking)
else:
data.update(top_contributors=_get_top_contributors())
return jingo.render(request, 'questions/questions.html', data)
示例5: questions
# 需要导入模块: from karma.manager import KarmaManager [as 别名]
# 或者: from karma.manager.KarmaManager import ranking [as 别名]
#.........这里部分代码省略.........
"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)