RemoveSubmissions.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2014-2015 Stefano Maggiolo <s.maggiolo@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. """Utility to remove one or more submissions.
  18. """
  19. import argparse
  20. import sys
  21. from cms import utf8_decoder
  22. from cms.db import Participation, SessionGen, Submission, Task, User, \
  23. ask_for_contest
  24. def ask_and_remove(session, submissions):
  25. ans = input("This will delete %d submissions. Are you sure? [y/N] "
  26. % len(submissions)).strip().lower()
  27. if ans in ["y", "yes"]:
  28. for submission in submissions:
  29. session.delete(submission)
  30. session.commit()
  31. print("Deleted.")
  32. else:
  33. print("Will not delete.")
  34. def remove_submissions_for_user(contest_id, username):
  35. with SessionGen() as session:
  36. user = session.query(User)\
  37. .filter(User.username == username)\
  38. .first()
  39. if user is None:
  40. print("Unable to find user.")
  41. return
  42. participation = session.query(Participation)\
  43. .filter(Participation.user_id == user.id)\
  44. .filter(Participation.contest_id == contest_id)\
  45. .first()
  46. if participation is None:
  47. print("User %s is not in the contest." % username)
  48. return
  49. submissions = session.query(Submission)\
  50. .filter(Submission.participation_id == participation.id)\
  51. .all()
  52. ask_and_remove(session, submissions)
  53. def remove_submissions_for_task(contest_id, task_name):
  54. with SessionGen() as session:
  55. task = session.query(Task)\
  56. .filter(Task.contest_id == contest_id)\
  57. .filter(Task.name == task_name).first()
  58. if task is None:
  59. print("Unable to find task.")
  60. return
  61. submissions = session.query(Submission)\
  62. .filter(Submission.task_id == task.id)\
  63. .all()
  64. ask_and_remove(session, submissions)
  65. def remove_submission(submission_id):
  66. with SessionGen() as session:
  67. submission = session.query(Submission)\
  68. .filter(Submission.id == submission_id)\
  69. .first()
  70. ask_and_remove(session, [submission])
  71. def main():
  72. """Parse arguments and launch process.
  73. """
  74. parser = argparse.ArgumentParser(
  75. description="Remove one or more submissions.")
  76. parser.add_argument("-c", "--contest-id", action="store", type=int,
  77. help="id of contest the user is in")
  78. parser.add_argument("-u", "--username", action="store", type=utf8_decoder,
  79. help="username of the user")
  80. parser.add_argument("-t", "--task_name", action="store", type=utf8_decoder,
  81. help="short name of the task")
  82. parser.add_argument("-s", "--submission_id", action="store",
  83. type=utf8_decoder, help="submission id")
  84. args = parser.parse_args()
  85. # We have a submission id, we do not need anything else.
  86. if args.submission_id is not None:
  87. if args.contest_id is not None \
  88. or args.username is not None or args.task_name is not None:
  89. print("Submission id require no other parameters.")
  90. return 1
  91. remove_submission(args.submission_id)
  92. return 0
  93. # Otherwise, it's either user or task, but not both.
  94. if args.username is not None and args.task_name is not None:
  95. print("Cannot set both task and user.")
  96. return 1
  97. elif args.username is None and args.task_name is None:
  98. print("Please set some filter.")
  99. return 1
  100. # In any case, we require a contest.
  101. if args.contest_id is None:
  102. args.contest_id = ask_for_contest()
  103. if args.username is not None:
  104. remove_submissions_for_user(contest_id=args.contest_id,
  105. username=args.username)
  106. elif args.task_name is not None:
  107. remove_submissions_for_task(contest_id=args.contest_id,
  108. task_name=args.task_name)
  109. return 0
  110. if __name__ == "__main__":
  111. sys.exit(main())