Exemplo n.º 1
0
    def render_params(self):
        ret = super(ContestHandler, self).render_params()

        ret["contest"] = self.contest

        if self.contest_url is not None:
            ret["contest_url"] = self.contest_url

        ret["phase"] = self.contest.phase(self.timestamp)

        ret["printing_enabled"] = (config.printer is not None)
        ret["questions_enabled"] = self.contest.allow_questions
        ret["testing_enabled"] = self.contest.allow_user_tests

        if self.current_user is not None:
            participation = self.current_user
            ret["participation"] = participation
            ret["user"] = participation.user

            res = compute_actual_phase(
                self.timestamp, self.contest.start, self.contest.stop,
                self.contest.analysis_start if self.contest.analysis_enabled
                else None,
                self.contest.analysis_stop if self.contest.analysis_enabled
                else None,
                self.contest.per_user_time, participation.starting_time,
                participation.delay_time, participation.extra_time)

            ret["actual_phase"], ret["current_phase_begin"], \
                ret["current_phase_end"], ret["valid_phase_begin"], \
                ret["valid_phase_end"] = res

            if ret["actual_phase"] == 0:
                ret["phase"] = 0

            # set the timezone used to format timestamps
            ret["timezone"] = get_timezone(participation.user, self.contest)

        # some information about token configuration
        ret["tokens_contest"] = self.contest.token_mode

        t_tokens = set(t.token_mode for t in self.contest.tasks)
        if len(t_tokens) == 1:
            ret["tokens_tasks"] = next(iter(t_tokens))
        else:
            ret["tokens_tasks"] = TOKEN_MODE_MIXED

        return ret
Exemplo n.º 2
0
    def render_params(self):
        ret = super(ContestHandler, self).render_params()

        ret["contest"] = self.contest

        if self.contest_url is not None:
            ret["contest_url"] = self.contest_url

        ret["phase"] = self.contest.phase(self.timestamp)

        ret["printing_enabled"] = (config.printer is not None)
        ret["questions_enabled"] = self.contest.allow_questions
        ret["testing_enabled"] = self.contest.allow_user_tests

        if self.current_user is not None:
            participation = self.current_user
            ret["participation"] = participation
            ret["user"] = participation.user

            res = compute_actual_phase(
                self.timestamp, self.contest.start, self.contest.stop,
                self.contest.analysis_start if self.contest.analysis_enabled
                else None, self.contest.analysis_stop
                if self.contest.analysis_enabled else None,
                self.contest.per_user_time, participation.starting_time,
                participation.delay_time, participation.extra_time)

            ret["actual_phase"], ret["current_phase_begin"], \
                ret["current_phase_end"], ret["valid_phase_begin"], \
                ret["valid_phase_end"] = res

            if ret["actual_phase"] == 0:
                ret["phase"] = 0

            # set the timezone used to format timestamps
            ret["timezone"] = get_timezone(participation.user, self.contest)

        # some information about token configuration
        ret["tokens_contest"] = self.contest.token_mode

        t_tokens = set(t.token_mode for t in self.contest.tasks)
        if len(t_tokens) == 1:
            ret["tokens_tasks"] = next(iter(t_tokens))
        else:
            ret["tokens_tasks"] = TOKEN_MODE_MIXED

        return ret
Exemplo n.º 3
0
    def render_params(self):
        ret = super(ContestHandler, self).render_params()

        ret["contest"] = self.contest

        if hasattr(self, "contest_url"):
            ret["contest_url"] = self.contest_url

        ret["phase"] = self.contest.phase(self.timestamp)

        ret["printing_enabled"] = (config.printer is not None)
        ret["questions_enabled"] = self.contest.allow_questions
        ret["testing_enabled"] = self.contest.allow_user_tests

        if self.current_user is not None:
            participation = self.current_user

            res = compute_actual_phase(
                self.timestamp, self.contest.start, self.contest.stop,
                self.contest.analysis_start if self.contest.analysis_enabled
                else None,
                self.contest.analysis_stop if self.contest.analysis_enabled
                else None,
                self.contest.per_user_time, participation.starting_time,
                participation.delay_time, participation.extra_time)

            ret["actual_phase"], ret["current_phase_begin"], \
                ret["current_phase_end"], ret["valid_phase_begin"], \
                ret["valid_phase_end"] = res

            if ret["actual_phase"] == 0:
                ret["phase"] = 0

            # set the timezone used to format timestamps
            ret["timezone"] = get_timezone(participation.user, self.contest)

        # some information about token configuration
        ret["tokens_contest"] = self._get_token_status(self.contest)

        t_tokens = sum(self._get_token_status(t) for t in self.contest.tasks)
        if t_tokens == 0:
            ret["tokens_tasks"] = 0  # all disabled
        elif t_tokens == 2 * len(self.contest.tasks):
            ret["tokens_tasks"] = 2  # all infinite
        else:
            ret["tokens_tasks"] = 1  # all finite or mixed

        # TODO Now all language names are shown in the active language.
        # It would be better to show them in the corresponding one.
        ret["lang_names"] = {}

        # Get language codes for allowed localizations
        lang_codes = self.langs.keys()
        if len(self.contest.allowed_localizations) > 0:
            lang_codes = filter_language_codes(
                lang_codes, self.contest.allowed_localizations)
        for lang_code, trans in self.langs.iteritems():
            language_name = None
            # Filter lang_codes with allowed localizations
            if lang_code not in lang_codes:
                continue
            try:
                language_name = translate_language_country_code(
                    lang_code, trans)
            except ValueError:
                language_name = translate_language_code(
                    lang_code, trans)
            ret["lang_names"][lang_code.replace("_", "-")] = language_name

        ret["cookie_lang"] = self.cookie_lang
        ret["browser_lang"] = self.browser_lang

        return ret
Exemplo n.º 4
0
    def render_params(self):
        """Return the default render params used by almost all handlers.

        return (dict): default render params

        """
        ret = {}
        ret["timestamp"] = self.timestamp
        ret["contest"] = self.contest
        ret["url_root"] = get_url_root(self.request.path)

        ret["phase"] = self.contest.phase(self.timestamp)

        ret["printing_enabled"] = (config.printer is not None)
        ret["questions_enabled"] = self.contest.allow_questions
        ret["testing_enabled"] = self.contest.allow_user_tests

        if self.current_user is not None:
            participation = self.current_user

            res = compute_actual_phase(self.timestamp, self.contest.start,
                                       self.contest.stop,
                                       self.contest.per_user_time,
                                       participation.starting_time,
                                       participation.delay_time,
                                       participation.extra_time)

            ret["actual_phase"], ret["current_phase_begin"], \
                ret["current_phase_end"], ret["valid_phase_begin"], \
                ret["valid_phase_end"] = res

            if ret["actual_phase"] == 0:
                ret["phase"] = 0

            # set the timezone used to format timestamps
            ret["timezone"] = get_timezone(participation.user, self.contest)

        # some information about token configuration
        ret["tokens_contest"] = self._get_token_status(self.contest)

        t_tokens = sum(self._get_token_status(t) for t in self.contest.tasks)
        if t_tokens == 0:
            ret["tokens_tasks"] = 0  # all disabled
        elif t_tokens == 2 * len(self.contest.tasks):
            ret["tokens_tasks"] = 2  # all infinite
        else:
            ret["tokens_tasks"] = 1  # all finite or mixed

        # TODO Now all language names are shown in the active language.
        # It would be better to show them in the corresponding one.
        ret["lang_names"] = {}

        # Get language codes for allowed localizations
        lang_codes = self.langs.keys()
        if len(self.contest.allowed_localizations) > 0:
            lang_codes = filter_language_codes(
                lang_codes, self.contest.allowed_localizations)
        for lang_code, trans in self.langs.iteritems():
            language_name = None
            # Filter lang_codes with allowed localizations
            if lang_code not in lang_codes:
                continue
            try:
                language_name = translate_language_country_code(
                    lang_code, trans)
            except ValueError:
                language_name = translate_language_code(lang_code, trans)
            ret["lang_names"][lang_code.replace("_", "-")] = language_name

        ret["cookie_lang"] = self.cookie_lang
        ret["browser_lang"] = self.browser_lang

        if self.current_user is not None:
            tree_menu = {'childs': {}, 'tasks': []}
            for t in self.contest.tasks:
                if not self.contest.restrict_level or self.current_user.user.level == t.level or self.current_user.user.level == "x" or t.level == "x":
                    node = tree_menu
                    if t.category:
                        for subcat in t.category.split('.'):
                            if subcat not in node['childs']:
                                node['childs'][subcat] = {
                                    'childs': {},
                                    'tasks': []
                                }
                            node = node['childs'][subcat]
                    node['tasks'].append(t)

            def make_list(tree, result, ancname="root"):
                for name in sorted(tree['childs']):
                    fullname = ancname + '.' + name
                    result.append({
                        'type': 'startcat',
                        'name': name,
                        'fullname': fullname,
                        'parent': ancname
                    })
                    make_list(tree['childs'][name], result, fullname)
                    result.append({'type': 'endcat'})
                for t in sorted(tree['tasks'], key=lambda t: t.name):
                    result.append({
                        'type': 'task',
                        'task': t,
                        'parent': ancname
                    })

            ret['list_tree_menu'] = []
            make_list(tree_menu, ret['list_tree_menu'])

        return ret
