update_15.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2014 Luca Versari <veluca93@gmail.com>
  4. # Copyright © 2016 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. """A class to update a dump created by CMS.
  19. Used by DumpImporter and DumpUpdater.
  20. This converts the dump to the new schema introduced to support user
  21. and contest separation.
  22. """
  23. def split_dict(src, *keys):
  24. ret = dict()
  25. for k in list(src.keys()):
  26. v = src[k]
  27. if k in keys:
  28. ret[k] = v
  29. del src[k]
  30. return ret
  31. class Updater:
  32. def __init__(self, data):
  33. assert data["_version"] == 14
  34. self.objs = data
  35. self.next_id = len(data)
  36. def get_id(self):
  37. while str(self.next_id) in self.objs:
  38. self.next_id += 1
  39. return str(self.next_id)
  40. def run(self):
  41. for k in list(self.objs.keys()):
  42. if k.startswith("_"):
  43. continue
  44. v = self.objs[k]
  45. if v["_class"] == "User":
  46. self.split_user(k, v)
  47. elif v["_class"] in ["Message", "PrintJob", "Question",
  48. "Submission", "UserTest"]:
  49. v["participation"] = v["user"]
  50. del v["user"]
  51. return self.objs
  52. def split_user(self, user_id, user_data):
  53. # Since all the other objects will now refer to the
  54. # participation, we use the same id for the participation, and
  55. # create a new one for the user.
  56. new_user_id = self.get_id()
  57. new_participation_id = user_id
  58. participation_data = split_dict(
  59. user_data,
  60. "contest", "delay_time",
  61. "extra_time", "ip",
  62. "messages", "usertests",
  63. "questions", "starting_time",
  64. "submissions", "hidden")
  65. self.objs[new_participation_id] = participation_data
  66. assert user_data["_class"] == "User"
  67. participation_data["_class"] = "Participation"
  68. participation_data["password"] = None
  69. user_data["participations"] = [new_participation_id]
  70. participation_data["user"] = new_user_id
  71. self.objs[new_user_id] = user_data
  72. # FIXME: information about primary_statements /
  73. # preferrend_languages are silently dropped
  74. # FIXME: deduplicate repeated names in contests, users and
  75. # tasks