Example #1
0
    def validate_session(self, roles=None):
        req = request._current_obj()
        user = None
        if req.authorization and req.authorization[0] == "Basic":
            user = self._get_user_from_session({"Authorization": "{0} {1}".format(*req.authorization)})

        return (user, self._check_user_has_roles(user, roles))
Example #2
0
def tmpl_globals():
    """Create and return a dictionary of global variables for all templates.

    This function was adapted from :func:`pylons.templating.pylons_globals`
    to better suite our needs. In particular we inject our own gettext
    functions and get a performance boost from following the translator SOP
    once here instead of on every gettext call.

    """
    conf = config._current_obj()
    g = conf['pylons.app_globals']
    c = tmpl_context._current_obj()
    t = translator._current_obj()
    req = request._current_obj()
    return {
        'config': conf,
        'c': c,
        'tmpl_context': c,
        'g': g,
        'app_globals': g,
        'h': conf['pylons.h'],
        'request': req,
        'settings': req.settings,
        'response': response, # don't eval the SOP because this is rarely used
        'translator': t,
        'ngettext': t.ngettext,
        'ungettext': t.ungettext, # compat with standard pylons_globals()
        '_': t.gettext,
        'N_': N_,
        'XML': XML,
    }
Example #3
0
def tmpl_globals():
    """Create and return a dictionary of global variables for all templates.

    This function was adapted from :func:`pylons.templating.pylons_globals`
    to better suite our needs. In particular we inject our own gettext
    functions and get a performance boost from following the translator SOP
    once here instead of on every gettext call.

    """
    conf = config._current_obj()
    g = conf['pylons.app_globals']
    c = tmpl_context._current_obj()
    t = translator._current_obj()
    return {
        'config': conf,
        'c': c,
        'tmpl_context': c,
        'g': g,
        'app_globals': g,
        'h': conf['pylons.h'],
        'request': request._current_obj(),
        'response': response, # don't eval the SOP because this is rarely used
        'translator': t,
        'ngettext': t.ngettext,
        'ungettext': t.ungettext, # compat with standard pylons_globals()
        '_': t.gettext,
        'N_': N_,
        'XML': XML,
    }
Example #4
0
def tmpl_globals():
    """Create and return a dictionary of global variables for all templates.

    This function was adapted from :func:`pylons.templating.pylons_globals`
    to better suite our needs. In particular we inject our own gettext
    functions and get a performance boost from following the translator SOP
    once here instead of on every gettext call.

    """
    conf = config._current_obj()
    g = conf["pylons.app_globals"]
    c = tmpl_context._current_obj()
    t = translator._current_obj()
    req = request._current_obj()
    return {
        "config": conf,
        "c": c,
        "tmpl_context": c,
        "g": g,
        "app_globals": g,
        "h": conf["pylons.h"],
        "request": req,
        "settings": req.settings,
        "response": response,  # don't eval the SOP because this is rarely used
        "translator": t,
        "ngettext": t.ngettext,
        "ungettext": t.ungettext,  # compat with standard pylons_globals()
        "_": t.gettext,
        "N_": N_,
        "XML": XML,
    }
Example #5
0
    def validate_session(self, roles=None):
        req = request._current_obj()
        user = None
        if req.authorization and req.authorization[0] == "Basic":
            user = self._get_user_from_session(
                {"Authorization": "{0} {1}".format(*req.authorization)})

        return (user, self._check_user_has_roles(user, roles))
Example #6
0
    def wrapper(fn, self, *args, **kwargs):
        if _service_doc:
            sdoc = _service_doc()
            try:
                if "oauth" not in sdoc["service_auth"]["service_authz"]:
                    return fn(self, *args, **kwargs)
            except:
                raise ValueError("Missing service_document for checking if OAUTH access is enabled.")

        if _pre_cond:
            precond = cont = _pre_cond()
        else:
            precond = cont = True

        if precond:
            success = { "status": status.Unknown, "user": None, "parameters": None }
            try:
                success["parameters"], success["user"] = _authobj.check_request(r._current_obj(), _mapper)
                if success["parameters"] is None:
                    success["status"] = status.NoSignature
                else:
                    success["status"] = status.Okay
            except BadOAuthSignature as e:
                success["status"] = status.BadSignature
                success["detail"] = e.message
                cont = False
            except:
                success["status"] = status.Error
                success["detail"] = repr(sys.exc_info())
                log.exception("Caught Exception in authorize")
                cont = False

            sess = session._current_obj()
            sess[_session_key] = success

            # log.error("in wrap:"+repr(sess[_session_key]))
            
            if cont and _roles:
                cont = UserHasRoles(_session_key, _roles)
        
            if _post_cond:
                cont = _post_cond(cont)


        if cont:
            try:
                return fn(self, *args, **kwargs)
            finally:
                pass
        else:
            h = {"WWW-Authenticate": "OAuth realm=\"{0}\"".format(_realm)}
            log.error("Authorization Required")
            res.headers.update(h)
            abort(401, "OAuth Authorization Required", headers=h)
Example #7
0
    def wrap_action(self, action_, *args, **kwargs):
        req = request._current_obj()

        try:
            self.predicate.check_authorization(req.environ)
        except NotAuthorizedError, e:
            reason = unicode(e)
            if req.environ.get('repoze.who.identity'):
                # The user is authenticated.
                code = 403
            else:
                # The user is not authenticated.
                code = 401
            if self.denial_handler:
                response.status = code
                return self.denial_handler(reason)
            abort(code, comment=reason)
Example #8
0
    def wrap_action(self, action_, *args, **kwargs):
        req = request._current_obj()

        try:
            self.predicate.check_authorization(req.environ)
        except NotAuthorizedError, e:
            reason = unicode(e)
            if req.environ.get('repoze.who.identity'):
                # The user is authenticated.
                code = 403
            else:
                # The user is not authenticated.
                code = 401
            if self.denial_handler:
                response.status = code
                return self.denial_handler(reason)
            abort(code, comment=reason)
 def default(self, *args, **kw):
     """
     This method is called whenever a request reaches this controller.
     It prepares the WSGI environment and delegates the request to the
     WSGI app.
     """
     # Push into SCRIPT_NAME the path components that have been consumed,
     from pylons import request
     request = request._current_obj()
     new_req = request.copy()
     to_pop = len(new_req.path_info.strip('/').split('/')) - len(args)
     for i in xrange(to_pop):
         new_req.path_info_pop()
     if not new_req.path_info:
         # Append trailing slash and redirect
         redirect(request.path_info + '/')
     new_req.body_file.seek(0)
     return self.delegate(new_req.environ, request.start_response)
def autocommit(func, *args, **kwargs):
    """Handle database transactions for the decorated controller actions.

    This decorator supports firing callbacks immediately after the
    transaction is committed or rolled back. This is useful when some
    external process needs to be called to process some new data, since
    it should only be called once that data is readable by new transactions.

    .. note:: If your callback makes modifications to the database, you must
        manually handle the transaction, or apply the @autocommit decorator
        to the callback itself.

    On the ingress, two attributes are added to the :class:`webob.Request`:

        ``request.commit_callbacks``
            A list of callback functions that should be called immediately
            after the DBSession has been committed by this decorator.

        ``request.rollback_callbacks``
            A list of callback functions that should be called immediately
            after the DBSession has been rolled back by this decorator.

    On the egress, we determine which callbacks should be called, remove
    the above attributes from the request, and then call the appropriate
    callbacks.

    """
    req = request._current_obj()
    req.commit_callbacks = []
    req.rollback_callbacks = []
    try:
        result = func(*args, **kwargs)
    except HTTPException, e:
        if 200 <= e.code < 400:
            _autocommit_commit(req)
        else:
            _autocommit_rollback(req)
        raise
