Skip to content

Commit 3ebbdf1

Browse files
Remove AnnotationSerializer
1 parent 90e71bb commit 3ebbdf1

6 files changed

Lines changed: 34 additions & 76 deletions

File tree

backend/annotation/serializers.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -142,45 +142,6 @@ def get_removable(self, annotation: LabelAnnotation) -> bool:
142142

143143
return False
144144

145-
146-
class AnnotationSerializer(serializers.Serializer):
147-
kbAnnotations = serializers.SerializerMethodField()
148-
labelAnnotations = serializers.SerializerMethodField()
149-
150-
class Meta:
151-
fields = ["kbAnnotations", "labelAnnotations"]
152-
153-
def get_kbAnnotations(self, obj):
154-
problem, last_session = self._get_problem_and_last_session()
155-
if not problem or not last_session:
156-
return []
157-
kb_annotations = KnowledgeBaseAnnotation.objects.filter(
158-
problem=problem, session=last_session, removed_at__isnull=True
159-
)
160-
return KnowledgeBaseAnnotationSerializer(kb_annotations, many=True).data
161-
162-
def get_labelAnnotations(self, obj):
163-
problem, last_session = self._get_problem_and_last_session()
164-
if not problem or not last_session:
165-
return []
166-
label_annotations = LabelAnnotation.objects.filter(
167-
problem=problem, session=last_session, removed_at__isnull=True
168-
)
169-
return LabelAnnotationSerializer(
170-
label_annotations, many=True, context=self.context
171-
).data
172-
173-
def _get_problem_and_last_session(self):
174-
problem = self.context.get("problem", None)
175-
user = self.context.get("user", None)
176-
if not problem or not user or user.is_authenticated is False:
177-
return None, None
178-
last_session = (
179-
AnnotationSession.objects.filter(user=user).order_by("-created_at").first()
180-
)
181-
return problem, last_session
182-
183-
184145
class SelectedLabelSerializer(serializers.Serializer):
185146
"""Serializer for a selected label in the save labels input."""
186147

backend/problem/serializers.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from rest_framework import serializers
22

3-
from annotation.serializers import (
4-
AnnotationSerializer,
5-
KnowledgeBaseAnnotationSerializer,
6-
)
3+
from annotation.serializers import KnowledgeBaseAnnotationSerializer, LabelAnnotationSerializer
74
from annotation.models import (
85
AnnotationSession,
96
KnowledgeBaseAnnotation,
7+
LabelAnnotation,
108
)
119
from problem.services import FracasData, SNLIData, SickData
1210
from problem.models import Problem, Sentence
@@ -23,7 +21,8 @@ class ProblemSerializer(serializers.ModelSerializer):
2321
hypothesis = serializers.SerializerMethodField()
2422
entailmentLabel = serializers.CharField(source="entailment_label")
2523
extraData = serializers.SerializerMethodField()
26-
annotations = serializers.SerializerMethodField()
24+
kbAnnotations = serializers.SerializerMethodField()
25+
labelAnnotations = serializers.SerializerMethodField()
2726

2827
class Meta:
2928
model = Problem
@@ -35,18 +34,19 @@ class Meta:
3534
"entailmentLabel",
3635
"extraData",
3736
"base",
38-
"annotations",
37+
"kbAnnotations",
38+
"labelAnnotations",
3939
]
4040

41-
def get_premises(self, problem):
41+
def get_premises(self, problem: Problem):
4242
"""Get list of premise texts."""
4343
return [premise.text for premise in problem.premises.all()]
4444

45-
def get_hypothesis(self, problem):
45+
def get_hypothesis(self, problem: Problem):
4646
"""Get hypothesis text."""
4747
return problem.hypothesis.text
4848

49-
def get_extraData(self, problem):
49+
def get_extraData(self, problem: Problem):
5050
"""Get dataset-specific extra data."""
5151
match problem.dataset:
5252
case Problem.Dataset.SICK:
@@ -58,17 +58,20 @@ def get_extraData(self, problem):
5858
case _:
5959
return {}
6060

