Task.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. # Copyright © 2018 Stefano Maggiolo <s.maggiolo@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. from cmsranking.Entity import Entity, InvalidData
  19. class Task(Entity):
  20. """The entity representing a task.
  21. It consists of the following properties:
  22. - name (unicode): the human-readable name of the task
  23. - short_name (unicode): a shorter name for the task, usually a
  24. code-name
  25. - contest (unicode): the id of the contest the task belongs to
  26. - max_score (float): the maximum achievable score for the task
  27. - score_precision (int): how many decimal places to show in scores
  28. - data_headers ([unicode]): a list with the descriptions of the
  29. extra fields that will be provided with each submission for the
  30. task
  31. - order (int): the order of the tasks inside of the contest
  32. """
  33. def __init__(self):
  34. """Set the properties to some default values.
  35. """
  36. Entity.__init__(self)
  37. self.name = None
  38. self.short_name = None
  39. self.contest = None
  40. self.max_score = None
  41. self.extra_headers = None
  42. self.order = None
  43. self.score_mode = None
  44. @staticmethod
  45. def validate(data):
  46. """Validate the given dictionary.
  47. See if it contains a valid representation of this entity.
  48. """
  49. try:
  50. assert isinstance(data, dict), \
  51. "Not a dictionary"
  52. assert isinstance(data['name'], str), \
  53. "Field 'name' isn't a string"
  54. assert isinstance(data['short_name'], str), \
  55. "Field 'short_name' isn't a string"
  56. assert isinstance(data['contest'], str), \
  57. "Field 'contest' isn't a string"
  58. assert isinstance(data['max_score'], float), \
  59. "Field 'max_score' isn't a float"
  60. assert isinstance(data['score_precision'], int), \
  61. "Field 'score_precision' isn't an integer"
  62. assert data['score_precision'] >= 0, \
  63. "Field 'score_precision' is negative"
  64. assert isinstance(data['extra_headers'], list), \
  65. "Field 'extra_headers' isn't a list of strings"
  66. assert isinstance(data['score_mode'], str), \
  67. "Field 'score_mode' isn't a string"
  68. for i in data['extra_headers']:
  69. assert isinstance(i, str), \
  70. "Field 'extra_headers' isn't a list of strings"
  71. assert isinstance(data['order'], int), \
  72. "Field 'order' isn't an integer"
  73. except KeyError as exc:
  74. raise InvalidData("Field %s is missing" % exc)
  75. except AssertionError as exc:
  76. raise InvalidData(str(exc))
  77. def set(self, data):
  78. self.validate(data)
  79. self.name = data['name']
  80. self.short_name = data['short_name']
  81. self.contest = data['contest']
  82. self.max_score = data['max_score']
  83. self.score_precision = data['score_precision']
  84. self.extra_headers = data['extra_headers']
  85. self.order = data['order']
  86. self.score_mode = data['score_mode']
  87. def get(self):
  88. result = self.__dict__.copy()
  89. del result['key']
  90. return result
  91. def consistent(self, stores):
  92. return "contest" not in stores or self.contest in stores["contest"]