Skip to content
Snippets Groups Projects
Commit 7dc58d90 authored by Valentin Rigal's avatar Valentin Rigal
Browse files

CreateTranscription

parent e5a43a34
No related branches found
No related tags found
No related merge requests found
......@@ -111,7 +111,7 @@ class TranscriptionEdit(RetrieveUpdateDestroyAPIView):
errors = defaultdict(list)
if Right.Write not in rights:
errors['__all__'].append('A write access to transcription element corpus is required.')
if transcription.source.slug != 'manual':
if transcription.worker_version or transcription.source and transcription.source.slug != 'manual':
errors['__all__'].append('Only manual transcriptions can be edited.')
if (errors):
raise PermissionDenied(errors)
......
......@@ -289,13 +289,12 @@ class TranscriptionCreateSerializer(serializers.ModelSerializer):
Allows the insertion of a manual transcription attached to an element
"""
type = EnumField(TranscriptionType)
source = DataSourceSlugField(tool_type=MLToolType.Recognizer, required=False)
worker_version = serializers.PrimaryKeyRelatedField(queryset=WorkerVersion.objects.all(), required=False, allow_null=True)
score = serializers.FloatField(min_value=0, max_value=1, required=False)
class Meta:
model = Transcription
fields = ('text', 'type', 'source', 'worker_version', 'score')
fields = ('text', 'type', 'worker_version', 'score')
def validate(self, data):
data = super().validate(data)
......@@ -305,46 +304,27 @@ class TranscriptionCreateSerializer(serializers.ModelSerializer):
if not element.zone_id:
raise ValidationError({'element': ['The element has no zone']})
source = data.get('source')
worker_version = data.get('worker_version')
if not source and not worker_version:
raise ValidationError({
'source': ['This field XOR worker_version field must be set to create a transcription'],
'worker_version': ['This field XOR source field must be set to create a transcription']
})
elif source and worker_version:
raise ValidationError({
'source': ['You can only refer to a DataSource XOR a WorkerVersion on a transcription'],
'worker_version': ['You can only refer to a DataSource XOR a WorkerVersion on a transcription']
})
elif source:
slug = source.slug
if slug == 'manual':
# Assert the type is allowed for manual transcription
allowed_transcription = element.type.allowed_transcription
if not allowed_transcription:
raise ValidationError({'element': ['The element type does not allow creating a manual transcription']})
if data['type'] is not allowed_transcription:
raise ValidationError({'type': [
f"Only transcriptions of type '{allowed_transcription.value}' are allowed for this element"
]})
return data
if worker_version is None:
# Assert the type is allowed for manual transcription
allowed_transcription = element.type.allowed_transcription
if not allowed_transcription:
raise ValidationError({'element': ['The element type does not allow creating a manual transcription']})
if data['type'] is not allowed_transcription:
raise ValidationError({'type': [
f"Only transcriptions of type '{allowed_transcription.value}' are allowed for this element"
]})
return data
# Additional validation for transcriptions with an internal source
# Additional validation for transcriptions with a worker version
if not data.get('score'):
raise ValidationError({'score': ['This field is required for non manual sources.']})
raise ValidationError({'score': ['This field is required for transcription with a worker version.']})
user = self.context['request'].user
if (not user or not user.is_internal):
if source:
raise ValidationError({'source': [
'An internal user is required to create a transcription with '
f'the internal source "{slug}"'
]})
raise ValidationError({'worker_version': [
'An internal user is required to create a transcription with '
f'the worker_version "{worker_version.id}"'
'An internal user is required to create a transcription refering to a worker_version'
]})
return data
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment