From c9d12e6cb250203b7dfc55d06495c070e357a480 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Fri, 6 Dec 2019 18:26:41 +1000 Subject: [PATCH 1/2] Inline getAnnotations function --- app/server/static/components/document_classification.vue | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/server/static/components/document_classification.vue b/app/server/static/components/document_classification.vue index 9f91311a..74b010c8 100644 --- a/app/server/static/components/document_classification.vue +++ b/app/server/static/components/document_classification.vue @@ -58,10 +58,6 @@ export default { mixins: [annotationMixin], methods: { - getAnnotation(label) { - return this.annotations[this.pageNumber].find(annotation => annotation.label === label.id); - }, - async submit() { const state = this.getState(); this.url = `docs?q=${this.searchQuery}&doc_annotations__isnull=${state}&offset=${this.offset}&ordering=${this.ordering}`; @@ -70,7 +66,9 @@ export default { }, async addLabel(label) { - const annotation = this.getAnnotation(label); + const annotations = this.annotations[this.pageNumber]; + const annotation = annotations.find(item => item.label === label.id); + if (annotation) { this.removeLabel(annotation); } else { From e0ec1232d20cde458c936b2f0b4931fe94f4c4f5 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Fri, 6 Dec 2019 18:44:31 +1000 Subject: [PATCH 2/2] Add single-class-classification mode --- ...002_project_single_class_classification.py | 18 +++++++++ app/api/models.py | 1 + app/api/serializers.py | 2 +- app/api/tests/test_api.py | 38 +++++++++++++++++++ app/api/views.py | 16 ++++++++ .../static/components/annotationMixin.js | 4 +- .../components/document_classification.vue | 4 ++ app/server/static/components/projects.vue | 13 +++++++ 8 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 app/api/migrations/0002_project_single_class_classification.py diff --git a/app/api/migrations/0002_project_single_class_classification.py b/app/api/migrations/0002_project_single_class_classification.py new file mode 100644 index 00000000..fbab8096 --- /dev/null +++ b/app/api/migrations/0002_project_single_class_classification.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.11 on 2019-12-06 08:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='project', + name='single_class_classification', + field=models.BooleanField(default=False), + ), + ] diff --git a/app/api/models.py b/app/api/models.py index 697ceb0f..444cbed7 100644 --- a/app/api/models.py +++ b/app/api/models.py @@ -32,6 +32,7 @@ class Project(PolymorphicModel): project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES) randomize_document_order = models.BooleanField(default=False) collaborative_annotation = models.BooleanField(default=False) + single_class_classification = models.BooleanField(default=False) def get_absolute_url(self): return reverse('upload', args=[self.id]) diff --git a/app/api/serializers.py b/app/api/serializers.py index fa2e0d26..1b1c6123 100644 --- a/app/api/serializers.py +++ b/app/api/serializers.py @@ -105,7 +105,7 @@ class ProjectSerializer(serializers.ModelSerializer): class Meta: model = Project fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', 'image', - 'updated_at', 'randomize_document_order', 'collaborative_annotation') + 'updated_at', 'randomize_document_order', 'collaborative_annotation', 'single_class_classification') read_only_fields = ('image', 'updated_at', 'users', 'current_users_role') diff --git a/app/api/tests/test_api.py b/app/api/tests/test_api.py index 92f699b4..179cb302 100644 --- a/app/api/tests/test_api.py +++ b/app/api/tests/test_api.py @@ -687,6 +687,18 @@ class TestAnnotationListAPI(APITestCase, TestUtilsMixin): sub_project_doc = mommy.make('Document', project=sub_project) mommy.make('SequenceAnnotation', document=sub_project_doc) + cls.classification_project = mommy.make('TextClassificationProject', + users=[project_member, another_project_member]) + cls.classification_project_label_1 = mommy.make('Label', project=cls.classification_project) + cls.classification_project_label_2 = mommy.make('Label', project=cls.classification_project) + cls.classification_project_document = mommy.make('Document', project=cls.classification_project) + cls.classification_project_url = reverse( + viewname='annotation_list', args=[cls.classification_project.id, cls.classification_project_document.id]) + assign_user_to_role(project_member=project_member, project=cls.classification_project, + role_name=settings.ROLE_ANNOTATOR) + assign_user_to_role(project_member=another_project_member, project=cls.classification_project, + role_name=settings.ROLE_ANNOTATOR) + cls.url = reverse(viewname='annotation_list', args=[main_project.id, main_project_doc.id]) cls.post_data = {'start_offset': 0, 'end_offset': 1, 'label': main_project_label.id} cls.num_entity_of_project_member = SequenceAnnotation.objects.filter(document=main_project_doc, @@ -737,6 +749,32 @@ class TestAnnotationListAPI(APITestCase, TestUtilsMixin): response = self.client.post(self.url, format='json', data=self.post_data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + def test_disallows_second_annotation_for_single_class_project(self): + self._patch_project(self.classification_project, 'single_class_classification', True) + + self.client.login(username=self.project_member_name, password=self.project_member_pass) + response = self.client.post(self.classification_project_url, format='json', + data={'label': self.classification_project_label_1.id}) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.post(self.classification_project_url, format='json', + data={'label': self.classification_project_label_2.id}) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + def test_disallows_second_annotation_for_single_class_shared_project(self): + self._patch_project(self.classification_project, 'single_class_classification', True) + self._patch_project(self.classification_project, 'collaborative_annotation', True) + + self.client.login(username=self.project_member_name, password=self.project_member_pass) + response = self.client.post(self.classification_project_url, format='json', + data={'label': self.classification_project_label_1.id}) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + self.client.login(username=self.another_project_member_name, password=self.another_project_member_pass) + response = self.client.post(self.classification_project_url, format='json', + data={'label': self.classification_project_label_2.id}) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + def _patch_project(self, project, attribute, value): old_value = getattr(project, attribute, None) setattr(project, attribute, value) diff --git a/app/api/views.py b/app/api/views.py index 94cb017f..61df8e85 100644 --- a/app/api/views.py +++ b/app/api/views.py @@ -187,12 +187,28 @@ class AnnotationList(generics.ListCreateAPIView): return queryset def create(self, request, *args, **kwargs): + self.check_single_class_classification(self.kwargs['project_id'], self.kwargs['doc_id'], request.user) + request.data['document'] = self.kwargs['doc_id'] return super().create(request, args, kwargs) def perform_create(self, serializer): serializer.save(document_id=self.kwargs['doc_id'], user=self.request.user) + @staticmethod + def check_single_class_classification(project_id, doc_id, user): + project = get_object_or_404(Project, pk=project_id) + if not project.single_class_classification: + return + + model = project.get_annotation_class() + annotations = model.objects.filter(document_id=doc_id) + if not project.collaborative_annotation: + annotations = annotations.filter(user=user) + + if annotations.exists(): + raise ValidationError('requested to create duplicate annotation for single-class-classification project') + class AnnotationDetail(generics.RetrieveUpdateDestroyAPIView): lookup_url_kwarg = 'annotation_id' diff --git a/app/server/static/components/annotationMixin.js b/app/server/static/components/annotationMixin.js index b1c1d2dd..faf97491 100644 --- a/app/server/static/components/annotationMixin.js +++ b/app/server/static/components/annotationMixin.js @@ -92,6 +92,7 @@ export default { prevLimit: 0, paginationPages: 0, paginationPage: 0, + singleClassClassification: false, isAnnotationApprover: false, isMetadataActive: false, isAnnotationGuidelineActive: false, @@ -196,7 +197,7 @@ export default { removeLabel(annotation) { const docId = this.docs[this.pageNumber].id; - HTTP.delete(`docs/${docId}/annotations/${annotation.id}`).then(() => { + return HTTP.delete(`docs/${docId}/annotations/${annotation.id}`).then(() => { const index = this.annotations[this.pageNumber].indexOf(annotation); this.annotations[this.pageNumber].splice(index, 1); }); @@ -258,6 +259,7 @@ export default { this.labels = response.data; }); HTTP.get().then((response) => { + this.singleClassClassification = response.data.single_class_classification; this.guideline = response.data.guideline; const roles = response.data.current_users_role; this.isAnnotationApprover = roles.is_annotation_approver || roles.is_project_admin; diff --git a/app/server/static/components/document_classification.vue b/app/server/static/components/document_classification.vue index 74b010c8..d3a24b51 100644 --- a/app/server/static/components/document_classification.vue +++ b/app/server/static/components/document_classification.vue @@ -72,6 +72,10 @@ export default { if (annotation) { this.removeLabel(annotation); } else { + if (this.singleClassClassification && annotations.length >= 1) { + await Promise.all(annotations.map(item => this.removeLabel(item))); + } + const docId = this.docs[this.pageNumber].id; const payload = { label: label.id, diff --git a/app/server/static/components/projects.vue b/app/server/static/components/projects.vue index a692ce4a..f0f1df04 100644 --- a/app/server/static/components/projects.vue +++ b/app/server/static/components/projects.vue @@ -64,6 +64,17 @@ ) | Share annotations across all users + div.field(v-if="projectType === 'DocumentClassification'") + label.checkbox + input( + v-model="singleClassClassification" + name="single_class_classification" + type="checkbox" + style="margin-right: 0.25em;" + required + ) + | Single-class classification + footer.modal-card-foot.pt20.pb20.pr20.pl20.has-background-white-ter button.button.is-primary(v-on:click="create()") Create button.button(v-on:click="isActive = !isActive") Cancel @@ -150,6 +161,7 @@ export default { projectNameError: '', username: '', isSuperuser: false, + singleClassClassification: false, randomizeDocumentOrder: false, collaborativeAnnotation: false, isProjectAdmin: null, @@ -208,6 +220,7 @@ export default { name: this.projectName, description: this.description, project_type: this.projectType, + single_class_classification: this.singleClassClassification, randomize_document_order: this.randomizeDocumentOrder, collaborative_annotation: this.collaborativeAnnotation, guideline: 'Please write annotation guideline.',