def autocommit(func, *args, **kwargs):
    """Handle database transactions for the decorated controller actions.

    This decorator supports firing callbacks immediately after the
    transaction is committed or rolled back. This is useful when some
    external process needs to be called to process some new data, since
    it should only be called once that data is readable by new transactions.

    .. note:: If your callback makes modifications to the database, you must
        manually handle the transaction, or apply the @autocommit decorator
        to the callback itself.

    On the ingress, two attributes are added to the :class:`webob.Request`:

        ``request.commit_callbacks``
            A list of callback functions that should be called immediately
            after the DBSession has been committed by this decorator.

        ``request.rollback_callbacks``
            A list of callback functions that should be called immediately
            after the DBSession has been rolled back by this decorator.

    On the egress, we determine which callbacks should be called, remove
    the above attributes from the request, and then call the appropriate
    callbacks.

    """
    req = request._current_obj()
    req.commit_callbacks = []
    req.rollback_callbacks = []
    try:
        result = func(*args, **kwargs)
    except HTTPException, e:
        if 200 <= e.code < 400:
            _autocommit_commit(req)
        else:
            _autocommit_rollback(req)
        raise
    def _render_response(self, controller, response):
        """
        Render response takes the dictionary returned by the
        controller calls the appropriate template engine. It uses
        information off of the decoration object to decide which engine
        and template to use, and removes anything in the exclude_names
        list from the returned dictionary.

        The exclude_names functionality allows you to pass variables to
        some template rendering engines, but not others. This behavior
        is particularly useful for rendering engines like JSON or other
        "web service" style engines which don't use and explicit
        template, or use a totally generic template.

        All of these values are populated into the context object by the
        expose decorator.
        """

        content_type, engine_name, template_name, exclude_names = \
            controller.decoration.lookup_template_engine(request)

        if content_type != CUSTOM_CONTENT_TYPE:
            pylons.response.headers['Content-Type'] = content_type

        # skip all the complicated stuff if we're don't have a response dict
        # to work with.
        if not isinstance(response, dict):
            return response

        # Save these objeccts as locals from the SOP to avoid expensive lookups
        req = request._current_obj()
        tmpl_context = pylons.tmpl_context._current_obj()
        use_legacy_renderer = config.get("use_legacy_renderer", True)

        # what causes this condition?  there are no tests for it.
        if template_name is None:
            return response

        # Prepare the engine, if it's not already been prepared.
        # json is a buffet engine ATM
        if use_legacy_renderer or 'json' == engine_name:
            # get the buffet handler
            buffet = pylons.buffet._current_obj()

            if engine_name not in _configured_engines():
                template_options = dict(config).get('buffet.template_options',
                                                    {})
                buffet.prepare(engine_name, **template_options)
                _configured_engines().add(engine_name)

        # if there is an identity, push it to the pylons template context
        tmpl_context.identity = req.environ.get('repoze.who.identity')

        # set up the tw renderer
        if config.get('use_toscawidgets',
                      True) and engine_name in ('genshi', 'mako'):
            tw.framework.default_view = engine_name

        # Setup the template namespace, removing anything that the user
        # has marked to be excluded.
        namespace = dict(tmpl_context=tmpl_context)
        if isinstance(response, dict):
            namespace.update(response)

        for name in exclude_names:
            namespace.pop(name)

        # If we are in a test request put the namespace where it can be
        # accessed directly
        if req.environ.get('paste.testing'):
            testing_variables = req.environ['paste.testing_variables']
            testing_variables['namespace'] = namespace
            testing_variables['template_name'] = template_name
            testing_variables['exclude_names'] = exclude_names
            testing_variables['controller_output'] = response

        # Render the result.
        if use_legacy_renderer or 'json' == engine_name:
            result = buffet.render(engine_name=engine_name,
                                   template_name=template_name,
                                   include_pylons_variables=False,
                                   namespace=namespace)
        else:
            result = tg_render(template_vars=namespace,
                               template_engine=engine_name,
                               template_name=template_name)

        return result
Example #13
0
 def i18n_index(self):
     obj = request._current_obj()
     locale_list = request.languages
     set_lang(request.languages)
     return unicode(_('basic index page'))
