Esempio n. 1
0
 def calculate_points(self, table=_pp_table):
     from judge.models import Problem
     public_problems = Problem.get_public_problems()
     data = (public_problems.filter(
         submission__user=self,
         submission__points__isnull=False).annotate(max_points=Max(
             'submission__points')).order_by('-max_points').values_list(
                 'max_points', flat=True).filter(max_points__gt=0))
     extradata = (public_problems.filter(
         submission__user=self,
         submission__result='AC').values('id').distinct().count())
     bonus_function = settings.DMOJ_PP_BONUS_FUNCTION
     points = sum(data)
     problems = len(data)
     pp = sum(x * y
              for x, y in zip(table, data)) + bonus_function(extradata)
     if not float_compare_equal(self.points, points) or \
        problems != self.problem_count or \
        not float_compare_equal(self.performance_points, pp):
         self.points = points
         self.problem_count = problems
         self.performance_points = pp
         self.save(update_fields=[
             'points', 'problem_count', 'performance_points'
         ])
         for org in self.organizations.get_queryset():
             org.calculate_points()
     return points
Esempio n. 2
0
 def calculate_points(self, table=_pp_table):
     from judge.models import Problem
     public_problems = Problem.get_public_problems()
     data = (public_problems.filter(
         submission__user=self,
         submission__points__isnull=False).annotate(max_points=Max(
             'submission__points')).order_by('-max_points').values_list(
                 'max_points', flat=True).filter(max_points__gt=0))
     extradata = (public_problems.filter(
         submission__user=self,
         submission__result='AC').values('id').distinct().count())
     bonus_function = settings.DMOJ_PP_BONUS_FUNCTION
     points = sum(data)
     problems = len(data)
     entries = min(len(data), len(table))
     pp = sum(map(mul, table[:entries],
                  data[:entries])) + bonus_function(extradata)
     if self.points != points or problems != self.problem_count or self.performance_points != pp:
         self.points = points
         self.problem_count = problems
         self.performance_points = pp
         self.save(update_fields=[
             'points', 'problem_count', 'performance_points'
         ])
     return points
Esempio n. 3
0
    def get_context_data(self, **kwargs):
        context = super(PostList, self).get_context_data(**kwargs)
        context['title'] = self.title or _('Page %d of Posts') % context['page_obj'].number
        context['first_page_href'] = reverse('home')
        context['page_prefix'] = reverse('blog_post_list')
        context['new_problems'] = Problem.get_public_problems() \
                                         .order_by('-date', '-id')[:settings.DMOJ_BLOG_NEW_PROBLEM_COUNT]

        context['has_clarifications'] = False
        if self.request.user.is_authenticated:
            participation = self.request.profile.current_contest
            if participation:
                clarifications = ProblemClarification.objects.filter(problem__in=participation.contest.problems.all())
                context['has_clarifications'] = clarifications.count() > 0
                context['clarifications'] = clarifications.order_by('-date')

        context['user_count'] = lazy(Profile.objects.count, int, int)
        context['problem_count'] = lazy(Problem.get_public_problems().count, int, int)
        context['submission_count'] = lazy(Submission.objects.count, int, int)
        context['language_count'] = lazy(Language.objects.count, int, int)

        now = timezone.now()

        visible_contests = Contest.get_visible_contests(self.request.user).order_by('start_time')

        context['current_contests'] = visible_contests.filter(start_time__lte=now, end_time__gt=now)
        context['future_contests'] = visible_contests.filter(start_time__gt=now)

        if self.request.user.is_authenticated:
            context['own_open_tickets'] = Ticket.tickets_list(self.request.user).filter(user=self.request.profile)
        else:
            context['own_open_tickets'] = []

        # Superusers better be staffs, not the spell-casting kind either.
        if self.request.user.is_staff:
            context['open_tickets'] = Ticket.tickets_list(self.request.user)[:10]
        else:
            context['open_tickets'] = []
        return context