Exemplo n.º 5
0
def test(contest_start, contest_stop, per_user_time, starting_time, delay_time,
         extra_time, intervals):
    """Helper to test compute_actual_phase.

    It takes all the parameters accepted by compute_actual_phase (with
    the same semantic but in a different format: instead of datetimes
    and timedeltas they have to be strings in the form "HH[:MM[:SS]]")
    with the exception of timestamp, which is substituted by intervals.
    This represents the partition into intervals that the first six
    parameters induce on the time. It's a tuple with an odd number of
    elements, the ones having even index being datetimes in the format
    "HH[:MM[:SS]]" and the ones having odd index being integers, i.e.
    "labels" for the time interval delimited by the datetime preceding
    and following the label. Additionally we will also consider the
    intervals [-infty, intervals[0]] (labeled -2) and [intervals[-1],
    +infty] (labeled +2).

    This function selects a sample of datetimes inside each interval
    (more "dense" near the boundaries), calls compute_actual_phase on
    each of them and checks that the label of the interval is returned.

    contest_start (string): the contest's start.
    contest_stop (string): the contest's stop.
    per_user_time (string|None): the amount of time allocated to each
        user; contest is USACO-like if given and traditional if not.
    starting_time (string|None): when the user started his time frame.
    delay_time (string): how much the user's start is delayed.
    extra_time (string): how much extra time is given to the user at
        the end.
    intervals (tuple): see above.

    raise (Exception): if compute_actual_phase doesn't behave as
        expected, or if some arguments weren't properly formatted.

    """
    contest_start = parse_datetime(contest_start)
    contest_stop = parse_datetime(contest_stop)
    per_user_time = parse_timedelta(per_user_time)
    starting_time = parse_datetime(starting_time)
    delay_time = parse_timedelta(delay_time)
    extra_time = parse_timedelta(extra_time)

    assert len(intervals) % 2 == 1
    parsed = list()
    valid_begin = None
    valid_end = None
    parsed.append((-2, None, parse_datetime(intervals[0])))
    for i in range(1, len(intervals), 2):
        status = intervals[i]
        begin = parse_datetime(intervals[i - 1])
        end = parse_datetime(intervals[i + 1])

        parsed.append((status, begin, end))

        if status == 0:
            valid_begin, valid_end = begin, end
    parsed.append((+2, parse_datetime(intervals[-1]), None))

    for status, begin, end in parsed:
        if begin is None:
            for step in TEST_STEPS:
                res = compute_actual_phase(end - step, contest_start,
                                           contest_stop, per_user_time,
                                           starting_time, delay_time,
                                           extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        end - step, res, (status, begin, end,
                                          valid_begin, valid_end))
        elif end is None:
            for step in TEST_STEPS:
                res = compute_actual_phase(begin + step, contest_start,
                                           contest_stop, per_user_time,
                                           starting_time, delay_time,
                                           extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        begin + step, res, (status, begin, end,
                                            valid_begin, valid_end))
        else:
            for step in TEST_STEPS:
                if begin + step > end - step:
                    break
                res = compute_actual_phase(begin + step, contest_start,
                                           contest_stop, per_user_time,
                                           starting_time, delay_time,
                                           extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        begin + step, res, (status, begin, end,
                                            valid_begin, valid_end))
                res = compute_actual_phase(end - step, contest_start,
                                           contest_stop, per_user_time,
                                           starting_time, delay_time,
                                           extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        end - step, res, (status, begin, end,
                                          valid_begin, valid_end))
Exemplo n.º 6
0
def test(contest_start, contest_stop, analysis_start, analysis_end,
         per_user_time, starting_time, delay_time, extra_time, intervals):
    """Helper to test compute_actual_phase.

    It takes all the parameters accepted by compute_actual_phase (with
    the same semantic but in a different format: instead of datetimes
    and timedeltas they have to be strings in the form "HH[:MM[:SS]]")
    with the exception of timestamp, which is substituted by intervals.
    This represents the partition into intervals that the first six
    parameters induce on the time. It's a tuple with an odd number of
    elements, the ones having even index being datetimes in the format
    "HH[:MM[:SS]]" and the ones having odd index being integers, i.e.
    "labels" for the time interval delimited by the datetime preceding
    and following the label. Additionally we will also consider the
    intervals [-infty, intervals[0]] (labeled -2) and [intervals[-1],
    +infty] (labeled +4).

    This function selects a sample of datetimes inside each interval
    (more "dense" near the boundaries), calls compute_actual_phase on
    each of them and checks that the label of the interval is returned.

    contest_start (string): the contest's start.
    contest_stop (string): the contest's stop.
    analysis_start (string): the analysis mode's start.
    analysis_stop (string): the analysis mode's stop.
    per_user_time (string|None): the amount of time allocated to each
        user; contest is USACO-like if given and traditional if not.
    starting_time (string|None): when the user started their time
        frame.
    delay_time (string): how much the user's start is delayed.
    extra_time (string): how much extra time is given to the user at
        the end.
    intervals (tuple): see above.

    raise (Exception): if compute_actual_phase doesn't behave as
        expected, or if some arguments weren't properly formatted.

    """
    contest_start = parse_datetime(contest_start)
    contest_stop = parse_datetime(contest_stop)
    analysis_start = parse_datetime(analysis_start)
    analysis_end = parse_datetime(analysis_end)
    per_user_time = parse_timedelta(per_user_time)
    starting_time = parse_datetime(starting_time)
    delay_time = parse_timedelta(delay_time)
    extra_time = parse_timedelta(extra_time)

    assert len(intervals) % 2 == 1
    parsed = list()
    valid_begin = None
    valid_end = None
    parsed.append((-2, None, parse_datetime(intervals[0])))
    for i in range(1, len(intervals), 2):
        status = intervals[i]
        begin = parse_datetime(intervals[i - 1])
        end = parse_datetime(intervals[i + 1])

        parsed.append((status, begin, end))

        if status == 0:
            valid_begin, valid_end = begin, end
    parsed.append((+4, parse_datetime(intervals[-1]), None))

    for status, begin, end in parsed:
        if begin is None:
            for step in TEST_STEPS:
                res = compute_actual_phase(
                    end - step, contest_start, contest_stop,
                    analysis_start, analysis_end,
                    per_user_time, starting_time, delay_time, extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        end - step, res, (status, begin, end,
                                          valid_begin, valid_end))
        elif end is None:
            for step in TEST_STEPS:
                res = compute_actual_phase(
                    begin + step, contest_start, contest_stop,
                    analysis_start, analysis_end,
                    per_user_time, starting_time, delay_time, extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        begin + step, res, (status, begin, end,
                                            valid_begin, valid_end))
        else:
            for step in TEST_STEPS:
                if begin + step > end - step:
                    break
                res = compute_actual_phase(
                    begin + step, contest_start, contest_stop,
                    analysis_start, analysis_end,
                    per_user_time, starting_time, delay_time, extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        begin + step, res, (status, begin, end,
                                            valid_begin, valid_end))
                res = compute_actual_phase(
                    end - step, contest_start, contest_stop,
                    analysis_start, analysis_end,
                    per_user_time, starting_time, delay_time, extra_time)
                assert res == (status, begin, end, valid_begin, valid_end), \
                    "Check on %s returned %s instead of %s" % (
                        end - step, res, (status, begin, end,
                                          valid_begin, valid_end))
Exemplo n.º 7
0
    def render_params(self):
        """Return the default render params used by almost all handlers.

        return (dict): default render params

        """
        ret = {}
        ret["timestamp"] = self.timestamp
        ret["contest"] = self.contest
        ret["url_root"] = get_url_root(self.request.path)

        ret["phase"] = self.contest.phase(self.timestamp)

        ret["printing_enabled"] = (config.printer is not None)

        if self.current_user is not None:
            participation = self.current_user

            res = compute_actual_phase(
                self.timestamp, self.contest.start, self.contest.stop,
                self.contest.per_user_time, participation.starting_time,
                participation.delay_time, participation.extra_time)

            ret["actual_phase"], ret["current_phase_begin"], \
                ret["current_phase_end"], ret["valid_phase_begin"], \
                ret["valid_phase_end"] = res

            if ret["actual_phase"] == 0:
                ret["phase"] = 0

            # set the timezone used to format timestamps
            ret["timezone"] = get_timezone(participation.user, self.contest)

        # some information about token configuration
        ret["tokens_contest"] = self._get_token_status(self.contest)

        t_tokens = sum(self._get_token_status(t) for t in self.contest.tasks)
        if t_tokens == 0:
            ret["tokens_tasks"] = 0  # all disabled
        elif t_tokens == 2 * len(self.contest.tasks):
            ret["tokens_tasks"] = 2  # all infinite
        else:
            ret["tokens_tasks"] = 1  # all finite or mixed

        # TODO Now all language names are shown in the active language.
        # It would be better to show them in the corresponding one.
        ret["lang_names"] = {}
        for lang_code, trans in self.langs.iteritems():
            language_name = None
            try:
                language_name = translate_language_country_code(
                    lang_code, trans)
            except ValueError:
                language_name = translate_language_code(
                    lang_code, trans)
            ret["lang_names"][lang_code.replace("_", "-")] = language_name

        ret["cookie_lang"] = self.cookie_lang
        ret["browser_lang"] = self.browser_lang

        return ret
Exemplo n.º 8
0
    def render_params(self):
        """Return the default render params used by almost all handlers.

        return (dict): default render params

        """
        ret = {}
        ret["timestamp"] = self.timestamp
        ret["contest"] = self.contest
        ret["url_root"] = get_url_root(self.request.path)

        ret["phase"] = self.contest.phase(self.timestamp)

        ret["printing_enabled"] = (config.printer is not None)

        if self.current_user is not None:
            participation = self.current_user

            res = compute_actual_phase(
                self.timestamp, self.contest.start, self.contest.stop,
                self.contest.per_user_time, participation.starting_time,
                participation.delay_time, participation.extra_time)

            ret["actual_phase"], ret["current_phase_begin"], \
                ret["current_phase_end"], ret["valid_phase_begin"], \
                ret["valid_phase_end"] = res

            if ret["actual_phase"] == 0:
                ret["phase"] = 0

            # set the timezone used to format timestamps
            ret["timezone"] = get_timezone(participation.user, self.contest)

        # some information about token configuration
        ret["tokens_contest"] = self._get_token_status(self.contest)

        t_tokens = sum(self._get_token_status(t) for t in self.contest.tasks)
        if t_tokens == 0:
            ret["tokens_tasks"] = 0  # all disabled
        elif t_tokens == 2 * len(self.contest.tasks):
            ret["tokens_tasks"] = 2  # all infinite
        else:
            ret["tokens_tasks"] = 1  # all finite or mixed

        # TODO Now all language names are shown in the active language.
        # It would be better to show them in the corresponding one.
        ret["lang_names"] = {}
        for lang_code, trans in self.langs.iteritems():
            language_name = None
            try:
                language_name = translate_language_country_code(
                    lang_code, trans)
            except ValueError:
                language_name = translate_language_code(
                    lang_code, trans)
            ret["lang_names"][lang_code.replace("_", "-")] = language_name

        ret["cookie_lang"] = self.cookie_lang
        ret["browser_lang"] = self.browser_lang

        return ret
