Fix spellcheck API call to use the updated data structure

Resolves MS-968 #complete
This commit is contained in:
Ramon Wenger 2024-05-15 09:30:53 +02:00
parent 6ad39bcdb2
commit ab842971fb
1 changed files with 26 additions and 13 deletions

View File

@ -1,8 +1,6 @@
import json
import graphene
from django.conf import settings
from graphene import relay
# class SpellCheckPartNode(graphene.ObjectType):
# sentence = graphene.String()
@ -12,11 +10,13 @@ from graphene import relay
# corrected = graphene.String()
from api.utils import get_object
from assignments.models import Assignment
from django.conf import settings
from graphene import relay
from spellcheck.client import TaskbaseClient
def sentence_offset_to_snake_case(result):
result['sentence_offset'] = result['sentenceOffset']
result["sentence_offset"] = result["sentenceOffset"]
return result
@ -42,27 +42,40 @@ class SpellCheck(relay.ClientIDMutation):
@classmethod
def mutate_and_get_payload(cls, root, info, **kwargs):
user = info.context.user
text = kwargs.get('text')
assignment_id = kwargs.get('assignment')
text = kwargs.get("text")
assignment_id = kwargs.get("assignment")
assignment = get_object(Assignment, assignment_id)
client = TaskbaseClient(settings.TASKBASE_USER, settings.TASKBASE_PASSWORD, settings.TASKBASE_BASEURL)
client = TaskbaseClient(
settings.TASKBASE_USER,
settings.TASKBASE_PASSWORD,
settings.TASKBASE_BASEURL,
)
if assignment.taskbase_id is None or assignment.taskbase_id == '':
if assignment.taskbase_id is None or assignment.taskbase_id == "":
# we need to use another user (with more privileges) here, because why not
superclient = TaskbaseClient(settings.TASKBASE_SUPERUSER, settings.TASKBASE_SUPERPASSWORD,
settings.TASKBASE_BASEURL)
superclient = TaskbaseClient(
settings.TASKBASE_SUPERUSER,
settings.TASKBASE_SUPERPASSWORD,
settings.TASKBASE_BASEURL,
)
superclient.register_assignment(assignment)
data = json.loads(client.spellcheck(assignment.taskbase_id, text))
# it seems that there is new nesting inside the data, we're interested in the gradingFeedback
feedback = data["gradingFeedback"]
# the property is called sentenceOffset, but graphene expects the property to be called sentence_offset.
# we convert it manually here. fixme: is there a better way to declare this in the SpellCheckStepNode?
transformed_results = list(map(lambda x: x['part'], data['steps']))
return cls(correct=data['correct'] == 'CORRECT', results=[
sentence_offset_to_snake_case(result) for result in transformed_results
])
transformed_results = list(map(lambda x: x["part"], feedback["steps"]))
return cls(
correct=feedback["correct"] == "CORRECT",
results=[
sentence_offset_to_snake_case(result) for result in transformed_results
],
)
class SpellCheckMutations: