AddTeam.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. """This script creates a new team in the database.
  18. """
  19. # We enable monkey patching to make many libraries gevent-friendly
  20. # (for instance, urllib3, used by requests)
  21. import gevent.monkey
  22. gevent.monkey.patch_all() # noqa
  23. import argparse
  24. import logging
  25. import sys
  26. from sqlalchemy.exc import IntegrityError
  27. from cms import utf8_decoder
  28. from cms.db import SessionGen, Team
  29. logger = logging.getLogger(__name__)
  30. def add_team(code, name):
  31. logger.info("Creating the team in the database.")
  32. team = Team(code=code, name=name)
  33. try:
  34. with SessionGen() as session:
  35. session.add(team)
  36. session.commit()
  37. except IntegrityError:
  38. logger.error("A team with the given code already exists.")
  39. return False
  40. logger.info("Team added.")
  41. return True
  42. def main():
  43. """Parse arguments and launch process.
  44. """
  45. parser = argparse.ArgumentParser(description="Add a team to CMS.")
  46. parser.add_argument("code", action="store", type=utf8_decoder,
  47. help="code of the team, e.g. country code")
  48. parser.add_argument("name", action="store", type=utf8_decoder,
  49. help="human readable name of the team")
  50. args = parser.parse_args()
  51. success = add_team(args.code, args.name)
  52. return 0 if success is True else 1
  53. if __name__ == "__main__":
  54. sys.exit(main())