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 Subchange(Entity):
23
    """The entity representing a change in the status of a submission.
24

25
    It consists of the following properties:
26
    - submission (unicode): the key of the affected submission
27
    - time (int): the time the change takes effect
28
    - score (float): optional, the new score
29
    - token (bool): optional, the new token status
30
    - extra ([unicode]): optional, the new details
31

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

36
        """
37 1
        Entity.__init__(self)
38 1
        self.submission = None
39 1
        self.time = None
40 1
        self.score = None
41 1
        self.token = None
42 1
        self.extra = None
43

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

48
        See if it contains a valid representation of this entity.
49

50
        """
51 1
        try:
52 1
            assert isinstance(data, dict), \
53
                "Not a dictionary"
54 1
            assert isinstance(data['submission'], str), \
55
                "Field 'submission' isn't a string"
56 1
            assert isinstance(data['time'], int), \
57
                "Field 'time' isn't an integer (unix timestamp)"
58 1
            if 'score' in data:
59 1
                assert isinstance(data['score'], float), \
60
                    "Field 'score' isn't a float"
61 1
            if 'token' in data:
62 0
                assert isinstance(data['token'], bool), \
63
                    "Field 'token' isn't a boolean"
64 1
            if 'extra' in data:
65 1
                assert isinstance(data['extra'], list), \
66
                    "Field 'extra' isn't a list of strings"
67 1
                for i in data['extra']:
68 0
                    assert isinstance(i, str), \
69
                        "Field 'extra' isn't a list of strings"
70 0
        except KeyError as exc:
71 0
            raise InvalidData("Field %s is missing" % exc)
72 0
        except AssertionError as exc:
73 0
            raise InvalidData(str(exc))
74

75 1
    def set(self, data):
76 1
        self.validate(data)
77 1
        self.submission = data['submission']
78 1
        self.time = data['time']
79 1
        self.score = (data['score'] if 'score' in data else None)
80 1
        self.token = (data['token'] if 'token' in data else None)
81 1
        self.extra = (data['extra'] if 'extra' in data else None)
82

83 1
    def get(self):
84 1
        result = self.__dict__.copy()
85 1
        del result['key']
86 1
        for field in ['score', 'token', 'extra']:
87 1
            if result[field] is None:
88 1
                del result[field]
89 1
        return result
90

91 1
    def consistent(self, stores):
92 1
        return "submission" not in stores \
93
               or self.submission in stores["submission"]

Read our documentation on viewing source code .

Loading