示例#1
0
文件: tests.py 项目: matrach/oioioi
 def test_utils(self):
     ofactory = partial(self.factory, self.observer)
     cfactory = partial(self.factory, self.cadmin)
     ufactory = partial(self.factory, User.objects.get(username='******'))
     self.assertFalse(can_enter_contest(ufactory(self.during)))
     self.assertTrue(is_contest_admin(cfactory(self.during)))
     self.assertTrue(can_enter_contest(cfactory(self.during)))
     self.assertTrue(is_contest_observer(ofactory(self.during)))
     self.assertTrue(can_enter_contest(ofactory(self.during)))
示例#2
0
 def test_utils(self):
     ofactory = partial(self.factory, self.observer)
     cfactory = partial(self.factory, self.cadmin)
     ufactory = partial(self.factory,
                        User.objects.get(username='******'))
     self.assertFalse(can_enter_contest(ufactory(self.during)))
     self.assertTrue(is_contest_admin(cfactory(self.during)))
     self.assertTrue(can_enter_contest(cfactory(self.during)))
     self.assertTrue(is_contest_observer(ofactory(self.during)))
     self.assertTrue(can_enter_contest(ofactory(self.during)))
示例#3
0
def navbar_tip_processor(request):
    if not getattr(request, 'contest', None):
        return {}
    if not request.user.is_authenticated():
        return {}
    if not can_enter_contest(request):
        return {}

    def generator():
        is_admin = is_contest_admin(request)
        messages = visible_messages(request)
        visible_ids = messages.values_list('id', flat=True)
        if is_admin:
            messages = unanswered_questions(messages)
        else:
            messages = new_messages(request, messages)
        count = messages.count()
        if count:
            text = ungettext('%(count)d NEW MESSAGE', '%(count)d NEW MESSAGES',
                    count) % {'count': count}

            if count == 1:
                m = messages.get()
                link = reverse('message', kwargs={
                        'contest_id': request.contest.id,
                        'message_id': m.top_reference_id
                            if m.top_reference_id in visible_ids else m.id
                    })
            else:
                link = reverse('contest_messages', kwargs={'contest_id':
                    request.contest.id})
            return make_navbar_badge(link, text)
        else:
            return ''
    return {'extra_navbar_right_messages': lazy(generator, unicode)()}
示例#4
0
def submit_view(request):
    try:
        token = request.POST['token']
        current_token = SubmitServiceToken.objects.filter(token=token)
        if not current_token.exists():
            raise PermissionDenied('AUTHORIZATION_FAILED')
        request.user = current_token[0].user
        if not can_enter_contest(request):
            raise PermissionDenied('AUTHORIZATION_FAILED')
        task_name = request.POST['task']
        file_name, file_extension = os.path.splitext(
            request.FILES['file'].name)
        if task_name:
            file_name = task_name
        pi = match_problem(visible_problem_instances(request), file_name)
        if not pi:
            raise SubmitServiceException(
                'NO_SUCH_PROBLEM',
                ', '.join([
                    x.short_name for x in visible_problem_instances(request)
                ]),
            )

        lang_exts = get_allowed_languages_extensions(pi)
        if file_extension[1:] not in lang_exts:
            raise ValueError('UNSUPPORTED_EXTENSION')

        form = SubmissionForm(
            request,
            {
                'problem_instance_id': pi.id,
                'user': request.user,
                'kind': 'NORMAL'
            },
            request.FILES,
        )
        if not form.is_valid():
            raise SubmitServiceException('INVALID_SUBMISSION', form.errors)
        submission = request.contest.controller.create_submission(
            request, pi, form.cleaned_data)
        result_url = reverse(
            'submission',
            kwargs={
                'contest_id': request.contest.id,
                'submission_id': submission.id
            },
        )
        result = {'result_url': result_url, 'submission_id': submission.id}
    except SubmitServiceException as exception_info:
        result = {
            'error_code':
            exception_info.args[0],
            'error_data':
            exception_info.args[1] if len(exception_info.args) == 2 else '',
        }
    except Exception as e:
        result = {'error_code': 'UNKNOWN_ERROR', 'error_data': str(e)}
    return result
示例#5
0
def navbar_tip_processor(request):
    if not getattr(request, 'contest', None):
        return {}
    if not request.user.is_authenticated():
        return {}
    if not can_enter_contest(request):
        return {}

    def generator():
        return make_navbar_badge(**navbar_messages_generator(request))
    return {'extra_navbar_right_messages': lazy(generator, six.text_type)()}
示例#6
0
def navbar_tip_processor(request):
    if not getattr(request, 'contest', None):
        return {}
    if not request.user.is_authenticated():
        return {}
    if not can_enter_contest(request):
        return {}

    def generator():
        return make_navbar_badge(**navbar_messages_generator(request))
    return {'extra_navbar_right_messages': lazy(generator, unicode)()}
def context_args(request):
    if request.user.is_authenticated() and can_enter_contest(request) \
            and request.contest is not None:
        return request.user, query_context_args(request.user, request.contest)
    ip_addr = request.META.get('REMOTE_ADDR')
    if not ip_addr:
        raise Http404(_("No IP address detected"))
    try:
        display = BalloonsDisplay.objects.get(ip_addr=ip_addr)
    except BalloonsDisplay.DoesNotExist:
        raise Http404(
            _("No balloons display configured for this IP: %s") % (ip_addr, ))
    return display.user, query_context_args(display.user, display.contest)
