def test_get_by_id_raises_exception_if_internal_error(self, mock_get_by_template_id_and_metadata_format_id):
        # Arrange
        mock_absent_id = ObjectId()

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

        # Act + Assert
        with self.assertRaises(exceptions.ModelError):
            oai_xsl_template_api.get_by_template_id_and_metadata_format_id(mock_absent_id, mock_absent_id)
    def test_get_by_id_raises_exception_if_object_does_not_exist(self, mock_get_by_template_id_and_metadata_format_id):
        # Arrange
        mock_absent_id = ObjectId()

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

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            oai_xsl_template_api.get_by_template_id_and_metadata_format_id(mock_absent_id, mock_absent_id)
    def delete(self, request):
        """Remove the mapping between a template, a metadata format and a XSLT

        Parameters:

            {
                "template_id": "value",
                "metadata_format_id": "value"
            }

        Args:

            request: HTTP request

        Returns:

            - code: 204
              content: Deletion succeed
            - code: 400
              content: Validation error
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Build serializer
            serializer = serializers.TemplateToMFUnMappingXSLTSerializer(
                data=request.data)
            # Validate data
            serializer.is_valid(True)
            # Get template id
            template_id = serializer.data.get("template_id")
            # Get metadata format id
            metadata_format_id = serializer.data.get("metadata_format_id")
            # Get mapping
            oai_xsl_template = (
                oai_xsl_template_api.get_by_template_id_and_metadata_format_id(
                    template_id, metadata_format_id))
            # Delete the mapping
            oai_xsl_template_api.delete(oai_xsl_template)

            return Response(status=status.HTTP_204_NO_CONTENT)
        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 as e:
            content = OaiPmhMessage.get_message_labelled(str(e))
            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 init_instance(self):
     try:
         oai_xsl_template = (
             oai_xsl_template_api.get_by_template_id_and_metadata_format_id(
                 self.validated_data.get("template", ""),
                 self.validated_data.get("oai_metadata_format", ""),
             ))
         self.instance = oai_xsl_template
     except exceptions.DoesNotExist as e:
         logger.warning("init_instance threw an exception: {0}".format(
             str(e)))
    def get_record(self):
        """ Response to GetRecord request.
       Returns:
           XML type response.

       """
        try:
            self.template_name = 'core_oaipmh_provider_app/user/xml/get_record.html'
            # Check if the identifier pattern is OK.
            record_id = CheckOaiPmhRequest.check_identifier(self.identifier)
            try:
                oai_data = oai_data_api.get_by_data(record_id)
            except:
                raise oai_provider_exceptions.IdDoesNotExist(self.identifier)
            try:
                metadata_format = oai_provider_metadata_format_api.get_by_metadata_prefix(
                    self.metadata_prefix)
                # Check if the record and the given metadata prefix use the same template.
                use_raw = metadata_format.is_template and (
                    oai_data.template == metadata_format.template)
                if oai_data.status != oai_status.DELETED:
                    if use_raw:
                        xml = oai_data.data.xml_content
                    else:
                        xslt = oai_xsl_template_api.get_by_template_id_and_metadata_format_id(
                            oai_data.template.id, metadata_format.id).xslt
                        xml = xsl_transformation_api.xsl_transform(
                            oai_data.data.xml_content, xslt.name)
                else:
                    xml = None
            except:
                raise oai_provider_exceptions.CannotDisseminateFormat(
                    self.metadata_prefix)

            record_info = {
                'identifier':
                self.identifier,
                'last_modified':
                UTCdatetime.datetime_to_utc_datetime_iso8601(
                    oai_data.oai_date_stamp),
                'sets':
                oai_provider_set_api.get_all_by_template_ids(
                    [oai_data.template.id]),
                'XML':
                xml,
                'deleted':
                oai_data.status == oai_status.DELETED
            }

            return self.render_to_response(record_info)
        except oai_provider_exceptions.OAIExceptions, e:
            return self.errors(e.errors)
    def test_get_by_id_returns_object(self, mock_get_by_id_and_metadata_format_id):
        # Arrange
        mock_oai_xsl_template = _create_oai_xsl_template()
        template_id = ObjectId()
        metadata_format_id = ObjectId()

        mock_get_by_id_and_metadata_format_id.return_value = mock_oai_xsl_template

        # Act
        result = oai_xsl_template_api.get_by_template_id_and_metadata_format_id(template_id, metadata_format_id)

        # Assert
        self.assertIsInstance(result, OaiXslTemplate)
    def _get_items(templates_id,
                   from_date,
                   until_date,
                   metadata_format,
                   include_metadata=False,
                   use_raw=True):
        items = []
        for template in templates_id:
            try:
                xslt = None
                if not use_raw:
                    xslt = oai_xsl_template_api.get_by_template_id_and_metadata_format_id(
                        template, metadata_format).xslt
                oai_data = oai_data_api.get_all_by_template(
                    template, from_date=from_date, until_date=until_date)
                for elt in oai_data:
                    # Only data from public workspace.
                    if elt.data.workspace is not None and \
                            workspace_api.is_workspace_public(elt.data.workspace):
                        identifier = '%s:%s:id/%s' % (
                            settings.OAI_SCHEME, settings.OAI_REPO_IDENTIFIER,
                            str(elt.data_id))
                        item_info = {
                            'identifier':
                            identifier,
                            'last_modified':
                            UTCdatetime.datetime_to_utc_datetime_iso8601(
                                elt.oai_date_stamp),
                            'sets':
                            oai_provider_set_api.get_all_by_template_ids(
                                [template]),
                            'deleted':
                            elt.status == oai_status.DELETED
                        }
                        if include_metadata and elt.status == oai_status.ACTIVE:
                            if use_raw:
                                item_info.update({'XML': elt.data.xml_content})
                            else:
                                xml = xsl_transformation_api.xsl_transform(
                                    elt.data.xml_content, xslt.name)
                                item_info.update({'XML': xml})

                        items.append(item_info)
            except (exceptions.DoesNotExist, exceptions.XMLError, Exception):
                pass

        # If there is no records
        if len(items) == 0:
            raise oai_provider_exceptions.NoRecordsMatch

        return items
    def get_record(self):
        """Response to GetRecord request.
        Returns:
            XML type response.

        """
        try:
            self.template_name = "core_oaipmh_provider_app/user/xml/get_record.html"
            # Check if the identifier pattern is OK.
            record_id = request_checker.check_identifier(self.identifier)
            try:
                oai_data = oai_data_api.get_by_data(record_id)
            except Exception:
                raise oai_provider_exceptions.IdDoesNotExist(self.identifier)

            try:
                metadata_format = (
                    oai_provider_metadata_format_api.get_by_metadata_prefix(
                        self.metadata_prefix))
                # Check if the record and the given metadata prefix use the same template.
                use_raw = (metadata_format.is_template
                           and oai_data.template == metadata_format.template)
                if oai_data.status != oai_status.DELETED:
                    xml = re.sub(r"<\?xml[^?]+\?>", "",
                                 oai_data.data.xml_content)

                    if not use_raw:
                        xslt = oai_xsl_template_api.get_by_template_id_and_metadata_format_id(
                            oai_data.template.id, metadata_format.id).xslt
                        xml = xsl_transformation_api.xsl_transform(
                            xml, xslt.name)
                else:
                    xml = None
            except Exception:
                raise oai_provider_exceptions.CannotDisseminateFormat(
                    self.metadata_prefix)

            record_info = {
                "identifier":
                self.identifier,
                "last_modified":
                UTCdatetime.datetime_to_utc_datetime_iso8601(
                    oai_data.oai_date_stamp),
                "sets":
                oai_provider_set_api.get_all_by_template_ids(
                    [oai_data.template.id], request=self.request),
                "xml":
                xml,
                "deleted":
                oai_data.status == oai_status.DELETED,
            }

            return self.render_to_response(record_info)
        except oai_provider_exceptions.OAIExceptions as e:
            return self.errors(e.errors)
        except oai_provider_exceptions.OAIException as e:
            return self.error(e)
        except Exception as e:
            return HttpResponse(
                {"content": escape(str(e))},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )
    def _get_items(
        template_id_list,
        metadata_format,
        oai_set=None,
        from_date=None,
        until_date=None,
        include_metadata=False,
        use_raw=True,
        page_nb=1,
        request=None,
    ):
        items = []
        output_resumption_token = None

        try:
            # FIXME filtering public data is needed since all data has
            # a OAI data associated with it
            public_workspace_list = workspace_api.get_all_public_workspaces()
            data_list = system_api.get_all_data_in_workspaces_for_templates(
                public_workspace_list, template_id_list)
            oai_data = oai_data_api.get_all_by_data_list(data_list,
                                                         from_date=from_date,
                                                         until_date=until_date)

            oai_data_paginator = Paginator(oai_data, RESULTS_PER_PAGE)

            # If there are more pages to display
            if oai_data_paginator.num_pages > page_nb:
                exp_date = UTCdatetime.datetime_to_utc_datetime_iso8601(
                    datetime.now() + timedelta(days=7))

                oai_request_page_object = oai_request_page_api.upsert(
                    OaiRequestPage(
                        template_id_list=template_id_list,
                        metadata_format=metadata_format.metadata_prefix,
                        oai_set=oai_set,
                        from_date=from_date,
                        until_date=until_date,
                        expiration_date=exp_date,
                        page_number=page_nb + 1,
                    ))

                output_resumption_token = {
                    "token": oai_request_page_object.resumption_token,
                    "expiration_date": exp_date,
                    "list_size": oai_data.count(),
                    "cursor": RESULTS_PER_PAGE * (page_nb - 1),
                }
            elif page_nb != 1:  # If on the last page, send empty token
                output_resumption_token = {
                    "token": "",
                    "list_size": oai_data.count(),
                    "cursor": RESULTS_PER_PAGE * (page_nb - 1),
                }

            for elt in oai_data_paginator.page(page_nb):
                identifier = "%s:%s:id/%s" % (
                    settings.OAI_SCHEME,
                    settings.OAI_REPO_IDENTIFIER,
                    str(elt.data_id),
                )
                item_info = {
                    "identifier":
                    identifier,
                    "last_modified":
                    UTCdatetime.datetime_to_utc_datetime_iso8601(
                        elt.oai_date_stamp),
                    "sets":
                    oai_provider_set_api.get_all_by_template_ids(
                        [elt.data.template], request=request),
                    "deleted":
                    elt.status == oai_status.DELETED,
                }

                # Add data information if needed
                if include_metadata and elt.status == oai_status.ACTIVE:
                    xml = re.sub(r"<\?xml[^?]+\?>", "", elt.data.xml_content)

                    if not use_raw:
                        # FIXME gets recomputed for every element
                        xslt = oai_xsl_template_api.get_by_template_id_and_metadata_format_id(
                            elt.data.template, metadata_format).xslt

                        xml = xsl_transformation_api.xsl_transform(
                            xml, xslt.name)

                    item_info.update({"xml": xml})

                items.append(item_info)
        except Exception as e:
            logger.warning("_get_items threw an exception: {0}".format(str(e)))

        if len(items) == 0:  # No records retrieved
            raise oai_provider_exceptions.NoRecordsMatch

        return items, output_resumption_token