RemoveUser.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2013-2016 Stefano Maggiolo <s.maggiolo@gmail.com>
  4. # Copyright © 2016 Myungwoo Chun <mc.tamaki@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 user.
  19. """
  20. import argparse
  21. import logging
  22. import sys
  23. from cms import utf8_decoder
  24. from cms.db import SessionGen, User
  25. logger = logging.getLogger(__name__)
  26. def remove_user(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. session.delete(user)
  34. session.commit()
  35. return True
  36. def main():
  37. """Parse arguments and launch process.
  38. """
  39. parser = argparse.ArgumentParser(
  40. description="Remove a user from a contest in CMS.")
  41. parser.add_argument("username", action="store", type=utf8_decoder,
  42. help="username of the user")
  43. args = parser.parse_args()
  44. success = remove_user(username=args.username)
  45. return 0 if success is True else 1
  46. if __name__ == "__main__":
  47. sys.exit(main())