User.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2011-2013 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from cmsranking.Entity import Entity, InvalidData
  18. class User(Entity):
  19. """The entity representing a user.
  20. It consists of the following properties:
  21. - f_name (unicode): the first name of the user
  22. - l_name (unicode): the last name of the user
  23. - team (unicode): the id of the team the user belongs to
  24. """
  25. def __init__(self):
  26. """Set the properties to some default values.
  27. """
  28. Entity.__init__(self)
  29. self.f_name = None
  30. self.l_name = None
  31. self.team = None
  32. @staticmethod
  33. def validate(data):
  34. """Validate the given dictionary.
  35. See if it contains a valid representation of this entity.
  36. """
  37. try:
  38. assert isinstance(data, dict), \
  39. "Not a dictionary"
  40. assert isinstance(data['f_name'], str), \
  41. "Field 'f_name' isn't a string"
  42. assert isinstance(data['l_name'], str), \
  43. "Field 'l_name' isn't a string"
  44. assert data['team'] is None or \
  45. isinstance(data['team'], str), \
  46. "Field 'team' isn't a string (or null)"
  47. except KeyError as exc:
  48. raise InvalidData("Field %s is missing" % exc)
  49. except AssertionError as exc:
  50. raise InvalidData(str(exc))
  51. def set(self, data):
  52. self.validate(data)
  53. self.f_name = data['f_name']
  54. self.l_name = data['l_name']
  55. self.team = data['team']
  56. def get(self):
  57. result = self.__dict__.copy()
  58. del result['key']
  59. return result
  60. def consistent(self, stores):
  61. return self.team is None or "team" not in stores \
  62. or self.team in stores["team"]