Merge branch 'feature/edit-custom-assignments' into develop

# Conflicts:
#	server/api/graphene_wagtail.py
This commit is contained in:
Lorenz Padberg 2024-05-03 15:36:37 +02:00
commit db97bb1be8
4 changed files with 62 additions and 54 deletions

View File

@ -175,7 +175,7 @@ export default {
};
case 'assignment':
return {
component: element.id ? 'assignment' : 'assignment-form', // prevent editing of existing assignments
component: 'assignment-form',
title: 'Aufgabe & Ergebnis',
icon: 'speech-bubble-icon',
};

View File

@ -1,7 +1,7 @@
# mysite/api/graphene_wagtail.py
# Taken from https://github.com/patrick91/wagtail-ql/blob/master/backend/graphene_utils/converter.py and slightly adjusted
import logging
from wagtail.images.views.serve import generate_image_url
from graphene.types import Scalar
from graphene_django.converter import convert_django_field
from graphql_relay import to_global_id
@ -13,7 +13,6 @@ from basicknowledge.models import BasicKnowledge
from books.models import CustomDocument
from surveys.models import Survey
logger = logging.getLogger(__name__)
@ -24,21 +23,22 @@ class GenericStreamFieldType(Scalar):
raw_data = stream_value.raw_data
return list(augment_fields(raw_data))
def get_document_json(document_id):
try:
document = CustomDocument.objects.get(id=document_id)
value = {
'value': document_id,
'id': document.id,
'file_name': document.filename,
'file_extension': document.file_extension,
'url': document.url,
'title': document.title,
'display_text': document.display_text
"value": document_id,
"id": document.id,
"file_name": document.filename,
"file_extension": document.file_extension,
"url": document.url,
"title": document.title,
"display_text": document.display_text,
}
return value
except CustomDocument.DoesNotExist:
logger.error('CustomDocument {} does not exist'.format(document_id))
logger.error("CustomDocument {} does not exist".format(document_id))
return None
@ -78,33 +78,36 @@ def augment_fields(raw_data):
try:
assignment = Assignment.objects.get(pk=assignment_id)
value = {
'title': assignment.title,
'assignment': assignment.assignment,
'id': to_global_id('AssignmentNode', assignment.pk)
"title": assignment.title,
"assignment": assignment.assignment,
"solution": assignment.solution,
"id": to_global_id("AssignmentNode", assignment.pk),
}
data['value'] = value
data["value"] = value
except Assignment.DoesNotExist:
logger.error('Assignment {} does not exist'.format(assignment_id))
if _type == 'survey':
_value = data['value']
survey_id = _value['survey_id']
logger.error("Assignment {} does not exist".format(assignment_id))
if _type == "survey":
_value = data["value"]
survey_id = _value["survey_id"]
try:
survey = Survey.objects.get(pk=survey_id)
value = {
'title': survey.title,
'id': to_global_id('SurveyNode', survey.pk)
"title": survey.title,
"id": to_global_id("SurveyNode", survey.pk),
}
data['value'] = value
data["value"] = value
except Survey.DoesNotExist:
logger.error('Survey {} does not exist'.format(survey_id))
if _type == 'basic_knowledge' or _type == 'instrument':
_value = data['value']
instrument = BasicKnowledge.objects.get(pk=_value['basic_knowledge'])
_value.update({
'slug': instrument.slug,
'foreground': instrument.new_type.category.foreground
})
data['value'] = _value
logger.error("Survey {} does not exist".format(survey_id))
if _type == "basic_knowledge" or _type == "instrument":
_value = data["value"]
instrument = BasicKnowledge.objects.get(pk=_value["basic_knowledge"])
_value.update(
{
"slug": instrument.slug,
"foreground": instrument.new_type.category.foreground,
}
)
data["value"] = _value
# value = dict(d['value'])
# if 'document' in value:
@ -120,25 +123,23 @@ def augment_fields(raw_data):
# if 'image' in value:
# value['image'] = value['image'].file.url
if _type == 'content_list_item':
item_data = data['value']
data['value'] = augment_fields(item_data)
if _type == "content_list_item":
item_data = data["value"]
data["value"] = augment_fields(item_data)
if _type == 'cms_document_block':
_value = data['value']
if _type == "cms_document_block":
_value = data["value"]
value = get_document_json(_value)
if value is not None:
data['value'] = value
if _type == 'solution' or _type == 'instruction':
_value = data['value']
document_id = _value.get('document')
data["value"] = value
if _type == "solution" or _type == "instruction":
_value = data["value"]
document_id = _value.get("document")
if document_id is not None:
document = get_document_json(document_id)
if document is not None:
_value['document'] = document
data['value'] = _value
_value["document"] = document
data["value"] = _value
return raw_data

View File

@ -1,7 +1,6 @@
import graphene
from graphene import relay
from wagtail.images.models import Image
from api.graphene_wagtail import generate_image_url
class ModuleInterface(relay.Node):
pk = graphene.Int()

View File

@ -58,6 +58,18 @@ def get_content_dict(content_type, id, value):
return {"type": content_type, "id": id, "value": value}
def create_assignment(value, user, module):
assignment = Assignment.objects.create(
title=value.get("title"),
assignment=value.get("assignment"),
solution=value.get("solution"),
owner=user,
module=module,
user_created=True,
)
return assignment
def handle_content_block(
content,
context=None,
@ -110,16 +122,12 @@ def handle_content_block(
if assignment.user_created and assignment.owner == context.user:
assignment.title = value.get("title")
assignment.assignment = value.get("assignment")
assignment.solution = value.get("solution")
assignment.save()
else:
assignment = create_assignment(value, context.user, module)
else:
assignment = Assignment.objects.create(
title=value.get("title"),
assignment=value.get("assignment"),
solution=value.get("solution"),
owner=context.user,
module=module,
user_created=True,
)
assignment = create_assignment(value, context.user, module)
content_type = "assignment"
value = {"assignment_id": assignment.id}