Example #14
0
    def _handleOAIRequest(self, format='html'):
        t_req = request._current_obj()
        t_res = response._current_obj()
        
        o = oaipmh()
        
        def GetRecord(params):
            try:
                from lr.mustache.oaipmh import GetRecord as must_GetRecord
                identifier = params["identifier"]
                if params["by_doc_ID"] == True:
                    docList = [o.get_record(params["identifier"])]
                else:
                    docList = o.get_records_by_resource(params["identifier"])

                doc_idx = 0
                valid_docs = 0
                mustache = must_GetRecord()
                for doc in docList:
                    if doc is not None:
                        doc_idx += 1
                        
                        if "payload_schema" in doc and params["metadataPrefix"] in doc["payload_schema"]:
                            valid_docs += 1
                        
                            if valid_docs == 1:
                                part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                                yield self._returnResponse(part, res=t_res)
                                
                            part = mustache.doc(doc)
                            yield self._returnResponse(part, res=t_res)
                        
                if doc_idx == 0:
                    raise IdDoesNotExistError(params['verb'], req=t_req)
                elif valid_docs == 0:
                    raise CannotDisseminateFormatError(params['verb'], req=t_req)
                else:
                    yield self._returnResponse(mustache.suffix(), res=t_res)
                
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield self._returnResponse(err.xml(e), res=t_res)
            
        
        
        def ListIdentifiers(params):
            try:
                from lr.mustache.oaipmh import ListIdentifiers as must_ListID
                
                doc_index = 0
                mustache = must_ListID()
                for ident in o.list_identifiers(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
                    doc_index += 1
                    log.debug(json.dumps(ident))
                    if doc_index == 1:
#                        self._initRender(params, c, t_req)
#                        part = t_render("/oaipmh-ListIdentifiers-prefix.mako")
                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                        yield self._returnResponse(part, res=t_res)
                    
                    part = mustache.doc(ident)
                    yield part
                
                if doc_index == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                else:
                    yield mustache.suffix()
                    
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield self._returnResponse(err.xml(e), res=t_res)
            except:
                log.exception("Unable to render template")

        
        def ListRecords(params):
            try:
                from lr.mustache.oaipmh import ListRecords as must_ListRec
                
                doc_index = 0
                mustache = must_ListRec()
                for record in o.list_records(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
                    doc_index += 1
                    log.debug(json.dumps(record))
                    if doc_index == 1:
                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                        yield self._returnResponse(part, res=t_res)
                    
                    part = mustache.doc(record)
                    yield self._returnResponse(part, res=t_res)
                    
                
                if doc_index == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                else:
                    yield mustache.suffix()
                
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield self._returnResponse(err.xml(e), res=t_res)
            except:
                log.exception("Unable to render template")
        
        def Identify(params=None):
            body = ""
            try:
                self._initRender(params)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body)
        
        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params)
                
                fmts = o.list_metadata_formats(identity=params["identifier"], by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body)
            except Error as e:
                raise e
#            except Exception as e:
#                raise NoMetadataFormats(params["verb"])
                
        
        def ListSets(params=None):
            raise NoSetHierarchyError(verb)
            
        def NotYetSupported(params=None):
            raise BadVerbError()
        
        
            
        switch = {
                  'GetRecord': GetRecord,
                  'ListRecords': ListRecords,
                  'ListIdentifiers': ListIdentifiers,
                  'Identify': Identify,
                  'ListMetadataFormats': ListMetadataFormats,
                  'ListSets': ListSets
                  }
        try:
            params = self._parseParams()
            
            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params["metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params["request_id"] = params["identifier"]
                
                return HarvestController.harvest(self, params, request.body, params['verb'].lower())
        
            verb = params['verb']
            
            return switch[verb](params)
        except Error as e:
            from lr.mustache.oaipmh import Error as err_stache
            err = err_stache()
            return self._returnResponse(err.xml(e), res=t_res)
    def _handleOAIRequest(self, format='html'):
        t_req = request._current_obj()
        t_res = response._current_obj()
        
        enable_flow_control = False
        fc_id_limit = None
        fc_doc_limit = None
        service_id = None
        serviceDoc = h.getServiceDocument(appConfig['lr.oaipmh.docid'])
        if serviceDoc != None:
            if 'service_id' in serviceDoc:
                service_id = serviceDoc['service_id']
                
            if 'service_data' in serviceDoc:
                serviceData = serviceDoc['service_data']
                if 'flow_control' in serviceData:
                    enable_flow_control = serviceData['flow_control']
                
                if enable_flow_control and 'id_limit' in serviceData:
                    fc_id_limit = serviceData['id_limit']
                elif enable_flow_control:
                    fc_id_limit = 100
                
                if enable_flow_control and 'doc_limit' in serviceData:
                    fc_doc_limit = serviceData['doc_limit']
                elif enable_flow_control:
                    fc_doc_limit = 100
        
        o = oaipmh()
        
        def GetRecord(params):
            try:
                from lr.mustache.oaipmh import GetRecord as must_GetRecord
                identifier = params["identifier"]
                if params["by_doc_ID"] == True:
                    resolver = OAIPMHDocumentResolver()
                    single_doc = o.get_record(params["identifier"])
                    if single_doc is not None: 
                        docList = [resolver.process({ "doc": single_doc })]
                    else:
                        docList = []
                else:
                    docList = o.get_records_by_resource(params["identifier"])

                doc_idx = 0
                valid_docs = 0
                mustache = must_GetRecord()
                for doc in docList:
                    if doc is not None:
                        doc_idx += 1
                        
                        if "payload_schema" in doc and params["metadataPrefix"] in map(lambda x: o_mod.getMetadataPrefix(x), doc["payload_schema"]) and OAIPMHDocumentResolver.PAYLOAD_ERROR not in doc:
                            valid_docs += 1
                        
                            if valid_docs == 1:
                                part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                                yield h.fixUtf8(self._returnResponse(part, res=t_res))
                                
                            part = mustache.doc(doc)
                            yield h.fixUtf8(self._returnResponse(part, res=t_res))
                        
                if doc_idx == 0:
                    raise IdDoesNotExistError(params['verb'], req=t_req)
                elif valid_docs == 0:
                    raise CannotDisseminateFormatError(params['verb'], req=t_req)
                else:
                    yield h.fixUtf8(self._returnResponse(mustache.suffix(), res=t_res))
                
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))

        def ListGeneric(params, showDocs=False, record_limit=None):
            if not showDocs:
                from lr.mustache.oaipmh import ListIdentifiers as must_ListID
                mustache = must_ListID()
            else:
                from lr.mustache.oaipmh import ListRecords as must_ListRec
                mustache = must_ListRec()
                
            try:
                
                doc_index = 0
                err_count = 0
                metadataPrefix=params["metadataPrefix"]
                from_date=params["from"]
                until_date=params["until"]
                doc_err = None
                rendered_init = False
                resumptionToken = None if "resumptionToken" not in params else params['resumptionToken']
                records = o.list_identifiers_or_records(metadataPrefix,
                                                from_date=from_date, 
                                                until_date=until_date, 
                                                rt=resumptionToken, 
                                                fc_limit=record_limit, 
                                                include_docs=showDocs )
                for ident in records:
                    doc_index += 1
                    doc_err = False
                    
                    if OAIPMHDocumentResolver.PAYLOAD_ERROR in ident:
                        err_count += 1
                        doc_err = True
                        log.debug("Payload Error detected, doc_index: {0}, err_count: {1}".format(doc_index, err_count))
                    
                    if doc_index - err_count == 1:
                        rendered_init = True
                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                        yield h.fixUtf8(self._returnResponse(part, res=t_res))

                    if doc_err is False and (record_limit is None or doc_index <= record_limit):
                        part = mustache.doc(ident)
                        yield h.fixUtf8(part)
                    elif enable_flow_control:
                        from lr.lib import resumption_token
                        if doc_index - err_count > 0 and doc_index > record_limit:
                            opts = o.list_opts(metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            token = resumption_token.get_token(serviceid=service_id, from_date=from_date, until_date=until_date, **opts)
                            part = mustache.resumptionToken(token)
                            yield h.fixUtf8(part)
                            break
                        elif doc_index - err_count == 0 and doc_index > record_limit:
                            opts = o.list_opts(metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            payload = resumption_token.get_payload(from_date=from_date, until_date=until_date, **opts)
                            records = o.list_identifiers_or_records(metadataPrefix,
                                                from_date=from_date, 
                                                until_date=until_date, 
                                                rt=payload, 
                                                fc_limit=record_limit, 
                                                include_docs=showDocs )
                            doc_index = 0
                            err_count = 0
                
                if doc_index == 0 and err_count == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                elif (doc_index - err_count) == 0:
                    raise CannotDisseminateFormatError(params['verb'], req=t_req)
                else:
                    if enable_flow_control and doc_index <= record_limit:
                        yield h.fixUtf8(mustache.resumptionToken())
                    yield h.fixUtf8(mustache.suffix())
                    
            except oaipmherrors.Error as e:
                if not rendered_init:
                    from lr.mustache.oaipmh import Error as err_stache
                    err = err_stache()
                    yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))
                else:
                    from lr.mustache.oaipmh import ErrorOnly as err_stache
                    err = err_stache()
                    yield h.fixUtf8(self._returnResponse(err.xml(e)+mustache.suffix(), res=t_res))
            except:
                log.exception("Unknown Error Occurred")

        def ListIdentifiers(params):
            return ListGeneric(params, False, fc_id_limit)
        
        def ListRecords(params):
            return ListGeneric(params, True, fc_doc_limit)
#        def ListRecords(params):
#            try:
#                from lr.mustache.oaipmh import ListRecords as must_ListRec
#                
#                doc_index = 0
#                mustache = must_ListRec()
#                for record in o.list_records(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
#                    doc_index += 1
#                    log.debug(json.dumps(record))
#                    if doc_index == 1:
#                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
#                        yield self._returnResponse(part, res=t_res)
#                    
#                    part = mustache.doc(record)
#                    yield self._returnResponse(part, res=t_res)
#                    
#                
#                if doc_index == 0:
#                    raise NoRecordsMatchError(params['verb'], req=t_req)
#                else:
#                    yield mustache.suffix()
#                
#            except oaipmherrors.Error as e:
#                from lr.mustache.oaipmh import Error as err_stache
#                err = err_stache()
#                yield self._returnResponse(err.xml(e), res=t_res)
#            except:
#                log.exception("Unable to render template")
        
        def Identify(params=None):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body, res=t_res)
        
        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)
                
                fmts = o.list_metadata_formats(identity=params["identifier"], by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body, res=t_res)
            except Error as e:
                raise e
        
        def ListSets(params=None):
            raise NoSetHierarchyError(verb)
            
        def NotYetSupported(params=None):
            raise BadVerbError()
        
        
            
        switch = {
                  'GetRecord': GetRecord,
                  'ListRecords': ListRecords,
                  'ListIdentifiers': ListIdentifiers,
                  'Identify': Identify,
                  'ListMetadataFormats': ListMetadataFormats,
                  'ListSets': ListSets
                  }
        try:
            params = self._parseParams(flow_control=enable_flow_control, serviceid=service_id)
            
            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params["metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params[self.REQUESTID] = params["identifier"]
                if params.has_key("from") and isinstance(params["from"], datetime):
                    params["from"] = h.convertToISO8601Zformat(params["from"])
                if params.has_key("until") and isinstance(params["until"], datetime):
                    params["until"] = h.convertToISO8601Zformat(params["until"])
                
                return HarvestController.harvest(self, params, request.body, params['verb'].lower())
        
            verb = params['verb']
            response.headers['Content-Type'] = "text/xml; charset=utf-8"
            
            return switch[verb](params)
        except Error as e:
            from lr.mustache.oaipmh import Error as err_stache
            err = err_stache()
            return self._returnResponse(err.xml(e), res=t_res)
Example #16
0
    def _handleOAIRequest(self, format='html'):
        t_req = request._current_obj()
        t_res = response._current_obj()

        enable_flow_control = False
        fc_id_limit = None
        fc_doc_limit = None
        service_id = None
        serviceDoc = h.getServiceDocument(appConfig['lr.oaipmh.docid'])
        if serviceDoc != None:
            if 'service_id' in serviceDoc:
                service_id = serviceDoc['service_id']

            if 'service_data' in serviceDoc:
                serviceData = serviceDoc['service_data']
                if 'flow_control' in serviceData:
                    enable_flow_control = serviceData['flow_control']

                if enable_flow_control and 'id_limit' in serviceData:
                    fc_id_limit = serviceData['id_limit']
                elif enable_flow_control:
                    fc_id_limit = 100

                if enable_flow_control and 'doc_limit' in serviceData:
                    fc_doc_limit = serviceData['doc_limit']
                elif enable_flow_control:
                    fc_doc_limit = 100

        o = oaipmh()

        def GetRecord(params):
            try:
                from lr.mustache.oaipmh import GetRecord as must_GetRecord
                identifier = params["identifier"]
                if params["by_doc_ID"] == True:
                    resolver = OAIPMHDocumentResolver()
                    single_doc = o.get_record(params["identifier"])
                    if single_doc is not None:
                        docList = [resolver.process({"doc": single_doc})]
                    else:
                        docList = []
                else:
                    docList = o.get_records_by_resource(params["identifier"])

                doc_idx = 0
                valid_docs = 0
                mustache = must_GetRecord()
                for doc in docList:
                    if doc is not None:
                        doc_idx += 1

                        if "payload_schema" in doc and params[
                                "metadataPrefix"] in map(
                                    lambda x: o_mod.getMetadataPrefix(x),
                                    doc["payload_schema"]
                                ) and OAIPMHDocumentResolver.PAYLOAD_ERROR not in doc:
                            valid_docs += 1

                            if valid_docs == 1:
                                part = mustache.prefix(**self._initMustache(
                                    args=params, req=t_req))
                                yield h.fixUtf8(
                                    self._returnResponse(part, res=t_res))

                            part = mustache.doc(doc)
                            yield h.fixUtf8(
                                self._returnResponse(part, res=t_res))

                if doc_idx == 0:
                    raise IdDoesNotExistError(params['verb'], req=t_req)
                elif valid_docs == 0:
                    raise CannotDisseminateFormatError(params['verb'],
                                                       req=t_req)
                else:
                    yield h.fixUtf8(
                        self._returnResponse(mustache.suffix(), res=t_res))

            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))

        def ListGeneric(params, showDocs=False, record_limit=None):
            if not showDocs:
                from lr.mustache.oaipmh import ListIdentifiers as must_ListID
                mustache = must_ListID()
            else:
                from lr.mustache.oaipmh import ListRecords as must_ListRec
                mustache = must_ListRec()

            try:

                doc_index = 0
                err_count = 0
                metadataPrefix = params["metadataPrefix"]
                from_date = params["from"]
                until_date = params["until"]
                doc_err = None
                rendered_init = False
                resumptionToken = None if "resumptionToken" not in params else params[
                    'resumptionToken']
                records = o.list_identifiers_or_records(metadataPrefix,
                                                        from_date=from_date,
                                                        until_date=until_date,
                                                        rt=resumptionToken,
                                                        fc_limit=record_limit,
                                                        include_docs=showDocs)
                for ident in records:
                    doc_index += 1
                    doc_err = False

                    if OAIPMHDocumentResolver.PAYLOAD_ERROR in ident:
                        err_count += 1
                        doc_err = True
                        log.debug(
                            "Payload Error detected, doc_index: {0}, err_count: {1}"
                            .format(doc_index, err_count))

                    if doc_index - err_count == 1:
                        rendered_init = True
                        part = mustache.prefix(
                            **self._initMustache(args=params, req=t_req))
                        yield h.fixUtf8(self._returnResponse(part, res=t_res))

                    if doc_err is False and (record_limit is None
                                             or doc_index <= record_limit):
                        part = mustache.doc(ident)
                        yield h.fixUtf8(part)
                    elif enable_flow_control:
                        from lr.lib import resumption_token
                        if doc_index - err_count > 0 and doc_index > record_limit:
                            opts = o.list_opts(
                                metadataPrefix,
                                h.convertToISO8601UTC(ident["node_timestamp"]),
                                until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            token = resumption_token.get_token(
                                serviceid=service_id,
                                from_date=from_date,
                                until_date=until_date,
                                **opts)
                            part = mustache.resumptionToken(token)
                            yield h.fixUtf8(part)
                            break
                        elif doc_index - err_count == 0 and doc_index > record_limit:
                            opts = o.list_opts(
                                metadataPrefix,
                                h.convertToISO8601UTC(ident["node_timestamp"]),
                                until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            payload = resumption_token.get_payload(
                                from_date=from_date,
                                until_date=until_date,
                                **opts)
                            records = o.list_identifiers_or_records(
                                metadataPrefix,
                                from_date=from_date,
                                until_date=until_date,
                                rt=payload,
                                fc_limit=record_limit,
                                include_docs=showDocs)
                            doc_index = 0
                            err_count = 0

                if doc_index == 0 and err_count == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                elif (doc_index - err_count) == 0:
                    raise CannotDisseminateFormatError(params['verb'],
                                                       req=t_req)
                else:
                    if enable_flow_control and doc_index <= record_limit:
                        yield h.fixUtf8(mustache.resumptionToken())
                    yield h.fixUtf8(mustache.suffix())

            except oaipmherrors.Error as e:
                if not rendered_init:
                    from lr.mustache.oaipmh import Error as err_stache
                    err = err_stache()
                    yield h.fixUtf8(self._returnResponse(err.xml(e),
                                                         res=t_res))
                else:
                    from lr.mustache.oaipmh import ErrorOnly as err_stache
                    err = err_stache()
                    yield h.fixUtf8(
                        self._returnResponse(err.xml(e) + mustache.suffix(),
                                             res=t_res))
            except:
                log.exception("Unknown Error Occurred")

        def ListIdentifiers(params):
            return ListGeneric(params, False, fc_id_limit)

        def ListRecords(params):
            return ListGeneric(params, True, fc_doc_limit)
#        def ListRecords(params):
#            try:
#                from lr.mustache.oaipmh import ListRecords as must_ListRec
#
#                doc_index = 0
#                mustache = must_ListRec()
#                for record in o.list_records(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
#                    doc_index += 1
#                    log.debug(json.dumps(record))
#                    if doc_index == 1:
#                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
#                        yield self._returnResponse(part, res=t_res)
#
#                    part = mustache.doc(record)
#                    yield self._returnResponse(part, res=t_res)
#
#
#                if doc_index == 0:
#                    raise NoRecordsMatchError(params['verb'], req=t_req)
#                else:
#                    yield mustache.suffix()
#
#            except oaipmherrors.Error as e:
#                from lr.mustache.oaipmh import Error as err_stache
#                err = err_stache()
#                yield self._returnResponse(err.xml(e), res=t_res)
#            except:
#                log.exception("Unable to render template")

        def Identify(params=None):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body, res=t_res)

        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)

                fmts = o.list_metadata_formats(identity=params["identifier"],
                                               by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body, res=t_res)
            except Error as e:
                raise e

        def ListSets(params=None):
            raise NoSetHierarchyError(verb)

        def NotYetSupported(params=None):
            raise BadVerbError()

        switch = {
            'GetRecord': GetRecord,
            'ListRecords': ListRecords,
            'ListIdentifiers': ListIdentifiers,
            'Identify': Identify,
            'ListMetadataFormats': ListMetadataFormats,
            'ListSets': ListSets
        }
        try:
            params = self._parseParams(flow_control=enable_flow_control,
                                       serviceid=service_id)

            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params[
                    "metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params[self.REQUESTID] = params["identifier"]
                if params.has_key("from") and isinstance(
                        params["from"], datetime):
                    params["from"] = h.convertToISO8601Zformat(params["from"])
                if params.has_key("until") and isinstance(
                        params["until"], datetime):
                    params["until"] = h.convertToISO8601Zformat(
                        params["until"])

                return HarvestController.harvest(self, params, request.body,
                                                 params['verb'].lower())

            verb = params['verb']
            response.headers['Content-Type'] = "text/xml; charset=utf-8"

            return switch[verb](params)
        except Error as e:
            from lr.mustache.oaipmh import Error as err_stache
            err = err_stache()
            return self._returnResponse(err.xml(e), res=t_res)
Example #17
0
 def i18n_index(self):
     obj = request._current_obj()
     locale_list = request.languages
     set_lang(request.languages)
     return unicode(_('basic index page'))