def getData(request): """ POST http://localhost/oai_pmh/api/getdata/ POST data query='{"url":"value"}' """ try: serializer = IdentifySerializer(data=request.DATA) if serializer.is_valid(): url = request.POST['url'] if str(url).__contains__('?'): registryURl = str(url).split('?')[0] #Check if the OAI Registry is available sickle = Sickle(registryURl) sickle.Identify() http_response = requests.get(url) if http_response.status_code == status.HTTP_200_OK: return Response(http_response.text, status=status.HTTP_200_OK) else: raise OAIAPIException(message='An error occurred.', status=http_response.status_code) else: raise OAIAPIException(message='An error occurred, url malformed.', status=status.HTTP_400_BAD_REQUEST) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except requests.HTTPError, err: content = APIMessage.getMessageLabelled(err.message) return Response(content, status=err.response.status_code)
def getRecord(request): """ POST http://localhost/oai_pmh/api/rest/getrecord POST data query='{"url":"value", "identifier":"value", "metadataprefix":"value"}' """ try: serializer = GetRecordSerializer(data=request.DATA) if serializer.is_valid(): url = request.DATA['url'] identifier = request.DATA['identifier'] metadataprefix = request.DATA['metadataprefix'] sickle = Sickle(url) grResponse = sickle.GetRecord(metadataPrefix=metadataprefix, identifier=identifier) record = Record(grResponse.xml) rtn=[] rtn.append({"identifier": record.header.identifier, "datestamp": record.header.datestamp, "deleted": record.deleted, "sets": record.header.setSpecs, "metadataPrefix": metadataprefix, "metadata": etree.tostring(record.xml.find('.//' + '{http://www.openarchives.org/OAI/2.0/}' + 'metadata/')) if not record.deleted else None, "raw": record.raw}) serializer = RecordSerializer(rtn) return Response(serializer.data, status=status.HTTP_200_OK) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled('An error occurred when attempting to retrieve record. %s'%e) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def listObjectAllRecords(request): """ POST http://localhost/oai_pmh/api/listobjectrecords POST data query='{"url":"value", "metadataprefix":"value"}' optional: '{"set":"value", "fromDate":"date", "untilDate":"date"}' """ try: serializer = ListRecordsSerializer(data=request.DATA) if serializer.is_valid(): url = request.DATA['url'] metadataPrefix = request.DATA.get('metadataprefix', None) set_h = request.DATA.get('set', None) fromDate = request.DATA.get('fromDate', None) untilDate = request.DATA.get('untilDate', None) resumptionToken = request.DATA.get('resumptionToken', None) http_response, token = getListRecords_model(url=url, metadataPrefix=metadataPrefix, resumptionToken=resumptionToken, set_h=set_h, fromDate=fromDate, untilDate=untilDate) if http_response.status_code == status.HTTP_200_OK: rtn = http_response.data #Else, we return a bad request response with the message provided by the API else: content = http_response.data[APIMessage.label] raise OAIAPILabelledException(message=content, status=http_response.status_code) serializer = RecordSerializer(rtn) return Response(serializer.data, status=status.HTTP_200_OK) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled('An error occurred when attempting to identify resource: %s'%e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def listIdentifiers(request): """ POST http://localhost/oai_pmh/api/listidentifiers POST data query='{"url":"value", "metadataprefix":"value"}' optional {"set":"value"} """ try: serializer = RegistryURLSerializer(data=request.DATA) if serializer.is_valid(): url = request.DATA['url'] metadataprefix = request.DATA['metadataprefix'] setH = request.DATA.get('set', None) sickle = Sickle(url) rsp = sickle.ListIdentifiers(metadataPrefix=metadataprefix, set=setH) rtn = [] try: while True: rtn.append( dict(rsp.next()) ) except StopIteration: pass serializer = ListIdentifierSerializer(rtn) return Response(serializer.data, status=status.HTTP_200_OK) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled('An error occurred when attempting to identify resource: %s'%e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update_my_set(request): """ PUT http://localhost/oai_pmh/api/update/my-set PUT data query='{"id":"value", "setSpec":"value","setName":"value"}' optional: '{"description":"value", "templates":["id1", "id2"..]}' """ try: #Serialization of the input data serializer = UpdateMySetSerializer(data=request.DATA) #If it's valid if serializer.is_valid(): #We retrieve all information id = request.DATA['id'] setSpec = request.DATA['setSpec'] setName = request.DATA['setName'] description = request.DATA.get('description', None) templates = request.DATA.get('templates', None) return update_my_set_model(id, setSpec, setName, templates, description) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def delete_my_set(request): """ POST http://localhost/oai_pmh/api/delete/my-set POST data query='{"set_id":"value"}' """ try: serializer = DeleteMySetSerializer(data=request.DATA) if serializer.is_valid(): id = request.DATA['set_id'] return delete_my_set_model(id) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def objectIdentify(request): """ POST http://localhost/oai_pmh/api/objectidentify POST data query='{"url":"value"}' """ try: serializer = IdentifySerializer(data=request.DATA) if serializer.is_valid(): url = request.DATA['url'] req = objectIdentifyByURL(url) return Response(req.data, status=req.status_code) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception: content = APIMessage.getMessageLabelled('An error occurred when attempting to identify resource') return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def add_my_metadataFormat(request): """ POST http://localhost/oai_pmh/api/add/my-metadataformat POST data query='{"metadataPrefix":"value", "schema":"schemaURL"}' """ try: serializer = MyMetadataFormatSerializer(data=request.DATA) if serializer.is_valid(): metadataprefix = request.DATA['metadataPrefix'] schema = request.DATA['schema'] return add_my_metadataFormat_model(metadataprefix, schema) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception, e: content = APIMessage.getMessageLabelled('Unable to add the new metadata format. \n%s'%e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def harvest(request): """ POST http://localhost/oai_pmh/api/harvest POST data query='{"registry_id":"value"}' """ #List of errors allErrors = [] try: serializer = HarvestSerializer(data=request.DATA) if serializer.is_valid(): registry_id = request.DATA['registry_id'] return harvest_model(registry_id) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled('An error occurred during the harvest process: %s'%e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def upload_oai_pmh_xslt(request): """ POST http://localhost/oai_pmh/api/upload/xslt POST data query='{"name": ""value, "filename": "value", "content": "value"} """ try: serializer = OaiXSLTSerializer(data=request.DATA) if serializer.is_valid(): name = request.DATA['name'] filename = request.DATA['filename'] xmlStr = request.DATA['content'] return upload_oai_pmh_xslt_model(name, filename, xmlStr) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def edit_oai_pmh_xslt(request): """ POST http://localhost/oai_pmh/api/edit/xslt POST data query='{"xslt_id":"value", "name": "value"}' """ try: serializer = EditXSLTSerializer(data=request.DATA) if serializer.is_valid(): #Get the ID id = request.DATA['xslt_id'] new_name = request.DATA['name'] return edit_oai_pmh_xslt_model(id, new_name) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update_my_metadataFormat(request): """ PUT http://localhost/oai_pmh/api/update/my-metadataFormat PUT data query='{"id":"value", "metadataPrefix":"value"}' """ try: serializer = UpdateMyMetadataFormatSerializer(data=request.DATA) if serializer.is_valid(): #We retrieve all information id = request.DATA['id'] metadataprefix = request.DATA['metadataPrefix'] return update_my_metadataFormat_model(id, metadataprefix) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def add_my_template_metadataFormat(request): """ POST http://localhost/oai_pmh/api/add/my-template-metadataformat POST data query='{"metadataPrefix":"value", "template":"templateID"}' """ try: #Serialization of the input data serializer = MyTemplateMetadataFormatSerializer(data=request.DATA) #If it's valid if serializer.is_valid(): metadataprefix = request.DATA['metadataPrefix'] template = request.DATA['template'] return add_my_template_metadataFormat_model(metadataprefix, template) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception, e: content = APIMessage.getMessageLabelled('Unable to add the new metadata format. \n%s'%e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update_my_registry(request): """ PUT http://localhost/oai_pmh/api/update/my-registry PUT data query='{"repositoryName":"value", "enableHarvesting":"True or False"}' """ try: #Serialization of the input data serializer = UpdateMyRegistrySerializer(data=request.DATA) #If it's valid if serializer.is_valid(): repositoryName = request.DATA['repositoryName'] enableHarvesting = request.DATA['enableHarvesting'] == 'True' return update_my_registry_model(repositoryName, enableHarvesting) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update_registry_info(request): """ PUT http://localhost/oai_pmh/api/update/registry-info PUT data query='{"registry_id":"value"}' id: string """ try: #Serialization of the input data serializer = UpdateRegistryInfo(data=request.DATA) if serializer.is_valid(): registry_id = request.DATA['registry_id'] return update_registry_info_model(registry_id) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled('Unable to update the registry information.') return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def reactivate_registry(request): """ POST http://localhost/oai_pmh/api/reactivate/registry POST data query='{"RegistryId":"value"}' """ try: #Serialization of the input data serializer = RegistryIdSerializer(data=request.DATA) #If it's valid if serializer.is_valid(): #Get the ID id = request.DATA['RegistryId'] return reactivate_registry_model(id) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def add_registry(request): """ POST http://localhost/oai_pmh/api/add/registry POST data query='{"url":"value","harvestrate":"number", "harvest":"True or False"}' """ try: #Serialization of the input data serializer = AddRegistrySerializer(data=request.DATA) #If all fields are okay if serializer.is_valid(): #Check the URL url = request.DATA['url'] harvestrate = request.DATA['harvestrate'] harvest = request.DATA['harvest'] == 'True' return add_registry_model(url, harvestrate, harvest) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def oai_pmh_conf_xslt(request): """ POST http://localhost/oai_pmh/api/oai-pmh-conf/xslt POST data query='{"template_id": "value", "my_metadata_format_id": "value", "xslt_id": "value", "activated": "value"}' """ try: serializer = OaiConfXSLTSerializer(data=request.DATA) if serializer.is_valid(): template_id = request.DATA['template_id'] my_metadata_format_id = request.DATA['my_metadata_format_id'] xslt_id = None if 'xslt_id' in request.DATA: xslt_id = request.DATA['xslt_id'] activated = request.DATA['activated'] == "True" return oai_pmh_conf_xslt_model(template_id, my_metadata_format_id, xslt_id, activated) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def add_my_set(request): """ PUT http://localhost/oai_pmh/api/add/my-set PUT data query='{"setSpec":"value", "setName":"value", templates:["id1", "id2"..]}' optional: {'description':'value'}" """ try: #Serialization of the input data serializer = MySetSerializer(data=request.DATA) #If it's valid if serializer.is_valid(): setSpec = request.DATA['setSpec'] setName = request.DATA['setName'] description = request.DATA.get('description', None) templates = request.DATA.getlist('templates') return add_my_set_model(setSpec, setName, templates, description) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update_registry(request): """ PUT http://localhost/oai_pmh/api/update/registry PUT data query='{"id":"value", "harvestrate":"value", "harvest":"True or False"}' id: string """ try: #Serialization of the input data serializer = UpdateRegistrySerializer(data=request.DATA) #If it's valid if serializer.is_valid(): #We retrieve all information id = request.DATA['id'] harvestrate = request.DATA['harvestrate'] harvest = request.DATA['harvest'] == 'True' return update_registry_model(id, harvestrate, harvest) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled(e.message) return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update_registry_harvest(request): """ PUT http://localhost/oai_pmh/api/update/registry-harvest PUT data query='{"id":"value", "metadataFormats":["id1", "id2"..], "sets":["id1", "id2"..]}' id: string """ try: #Serialization of the input data serializer = UpdateRegistryHarvestSerializer(data=request.DATA) #If it's valid if serializer.is_valid(): id = request.DATA['id'] sets = request.DATA.get('sets') metadataFormats = request.DATA.get('metadataFormats') return update_registry_harvest_model(id, sets, metadataFormats) else: raise OAIAPISerializeLabelledException(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) except OAIAPIException as e: return e.response() except Exception as e: content = APIMessage.getMessageLabelled('Unable to update the harvest configuration for the registry.') return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)