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
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as
8
# published by the Free Software Foundation, either version 3 of the
9
# License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18

19 1
from cmsranking.Entity import Entity, InvalidData
20

21

22 1
class Contest(Entity):
23
    """The entity representing a contest.
24

25
    It consists of the following properties:
26
    - name (unicode): the human-readable name of the contest
27
    - begin (int): the unix timestamp at which the contest begins
28
    - end (int): the unix timestamp at which the contest ends
29
    - score_precision (int): how many decimal places to show in scores
30

31
    """
32 1
    def __init__(self):
33
        """Set the properties to some default values.
34

35
        """
36 1
        Entity.__init__(self)
37 1
        self.name = None
38 1
        self.begin = None
39 1
        self.end = None
40 1
        self.score_precision = None
41

42 1
    @staticmethod
43
    def validate(data):
44
        """Validate the given dictionary.
45

46
        See if it contains a valid representation of this entity.
47

48
        """
49 1
        try:
50 1
            assert isinstance(data, dict), \
51
                "Not a dictionary"
52 1
            assert isinstance(data['name'], str), \
53
                "Field 'name' isn't a string"
54 1
            assert isinstance(data['begin'], int), \
55
                "Field 'begin' isn't an integer"
56 1
            assert isinstance(data['end'], int), \
57
                "Field 'end' isn't an integer"
58 1
            assert data['begin'] <= data['end'], \
59
                "Field 'begin' is greater than 'end'"
60 1
            assert isinstance(data['score_precision'], int), \
61
                "Field 'score_precision' isn't an integer"
62 1
            assert data['score_precision'] >= 0, \
63
                "Field 'score_precision' is negative"
64 0
        except KeyError as exc:
65 0
            raise InvalidData("Field %s is missing" % exc)
66 0
        except AssertionError as exc:
67 0
            raise InvalidData(str(exc))
68

69 1
    def set(self, data):
70 1
        self.validate(data)
71 1
        self.name = data['name']
72 1
        self.begin = data['begin']
73 1
        self.end = data['end']
74 1
        self.score_precision = data['score_precision']
75

76 1
    def get(self):
77 1
        result = self.__dict__.copy()
78 1
        del result['key']
79 1
        return result

Read our documentation on viewing source code .

Loading