Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • arkindex/backend
1 result
Show changes
Commits on Source (4)
1.1.2-rc2
1.1.2
......@@ -123,15 +123,22 @@ class MetaDataLightSerializer(serializers.ModelSerializer):
instance.value = bleach.clean(instance.value, tags=tags)
return super().to_representation(instance)
def check_allowed(self, corpus=None, type=None, name=None, instance=None, **kwargs):
def check_allowed(self, corpus, type=None, name=None, instance=None, **kwargs):
"""
Assert metadata type and value are allowed in this corpus
"""
message = f'Metadata with this type ({type}) and name ({name}) are not allowed for this corpus'
if instance:
type = instance.type
name = instance.name
message = f'Edition of metadata with this type ({type}) and name ({name}) are disallowed for this corpus'
if type is None:
type = instance.type
if name is None:
name = instance.name
message = f'Edition of metadata with this type ({type}) and name ({name}) is not allowed for this corpus'
# Ensure there always is a type and a name. Otherwise, an endpoint might make the user believe an allowed
# metadata is missing when there is just a bug in our code, because Django does not mind filtering by None.
assert type is not None and name is not None, 'Both a type and name are required to check for allowed metadata'
if not AllowedMetaData.objects.filter(corpus=corpus, type=type, name=name):
raise ValidationError({
'__all__': [message]
......@@ -179,8 +186,9 @@ class MetaDataLightSerializer(serializers.ModelSerializer):
if not (user.is_admin or user.is_internal):
# Assert actual instance is part of AllowedMetaData
self.check_allowed(corpus=element.corpus, instance=instance)
# Assert update is part of AllowedMetaData
self.check_allowed(corpus=element.corpus, **validated_data)
# Assert the new values is part of AllowedMetaData
# Still provide the instance so that the check can rely on the original values for partial updates
self.check_allowed(corpus=element.corpus, instance=instance, **validated_data)
new_name = validated_data.get('name') or instance.name
metadata = element.metadatas \
.filter(name=new_name) \
......
......@@ -313,7 +313,7 @@ class TestMetaData(FixtureAPITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(
response.json(),
{'__all__': ['Edition of metadata with this type (Location) and name (not editable) are disallowed for this corpus']}
{'__all__': ['Edition of metadata with this type (Location) and name (not editable) is not allowed for this corpus']}
)
def test_admin_delete_any(self):
......@@ -396,6 +396,55 @@ class TestMetaData(FixtureAPITestCase):
self.assertEqual(self.metadata.name, 'date')
self.assertEqual(self.metadata.value, '2019-12-04')
def test_patch_metadata_type(self):
self.client.force_login(self.user)
self.assertEqual(self.metadata.type, MetaType.Text)
self.corpus.allowed_metadatas.create(type=MetaType.Date, name='folio')
response = self.client.patch(
reverse('api:metadata-edit', kwargs={'pk': str(self.metadata.id)}),
data={
'type': 'date'
}
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
self.assertDictEqual(response.json(), {
'dates': [],
'id': str(self.metadata.id),
'name': 'folio',
'type': 'date',
'value': '123',
'entity': None,
'worker_version': None,
})
self.metadata.refresh_from_db()
self.assertEqual(self.metadata.type, MetaType.Date)
self.assertEqual(self.metadata.name, 'folio')
self.assertEqual(self.metadata.value, '123')
def test_patch_metadata_value(self):
self.client.force_login(self.user)
self.assertEqual(self.metadata.type, MetaType.Text)
response = self.client.patch(
reverse('api:metadata-edit', kwargs={'pk': str(self.metadata.id)}),
data={
'value': '2019-12-04'
}
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
self.assertDictEqual(response.json(), {
'dates': [],
'id': str(self.metadata.id),
'name': 'folio',
'type': 'text',
'value': '2019-12-04',
'entity': None,
'worker_version': None,
})
self.metadata.refresh_from_db()
self.assertEqual(self.metadata.type, MetaType.Text)
self.assertEqual(self.metadata.name, 'folio')
self.assertEqual(self.metadata.value, '2019-12-04')
def test_patch_disallowed(self):
self.client.force_login(self.user)
response = self.client.patch(
......@@ -409,7 +458,21 @@ class TestMetaData(FixtureAPITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(
response.json(),
{'__all__': ['Metadata with this type (Date) and name (death of William I, Count of Hainaut) are not allowed for this corpus']}
{'__all__': ['Edition of metadata with this type (Date) and name (death of William I, Count of Hainaut) is not allowed for this corpus']}
)
def test_patch_partial_disallowed(self):
self.client.force_login(self.user)
response = self.client.patch(
reverse('api:metadata-edit', kwargs={'pk': str(self.metadata.id)}),
data={
'name': 'death of William I, Count of Hainaut',
}
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(
response.json(),
{'__all__': ['Edition of metadata with this type (Text) and name (death of William I, Count of Hainaut) is not allowed for this corpus']}
)
def test_patch_from_any_internal(self):
......@@ -472,7 +535,7 @@ class TestMetaData(FixtureAPITestCase):
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(response.json(), {
'__all__': ['Edition of metadata with this type (Location) and name (not editable) are disallowed for this corpus']
'__all__': ['Edition of metadata with this type (Location) and name (not editable) is not allowed for this corpus']
})
metadata.refresh_from_db()
self.assertEqual(metadata.name, 'not editable')
......