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.
 
 
 

71 lines
2.4 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 base64
  17. import mimetypes
  18. tutor.qtype_inherit('smallbox')
  19. base, _ = tutor.question("smallbox")
  20. always_rerender = True
  21. defaults.update({
  22. 'csq_soln_filename': 'solution.txt',
  23. 'csq_allow_save': False,
  24. 'csq_soln_type': 'string',
  25. })
  26. def handle_submission(submissions, **info):
  27. o = {'score': None, 'msg': '', 'rerender': True}
  28. name = info['csq_name']
  29. ll = submissions.get(name, None)
  30. if ll is not None:
  31. submissions[name] = csm_tools.data_uri.DataURI(ll[1]).data
  32. o.update(base['handle_submission'](submissions, **info))
  33. return o
  34. def render_html(last_log, **info):
  35. out = '''<input type="file" id=%(name)s name="%(name)s" />'''
  36. out %= {'name': info['csq_name']}
  37. ll = last_log.get(info['csq_name'], None)
  38. if ll is not None:
  39. try:
  40. name, data = ll
  41. out += '<br/>'
  42. name = '.'.join(info['cs_path_info'] + [info['cs_username'], name])
  43. out += ('<a href="%s" '
  44. 'download="%s">Download Most '
  45. 'Recent Submission</a>') % (data, name)
  46. except:
  47. pass
  48. return out
  49. def answer_display(**info):
  50. name = info['csq_soln_filename']
  51. if info['csq_soln_type'] == 'string':
  52. data = csm_tools.data_uri.DataURI.make('text/plain', None, True,
  53. info['csq_soln'])
  54. else:
  55. data = csm_tools.data_uri.DataURI.from_file(info['csq_soln'])
  56. ext = mimetypes.guess_extension(data.mimetype) or '.txt'
  57. name = name.rsplit('.', 1) + ext
  58. return ('<a href="%s" '
  59. 'download="%s">Download Most '
  60. 'Recent Submission</a>') % (data, name)