Submission.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Submission(Entity):
  19. """The entity representing a submission.
  20. It consists of the following properties:
  21. - user (unicode): the key of the user who submitted
  22. - task (unicode): the key of the task of the submission
  23. - time (int): the time the submission has been submitted
  24. """
  25. def __init__(self):
  26. """Set the properties to some default values.
  27. """
  28. Entity.__init__(self)
  29. self.user = None
  30. self.task = None
  31. self.time = 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['user'], str), \
  41. "Field 'user' isn't a string"
  42. assert isinstance(data['task'], str), \
  43. "Field 'task' isn't a string"
  44. assert isinstance(data['time'], int), \
  45. "Field 'time' isn't an integer (unix timestamp)"
  46. except KeyError as exc:
  47. raise InvalidData("Field %s is missing" % exc)
  48. except AssertionError as exc:
  49. raise InvalidData(str(exc))
  50. def set(self, data):
  51. self.validate(data)
  52. self.user = data['user']
  53. self.task = data['task']
  54. self.time = data['time']
  55. def get(self):
  56. result = self.__dict__.copy()
  57. del result['key']
  58. del result['score']
  59. del result['token']
  60. del result['extra']
  61. return result
  62. def consistent(self, stores):
  63. return ("task" not in stores or self.task in stores["task"]) \
  64. and ("user" not in stores or self.user in stores["user"])