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.
 
 
 

63 lines
1.9 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. course = cs_form.get("course", None)
  18. api_token = cs_form.get("api_token", None)
  19. messages = cs_form.get("messages", None)
  20. from_addr = cs_form.get("from_address", None)
  21. error = None
  22. if api_token is None:
  23. error = "api_token is required"
  24. if error is None:
  25. output = csm_api.get_user_information(globals(), api_token=api_token, course=course)
  26. if output["ok"]:
  27. uinfo = output["user_info"]
  28. if "email" not in uinfo["permissions"]:
  29. error = "Permission Denied"
  30. if error is None:
  31. try:
  32. messages = json.loads(messages)
  33. except:
  34. error = "error loading messages"
  35. required_fields = ("recipient", "subject", "body")
  36. if error is None:
  37. out = []
  38. for m in messages:
  39. if any(i not in m for i in required_fields):
  40. out.append("Required field missing")
  41. continue
  42. out.append(
  43. csm_mail.internal_message(
  44. globals(), course, m["recipient"], m["subject"], m["body"], from_addr
  45. )
  46. )
  47. cs_handler = "raw_response"
  48. content_type = "application/json"
  49. if error is not None:
  50. out = {"ok": False, "error": error}
  51. else:
  52. out = {"ok": True, "responses": out}
  53. response = json.dumps(out)