|
1 | 1 | from rest_framework import serializers |
2 | | -from annotation.models import ProblemAnnotation, KnowledgeBaseAnnotation |
| 2 | +from annotation.models import ( |
| 3 | + AnnotationSession, |
| 4 | + ProblemAnnotation, |
| 5 | + KnowledgeBaseAnnotation, |
| 6 | +) |
3 | 7 |
|
4 | 8 |
|
5 | 9 | class KnowledgeBaseAnnotationSerializer(serializers.ModelSerializer): |
@@ -33,23 +37,48 @@ def update( |
33 | 37 |
|
34 | 38 |
|
35 | 39 | class ProblemAnnotationSerializer(serializers.ModelSerializer): |
36 | | - kb_items = KnowledgeBaseAnnotationSerializer( |
37 | | - many=True, source="session.kb_annotations" |
38 | | - ) |
39 | 40 |
|
40 | 41 | class Meta: |
41 | 42 | model = ProblemAnnotation |
42 | 43 | fields = [ |
43 | 44 | "id", |
44 | | - "entailment_label", |
45 | 45 | "created_at", |
| 46 | + "entailment_label", |
| 47 | + ] |
| 48 | + |
| 49 | + |
| 50 | +class AnnotationSerializer(serializers.Serializer): |
| 51 | + kbAnnotations = serializers.SerializerMethodField() |
| 52 | + problemAnnotations = serializers.SerializerMethodField() |
| 53 | + |
| 54 | + class Meta: |
| 55 | + fields = [ |
| 56 | + "kbAnnotations", |
| 57 | + "problemAnnotations", |
46 | 58 | ] |
47 | 59 |
|
48 | | - def get_kb_items(self, problem_annotation: ProblemAnnotation): |
49 | | - """Get the KnowledgeBaseAnnotation items related to this ProblemAnnotation.""" |
| 60 | + def get_kbAnnotations(self, obj): |
| 61 | + problem, last_session = self._get_problem_and_last_session() |
| 62 | + if not problem or not last_session: |
| 63 | + return [] |
50 | 64 | kb_annotations = KnowledgeBaseAnnotation.objects.filter( |
51 | | - session=problem_annotation.session |
| 65 | + session=last_session, problem=problem |
| 66 | + ) |
| 67 | + return KnowledgeBaseAnnotationSerializer(kb_annotations, many=True).data |
| 68 | + |
| 69 | + def get_problemAnnotations(self, obj): |
| 70 | + problem, last_session = self._get_problem_and_last_session() |
| 71 | + problem_annotations = ProblemAnnotation.objects.filter( |
| 72 | + session=last_session, problem=problem |
| 73 | + ) |
| 74 | + return ProblemAnnotationSerializer(problem_annotations, many=True).data |
| 75 | + |
| 76 | + def _get_problem_and_last_session(self): |
| 77 | + problem = self.context.get("problem", None) |
| 78 | + user = self.context.get("user", None) |
| 79 | + if not problem or not user or user.is_authenticated is False: |
| 80 | + return None, None |
| 81 | + last_session = ( |
| 82 | + AnnotationSession.objects.filter(user=user).order_by("-created_at").first() |
52 | 83 | ) |
53 | | - return KnowledgeBaseAnnotationSerializer( |
54 | | - kb_annotations, many=True |
55 | | - ).data |
| 84 | + return problem, last_session |
0 commit comments