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 Submission(Entity):
23
    """The entity representing a submission.
24

25
    It consists of the following properties:
26
    - user (unicode): the key of the user who submitted
27
    - task (unicode): the key of the task of the submission
28
    - time (int): the time the submission has been submitted
29

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

34
        """
35 1
        Entity.__init__(self)
36 1
        self.user = None
37 1
        self.task = None
38 1
        self.time = None
39

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

44
        See if it contains a valid representation of this entity.
45

46
        """
47 1
        try:
48 1
            assert isinstance(data, dict), \
49
                "Not a dictionary"
50 1
            assert isinstance(data['user'], str), \
51
                "Field 'user' isn't a string"
52 1
            assert isinstance(data['task'], str), \
53
                "Field 'task' isn't a string"
54 1
            assert isinstance(data['time'], int), \
55
                "Field 'time' isn't an integer (unix timestamp)"
56 0
        except KeyError as exc:
57 0
            raise InvalidData("Field %s is missing" % exc)
58 0
        except AssertionError as exc:
59 0
            raise InvalidData(str(exc))
60

61 1
    def set(self, data):
62 1
        self.validate(data)
63 1
        self.user = data['user']
64 1
        self.task = data['task']
65 1
        self.time = data['time']
66

67 1
    def get(self):
68 1
        result = self.__dict__.copy()
69 1
        del result['key']
70 1
        del result['score']
71 1
        del result['token']
72 1
        del result['extra']
73 1
        return result
74

75 1
    def consistent(self, stores):
76 1
        return ("task" not in stores or self.task in stores["task"]) \
77
               and ("user" not in stores or self.user in stores["user"])

Read our documentation on viewing source code .

Loading