Skip to content

Commit

Permalink
feat: allow viewing attachments in studio
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturGaspar committed Oct 14, 2024
1 parent df7817a commit cd408c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 19 additions & 0 deletions ai_eval/shortanswer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from django.utils.translation import gettext_noop as _
from web_fragments.fragment import Fragment
from webob import Response
from xblock.core import XBlock
from xblock.exceptions import JsonHandlerError
from xblock.fields import Dict, Integer, String, Scope
Expand Down Expand Up @@ -175,6 +176,24 @@ def submit_studio_edits(self, data, suffix=''):
data["values"]["attachments"][key] = self.attachments[key]
return super().submit_studio_edits.__wrapped__(self, data, suffix)

@XBlock.handler
def view_attachment(self, request, suffix=''):
# TODO: restrict to studio
key = request.GET['key']
try:
data = self.attachments[key]
except KeyError:
# TODO: 404
raise
return Response(
body=data.encode(),
headerlist=[
("Content-Type", "application/octet-stream"),
# TODO: escape and encode filename
("Content-Disposition", f'attachment; filename="{key}"'),
]
)

@staticmethod
def workbench_scenarios():
"""A canned scenario for display in the workbench."""
Expand Down
15 changes: 14 additions & 1 deletion ai_eval/static/js/src/shortanswer_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ function ShortAnswerAIEvalXBlock(runtime, element) {

StudioEditableXBlockMixin(runtime, element);

var viewAttachmentUrl = runtime.handlerUrl(element, "view_attachment");

var $input = $('#xb-field-edit-attachments');

var buildFileInput = function() {
Expand All @@ -13,7 +15,18 @@ function ShortAnswerAIEvalXBlock(runtime, element) {
var files = JSON.parse($input.val());
for (var filename of Object.keys(files)) {
var $fileItem = $('<li class="list-settings-item"/>');
$fileItem.append(filename);
var $fileLink;
if (files[filename] === null) {
/* File that already exists. */
$fileLink = $('<a/>');
$fileLink.attr("target", "_blank");
var fileLinkQuery = new URLSearchParams({key: filename}).toString();
$fileLink.attr('href', `${viewAttachmentUrl}?${fileLinkQuery}`);
} else {
$fileLink = $('<span/>');
}
$fileLink.append(filename);
$fileItem.append($fileLink);
var $deleteButton = $('<button class="action" type="button"/>');
$deleteButton.append($('<i class="icon fa fa-trash"/>'));
var $deleteButtonText = $('<span class="sr"/>');
Expand Down

0 comments on commit cd408c0

Please sign in to comment.