RemoveContest.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2016 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 a contest.
  18. """
  19. import argparse
  20. import sys
  21. from cms.db import Contest, SessionGen, ask_for_contest
  22. def ask(contest):
  23. ans = input("This will delete contest `%s' (with id %s) and all related "
  24. "data, including submissions. Are you sure? [y/N] "
  25. % (contest.name, contest.id)).strip().lower()
  26. return ans in ["y", "yes"]
  27. def remove_contest(contest_id):
  28. with SessionGen() as session:
  29. contest = session.query(Contest)\
  30. .filter(Contest.id == contest_id).first()
  31. if not contest:
  32. print("No contest with id %s found." % contest_id)
  33. return False
  34. contest_name = contest.name
  35. if not ask(contest):
  36. print("Not removing contest `%s'." % contest_name)
  37. return False
  38. session.delete(contest)
  39. session.commit()
  40. print("Contest `%s' removed." % contest_name)
  41. return True
  42. def main():
  43. """Parse arguments and launch process.
  44. """
  45. parser = argparse.ArgumentParser(
  46. description="Remove a contest from CMS.")
  47. parser.add_argument("-c", "--contest-id", action="store", type=int,
  48. help="id of the contest")
  49. args = parser.parse_args()
  50. if args.contest_id is None:
  51. args.contest_id = ask_for_contest()
  52. success = remove_contest(contest_id=args.contest_id)
  53. return 0 if success is True else 1
  54. if __name__ == "__main__":
  55. sys.exit(main())