+ def _format_prompt(self, question):
+ lines = ['<p>{}</p>'.format(question.format_prompt(newline='<br/>\n'))]
+ for multimedia in question.multimedia:
+ lines.append(self._format_multimedia(multimedia=multimedia))
+ return '\n'.join(lines)
+
+ def _format_multimedia(self, multimedia):
+ content_type = multimedia['content-type']
+ if 'path' in multimedia:
+ uid = _hashlib.sha1(
+ str(multimedia).encode('unicode-escape')
+ ).hexdigest()
+ path = self.ui.quiz.multimedia_path(multimedia)
+ self._local_media[uid] = {
+ 'content-type': content_type,
+ 'path': path,
+ }
+ url = '../media/{}'.format(uid)
+ else:
+ raise NotImplementedError(multimedia)
+ if content_type.startswith('image/'):
+ return '<p><img src="{}" /></p>'.format(url)
+ else:
+ raise NotImplementedError(content_type)
+
+ def _media(self, environ, start_response):
+ try:
+ uid, = environ['{}.url_args'.format(self.setting)]
+ except:
+ raise HandlerError(404, 'Not Found')
+ if uid not in self._local_media:
+ raise HandlerError(404, 'Not Found')
+ content_type = self._local_media[uid]['content-type']
+ with open(self._local_media[uid]['path'], 'rb') as f:
+ content = f.read()
+ return self.ok_response(
+ environ, start_response, content=content,
+ content_type=content_type)
+