CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
2.5 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import json
  17. api_token = cs_form.get("api_token", None)
  18. path = cs_form.get("path", None)
  19. section = cs_form.get("section", None)
  20. error = None
  21. if api_token is None:
  22. error = "api_token is required"
  23. try:
  24. path = opath = json.loads(path)
  25. except:
  26. error = "invalid path: %s" % path
  27. if error is None:
  28. output = csm_api.get_user_information(
  29. globals(), api_token=api_token, course=path[0]
  30. )
  31. if output["ok"]:
  32. uinfo = output["user_info"]
  33. if "groups" not in uinfo["permissions"] and "admin" not in uinfo["permissions"]:
  34. error = "Permission Denied"
  35. if error is not None:
  36. output = {"ok": False, "error": error}
  37. else:
  38. ctx = csm_loader.generate_context(opath)
  39. groups = csm_groups.list_groups(ctx, path)
  40. all_students = {
  41. i: csm_user.read_user_file(globals(), path[0], i, {})
  42. for i in csm_user.list_all_users(globals(), path[0])
  43. }
  44. if section is not None:
  45. groups = groups.get(section, {})
  46. all_partnered = sum(groups.values(), [])
  47. unpartnered = [
  48. i
  49. for i in all_students
  50. if i not in all_partnered
  51. and str(all_students[i].get("section", "default")) == section
  52. ]
  53. groups["_unpartnered"] = unpartnered
  54. else:
  55. for section in groups:
  56. secgrp = groups[section]
  57. all_partnered = sum(secgrp.values(), [])
  58. unpartnered = [
  59. i
  60. for i in all_students
  61. if i not in all_partnered
  62. and str(all_students[i].get("section", "default")) == section
  63. ]
  64. secgrp["_unpartnered"] = unpartnered
  65. output = {"ok": True, "groups": groups}
  66. cs_handler = "raw_response"
  67. content_type = "application/json"
  68. response = json.dumps(output)