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.
 
 
 

61 lines
2.1 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2017 Adam Hartz <hartz@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 re
  17. from http import HTTPStatus
  18. def handle(context):
  19. uname = context.get('cs_username', 'None')
  20. logname = 'staticaccess'
  21. fname = context['filename']
  22. user = context['cs_user_info']
  23. perms = user.get('permissions', [])
  24. rel = context['csm_tutor'].get_release_date(context)
  25. ts = context['cs_timestamp']
  26. current = context['csm_time'].from_detailed_timestamp(ts)
  27. log_entry = {
  28. k: v
  29. for (k, v) in context.items()
  30. if k in {'cs_timestamp', 'cs_path_info', 'cs_ip', 'cs_user_info'}
  31. }
  32. m = None
  33. status = HTTPStatus.OK
  34. if 'view' not in perms and 'view_all' not in perms or uname == 'None':
  35. m = 'You are not authorized to view this handout.'
  36. status = HTTPStatus.UNAUTHORIZED
  37. elif 'view' in perms and current < rel:
  38. reltime = context['csm_time'].short_timestamp(rel)
  39. m = ('This handout is not yet available. '
  40. 'It will become available at: %s') % reltime
  41. status = HTTPStatus.NOT_FOUND
  42. log_entry['success'] = m is None
  43. context['csm_cslog'].update_log(context['cs_course'], uname, logname,
  44. log_entry)
  45. if m is None:
  46. return context['csm_dispatch'].serve_static_file(context, fname, context['cs_env'])
  47. else:
  48. return ((status.value, status.phrase), {'Content-type': 'text/plain',
  49. 'Content-length': str(len(m))}, m)