Allow user to change assignments in custom content blocks
This commit is contained in:
parent
b8290838a1
commit
9d6085c7f1
|
|
@ -175,7 +175,7 @@ export default {
|
||||||
};
|
};
|
||||||
case 'assignment':
|
case 'assignment':
|
||||||
return {
|
return {
|
||||||
component: element.id ? 'assignment' : 'assignment-form', // prevent editing of existing assignments
|
component: 'assignment-form',
|
||||||
title: 'Aufgabe & Ergebnis',
|
title: 'Aufgabe & Ergebnis',
|
||||||
icon: 'speech-bubble-icon',
|
icon: 'speech-bubble-icon',
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,16 @@
|
||||||
# Taken from https://github.com/patrick91/wagtail-ql/blob/master/backend/graphene_utils/converter.py and slightly adjusted
|
# Taken from https://github.com/patrick91/wagtail-ql/blob/master/backend/graphene_utils/converter.py and slightly adjusted
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from graphene.types import Scalar
|
|
||||||
from graphene_django.converter import convert_django_field
|
|
||||||
from graphql_relay import to_global_id
|
|
||||||
from wagtail.fields import StreamField
|
|
||||||
from wagtail.documents.models import Document
|
|
||||||
from wagtail.images.models import Image
|
|
||||||
|
|
||||||
from assignments.models import Assignment
|
from assignments.models import Assignment
|
||||||
from basicknowledge.models import BasicKnowledge
|
from basicknowledge.models import BasicKnowledge
|
||||||
from books.models import CustomDocument
|
from books.models import CustomDocument
|
||||||
|
from graphene.types import Scalar
|
||||||
|
from graphene_django.converter import convert_django_field
|
||||||
|
from graphql_relay import to_global_id
|
||||||
from surveys.models import Survey
|
from surveys.models import Survey
|
||||||
|
from wagtail.documents.models import Document
|
||||||
|
from wagtail.fields import StreamField
|
||||||
|
from wagtail.images.models import Image
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -24,69 +23,73 @@ class GenericStreamFieldType(Scalar):
|
||||||
raw_data = stream_value.raw_data
|
raw_data = stream_value.raw_data
|
||||||
return list(augment_fields(raw_data))
|
return list(augment_fields(raw_data))
|
||||||
|
|
||||||
|
|
||||||
def get_document_json(document_id):
|
def get_document_json(document_id):
|
||||||
try:
|
try:
|
||||||
document = CustomDocument.objects.get(id=document_id)
|
document = CustomDocument.objects.get(id=document_id)
|
||||||
value = {
|
value = {
|
||||||
'value': document_id,
|
"value": document_id,
|
||||||
'id': document.id,
|
"id": document.id,
|
||||||
'file_name': document.filename,
|
"file_name": document.filename,
|
||||||
'file_extension': document.file_extension,
|
"file_extension": document.file_extension,
|
||||||
'url': document.url,
|
"url": document.url,
|
||||||
'title': document.title,
|
"title": document.title,
|
||||||
'display_text': document.display_text
|
"display_text": document.display_text,
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
except CustomDocument.DoesNotExist:
|
except CustomDocument.DoesNotExist:
|
||||||
logger.error('CustomDocument {} does not exist'.format(document_id))
|
logger.error("CustomDocument {} does not exist".format(document_id))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def augment_fields(raw_data):
|
def augment_fields(raw_data):
|
||||||
for data in raw_data:
|
for data in raw_data:
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
_type = data['type']
|
_type = data["type"]
|
||||||
if _type == 'image_block':
|
if _type == "image_block":
|
||||||
_value = data['value']
|
_value = data["value"]
|
||||||
value = {
|
value = {
|
||||||
# 'value': _value,
|
# 'value': _value,
|
||||||
# 'id': d['id'],
|
# 'id': d['id'],
|
||||||
'path': Image.objects.get(id=_value).file.url
|
"path": Image.objects.get(id=_value).file.url
|
||||||
}
|
}
|
||||||
data['value'] = value
|
data["value"] = value
|
||||||
if _type == 'assignment':
|
if _type == "assignment":
|
||||||
_value = data['value']
|
_value = data["value"]
|
||||||
assignment_id = _value['assignment_id']
|
assignment_id = _value["assignment_id"]
|
||||||
try:
|
try:
|
||||||
assignment = Assignment.objects.get(pk=assignment_id)
|
assignment = Assignment.objects.get(pk=assignment_id)
|
||||||
value = {
|
value = {
|
||||||
'title': assignment.title,
|
"title": assignment.title,
|
||||||
'assignment': assignment.assignment,
|
"assignment": assignment.assignment,
|
||||||
'id': to_global_id('AssignmentNode', assignment.pk)
|
"solution": assignment.solution,
|
||||||
|
"id": to_global_id("AssignmentNode", assignment.pk),
|
||||||
}
|
}
|
||||||
data['value'] = value
|
data["value"] = value
|
||||||
except Assignment.DoesNotExist:
|
except Assignment.DoesNotExist:
|
||||||
logger.error('Assignment {} does not exist'.format(assignment_id))
|
logger.error("Assignment {} does not exist".format(assignment_id))
|
||||||
if _type == 'survey':
|
if _type == "survey":
|
||||||
_value = data['value']
|
_value = data["value"]
|
||||||
survey_id = _value['survey_id']
|
survey_id = _value["survey_id"]
|
||||||
try:
|
try:
|
||||||
survey = Survey.objects.get(pk=survey_id)
|
survey = Survey.objects.get(pk=survey_id)
|
||||||
value = {
|
value = {
|
||||||
'title': survey.title,
|
"title": survey.title,
|
||||||
'id': to_global_id('SurveyNode', survey.pk)
|
"id": to_global_id("SurveyNode", survey.pk),
|
||||||
}
|
}
|
||||||
data['value'] = value
|
data["value"] = value
|
||||||
except Survey.DoesNotExist:
|
except Survey.DoesNotExist:
|
||||||
logger.error('Survey {} does not exist'.format(survey_id))
|
logger.error("Survey {} does not exist".format(survey_id))
|
||||||
if _type == 'basic_knowledge' or _type == 'instrument':
|
if _type == "basic_knowledge" or _type == "instrument":
|
||||||
_value = data['value']
|
_value = data["value"]
|
||||||
instrument = BasicKnowledge.objects.get(pk=_value['basic_knowledge'])
|
instrument = BasicKnowledge.objects.get(pk=_value["basic_knowledge"])
|
||||||
_value.update({
|
_value.update(
|
||||||
'slug': instrument.slug,
|
{
|
||||||
'foreground': instrument.new_type.category.foreground
|
"slug": instrument.slug,
|
||||||
})
|
"foreground": instrument.new_type.category.foreground,
|
||||||
data['value'] = _value
|
}
|
||||||
|
)
|
||||||
|
data["value"] = _value
|
||||||
|
|
||||||
# value = dict(d['value'])
|
# value = dict(d['value'])
|
||||||
# if 'document' in value:
|
# if 'document' in value:
|
||||||
|
|
@ -102,25 +105,23 @@ def augment_fields(raw_data):
|
||||||
# if 'image' in value:
|
# if 'image' in value:
|
||||||
# value['image'] = value['image'].file.url
|
# value['image'] = value['image'].file.url
|
||||||
|
|
||||||
if _type == 'content_list_item':
|
if _type == "content_list_item":
|
||||||
item_data = data['value']
|
item_data = data["value"]
|
||||||
data['value'] = augment_fields(item_data)
|
data["value"] = augment_fields(item_data)
|
||||||
|
|
||||||
if _type == 'cms_document_block':
|
if _type == "cms_document_block":
|
||||||
_value = data['value']
|
_value = data["value"]
|
||||||
value = get_document_json(_value)
|
value = get_document_json(_value)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
data['value'] = value
|
data["value"] = value
|
||||||
if _type == 'solution' or _type == 'instruction':
|
if _type == "solution" or _type == "instruction":
|
||||||
_value = data['value']
|
_value = data["value"]
|
||||||
document_id = _value.get('document')
|
document_id = _value.get("document")
|
||||||
if document_id is not None:
|
if document_id is not None:
|
||||||
document = get_document_json(document_id)
|
document = get_document_json(document_id)
|
||||||
if document is not None:
|
if document is not None:
|
||||||
_value['document'] = document
|
_value["document"] = document
|
||||||
data['value'] = _value
|
data["value"] = _value
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return raw_data
|
return raw_data
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,18 @@ def get_content_dict(content_type, id, value):
|
||||||
return {"type": content_type, "id": id, "value": 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(
|
def handle_content_block(
|
||||||
content,
|
content,
|
||||||
context=None,
|
context=None,
|
||||||
|
|
@ -110,16 +122,12 @@ def handle_content_block(
|
||||||
if assignment.user_created and assignment.owner == context.user:
|
if assignment.user_created and assignment.owner == context.user:
|
||||||
assignment.title = value.get("title")
|
assignment.title = value.get("title")
|
||||||
assignment.assignment = value.get("assignment")
|
assignment.assignment = value.get("assignment")
|
||||||
|
assignment.solution = value.get("solution")
|
||||||
assignment.save()
|
assignment.save()
|
||||||
|
else:
|
||||||
|
assignment = create_assignment(value, context.user, module)
|
||||||
else:
|
else:
|
||||||
assignment = Assignment.objects.create(
|
assignment = create_assignment(value, context.user, module)
|
||||||
title=value.get("title"),
|
|
||||||
assignment=value.get("assignment"),
|
|
||||||
solution=value.get("solution"),
|
|
||||||
owner=context.user,
|
|
||||||
module=module,
|
|
||||||
user_created=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
content_type = "assignment"
|
content_type = "assignment"
|
||||||
value = {"assignment_id": assignment.id}
|
value = {"assignment_id": assignment.id}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue