RemoveParticipation.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2016 Myungwoo Chun <mc.tamaki@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. """Utility to remove a participation.
  19. """
  20. import argparse
  21. import logging
  22. import sys
  23. from cms import utf8_decoder
  24. from cms.db import SessionGen, Contest, User, Participation, ask_for_contest
  25. logger = logging.getLogger(__name__)
  26. def remove_participation(contest_id, username):
  27. with SessionGen() as session:
  28. user = session.query(User)\
  29. .filter(User.username == username).first()
  30. if user is None:
  31. logger.error("User %s does not exist.", username)
  32. return False
  33. contest = session.query(Contest)\
  34. .filter(Contest.id == contest_id).first()
  35. if contest is None:
  36. logger.error("Contest id %d does not exist.", contest_id)
  37. return False
  38. participation = session.query(Participation)\
  39. .filter(Participation.contest_id == contest_id)\
  40. .filter(Participation.user == user).first()
  41. if participation is None:
  42. logger.error("Participation of %s in contest %d does not exists.",
  43. username, contest_id)
  44. return False
  45. session.delete(participation)
  46. session.commit()
  47. return True
  48. def main():
  49. """Parse arguments and launch process.
  50. """
  51. parser = argparse.ArgumentParser(
  52. description="Remove a participation from a contest in CMS.")
  53. parser.add_argument("username", action="store", type=utf8_decoder,
  54. help="username of the user")
  55. parser.add_argument("-c", "--contest-id", action="store", type=int,
  56. help="id of contest the user is in")
  57. args = parser.parse_args()
  58. if args.contest_id is None:
  59. args.contest_id = ask_for_contest()
  60. success = remove_participation(contest_id=args.contest_id,
  61. username=args.username)
  62. return 0 if success is True else 1
  63. if __name__ == "__main__":
  64. sys.exit(main())