script to award theme reviewer points retroactively (bug 909985)

This commit is contained in:
Kevin Ngo 2013-10-01 16:14:41 -07:00
Родитель ecbfdcdcdd
Коммит 69e80986cc
3 изменённых файлов: 58 добавлений и 26 удалений

Просмотреть файл

@ -0,0 +1,33 @@
import datetime
from django.core.management.base import BaseCommand
from django.db.models import Q
import amo
from amo.utils import chunked
from devhub.models import ActivityLog
import mkt.constants.reviewers as rvw
from mkt.reviewers.tasks import _batch_award_points
class Command(BaseCommand):
help = ('Retroactively award theme reviewer points for all the theme '
'reviewers done since the Great Theme Migration to amo up to when '
'we started recording points. MEANT FOR ONE-TIME RUN.')
def handle(self, *args, **options):
start_date = datetime.date(2013, 8, 27)
# Get theme reviews that are approves and rejects from before we started
# awarding.
approve = '"action": %s' % rvw.ACTION_APPROVE
reject = '"action": %s' % rvw.ACTION_REJECT
al_ids = (ActivityLog.objects.filter(
Q(_details__contains=approve) | Q(_details__contains=reject),
action=amo.LOG.THEME_REVIEW.id, created__lte=start_date)
.values_list('id', flat=True))
for chunk in chunked(al_ids, 1000):
# Review and thou shall receive.
_batch_award_points.delay(chunk)

Просмотреть файл

@ -1,3 +1,5 @@
import json
from django.conf import settings
from celeryutils import task
@ -8,6 +10,7 @@ from addons.tasks import create_persona_preview_images
from amo.decorators import write
from amo.storage_utils import copy_stored_file, move_stored_file
from amo.utils import LocalFileStorage, send_mail_jinja
from devhub.models import ActivityLog
from editors.models import ReviewerScore
import mkt.constants.reviewers as rvw
@ -116,15 +119,15 @@ def reject_rereview(theme):
@task
@write
def _batch_award_points(activity_logs, **kwargs):
"""For migration award_theme_rev_points."""
for log in activity_logs:
if not ReviewerScore.objects.filter(
user=log.user, addon=log.arguments[0],
score=amo.REVIEWED_SCORES.get(amo.REVIEWED_PERSONA),
note_key=amo.REVIEWED_PERSONA, note='RETROACTIVE').exists():
def _batch_award_points(activity_log_ids, **kwargs):
"""For command award_theme_points."""
activity_logs = (ActivityLog.objects.filter(id__in=activity_log_ids)
.select_related('user'))
ReviewerScore.objects.create(
user=log.user, addon=log.arguments[0],
score=amo.REVIEWED_SCORES.get(amo.REVIEWED_PERSONA),
note_key=amo.REVIEWED_PERSONA, note='RETROACTIVE')
score = amo.REVIEWED_SCORES.get(amo.REVIEWED_PERSONA)
ReviewerScore.objects.bulk_create(
[ReviewerScore(user=log.user, score=score, note='RETROACTIVE',
note_key=amo.REVIEWED_PERSONA,
addon_id=json.loads(log._arguments)[0]['addons.addon'])
for log in activity_logs]
)

Просмотреть файл

@ -1,6 +1,7 @@
import datetime
from django.conf import settings
from django.core.management import call_command
from django.db.models import Q
import mock
@ -234,10 +235,8 @@ class TestRefundsEscalationTask(amo.tests.TestCase):
class TestBatchAwardPoints(amo.tests.TestCase):
fixtures = fixture('user_999', 'user_2519')
def test_migration(self):
mig = __import__('migrations.661-award-theme-points')
run = getattr(mig, '661-award-theme-points').run
def test_award_theme_points_command(self):
# A review from before we started awarding points (award).
user_999 = UserProfile.objects.get(username='regularuser')
amo.log(amo.LOG.THEME_REVIEW, addon_factory(), details={
'action': rvw.ACTION_APPROVE
@ -245,21 +244,18 @@ class TestBatchAwardPoints(amo.tests.TestCase):
al = ActivityLog.objects.get()
al.created = datetime.date(2013, 4, 30)
al.save()
run()
call_command('award_theme_points')
eq_(ReviewerScore.objects.count(), 1)
eq_(ReviewerScore.objects.get().note_key, amo.REVIEWED_PERSONA)
eq_(ReviewerScore.objects.get().note, 'RETROACTIVE')
amo.log(amo.LOG.THEME_REVIEW, addon_factory(), details={
'action': rvw.ACTION_APPROVE
}, user=user_999)
al = ActivityLog.objects.order_by('-created')[0]
# A review from after we started awarding points (don't award).
al.created = datetime.date(2013, 8, 30)
al.save()
run()
call_command('award_theme_points')
eq_(ReviewerScore.objects.count(), 1)
def test_batch_award_points(self):
def test_batch_award_points_task(self):
user_999 = UserProfile.objects.get(username='regularuser')
addon_999 = addon_factory()
amo.log(amo.LOG.THEME_REVIEW, addon_999, details={
@ -277,14 +273,14 @@ class TestBatchAwardPoints(amo.tests.TestCase):
'action': rvw.ACTION_REJECT
}, user=user_2519)
# Mostly copied and pasted from migration award_theme_rev_points.py.
# Mostly copied and pasted from award_theme_points command.
approve = '"action": %s' % rvw.ACTION_APPROVE
reject = '"action": %s' % rvw.ACTION_REJECT
logs = ActivityLog.objects.filter(
log_ids = ActivityLog.objects.filter(
(Q(_details__contains=approve) | Q(_details__contains=reject)),
action=amo.LOG.THEME_REVIEW.id)
action=amo.LOG.THEME_REVIEW.id).values_list('id', flat=True)
_batch_award_points(logs)
_batch_award_points(log_ids)
eq_(ReviewerScore.objects.count(), 2)