示例#8
0
def context_args(request):
    if request.user.is_authenticated and can_enter_contest(request) \
            and request.contest is not None:
        return request.user, query_context_args(request.user, request.contest)
    ip_addr = request.META.get('REMOTE_ADDR')
    if not ip_addr:
        raise Http404(_("No IP address detected"))
    try:
        display = BalloonsDisplay.objects.get(ip_addr=ip_addr)
    except BalloonsDisplay.DoesNotExist:
        raise Http404(_("No balloons display configured for this IP: %s")
            % (ip_addr,))
    return display.user, query_context_args(display.user, display.contest)
示例#9
0
def submit_view(request):
    try:
        token = request.POST['token']
        current_token = SubmitServiceToken.objects.filter(token=token)
        if not current_token.exists():
            raise PermissionDenied('AUTHORIZATION_FAILED')
        request.user = current_token[0].user
        if not can_enter_contest(request):
            raise PermissionDenied('AUTHORIZATION_FAILED')
        task_name = request.POST['task']
        file_name, file_extension = \
            os.path.splitext(request.FILES['file'].name)
        if task_name:
            file_name = task_name
        pi = match_problem(visible_problem_instances(request), file_name)
        if not pi:
            raise SubmitServiceException('NO_SUCH_PROBLEM', ', '.join([
                    x.short_name for x in visible_problem_instances(request)]))

        lang_exts = sum(
            getattr(settings, 'SUBMITTABLE_EXTENSIONS', {}).values(), [])
        if file_extension[1:] not in lang_exts:
            raise ValueError('UNSUPPORTED_EXTENSION')

        form = SubmissionForm(request, {
            'problem_instance_id': pi.id,
            'user': request.user,
            'kind': 'NORMAL'
        }, request.FILES)
        if not form.is_valid():
            raise SubmitServiceException('INVALID_SUBMISSION', form.errors)
        submission = request.contest.controller \
            .create_submission(request, pi, form.cleaned_data)
        result_url = reverse('oioioi.contests.views.submission_view',
                                kwargs={'contest_id': request.contest.id,
                                'submission_id': submission.id})
        result = {'result_url': result_url, 'submission_id': submission.id}
    except SubmitServiceException as exception_info:
        result = {'error_code': exception_info.args[0],
                  'error_data': exception_info.args[1]
                  if len(exception_info.args) == 2 else ''}
    except StandardError as e:
        result = {'error_code': 'UNKNOWN_ERROR',
                  'error_data': str(e)}
    return result
示例#10
0
    def ddzone_generator():
        # show drag and drop zone only for contest with submitting some files
        if (not hasattr(request, 'contest')
                or not hasattr(request.contest, 'controller')
                or not isinstance(request.contest.controller,
                                  ProgrammingContestController)):
            return ''
        # do not show drag and drop zone when no available problems
        # (the following require controller in request)
        if (not has_any_submittable_problem(request)
                or not contest_exists(request)
                or not can_enter_contest(request)
                or getattr(request, 'hide_drag_and_drop', False)):
            return ''

        c = {'contest_id': request.contest.id}
        c.update(csrf(request))
        return render_to_string('programs/drag_and_drop.html', c)
示例#11
0
    def ddzone_generator():
        # show drag and drop zone only for contest with submitting some files
        if not hasattr(request, 'contest') or \
                not hasattr(request.contest, 'controller') or \
                not isinstance(request.contest.controller,
                               ProgrammingContestController):
            return ''
        # do not show drag and drop zone when no available problems
        # (the following require controller in request)
        if not has_any_submittable_problem(request) \
                or not contest_exists(request) \
                or not can_enter_contest(request) \
                or getattr(request, 'hide_drag_and_drop', False):
            return ''

        c = {'contest_id': request.contest.id}
        c.update(csrf(request))
        return render_to_string('programs/drag_and_drop.html', c)
示例#12
0
def navbar_tip_processor(request):
    if not getattr(request, 'contest', None):
        return {}
    if not request.user.is_authenticated():
        return {}
    if not can_enter_contest(request):
        return {}

    def generator():
        is_admin = is_contest_admin(request)
        messages = visible_messages(request)
        visible_ids = messages.values_list('id', flat=True)
        if is_admin:
            messages = unanswered_questions(messages)
        else:
            messages = new_messages(request, messages)
        count = messages.count()
        if count:
            text = ungettext('%(count)d NEW MESSAGE', '%(count)d NEW MESSAGES',
                             count) % {
                                 'count': count
                             }

            if count == 1:
                m = messages.get()
                link = reverse('message',
                               kwargs={
                                   'contest_id':
                                   request.contest.id,
                                   'message_id':
                                   m.top_reference_id if m.top_reference_id
                                   in visible_ids else m.id
                               })
            else:
                link = reverse('contest_messages',
                               kwargs={'contest_id': request.contest.id})
            return make_navbar_badge(link, text)
        else:
            return ''

    return {'extra_navbar_right_messages': lazy(generator, unicode)()}
示例#13
0
 def default_view(self, request):
     if request.contest and can_enter_contest(request):
         return reverse('contest_dashboard',
                 kwargs={'contest_id': self.contest.id})
     else:
         return super(DashboardDefaultViewMixin, self).default_view(request)
示例#14
0
 def default_view(self, request):
     if request.contest and can_enter_contest(request):
         return reverse('contest_dashboard',
                        kwargs={'contest_id': self.contest.id})
     else:
         return super(DashboardDefaultViewMixin, self).default_view(request)
示例#15
0
 def _is_problem_statement_visible(self, request, pi):
     controller = request.contest.controller
     return contest_exists(request) and \
            can_enter_contest(request) and \
            controller.can_see_problem(request, pi) and \
            controller.can_see_statement(request, pi)
示例#16
0
 def can_see_livedata(self, request):
     return can_enter_contest(request)