Skip to content

Commit

Permalink
[bug 716645] Recalculate num_votes_past_week.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlr committed Jan 25, 2012
1 parent 3855738 commit 98ef667
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions apps/questions/cron.py
Expand Up @@ -6,7 +6,7 @@

import cronjobs

from questions.models import QuestionVote
from questions.models import Question, QuestionVote
from questions.tasks import update_question_vote_chunk
from sumo.utils import chunked

Expand All @@ -15,13 +15,23 @@
def update_weekly_votes():
"""Keep the num_votes_past_week value accurate."""

recent = datetime.now() - timedelta(days=14)

# Get all questions (id) with a vote in the last week.
recent = datetime.now() - timedelta(days=7)
q = QuestionVote.objects.filter(created__gte=recent)
q = q.values_list('question_id', flat=True).order_by('question')
q = q.distinct()
q_with_recent_votes = list(q)

# Get all questions with num_votes_past_week > 0
q = Question.objects.filter(num_votes_past_week__gt=0)
q = q.values_list('id', flat=True)
q_with_nonzero_votes = list(q)

# Union.
qs_to_update = list(set(q_with_recent_votes + q_with_nonzero_votes))

for chunk in chunked(q, 50):
# Chunk them for tasks.
for chunk in chunked(qs_to_update, 50):
update_question_vote_chunk.apply_async(args=[chunk])


Expand Down

0 comments on commit 98ef667

Please sign in to comment.