Esempio n. 4
0
def hot_problems(duration, limit):
    cache_key = 'hot_problems:%d:%d' % (duration.total_seconds(), limit)
    qs = cache.get(cache_key)
    if qs is None:
        qs = Problem.get_public_problems() \
                    .filter(submission__date__gt=timezone.now() - duration, points__gt=3, points__lt=25)
        qs0 = qs.annotate(k=Count('submission__user', distinct=True)).order_by(
            '-k').values_list('k', flat=True)

        if not qs0:
            return []
        # make this an aggregate
        mx = float(qs0[0])

        qs = qs.annotate(
            unique_user_count=Count('submission__user', distinct=True))
        # fix braindamage in excluding CE
        qs = qs.annotate(submission_volume=Count(
            Case(
                When(submission__result='AC', then=1),
                When(submission__result='WA', then=1),
                When(submission__result='IR', then=1),
                When(submission__result='RTE', then=1),
                When(submission__result='TLE', then=1),
                When(submission__result='OLE', then=1),
                output_field=FloatField(),
            )))
        qs = qs.annotate(ac_volume=Count(
            Case(
                When(submission__result='AC', then=1),
                output_field=FloatField(),
            )))
        qs = qs.filter(unique_user_count__gt=max(mx / 3.0, 1))

        qs = qs.annotate(ordering=ExpressionWrapper(
            0.5 * F('points') *
            (0.4 * F('ac_volume') / F('submission_volume') +
             0.6 * F('ac_rate')) + 100 * e**(F('unique_user_count') / mx),
            output_field=FloatField(),
        )).order_by('-ordering').defer('description')[:limit]

        cache.set(cache_key, qs, 900)
    return qs
Esempio n. 5
0
    def get_context_data(self, **kwargs):
        context = super(PostList, self).get_context_data(**kwargs)
        context['title'] = self.title or _('Page %d of Posts') % context['page_obj'].number
        context['first_page_href'] = reverse('home')
        context['page_prefix'] = reverse('blog_post_list')
        context['comments'] = Comment.most_recent(self.request.user, 10)
        context['new_problems'] = Problem.get_public_problems() \
                                         .order_by('-date', 'code')[:settings.DMOJ_BLOG_NEW_PROBLEM_COUNT]
        context['page_titles'] = CacheDict(lambda page: Comment.get_page_title(page))

        context['has_clarifications'] = False
        if self.request.user.is_authenticated:
            participation = self.request.profile.current_contest
            if participation:
                clarifications = ProblemClarification.objects.filter(problem__in=participation.contest.problems.all())
                context['has_clarifications'] = clarifications.count() > 0
                context['clarifications'] = clarifications.order_by('-date')

        context['user_count'] = Profile.objects.count
        context['problem_count'] = Problem.get_public_problems().count
        context['submission_count'] = lambda: Submission.objects.aggregate(max_id=Max('id'))['max_id'] or 0
        context['language_count'] = Language.objects.count

        context['post_comment_counts'] = {
            int(page[2:]): count for page, count in
            Comment.objects
                   .filter(page__in=['b:%d' % post.id for post in context['posts']], hidden=False)
                   .values_list('page').annotate(count=Count('page')).order_by()
        }

        now = timezone.now()

        # Dashboard stuff
        if self.request.user.is_authenticated:
            user = self.request.profile
            context['recently_attempted_problems'] = (Submission.objects.filter(user=user)
                                                      .exclude(problem__in=user_completed_ids(user))
                                                      .values_list('problem__code', 'problem__name', 'problem__points')
                                                      .annotate(points=Max('points'), latest=Max('date'))
                                                      .order_by('-latest')
                                                      [:settings.DMOJ_BLOG_RECENTLY_ATTEMPTED_PROBLEMS_COUNT])

        visible_contests = Contest.get_visible_contests(self.request.user).filter(is_visible=True) \
                                  .order_by('start_time')

        context['current_contests'] = visible_contests.filter(start_time__lte=now, end_time__gt=now)
        context['future_contests'] = visible_contests.filter(start_time__gt=now)

        if self.request.user.is_authenticated:
            context['own_open_tickets'] = (
                Ticket.objects.filter(user=self.request.profile, is_open=True).order_by('-id')
                              .prefetch_related('linked_item').select_related('user__user')
            )
        else:
            context['own_open_tickets'] = []

        # Superusers better be staffs, not the spell-casting kind either.
        if self.request.user.is_staff:
            tickets = (Ticket.objects.order_by('-id').filter(is_open=True).prefetch_related('linked_item')
                             .select_related('user__user'))
            context['open_tickets'] = filter_visible_tickets(tickets, self.request.user)[:10]
        else:
            context['open_tickets'] = []

        context['problem_types'] = ProblemType.objects.all()
        context['categories'] = ProblemGroup.objects.all()
        return context
Esempio n. 6
0
 def items(self):
     return Problem.get_public_problems().values_list('code')
Esempio n. 7
0
 def items(self):
     return Problem.get_public_problems().order_by('-date', '-id')[:25]