def validate(self, attrs): answer = attrs.get("answer") if self.instance: if (not self.instance.question.reader_study. allow_answer_modification): raise ValidationError( "This reader study does not allow answer modification.") if list(attrs.keys()) != ["answer"]: raise ValidationError("Only the answer field can be modified.") question = self.instance.question images = self.instance.images.all() creator = self.instance.creator else: question = attrs.get("question") images = attrs.get("images") creator = self.context.get("request").user Answer.validate( creator=creator, question=question, answer=answer, images=images, instance=self.instance, ) if self.instance: add_scores.apply_async( kwargs={ "instance_pk": str(self.instance.pk), "pk_set": list( map(str, images.values_list("pk", flat=True))), }) return attrs if not self.instance else {"answer": answer}
def assign_score(instance, action, reverse, model, pk_set, **_): if action != "post_add": return add_scores.apply_async(kwargs={ "instance_pk": str(instance.pk), "pk_set": list(map(str, pk_set)), })
def validate(self, attrs): answer = attrs.get("answer") last_edit_duration = attrs.get("last_edit_duration") if self.instance: if ( not self.instance.question.reader_study.allow_answer_modification ): raise ValidationError( "This reader study does not allow answer modification." ) if not set(attrs.keys()).issubset( {"answer", "last_edit_duration"} ): raise ValidationError( "Only the answer and last_edit_duration field can be modified." ) question = self.instance.question images = self.instance.images.all() display_set = self.instance.display_set creator = self.instance.creator else: question = attrs.get("question") images = attrs.get("images") display_set = attrs.get("display_set") creator = self.context.get("request").user Answer.validate( creator=creator, question=question, answer=answer, images=images, display_set=display_set, instance=self.instance, ) if self.instance: if images is not None and images.count() > 0: on_commit( lambda: add_scores.apply_async( kwargs={ "instance_pk": str(self.instance.pk), "pk_set": list( map(str, images.values_list("pk", flat=True)) ), } ) ) if display_set is not None: on_commit( lambda: add_scores_for_display_set.apply_async( kwargs={ "instance_pk": str(self.instance.pk), "ds_pk": display_set.pk, } ) ) return ( attrs if not self.instance else {"answer": answer, "last_edit_duration": last_edit_duration} )