Exemplo n.º 9
0
    def render_params(self):
        """Return the default render params used by almost all handlers.

        return (dict): default render params

        """
        ret = {}
        ret["timestamp"] = self.timestamp
        ret["contest"] = self.contest
        ret["url_root"] = get_url_root(self.request.path)

        ret["phase"] = self.contest.phase(self.timestamp)

        ret["printing_enabled"] = (config.printer is not None)
        ret["questions_enabled"] = self.contest.allow_questions
        ret["testing_enabled"] = self.contest.allow_user_tests

        if self.current_user is not None:
            participation = self.current_user

            res = compute_actual_phase(
                self.timestamp, self.contest.start, self.contest.stop,
                self.contest.per_user_time, participation.starting_time,
                participation.delay_time, participation.extra_time)

            ret["actual_phase"], ret["current_phase_begin"], \
                ret["current_phase_end"], ret["valid_phase_begin"], \
                ret["valid_phase_end"] = res

            if ret["actual_phase"] == 0:
                ret["phase"] = 0

            # set the timezone used to format timestamps
            ret["timezone"] = get_timezone(participation.user, self.contest)

        # some information about token configuration
        ret["tokens_contest"] = self._get_token_status(self.contest)

        t_tokens = sum(self._get_token_status(t) for t in self.contest.tasks)
        if t_tokens == 0:
            ret["tokens_tasks"] = 0  # all disabled
        elif t_tokens == 2 * len(self.contest.tasks):
            ret["tokens_tasks"] = 2  # all infinite
        else:
            ret["tokens_tasks"] = 1  # all finite or mixed

        # TODO Now all language names are shown in the active language.
        # It would be better to show them in the corresponding one.
        ret["lang_names"] = {}

        # Get language codes for allowed localizations
        lang_codes = self.langs.keys()
        if len(self.contest.allowed_localizations) > 0:
            lang_codes = filter_language_codes(
                lang_codes, self.contest.allowed_localizations)
        for lang_code, trans in self.langs.iteritems():
            language_name = None
            # Filter lang_codes with allowed localizations
            if lang_code not in lang_codes:
                continue
            try:
                language_name = translate_language_country_code(
                    lang_code, trans)
            except ValueError:
                language_name = translate_language_code(
                    lang_code, trans)
            ret["lang_names"][lang_code.replace("_", "-")] = language_name

        ret["cookie_lang"] = self.cookie_lang
        ret["browser_lang"] = self.browser_lang
        
        if self.current_user is not None:
            tree_menu = {'childs':{}, 'tasks': []}
            for t in self.contest.tasks:
                if not self.contest.restrict_level or self.current_user.user.level ==  t.level or self.current_user.user.level == "x" or t.level == "x":
                    node = tree_menu
                    if t.category:
                        for subcat in t.category.split('.'):
                            if subcat not in node['childs']:
                                node['childs'][subcat] = {'childs':{}, 'tasks': []}
                            node = node['childs'][subcat]
                    node['tasks'].append(t)
            
            def make_list(tree, result, ancname="root"):
                for name in sorted(tree['childs']):
                    fullname = ancname+'.'+name
                    result.append({'type': 'startcat', 'name' : name, 'fullname' : fullname, 'parent' : ancname})
                    make_list(tree['childs'][name], result, fullname)
                    result.append({'type': 'endcat'})
                for t in sorted(tree['tasks'], key=lambda t: t.name):
                    result.append({'type': 'task', 'task' : t, 'parent' : ancname})
                
            
            ret['list_tree_menu'] = []
            make_list(tree_menu, ret['list_tree_menu'])
        
        return ret