def list_records(self):
     try:
         items = []
         #Template name
         self.template_name = 'oai_pmh/xml/list_records.xml'
         query = dict()
         #Handle FROM and UNTIL
         query = self.check_dates()
         #Get the metadataformat for the provided prefix
         try:
             myMetadataFormat = OaiMyMetadataFormat.objects.get(metadataPrefix=self.metadataPrefix)
         except Exception, e:
             raise cannotDisseminateFormat(self.metadataPrefix)
         if myMetadataFormat.isTemplate:
             #GEt the corresponding template
             templatesID = [str(myMetadataFormat.template.id)]
         else:
             #Get information about templates using this MF
             objTempMfXslt = OaiTemplMfXslt.objects(myMetadataFormat=myMetadataFormat, activated=True).all()
              #Ids
             templatesID = [str(x.template.id) for x in objTempMfXslt]
         #if a set was provided, we filter the templates
         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
Beispiel #2
0
 def list_records(self):
     try:
         items = []
         #Template name
         self.template_name = 'oai_pmh/xml/list_records.xml'
         query = dict()
         #Handle FROM and UNTIL
         query = self.check_dates()
         #Get the metadataformat for the provided prefix
         try:
             myMetadataFormat = OaiMyMetadataFormat.objects.get(metadataPrefix=self.metadataPrefix)
         except Exception, e:
             raise cannotDisseminateFormat(self.metadataPrefix)
         if myMetadataFormat.isTemplate:
             #GEt the corresponding template
             templatesID = [str(myMetadataFormat.template.id)]
         else:
             #Get information about templates using this MF
             objTempMfXslt = OaiTemplMfXslt.objects(myMetadataFormat=myMetadataFormat, activated=True).all()
              #Ids
             templatesID = [str(x.template.id) for x in objTempMfXslt]
         #if a set was provided, we filter the templates
         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
    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': self.get_last_modified_date(i),
                        'sets': sets,
                        'deleted': i.get('status', '') == Status.DELETED
                    }
                    items.append(item_info)
            #If there is no records
            if len(items) == 0:
                raise noRecordsMatch

            return self.render_to_response({'items': items})
    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': self.get_last_modified_date(i),
                        'sets': sets,
                        'deleted': i.get('status', '') == Status.DELETED
                    }
                    items.append(item_info)
            #If there is no records
            if len(items) == 0:
                raise noRecordsMatch

            return self.render_to_response({'items': items})
Beispiel #6
0
    def list_metadata_formats(self):
        try:
            #Template name
            self.template_name = 'oai_pmh/xml/list_metadata_formats.xml'
            items = []
            # If an identifier is provided, with look for its metadataformats
            if self.identifier != None:
                id = self.check_identifier()
                #We retrieve the template id for this record
                listId = []
                listId.append(id)
                listSchemaIds = XMLdata.getByIDsAndDistinctBy(listId, 'schema')
                if len(listSchemaIds) == 0:
                    raise idDoesNotExist(self.identifier)
                #Get metadata formats information for this template. The metadata formats must be activated
                metadataFormats = OaiTemplMfXslt.objects(
                    template__in=listSchemaIds,
                    activated=True).distinct(field='myMetadataFormat')
                #Get the template metadata format if existing
                metadataFormatsTemplate = OaiMyMetadataFormat.objects(
                    template__in=listSchemaIds, isTemplate=True).all()
                if len(metadataFormatsTemplate) != 0:
                    metadataFormats.extend(metadataFormatsTemplate)
            else:
                #No identifier provided. We return all metadata formats available
                metadataFormats = OaiMyMetadataFormat.objects().all()
            #If there is no metadata formats, we raise noMetadataFormat
            if len(metadataFormats) == 0:
                raise noMetadataFormat
            else:
                #Fill the response
                for metadataFormat in metadataFormats:
                    item_info = {
                        'metadataNamespace': metadataFormat.metadataNamespace,
                        'metadataPrefix': metadataFormat.metadataPrefix,
                        'schema': metadataFormat.schema
                    }
                    items.append(item_info)

            return self.render_to_response({'items': items})
        except OAIExceptions, e:
            return self.errors(e.errors)
    def list_metadata_formats(self):
        try:
            #Template name
            self.template_name = 'oai_pmh/xml/list_metadata_formats.xml'
            items = []
            # If an identifier is provided, with look for its metadataformats
            if self.identifier != None:
                id = self.check_identifier()
                #We retrieve the template id for this record
                listId = []
                listId.append(id)
                listSchemaIds = XMLdata.getByIDsAndDistinctBy(listId, 'schema')
                if len(listSchemaIds) == 0:
                    raise idDoesNotExist(self.identifier)
                #Get metadata formats information for this template. The metadata formats must be activated
                metadataFormats = OaiTemplMfXslt.objects(template__in=listSchemaIds, activated=True).distinct(field='myMetadataFormat')
                #Get the template metadata format if existing
                metadataFormatsTemplate = OaiMyMetadataFormat.objects(template__in=listSchemaIds, isTemplate=True).all()
                if len(metadataFormatsTemplate) != 0:
                    metadataFormats.extend(metadataFormatsTemplate)
            else:
                #No identifier provided. We return all metadata formats available
                metadataFormats = OaiMyMetadataFormat.objects().all()
            #If there is no metadata formats, we raise noMetadataFormat
            if len(metadataFormats) == 0:
                raise noMetadataFormat
            else:
                #Fill the response
                for metadataFormat in metadataFormats:
                    item_info = {
                        'metadataNamespace': metadataFormat.metadataNamespace,
                        'metadataPrefix':  metadataFormat.metadataPrefix,
                        'schema':  metadataFormat.schema
                    }
                    items.append(item_info)

            return self.render_to_response({'items': items})
        except OAIExceptions, e:
            return self.errors(e.errors)
Beispiel #8
0
    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, includeDeleted=True)
            #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['title'], 'content': self.cleanXML(data['xml_file'])}]
            if hasToBeTransformed:
                dataXML = self.getXMLTranformXSLT(dataToTransform, xslt)
            else:
                dataXML = dataToTransform

            #Fill the response
            record_info = {
                'identifier': self.identifier,
                'last_modified': self.get_last_modified_date(data),
                'sets': sets,
                'XML': dataXML[0]['content'],
                'deleted': data.get('status', '') == Status.DELETED
            }
            return self.render_to_response(record_info)
        except OAIExceptions, e:
            return self.errors(e.errors)
 def dump_oai_templ_mf_xslt(self):
     self.assertEquals(len(OaiTemplMfXslt.objects()), 0)
     self.restoreDump(join(DUMP_OAI_PMH_TEST_PATH, 'oai_templ_mf_xslt.bson'), 'oai_templ_mf_xslt')
     self.assertTrue(len(OaiTemplMfXslt.objects()) > 0)
Beispiel #10
0
 def dump_oai_templ_mf_xslt(self):
     self.assertEquals(len(OaiTemplMfXslt.objects()), 0)
     self.restoreDump(
         join(DUMP_OAI_PMH_TEST_PATH, 'oai_templ_mf_xslt.bson'),
         'oai_templ_mf_xslt')
     self.assertTrue(len(OaiTemplMfXslt.objects()) > 0)
def oai_pmh_conf_xslt(request):
    if request.method == 'POST':
        try:
            errors = []
            AssociateFormSet = formset_factory(AssociateXSLT, extra=3)
            article_formset = AssociateFormSet(request.POST)
            if article_formset.is_valid():
                for f in article_formset:
                    cd = f.cleaned_data
                    myMetadataFormat_id = cd.get('oai_my_mf_id')
                    template_id = cd.get('template_id')
                    activated = cd.get('activated')
                    xslt = cd.get('oai_pmh_xslt_file')
                    xslt_id = None
                    if xslt:
                        xslt_id = xslt.id
                    req = oai_pmh_conf_xslt_model(template_id, myMetadataFormat_id, xslt_id, activated)
                    #If sth wrong happened, we keep the error
                    if req.status_code != status.HTTP_200_OK:
                        data = req.data
                        errors.append(data[APIMessage.label])
                #Good treatment
                if len(errors) == 0:
                    messages.add_message(request, messages.INFO, 'XSLT edited with success.')
                    return HttpResponse(json.dumps({}), content_type='application/javascript')
                #Else, we return a bad request response with the message provided by the API
                else:
                    return HttpResponseBadRequest(errors)
            else:
                return HttpResponseBadRequest([x['__all__'] for x in article_formset.errors if '__all__' in x],
                                              content_type='application/javascript')
        except OAIAPIException as e:
            return HttpResponseBadRequest(e.message)
        except Exception:
            return HttpResponseBadRequest('An error occurred. Please contact your administrator.')
    else:
        template = loader.get_template('oai_pmh/admin/oai_pmh_conf_xslt.html')
        template_id = request.GET.get('id', None)
        if template_id is not None:
            templateName = Template.objects.only('title').get(pk=template_id).title
            allXsltFiles = OaiXslt.objects.all()
            myMetadataFormats = OaiMyMetadataFormat.objects(isTemplate=False or None).all()

            infos= dict()
            for myMetadataFormat in myMetadataFormats:
                try:
                    obj = OaiTemplMfXslt.objects(template=template_id, myMetadataFormat=myMetadataFormat).get()
                    infos[myMetadataFormat] = {'oai_pmh_xslt_file': obj.xslt.id if obj.xslt else None,
                                               'activated': obj.activated}
                except:
                    infos[myMetadataFormat] = {'oai_pmh_xslt_file': None, 'activated': False}

            AssociateFormSet = formset_factory(AssociateXSLT, extra=0)
            init = [{'template_id': template_id, 'oai_my_mf_id': x.id, 'oai_name': x.metadataPrefix,
                     'oai_pmh_xslt_file': infos[x]['oai_pmh_xslt_file'], 'activated': infos[x]['activated']}
                    for x in myMetadataFormats]
            formset = AssociateFormSet(initial=init)

            context = RequestContext(request,{
                'metadataFormats': myMetadataFormats,
                'xsltFiles': allXsltFiles,
                'formSet': formset,
                'templateName': templateName
            })

            return HttpResponse(template.render(context))