Team.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 Team(Entity):
  19. """The entity representing a team.
  20. It consists of the following properties:
  21. - name (unicode): the human-readable name of the team
  22. """
  23. def __init__(self):
  24. """Set the properties to some default values.
  25. """
  26. Entity.__init__(self)
  27. self.name = None
  28. @staticmethod
  29. def validate(data):
  30. """Validate the given dictionary.
  31. See if it contains a valid representation of this entity.
  32. """
  33. try:
  34. assert isinstance(data, dict), \
  35. "Not a dictionary"
  36. assert isinstance(data['name'], str), \
  37. "Field 'name' isn't a string"
  38. except KeyError as exc:
  39. raise InvalidData("Field %s is missing" % exc)
  40. except AssertionError as exc:
  41. raise InvalidData(str(exc))
  42. def set(self, data):
  43. self.validate(data)
  44. self.name = data['name']
  45. def get(self):
  46. result = self.__dict__.copy()
  47. del result['key']
  48. return result