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 User(Entity):
|
23
|
|
"""The entity representing a user.
|
24
|
|
|
25
|
|
It consists of the following properties:
|
26
|
|
- f_name (unicode): the first name of the user
|
27
|
|
- l_name (unicode): the last name of the user
|
28
|
|
- team (unicode): the id of the team the user belongs to
|
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.f_name = None
|
37
|
1
|
self.l_name = None
|
38
|
1
|
self.team = 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['f_name'], str), \
|
51
|
|
"Field 'f_name' isn't a string"
|
52
|
1
|
assert isinstance(data['l_name'], str), \
|
53
|
|
"Field 'l_name' isn't a string"
|
54
|
1
|
assert data['team'] is None or \
|
55
|
|
isinstance(data['team'], str), \
|
56
|
|
"Field 'team' isn't a string (or null)"
|
57
|
0
|
except KeyError as exc:
|
58
|
0
|
raise InvalidData("Field %s is missing" % exc)
|
59
|
0
|
except AssertionError as exc:
|
60
|
0
|
raise InvalidData(str(exc))
|
61
|
|
|
62
|
1
|
def set(self, data):
|
63
|
1
|
self.validate(data)
|
64
|
1
|
self.f_name = data['f_name']
|
65
|
1
|
self.l_name = data['l_name']
|
66
|
1
|
self.team = data['team']
|
67
|
|
|
68
|
1
|
def get(self):
|
69
|
1
|
result = self.__dict__.copy()
|
70
|
1
|
del result['key']
|
71
|
1
|
return result
|
72
|
|
|
73
|
1
|
def consistent(self, stores):
|
74
|
1
|
return self.team is None or "team" not in stores \
|
75
|
|
or self.team in stores["team"]
|