def oai_pmh_my_infos(request):
    #Template name
    template = loader.get_template('oai_pmh/admin/oai_pmh_my_infos.html')
    #Get settings information in database
    information = OaiSettings.objects.get()
    if information:
        name = information.repositoryName
        repoIdentifier = information.repositoryIdentifier
        enableHarvesting = information.enableHarvesting
    else:
        name = settings.OAI_NAME
        repoIdentifier = settings.OAI_REPO_IDENTIFIER
        enableHarvesting = False
    #Fill the information
    data_provider = {
            'name': name,
            'baseURL': settings.OAI_HOST_URI + "/oai_pmh/server/",
            'protocole_version': settings.OAI_PROTOCOLE_VERSION,
            'admins': (email for name, email in settings.OAI_ADMINS),
            # 'earliest_date': self.getEarliestDate(),   # placeholder
            'deleted': settings.OAI_DELETED_RECORD,
            'granularity': settings.OAI_GRANULARITY,
            'identifier_scheme': settings.OAI_SCHEME,
            'repository_identifier': repoIdentifier,
            'identifier_delimiter': settings.OAI_DELIMITER,
            'sample_identifier': settings.OAI_SAMPLE_IDENTIFIER,
            'enable_harvesting': enableHarvesting,
        }
    #Form to manage MF
    metadataformat_form = MyMetadataFormatForm()
    #Form to manage template MF
    template_metadataformat_form = MyTemplateMetadataFormatForm()
    #Form to manage set
    set_form = MySetForm()
    #Get my server's Set
    sets = OaiMySet.objects.all()
    #Get my server's MF
    metadataFormats = OaiMyMetadataFormat.objects(isDefault=False, isTemplate=False or None).all()
    #Get my server's default MF
    defaultMetadataFormats = OaiMyMetadataFormat.objects(isDefault=True).all()
    #Get my server's template MF
    templateMetadataFormats = OaiMyMetadataFormat.objects(isTemplate=True).all()
    context = RequestContext(request, {
        'data_provider': data_provider,
        'metadataformat_form': metadataformat_form,
        'template_metadataformat_form' : template_metadataformat_form,
        'metadataFormats': metadataFormats,
        'set_form': set_form,
        'sets': sets,
        'defaultMetadataFormats': defaultMetadataFormats,
        'templateMetadataFormats': templateMetadataFormats
    })

    return HttpResponse(template.render(context))
 def test_list_metadataformat_no_identifier(self):
     self.dump_oai_my_metadata_format()
     data = {'verb': 'ListMetadataFormats'}
     r = self.doRequestServer(data=data)
     self.isStatusOK(r.status_code)
     self.checkTagExist(r.text, 'ListMetadataFormats')
     self.checkTagCount(r.text, 'metadataFormat', len(OaiMyMetadataFormat.objects().all()))
 def test_list_metadataformat_no_identifier(self):
     self.dump_oai_my_metadata_format()
     data = {'verb': 'ListMetadataFormats'}
     r = self.doRequestServer(data=data)
     self.isStatusOK(r.status_code)
     self.checkTagExist(r.text, 'ListMetadataFormats')
     self.checkTagCount(r.text, 'metadataFormat', len(OaiMyMetadataFormat.objects().all()))
Example #4
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)
Example #6
0
def load_metadata_prefixes():
    """
    Load default metadata prefixes for OAI-PMH
    """
    metadataPrefixes = OaiMyMetadataFormat.objects.all()
    if len(metadataPrefixes) == 0:
        #Add OAI_DC metadata prefix
        schemaURL = "http://www.openarchives.org/OAI/2.0/oai_dc.xsd"
        file = open(os.path.join(SITE_ROOT, 'oai_pmh', 'resources', 'xsd', 'oai_dc.xsd'),'r')
        xsdContent = file.read()
        dom = etree.fromstring(xsdContent.encode('utf-8'))
        if 'targetNamespace' in dom.find(".").attrib:
            metadataNamespace = dom.find(".").attrib['targetNamespace'] or "namespace"
        else:
            metadataNamespace = "http://www.w3.org/2001/XMLSchema"
        OaiMyMetadataFormat(metadataPrefix='oai_dc',
                            schema=schemaURL,
                            metadataNamespace=metadataNamespace,
                            xmlSchema=xsdContent,
                            isDefault=True).save()
