1
#!/usr/bin/env python3
2

3
# Contest Management System - http://cms-dev.github.io/
4
# Copyright © 2011-2013 Luca Wehrstedt <luca.wehrstedt@gmail.com>
5
# Copyright © 2018 Stefano Maggiolo <s.maggiolo@gmail.com>
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU Affero General Public License for more details.
16
#
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20 1
from cmsranking.Entity import Entity, InvalidData
21

22

23 1
class Task(Entity):
24
    """The entity representing a task.
25

26
    It consists of the following properties:
27
    - name (unicode): the human-readable name of the task
28
    - short_name (unicode): a shorter name for the task, usually a
29
        code-name
30
    - contest (unicode): the id of the contest the task belongs to
31
    - max_score (float): the maximum achievable score for the task
32
    - score_precision (int): how many decimal places to show in scores
33
    - data_headers ([unicode]): a list with the descriptions of the
34
        extra fields that will be provided with each submission for the
35
        task
36
    - order (int): the order of the tasks inside of the contest
37

38
    """
39 1
    def __init__(self):
40
        """Set the properties to some default values.
41

42
        """
43 1
        Entity.__init__(self)
44 1
        self.name = None
45 1
        self.short_name = None
46 1
        self.contest = None
47 1
        self.max_score = None
48 1
        self.extra_headers = None
49 1
        self.order = None
50 1
        self.score_mode = None
51

52 1
    @staticmethod
53
    def validate(data):
54
        """Validate the given dictionary.
55

56
        See if it contains a valid representation of this entity.
57

58
        """
59 1
        try:
60 1
            assert isinstance(data, dict), \
61
                "Not a dictionary"
62 1
            assert isinstance(data['name'], str), \
63
                "Field 'name' isn't a string"
64 1
            assert isinstance(data['short_name'], str), \
65
                "Field 'short_name' isn't a string"
66 1
            assert isinstance(data['contest'], str), \
67
                "Field 'contest' isn't a string"
68 1
            assert isinstance(data['max_score'], float), \
69
                "Field 'max_score' isn't a float"
70 1
            assert isinstance(data['score_precision'], int), \
71
                "Field 'score_precision' isn't an integer"
72 1
            assert data['score_precision'] >= 0, \
73
                "Field 'score_precision' is negative"
74 1
            assert isinstance(data['extra_headers'], list), \
75
                "Field 'extra_headers' isn't a list of strings"
76 1
            assert isinstance(data['score_mode'], str), \
77
                "Field 'score_mode' isn't a string"
78 1
            for i in data['extra_headers']:
79 0
                assert isinstance(i, str), \
80
                    "Field 'extra_headers' isn't a list of strings"
81 1
            assert isinstance(data['order'], int), \
82
                "Field 'order' isn't an integer"
83 0
        except KeyError as exc:
84 0
            raise InvalidData("Field %s is missing" % exc)
85 0
        except AssertionError as exc:
86 0
            raise InvalidData(str(exc))
87

88 1
    def set(self, data):
89 1
        self.validate(data)
90 1
        self.name = data['name']
91 1
        self.short_name = data['short_name']
92 1
        self.contest = data['contest']
93 1
        self.max_score = data['max_score']
94 1
        self.score_precision = data['score_precision']
95 1
        self.extra_headers = data['extra_headers']
96 1
        self.order = data['order']
97 1
        self.score_mode = data['score_mode']
98

99 1
    def get(self):
100 1
        result = self.__dict__.copy()
101 1
        del result['key']
102 1
        return result
103

104 1
    def consistent(self, stores):
105 1
        return "contest" not in stores or self.contest in stores["contest"]

Read our documentation on viewing source code .

Loading