diff --git a/judge/ratings.py b/judge/ratings.py index 6a3dac6..b3ff7c6 100644 --- a/judge/ratings.py +++ b/judge/ratings.py @@ -2,7 +2,7 @@ import math from bisect import bisect from operator import attrgetter, itemgetter -from django.db import connection, transaction +from django.db import transaction from django.db.models import Count, OuterRef, Subquery from django.db.models.functions import Coalesce from django.utils import timezone @@ -152,18 +152,12 @@ def rate_contest(contest): now = timezone.now() ratings = [Rating(user_id=i, contest=contest, rating=r, volatility=v, last_rated=now, participation_id=p, rank=z) for i, p, r, v, z in zip(user_ids, participation_ids, rating, volatility, ranking)] - cursor = connection.cursor() - cursor.execute('CREATE TEMPORARY TABLE _profile_rating_update(id integer, rating integer)') - cursor.executemany('INSERT INTO _profile_rating_update VALUES (%s, %s)', list(zip(user_ids, rating))) with transaction.atomic(): Rating.objects.filter(contest=contest).delete() Rating.objects.bulk_create(ratings) - cursor.execute(''' - UPDATE `%s` p INNER JOIN `_profile_rating_update` tmp ON (p.id = tmp.id) - SET p.rating = tmp.rating - ''' % Profile._meta.db_table) - cursor.execute('DROP TABLE _profile_rating_update') - cursor.close() + Profile.objects.filter(contest_history__contest=contest, contest_history__virtual=0) \ + .update(rating=Subquery(Rating.objects.filter(user=OuterRef('id')) \ + .order_by('-contest__end_time').values('rating')[:1])) return old_rating, old_volatility, ranking, times_ranked, rating, volatility