def get_last_modified_date(self, element): try: date = datestamp.datetime_to_datestamp(element['oai_datestamp']) except: date = datestamp.datetime_to_datestamp(datetime.datetime.min) return date
def get_last_modified_date(self, element): try: date = datestamp.datetime_to_datestamp(element['oai_datestamp']) except: date = datestamp.datetime_to_datestamp(datetime.datetime.min) return date
def render_to_response(self, context, **response_kwargs): # all OAI responses should be XML if 'content_type' not in response_kwargs: response_kwargs['content_type'] = self.content_type # add common context data needed for all responses context.update({ 'now': datestamp.datetime_to_datestamp(datetime.datetime.now()), 'verb': self.oai_verb, 'identifier': self.identifier if hasattr(self, 'identifier') else None, 'metadataPrefix': self.metadataPrefix if hasattr(self, 'metadataPrefix') else None, 'url': self.request.build_absolute_uri(self.request.path), 'from': self.From if hasattr(self, 'From') else None, 'until': self.until if hasattr(self, 'until') else None, 'set': self.set if hasattr(self, 'set') else None, }) #Render the template with the context information return super(TemplateView, self) \ .render_to_response(context, **response_kwargs)
def get_record(self): try: #Bool if we need to transform the XML via XSLT hasToBeTransformed = False #Check if the identifier pattern is OK id = self.check_identifier() #Template name self.template_name = 'oai_pmh/xml/get_record.xml' query = dict() #Convert id to ObjectId try: query['_id'] = ObjectId(id) #The record has to be published query['ispublished'] = True except Exception: raise idDoesNotExist(self.identifier) data = XMLdata.executeQueryFullResult(query) #This id doesn't exist if len(data) == 0: raise idDoesNotExist(self.identifier) data = data[0] #Get the template for the identifier template = data['schema'] #Retrieve sets for this template sets = OaiMySet.objects(templates=template).all() #Retrieve the XSLT for the transformation try: #Get the metadataformat for the provided prefix myMetadataFormat = OaiMyMetadataFormat.objects.get(metadataPrefix=self.metadataPrefix) #If this metadata prefix is not associated to a template, we need to retrieve the XSLT to do the transformation if not myMetadataFormat.isTemplate: hasToBeTransformed = True #Get information about the XSLT for the MF and the template objTempMfXslt = OaiTemplMfXslt.objects(myMetadataFormat=myMetadataFormat, template=template, activated=True).get() #If no information or desactivated if not objTempMfXslt.xslt: raise cannotDisseminateFormat(self.metadataPrefix) else: #Get the XSLT for the transformation xslt = objTempMfXslt.xslt except: raise cannotDisseminateFormat(self.metadataPrefix) #Transform XML data dataToTransform = [{'title': data['_id'], 'content': self.cleanXML(xmltodict.unparse(data['content']))}] if hasToBeTransformed: dataXML = self.getXMLTranformXSLT(dataToTransform, xslt) else: dataXML = dataToTransform #Fill the response record_info = { 'identifier': self.identifier, 'last_modified': datestamp.datetime_to_datestamp(data['publicationdate']) if 'publicationdate' in data else datestamp.datetime_to_datestamp(datetime.datetime.min), 'sets': sets, 'XML': dataXML[0]['content'] } return self.render_to_response(record_info) except OAIExceptions, e: return self.errors(e.errors)
def get_earliest_date(self): try: #Get the earliest publication date for the identify request response data = XMLdata.getMinValue('oai_datestamp') #If we have a date if data != None: return datestamp.datetime_to_datestamp(data) else: return '' except Exception: return ''
def get_earliest_date(self): try: #Get the earliest publication date for the identify request response data = XMLdata.getMinValue('publicationdate') #If we have a date if data != None: return datestamp.datetime_to_datestamp(data) else: return '' except Exception: return ''
def list_identifiers(self): try: #Template name self.template_name = 'oai_pmh/xml/list_identifiers.xml' query = dict() items=[] #Handle FROM and UNTIL query = self.check_dates() try: #Get the metadata format thanks to the prefix myMetadataFormat = OaiMyMetadataFormat.objects.get(metadataPrefix=self.metadataPrefix) #Get all template using it (activated True) templates = OaiTemplMfXslt.objects(myMetadataFormat=myMetadataFormat, activated=True).distinct(field="template") #Ids templatesID = [str(x.id) for x in templates] #If myMetadataFormat is linked to a template, we add the template id if myMetadataFormat.isTemplate: templatesID.append(str(myMetadataFormat.template.id)) except: #The metadata format doesn't exist raise cannotDisseminateFormat(self.metadataPrefix) if self.set: try: setsTemplates = OaiMySet.objects(setSpec=self.set).only('templates').get() templatesID = set(templatesID).intersection([str(x.id) for x in setsTemplates.templates]) except Exception, e: raise noRecordsMatch for template in templatesID: #Retrieve sets for this template sets = OaiMySet.objects(templates=template).all() query['schema'] = template #The record has to be published query['ispublished'] = True #Get all records for this template data = XMLdata.executeQueryFullResult(query) #IF no records, go to the next template if len(data) == 0: continue for i in data: #Fill the response identifier = '%s:%s:id/%s' % (settings.OAI_SCHEME, settings.OAI_REPO_IDENTIFIER, str(i['_id'])) item_info = { 'identifier': identifier, 'last_modified': datestamp.datetime_to_datestamp(i['publicationdate']) if 'publicationdate' in i else datestamp.datetime_to_datestamp(datetime.datetime.min), 'sets': sets } items.append(item_info) #If there is no records if len(items) == 0: raise noRecordsMatch return self.render_to_response({'items': items})
def render_to_response(self, context, **response_kwargs): # all OAI responses should be XML if 'content_type' not in response_kwargs: response_kwargs['content_type'] = self.content_type # add common context data needed for all responses context.update({ 'now': datestamp.datetime_to_datestamp(datetime.datetime.now()), 'verb': self.oai_verb, 'identifier': self.identifier if hasattr(self, 'identifier') else None, 'metadataPrefix': self.metadataPrefix if hasattr(self, 'metadataPrefix') else None, 'url': self.request.build_absolute_uri(self.request.path), 'from': self.From if hasattr(self, 'From') else None, 'until': self.until if hasattr(self, 'until') else None, 'set': self.set if hasattr(self, 'set') else None, }) #Render the template with the context information return super(TemplateView, self) \ .render_to_response(context, **response_kwargs)
def get_record(self): try: #Bool if we need to transform the XML via XSLT hasToBeTransformed = False #Check if the identifier pattern is OK id = self.check_identifier() #Template name self.template_name = 'oai_pmh/xml/get_record.xml' query = dict() #Convert id to ObjectId try: query['_id'] = ObjectId(id) #The record has to be published query['ispublished'] = True except Exception: raise idDoesNotExist(self.identifier) data = XMLdata.executeQueryFullResult(query) #This id doesn't exist if len(data) == 0: raise idDoesNotExist(self.identifier) data = data[0] #Get the template for the identifier template = data['schema'] #Retrieve sets for this template sets = OaiMySet.objects(templates=template).all() #Retrieve the XSLT for the transformation try: #Get the metadataformat for the provided prefix myMetadataFormat = OaiMyMetadataFormat.objects.get( metadataPrefix=self.metadataPrefix) #If this metadata prefix is not associated to a template, we need to retrieve the XSLT to do the transformation if not myMetadataFormat.isTemplate: hasToBeTransformed = True #Get information about the XSLT for the MF and the template objTempMfXslt = OaiTemplMfXslt.objects( myMetadataFormat=myMetadataFormat, template=template, activated=True).get() #If no information or desactivated if not objTempMfXslt.xslt: raise cannotDisseminateFormat(self.metadataPrefix) else: #Get the XSLT for the transformation xslt = objTempMfXslt.xslt except: raise cannotDisseminateFormat(self.metadataPrefix) #Transform XML data dataToTransform = [{ 'title': data['_id'], 'content': self.cleanXML(xmltodict.unparse(data['content'])) }] if hasToBeTransformed: dataXML = self.getXMLTranformXSLT(dataToTransform, xslt) else: dataXML = dataToTransform #Fill the response record_info = { 'identifier': self.identifier, 'last_modified': datestamp.datetime_to_datestamp(data['publicationdate']) if 'publicationdate' in data else datestamp.datetime_to_datestamp(datetime.datetime.min), 'sets': sets, 'XML': dataXML[0]['content'] } return self.render_to_response(record_info) except OAIExceptions, e: return self.errors(e.errors)
def list_identifiers(self): try: #Template name self.template_name = 'oai_pmh/xml/list_identifiers.xml' query = dict() items = [] #Handle FROM and UNTIL query = self.check_dates() try: #Get the metadata format thanks to the prefix myMetadataFormat = OaiMyMetadataFormat.objects.get( metadataPrefix=self.metadataPrefix) #Get all template using it (activated True) templates = OaiTemplMfXslt.objects( myMetadataFormat=myMetadataFormat, activated=True).distinct(field="template") #Ids templatesID = [str(x.id) for x in templates] #If myMetadataFormat is linked to a template, we add the template id if myMetadataFormat.isTemplate: templatesID.append(str(myMetadataFormat.template.id)) except: #The metadata format doesn't exist raise cannotDisseminateFormat(self.metadataPrefix) if self.set: try: setsTemplates = OaiMySet.objects( setSpec=self.set).only('templates').get() templatesID = set(templatesID).intersection( [str(x.id) for x in setsTemplates.templates]) except Exception, e: raise noRecordsMatch for template in templatesID: #Retrieve sets for this template sets = OaiMySet.objects(templates=template).all() query['schema'] = template #The record has to be published query['ispublished'] = True #Get all records for this template data = XMLdata.executeQueryFullResult(query) #IF no records, go to the next template if len(data) == 0: continue for i in data: #Fill the response identifier = '%s:%s:id/%s' % (settings.OAI_SCHEME, settings.OAI_REPO_IDENTIFIER, str(i['_id'])) item_info = { 'identifier': identifier, 'last_modified': datestamp.datetime_to_datestamp(i['publicationdate']) if 'publicationdate' in i else datestamp.datetime_to_datestamp(datetime.datetime.min), 'sets': sets } items.append(item_info) #If there is no records if len(items) == 0: raise noRecordsMatch return self.render_to_response({'items': items})
#Get the XSLT file xslt = objTempMfXslt(template=template).get().xslt #Transform all XML data (1 call) dataXML = self.getXMLTranformXSLT(dataToTransform, xslt) #Add each record for elt in data: identifier = '%s:%s:id/%s' % (settings.OAI_SCHEME, settings.OAI_REPO_IDENTIFIER, elt['_id']) xmlStr = filter(lambda xml: xml['title'] == elt['_id'], dataXML)[0] record_info = { 'identifier': identifier, 'last_modified': datestamp.datetime_to_datestamp(elt['publicationdate']) if 'publicationdate' in elt else datestamp.datetime_to_datestamp(datetime.datetime.min), 'sets': sets, 'XML': xmlStr['content'] } items.append(record_info) #If there is no records if len(items) == 0: raise noRecordsMatch return self.render_to_response({'items': items}) except OAIExceptions, e:
if myMetadataFormat.isTemplate: #No transformation needed dataXML = dataToTransform else: #Get the XSLT file xslt = objTempMfXslt(template=template).get().xslt #Transform all XML data (1 call) dataXML = self.getXMLTranformXSLT(dataToTransform, xslt) #Add each record for elt in data: identifier = '%s:%s:id/%s' % (settings.OAI_SCHEME, settings.OAI_REPO_IDENTIFIER, elt['_id']) xmlStr = filter(lambda xml: xml['title'] == elt['_id'], dataXML)[0] record_info = { 'identifier': identifier, 'last_modified': datestamp.datetime_to_datestamp(elt['publicationdate']) if 'publicationdate' in elt else datestamp.datetime_to_datestamp(datetime.datetime.min), 'sets': sets, 'XML': xmlStr['content'] } items.append(record_info) #If there is no records if len(items) == 0: raise noRecordsMatch return self.render_to_response({'items': items}) except OAIExceptions, e: return self.errors(e.errors) except OAIException, e: return self.error(e) except Exception, e: