def test_get_by_data_raises_exception_if_internal_error( self, mock_get_by_data): # Arrange mock_absent_data = Data() mock_get_by_data.side_effect = exceptions.ModelError("Error.") # Act + Assert with self.assertRaises(exceptions.ModelError): oai_data_api.get_by_data(mock_absent_data)
def test_get_by_data_raises_exception_if_object_does_not_exist( self, mock_get_by_data): # Arrange mock_absent_data = Data() mock_get_by_data.side_effect = exceptions.DoesNotExist("Error.") # Act + Assert with self.assertRaises(exceptions.DoesNotExist): oai_data_api.get_by_data(mock_absent_data)
def test_get_by_data_returns_object(self, mock_get_by_data): # Arrange mock_oai_data = _create_oai_data() mock_oai_data.data = Data() mock_get_by_data.return_value = mock_oai_data # Act result = oai_data_api.get_by_data(mock_get_by_data.data) # Assert self.assertIsInstance(result, OaiData)
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 post_delete_data(sender, document, **kwargs): """ Method executed after a deletion of a Data object. Args: sender: Class. document: OaiData document. **kwargs: Args. """ try: oai_data = oai_data_api.get_by_data(document) oai_data.oai_date_stamp = datetime.now() oai_data.status = status.DELETED oai_data_api.upsert(oai_data) except Exception as e: pass
def post_delete_data(sender, document, **kwargs): """Method executed after a deletion of a Data object. Args: sender: Class. document: OaiData document. **kwargs: Args. """ try: oai_data = oai_data_api.get_by_data(document) oai_data.oai_date_stamp = datetime.now() oai_data.status = status.DELETED oai_data_api.upsert(oai_data) except exceptions.DoesNotExist: logger.warning( "post_delete_data: no oai data found for the given document: {0}". format(str(document.id))) except Exception as e: logger.warning("post_delete_data 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 = 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, )