61-
def get_annotations(self, problem: Problem):
62-
request = self.context.get("request", None)
63-
if not request or not request.user:
64-
return None
65-
return AnnotationSerializer(
66-
{},
67-
context={
68-
"problem": problem,
69-
"user": request.user,
70-
},
71-
).data
61+
def get_kbAnnotations(self, problem: Problem):
62+
"""Get knowledge base annotations for the problem."""
63+
kb_annotations = KnowledgeBaseAnnotation.objects.filter(
64+
problem=problem, removed_at__isnull=True
65+
)
66+
kb_data = KnowledgeBaseAnnotationSerializer(kb_annotations, many=True).data
67+
return kb_data
68+
69+
def get_labelAnnotations(self, problem: Problem):
70+
"""Get label annotations for the problem."""
71+
label_annotations = LabelAnnotation.objects.filter(
72+
problem=problem, removed_at__isnull=True
73+
)
74+
return LabelAnnotationSerializer(label_annotations, many=True).data
7275

7376

7477
class ProblemInputSerializer(serializers.Serializer):

frontend/src/app/annotate/annotation-input/annotation-input.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class AnnotationInputComponent implements OnInit {
162162
}
163163

164164
private buildForm(problem: Problem): ParseInputForm {
165-
const kbItems = this.buildKbForms(problem.annotations?.kbAnnotations ?? []);
165+
const kbItems = this.buildKbForms(problem.kbAnnotations ?? []);
166166

167167
return new FormGroup({
168168
id: new FormControl<number | null>(problem.id, {

frontend/src/app/annotate/annotation-input/problem-details/problem-details.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class ProblemDetailsComponent {
7979
baseProblemId: problem.base?.toString() ?? null,
8080
dataset: problem.dataset,
8181
entailmentLabel: problem.entailmentLabel,
82-
labelAnnotations: problem.annotations?.labelAnnotations ?? [],
82+
labelAnnotations: problem.labelAnnotations ?? [],
8383
};
8484

8585
switch (problem.dataset) {

frontend/src/app/services/problem.service.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,13 @@ export class ProblemService {
132132
premises: existingProblem?.premises ?? [],
133133
entailmentLabel: EntailmentLabel.UNKNOWN,
134134
extraData: null,
135-
annotations: {
136-
kbAnnotations: existingProblem?.annotations?.kbAnnotations.map(annotation => ({
137-
...annotation, id: null,
138-
})) ?? [],
139-
labelAnnotations: existingProblem?.annotations?.labelAnnotations.map(annotation => ({
140-
...annotation,
141-
id: null,
142-
})) ?? [],
143-
}
135+
kbAnnotations: existingProblem?.kbAnnotations.map(annotation => ({
136+
...annotation, id: null,
137+
})) ?? [],
138+
labelAnnotations: existingProblem?.labelAnnotations.map(annotation => ({
139+
...annotation,
140+
id: null,
141+
})) ?? [],
144142
}
145143
};
146144
}

frontend/src/app/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,14 @@ export interface LabelAnnotation extends BaseAnnotation {
5858
attachedByCurrentUser: boolean;
5959
}
6060

61-
interface Annotation {
62-
kbAnnotations: KnowledgeBaseAnnotation[];
63-
labelAnnotations: LabelAnnotation[];
64-
}
65-
6661
interface ProblemBase {
6762
id: number | null;
6863
base: number | null;
6964
premises: string[];
7065
hypothesis: string | null;
7166
entailmentLabel: EntailmentLabel;
72-
annotations: Annotation | null;
67+
kbAnnotations: KnowledgeBaseAnnotation[];
68+
labelAnnotations: LabelAnnotation[];
7369
}
7470

7571
interface SickProblem extends ProblemBase {

0 commit comments

Comments
 (0)