from collections import defaultdict
from django.core.cache import cache
from django.db.models import F, Count
from judge.models import Submission

__all__ = ['contest_completed_ids', 'user_completed_ids']


def contest_completed_ids(participation):
    key = 'contest_complete:%d' % participation.id
    result = cache.get(key)
    if result is None:
        result = set(participation.submissions.filter(submission__result='AC', points=F('problem__points'))
                                  .values_list('problem__problem__id', flat=True).distinct())
        cache.set(key, result, 86400)
    return result


def user_completed_ids(profile):
    key = 'user_complete:%d' % profile.id
    result = cache.get(key)
    if result is None:
        result = set(Submission.objects.filter(user=profile, result='AC', points=F('problem__points'))
                               .values_list('problem_id', flat=True).distinct())
        cache.set(key, result, 86400)
    return result


def get_result_table(*args, **kwargs):
    if args:
        submissions = args[0]
        if kwargs:
            raise ValueError("Can't pass both queryset and keyword filters")
    else:
        submissions = Submission.objects.filter(**kwargs) if kwargs is not None else Submission.objects
    raw = submissions.values('result').annotate(count=Count('result')).values_list('result', 'count')
    results = defaultdict(int, raw)
    return [('Accepted', 'AC', results['AC']),
            ('Wrong Answer', 'WA', results['WA']),
            ('Compile Error', 'CE', results['CE']),
            ('Time Limit Exceed', 'TLE', results['TLE']),
            ('Memory Limit Exceed', 'MLE', results['MLE']),
            ('Invalid Return', 'IR', results['IR']),
            ('Total', 'TOT', sum(results.values()))]
@@ -29,16 +29,17 @@
 ultdict%0A
+%0A
 from dja
@@ -104,16 +104,68 @@
 , Count%0A
+from django.utils.translation import ugettext as _%0A%0A
 from jud
@@ -1155,16 +1155,18 @@
 ueError(
+_(
 %22Can't p
@@ -1204,16 +1204,17 @@
 ilters%22)
+)
 %0A    els
@@ -1471,16 +1471,18 @@
 eturn %5B(
+_(
 'Accepte
@@ -1483,16 +1483,17 @@
 ccepted'
+)
 , 'AC', 
@@ -1520,16 +1520,18 @@
         
+(_
 ('Wrong 
@@ -1537,16 +1537,17 @@
  Answer'
+)
 , 'WA', 
@@ -1574,16 +1574,18 @@
         
+(_
 ('Compil
@@ -1592,16 +1592,17 @@
 e Error'
+)
 , 'CE', 
@@ -1629,16 +1629,18 @@
         
+(_
 ('Time L
@@ -1651,16 +1651,17 @@
  Exceed'
+)
 , 'TLE',
@@ -1690,16 +1690,18 @@
         
+(_
 ('Memory
@@ -1714,16 +1714,17 @@
  Exceed'
+)
 , 'MLE',
@@ -1753,16 +1753,18 @@
         
+(_
 ('Invali
@@ -1772,16 +1772,17 @@
  Return'
+)
 , 'IR', 
@@ -1813,16 +1813,18 @@
     
+(_
 ('Total'
 , 'T
@@ -1819,16 +1819,17 @@
 ('Total'
+)
 , 'TOT',

Translate status table codes #337