Skip to content

Instantly share code, notes, and snippets.

@cnk
Created June 10, 2016 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnk/9af0a75e3fc90497045f24b92c96e1df to your computer and use it in GitHub Desktop.
Save cnk/9af0a75e3fc90497045f24b92c96e1df to your computer and use it in GitHub Desktop.
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
in update view
serializer not valid
{'title': ['This field may not be blank.']}
class CBMultipleChoiceQuestionSerializer(serializers.CourseScopedRelativeHyperlinkedModelSerializer):
# The related objects are not really read only, but need to be marked as such so that
# the validate_<fieldname> methods do not get run. We deal with them in our 'validate' method.
question_text = CBTextElementSerializer(read_only=True)
answer_explanation = CBTextElementSerializer(read_only=True)
answers = CBMultipleChoiceAnswerSerializer(read_only=True, many=True)
correct_answer = CBMultipleChoiceAnswerSerializer(read_only=True)
hints = CBHintSerializer(many=True, required=False)
files = CBMultipleChoiceQuestionFileSerializer(many=True, read_only=True)
class Meta:
model = MultipleChoiceQuestion
fields = ('url', 'id', 'model_name', 'title', 'question_text', 'answer_explanation', 'hints', 'hint_display',
'correct_answer', 'answers', 'student_answer_url', 'block_state',
'ready_preview', 'files')
def validate(self, data):
'''Mostly this just shovels data from the request into validated_data. '''
print('in MC question validate - initial_data')
pprint(self.initial_data)
print('and passed in data is:')
pprint(data)
errors = {}
if 'question_text' not in self.initial_data or not self.initial_data['question_text']:
errors['question_text'] = 'This field is required.'
else:
data['question_text'] = self.initial_data['question_text']
# if we don't get answer_explanation, we will want to remove it, so send None to update
data['answer_explanation'] = self.initial_data.get('answer_explanation', None)
answers = []
if 'answers[]' in self.initial_data:
answers = []
for answer in self.initial_data.getlist('answers[]'):
answers.append(json.loads(answer))
data['answers'] = answers
else:
errors['answers[]'] = 'Multiple choice questions require a list of one or more answers'
# validate correct_answer_id is in the list of answers provided
answer_ids = [a['id'] for a in data['answers']]
if 'correct_answer_id' not in self.initial_data or int(self.initial_data['correct_answer_id']) not in answer_ids: # noqa
errors['correct_answer_id'] = 'Invalid correct answer reference.'
else:
data['correct_answer_id'] = int(self.initial_data['correct_answer_id'])
# now process hints
hints = []
if 'hints[]' in self.initial_data:
for hint in self.initial_data.getlist('hints[]'):
hints.append(json.loads(hint))
if 'new_hints[]' in self.initial_data:
for hint in self.initial_data.getlist('new_hints[]'):
hints.append(json.loads(hint))
if hints:
for hint in hints:
if 'hint_text' not in hint or not hint['hint_text']:
errors['hints[]'] = 'All hints must have content.'
data['hints'] = hints
if 'deleted_hints[]' in self.initial_data:
deleted_hints = []
for hint in self.initial_data.getlist('deleted_hints[]'):
deleted_hints.append(json.loads(hint))
data['deleted_hints'] = deleted_hints
if errors:
pprint(errors)
raise ValidationError(errors)
return data
@api_view(['POST', 'PUT'])
@permission_classes((IsAuthenticated, IsOnTeamForCourse))
def update(request, course_key, pk):
print('in update view')
question = get_object_or_404(MultipleChoiceQuestion, pk=pk)
serializer = CBMultipleChoiceQuestionSerializer(question, data=request.data, context={'request': request})
if serializer.is_valid():
serializer.save()
else:
print('serializer not valid')
print(serializer.errors)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
block = MaterialVersionBlock.objects.get(block_type='MultipleChoiceQuestion', block_id=question.id)
serializer = CBMaterialVersionBlockSerializer(block, context={'request': request})
return Response(serializer.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment