diff --git a/server/core/settings.py b/server/core/settings.py index 245f60f3..6d20ef89 100644 --- a/server/core/settings.py +++ b/server/core/settings.py @@ -363,4 +363,6 @@ DEFAULT_FROM_EMAIL = 'myskillbox ' TASKBASE_USER = os.environ.get("TASKBASE_USER") TASKBASE_PASSWORD = os.environ.get("TASKBASE_PASSWORD") +TASKBASE_SUPERUSER = os.environ.get("TASKBASE_SUPERUSER") +TASKBASE_SUPERPASSWORD = os.environ.get("TASKBASE_SUPERPASSWORD") TASKBASE_BASEURL = os.environ.get("TASKBASE_BASEURL") diff --git a/server/spellcheck/client.py b/server/spellcheck/client.py index 3479e70e..563a5ee7 100644 --- a/server/spellcheck/client.py +++ b/server/spellcheck/client.py @@ -1,6 +1,8 @@ +import json import re import requests + # from spellcheck.client import TaskbaseClient # client = TaskbaseClient('info@iterativ.ch', 'myverysafepassword1234', 'https://dev-iterativ.taskbase.com') # client.spellcheck('aOciP9H7tNu7pLsR4ohllk', 'Dies ist ein Sats mit filen Felern') @@ -30,6 +32,37 @@ class TaskbaseClient: data = response.json() self.token = data['accessToken'] + def register_assignment(self, assignment): + if self.token is None: + self.login() + + headers = {'Authorization': 'Bearer {}'.format(self.token), 'Content-Type': 'application/json'} + payload = {'type': 'SPELL_CHECK'} + # we first need to register the assignment via API, so we get an ID + response = requests.post('{}/api/task'.format(self.base_url), json=payload, headers=headers) + if response.status_code == 200: + data = response.json() + assignment.taskbase_id = data['id'] + assignment.save() + + payload = { + "id": assignment.taskbase_id, + "type": "SPELL_CHECK", + "documentType": "SPELL_CHECK", + "title": assignment.title, + "description": assignment.assignment, + "solutionSteps": [assignment.solution] if assignment.solution is not None else [] + } + + # we can then update the task via API with the description, title and sample solution + response = requests.post('{}/api/task/{}'.format(self.base_url, assignment.taskbase_id), json=payload, + headers=headers) + + if response.status_code != 200: + raise Exception('Something went wrong') + else: # todo: define what to do here + raise Exception('Something went wrong') + def spellcheck(self, task, text): if self.token is None: self.login() @@ -47,5 +80,5 @@ class TaskbaseClient: response = requests.post('{}/api/grade'.format(self.base_url), json=payload, headers=headers) if response.status_code == 200: return response.content - else: + else: # todo: define what to do here raise Exception('Something went wrong') diff --git a/server/spellcheck/mutations.py b/server/spellcheck/mutations.py index 25281944..62fe7866 100644 --- a/server/spellcheck/mutations.py +++ b/server/spellcheck/mutations.py @@ -10,6 +10,8 @@ from graphene import relay # length = graphene.Int() # affected = graphene.String() # corrected = graphene.String() +from api.utils import get_object +from assignments.models import Assignment from spellcheck.client import TaskbaseClient @@ -22,24 +24,6 @@ class SpellCheckStepNode(graphene.ObjectType): affected = graphene.String() corrected = graphene.String() - # def resolve_sentence(self, *args, **kwargs): - # print(args) - # print(kwargs) - # print(self) - # return self.part.sentence - - # def resolve_offset(self): - # return self.part['offset'] - # - # def resolve_length(self): - # return self.part['length'] - # - # def resolve_affected(self): - # return self.part['affected'] - # - # def resolve_corrected(self): - # return self.part['corrected'] - class SpellCheck(relay.ClientIDMutation): class Input: @@ -53,68 +37,21 @@ class SpellCheck(relay.ClientIDMutation): def mutate_and_get_payload(cls, root, info, **kwargs): user = info.context.user text = kwargs.get('text') - assignment = kwargs.get('assignment') + assignment_id = kwargs.get('assignment') + + assignment = get_object(Assignment, assignment_id) client = TaskbaseClient(settings.TASKBASE_USER, settings.TASKBASE_PASSWORD, settings.TASKBASE_BASEURL) - data = json.loads(client.spellcheck('aOciP9H7tNu7pLsR4ohllk', text)) + 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.register_assignment(assignment) + + data = json.loads(client.spellcheck(assignment.taskbase_id, text)) - # data = { - # "correct": "WRONG", - # "solution": "", - # "steps": [ - # { - # "id": 755, - # "part": { - # "sentence": "Das ist ein text mit Felern", - # "offset": 12, - # "length": 4, - # "affected": "text", - # "corrected": "Text", - # "mistakeType": "SPELLING", - # "mistakeSubtype": "CAPITALIZATION", - # "partType": "com.taskbase.lap.server.services.step.tokenizer.SpellCheckPart" - # }, - # "partHash": "802479c1bf90c4ad0f998d550d34c18d502c948b3d72ae2d571808fb24823055", - # "count": 2 - # }, - # { - # "id": 759, - # "part": { - # "sentence": "Das ist ein text mit Felern", - # "offset": 21, - # "length": 6, - # "affected": "Felern", - # "corrected": "Feiern", - # "mistakeType": "SPELLING", - # "mistakeSubtype": "TYPO", - # "partType": "com.taskbase.lap.server.services.step.tokenizer.SpellCheckPart" - # }, - # "partHash": "b6128347caec0cd0d81afd00f03e26bd71785ff12f35e892796d584adfbb18bf", - # "count": 2 - # } - # ] - # } return cls(correct=data['correct'] == 'CORRECT', results=map(lambda x: x['part'], data['steps'])) - # user = info.context.user - # module_id = kwargs.get('module') - # bookmarked = kwargs.get('bookmarked') - # - # module = get_object(Module, module_id) - # - # if bookmarked: - # ModuleBookmark.objects.create( - # module=module, - # user=user - # ) - # else: - # ModuleBookmark.objects.get( - # module=module, - # user=user - # ).delete() - # - # return cls(success=True) class SpellCheckMutations: