Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extend TrackingMiddleware in customized extension/plugin #4451

Open
LingboTang opened this issue Sep 10, 2018 · 2 comments
Open

How to extend TrackingMiddleware in customized extension/plugin #4451

LingboTang opened this issue Sep 10, 2018 · 2 comments
Assignees

Comments

@LingboTang
Copy link

CKAN Version if known (or site URL)

2.7.3

Please describe the expected behaviour

I've read IMiddleware docs in official docs. I should be able to implement this interface and extend the TrackingMiddleware with my own implementation.

Please describe the actual behaviour

I defined a MyTrackingMiddleware class and used that in my plugin.py like this:

from ckan.config.middleware.common_middleware import TrackingMiddleware
import urllib2
import hashlib
import sqlalchemy as sa
import pylons.config as config

class MyTrackingMiddleware(TrackingMiddleware):

    def __init__(self, app, config):
        self.app = app
        self.config = config

    def __call__(self, environ, start_response):
        super(MyTrackingMiddleware, self).__call__(environ, start_response)
        if path == '/_tracking' and method == 'GET':
            print("===============YOUR METHOD IS GET=================")
            '''
            Handle get request
            '''
        return self.app(environ, start_response)

class MyPlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.IMiddleware, inherit=True)

    def make_middleware(self, app, config):
        app = MyTrackingMiddleware(app, config)
        return app

I visited some package, MyTrackingMiddleware has never received requests to /_tracking. All the paths I got from environ are package urls. I think it is because the environ I passed in MyTrackingMiddleware is not within the global context. Instead, the environ I got is from local context. However, I don't know how to pass the global environ.

What steps can be taken to reproduce the issue?

My steps has already provided in the last section.

@wardi wardi self-assigned this Sep 11, 2018
@wardi
Copy link
Contributor

wardi commented Sep 11, 2018

It looks like the standard TrackingMiddleware intercepts all POST requests to `/_tracking'. As a quick hack you could disable the TrackingMiddleware in your copy of ckan to see if that resolves the issue.

If that does work, we could change the middleware order so that custom middleware comes earlier in the stack. We'd have to check if that would break any existing middleware plugins of course.

@LingboTang
Copy link
Author

@wardi Thanks for your advice. I disabled tracking_enabled in dev.ini, at least I won't see request being sent to /_tracking, so that it gives me the flexibility to handle tracking on application level.

@LingboTang LingboTang changed the title How to extend TrackingMiddleware in my own extension/plugin How to extend TrackingMiddleware in customized extension/plugin Sep 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants