Contest.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 Contest(Entity):
  19. """The entity representing a contest.
  20. It consists of the following properties:
  21. - name (unicode): the human-readable name of the contest
  22. - begin (int): the unix timestamp at which the contest begins
  23. - end (int): the unix timestamp at which the contest ends
  24. - score_precision (int): how many decimal places to show in scores
  25. """
  26. def __init__(self):
  27. """Set the properties to some default values.
  28. """
  29. Entity.__init__(self)
  30. self.name = None
  31. self.begin = None
  32. self.end = None
  33. self.score_precision = None
  34. @staticmethod
  35. def validate(data):
  36. """Validate the given dictionary.
  37. See if it contains a valid representation of this entity.
  38. """
  39. try:
  40. assert isinstance(data, dict), \
  41. "Not a dictionary"
  42. assert isinstance(data['name'], str), \
  43. "Field 'name' isn't a string"
  44. assert isinstance(data['begin'], int), \
  45. "Field 'begin' isn't an integer"
  46. assert isinstance(data['end'], int), \
  47. "Field 'end' isn't an integer"
  48. assert data['begin'] <= data['end'], \
  49. "Field 'begin' is greater than 'end'"
  50. assert isinstance(data['score_precision'], int), \
  51. "Field 'score_precision' isn't an integer"
  52. assert data['score_precision'] >= 0, \
  53. "Field 'score_precision' is negative"
  54. except KeyError as exc:
  55. raise InvalidData("Field %s is missing" % exc)
  56. except AssertionError as exc:
  57. raise InvalidData(str(exc))
  58. def set(self, data):
  59. self.validate(data)
  60. self.name = data['name']
  61. self.begin = data['begin']
  62. self.end = data['end']
  63. self.score_precision = data['score_precision']
  64. def get(self):
  65. result = self.__dict__.copy()
  66. del result['key']
  67. return result