Esempio n. 1
0
    def test_get_by_id_raises_exception_if_internal_error(
            self, mock_get_by_id):
        # Arrange
        mock_absent_id = ObjectId()

        mock_get_by_id.side_effect = exceptions.ModelError("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.ModelError):
            provider_metadata_format_api.get_by_id(mock_absent_id)
Esempio n. 2
0
    def test_get_by_id_raises_exception_if_object_does_not_exist(
            self, mock_get_by_id):
        # Arrange
        mock_absent_id = ObjectId()

        mock_get_by_id.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            provider_metadata_format_api.get_by_id(mock_absent_id)
Esempio n. 3
0
def xsl_template_view(request, metadata_format_id):
    assets = {
        "js": [
            AddObjectModalView.get_modal_js_path(),
            EditObjectModalView.get_modal_js_path(),
            DeleteObjectModalView.get_modal_js_path(),
        ],
    }

    modals = [
        AddObjectModalView.get_modal_html_path(),
        EditObjectModalView.get_modal_html_path(),
        DeleteObjectModalView.get_modal_html_path(),
    ]

    metadata_format = oai_metadata_format_api.get_by_id(metadata_format_id)

    context = {
        "xsl_templates": _get_xsl_templates(metadata_format),
        "metadata_format": metadata_format,
    }

    return admin_render(
        request,
        "core_oaipmh_provider_app/admin/registry/xsl_template.html",
        assets=assets,
        context=context,
        modals=modals,
    )
    def delete(self, request, metadata_format_id):
        """Delete a MetadataFormat

        Args:

            request: HTTP request
            metadata_format_id: ObjectId

        Returns:

            - code: 204
              content: Deletion succeed
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            metadata_format = oai_provider_metadata_format_api.get_by_id(
                metadata_format_id)
            oai_provider_metadata_format_api.delete(metadata_format)

            return Response(status=status.HTTP_204_NO_CONTENT)
        except exceptions.DoesNotExist:
            content = OaiPmhMessage.get_message_labelled(
                "No Metadata Format found with the given id.")
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except exceptions_oai.OAIAPIException as e:
            return e.response()
        except Exception as e:
            content = OaiPmhMessage.get_message_labelled(str(e))
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    def patch(self, request, metadata_format_id):
        """Update a MetadataFormat

        Parameters:

            {
                "metadata_prefix" : "value"
            }

        Args:

            request: HTTP request
            metadata_format_id: ObjectId

        Returns:

            - code: 200
              content: Success message
            - code: 400
              content: Validation error
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            metadata_format = oai_provider_metadata_format_api.get_by_id(
                metadata_format_id)
            # Build serializer
            serializer = serializers.UpdateMetadataFormatSerializer(
                instance=metadata_format,
                data=request.data,
                context={"request": request},
            )
            # Validate data
            serializer.is_valid(True)
            # Save data
            serializer.save()
            content = OaiPmhMessage.get_message_labelled(
                "Metadata Format updated with success.")

            return Response(content, status=status.HTTP_200_OK)
        except ValidationError as validation_exception:
            content = OaiPmhMessage.get_message_labelled(
                validation_exception.detail)
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
        except exceptions.DoesNotExist:
            content = OaiPmhMessage.get_message_labelled(
                "No Metadata Format found with the given id.")
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except exceptions_oai.OAIAPIException as e:
            return e.response()
        except Exception as e:
            content = OaiPmhMessage.get_message_labelled(str(e))
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Esempio n. 6
0
    def test_get_by_id_return_object(self, mock_get_by_id):
        # Arrange
        mock_oai_provider_metadata_format = _create_mock_oai_provider_metadata_format(
        )
        mock_oai_provider_metadata_format.id = ObjectId()

        mock_get_by_id.return_value = mock_oai_provider_metadata_format

        # Act
        result = provider_metadata_format_api.get_by_id(mock_get_by_id.id)

        # Assert
        self.assertIsInstance(result, OaiProviderMetadataFormat)
Esempio n. 7
0
    def validate_oai_metadata_format(oai_metadata_format):
        """ Validate oai_metadata_format field

        Args:
            oai_metadata_format:

        Returns:

        """
        oai_metadata_format_object = oai_provider_metadata_format_api.get_by_id(
            oai_metadata_format)

        if oai_metadata_format_object.is_template:
            raise ValidationError(
                'Impossible to map a XSLT to a template metadata format')

        return oai_metadata_format
    def get(self, request, metadata_format_id):
        """Get a MetadataFormat

        Parameters:

            {
                "metadata_prefix" : "value",
                "schema_url" : "URL"
            }

        Args:

            request: HTTP request
            metadata_format_id: ObjectId

        Returns:

            - code: 200
              content: MetadataFormat
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            metadata_format = oai_provider_metadata_format_api.get_by_id(
                metadata_format_id)
            serializer = serializers.OaiProviderMetadataFormatSerializer(
                metadata_format, context={"request": request})

            return Response(serializer.data, status=status.HTTP_200_OK)
        except exceptions.DoesNotExist:
            content = OaiPmhMessage.get_message_labelled(
                "No Metadata Format found with the given id.")
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except exceptions_oai.OAIAPIException as e:
            return e.response()
        except Exception as e:
            content = OaiPmhMessage.get_message_labelled(str(e))
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)