Example #7
0
 def dump_oai_my_metadata_format(self):
     self.assertEquals(len(OaiMyMetadataFormat.objects()), 0)
     self.restoreDump(
         join(DUMP_OAI_PMH_TEST_PATH, 'oai_my_metadata_format.bson'),
         'oai_my_metadata_format')
     self.assertTrue(len(OaiMyMetadataFormat.objects()) > 0)
 def dump_oai_my_metadata_format_bad(self):
     self.assertEquals(len(OaiMyMetadataFormat.objects()), 0)
     self.restoreDump(join(DUMP_OAI_PMH_TEST_PATH, 'oai_my_metadata_format_bad.bson'), 'oai_my_metadata_format')
     self.assertTrue(len(OaiMyMetadataFormat.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))
Example #10
0
def load_metadata_prefixes():
    """
    Load default metadata prefixes for OAI-PMH
    """
    metadataPrefixes = OaiMyMetadataFormat.objects.all()
    if len(metadataPrefixes) == 0:
        #Add OAI_DC metadata prefix
        schemaURL = "http://www.openarchives.org/OAI/2.0/oai_dc.xsd"
        file = open(
            os.path.join(SITE_ROOT, 'oai_pmh', 'resources', 'xsd',
                         'oai_dc.xsd'), 'r')
        xsdContent = file.read()
        dom = etree.fromstring(xsdContent.encode('utf-8'))
        if 'targetNamespace' in dom.find(".").attrib:
            metadataNamespace = dom.find(
                ".").attrib['targetNamespace'] or "namespace"
        else:
            metadataNamespace = "http://www.w3.org/2001/XMLSchema"
        OaiMyMetadataFormat(metadataPrefix='oai_dc',
                            schema=schemaURL,
                            metadataNamespace=metadataNamespace,
                            xmlSchema=xsdContent,
                            isDefault=True).save()
        #Add NMRR templates as metadata prefixes
        templates = {
            'all': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_all'
            },
            'organization': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_org'
            },
            'datacollection': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_datacol'
            },
            'repository': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_repo'
            },
            'projectarchive': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_proj'
            },
            'database': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_database'
            },
            'dataset': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_dataset'
            },
            'service': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_serv'
            },
            'informational': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_info'
            },
            'software': {
                'path': 'res-md.xsd',
                'metadataPrefix': 'oai_soft'
            },
        }

        for template_name, info in templates.iteritems():
            try:
                template = Template.objects.get(title=template_name,
                                                filename=info['path'])
                #Check if the XML is well formed
                try:
                    xml_schema = template.content
                    dom = etree.fromstring(xml_schema.encode('utf-8'))
                    if 'targetNamespace' in dom.find(".").attrib:
                        metadataNamespace = dom.find(
                            ".").attrib['targetNamespace'] or "namespace"
                    else:
                        metadataNamespace = "http://www.w3.org/2001/XMLSchema"
                except XMLSyntaxError:
                    print(
                        'ERROR : Impossible to set the template "%s" as a metadata prefix. The template XML is not well formed.'
                        % template_name)
                #Create a schema URL
                schemaURL = OAI_HOST_URI + reverse('getXSD',
                                                   args=[template.filename])
                #Add in database
                OaiMyMetadataFormat(metadataPrefix=info['metadataPrefix'],
                                    schema=schemaURL,
                                    metadataNamespace=metadataNamespace,
                                    xmlSchema='',
                                    isDefault=False,
                                    isTemplate=True,
                                    template=template).save()
            except Exception, e:
                print(
                    'ERROR : Impossible to set the template "{!s}" as a metadata prefix. {!s}'
                    .format(template_name, e.message))