sciyoshi / pyfacebook

PyFacebook
572 stars 136 forks source link

WSGI / pylons-0.9.7 fixes #24

Open tw1nturb0 opened 14 years ago

tw1nturb0 commented 14 years ago

Using pyfacebook with pylons has issues with redirects nesting inside the application iframe. It appears this issue was already addressed for Django with commit http://github.com/sciyoshi/pyfacebook/commit/2a6b442c0e2d7674f9c347a340045286d3ece8f2. Additionally, the transition from Paster to WebOb HTTP exceptions on an earlier commit needed some updates as some variable names differ.

Not sure of the best way to paste in this patch (all in wsgi.py), so here goes:

Top of the file: import re

Replace CanvasRedirect definition with: class CanvasRedirect(HTTPOk):

    """This is for redirects within the canvas using FBML."""

    try:
        from string import Template
    except ImportError:
        from webob.util.stringtemplate import Template

    html_template_obj = Template('<html><head></head><body>${body}</body></html>')
    body_template_obj = Template('<fb:redirect url="${location}" />')

    def __init__(self, location):
        HTTPOk.__init__(self, headers = { 'location': location } )

class TopLevelRedirect(HTTPOk):

    """This is for redirects to top-level Facebook pages."""

    try:
        from string import Template
    except ImportError:
        from webob.util.stringtemplate import Template

    html_template_obj = Template('<html><head></head><body>${body}</body></html>')
    body_template_obj = Template('<script type="text/javascript">\ntop.location.href = "${location}";\n</script>')

    def __init__(self, location):
        HTTPOk.__init__(self, headers = { 'location': location } )

Change definition of redirect_to:

    def redirect_to(self, url):
        """Wrap Pylons' redirect_to function so that it works in_canvas.

        By the way, this won't work until after you call
        check_session().

        """

        if self.in_canvas:
            raise CanvasRedirect(url)
        elif re.search("^https?:\/\/([^\/]*\.)?facebook\.com(:\d+)?", url.lower()):
            raise TopLevelRedirect(url)
        else:
            pylons_redirect_to(url)
tw1nturb0 commented 14 years ago

Also needs the _HTTPMove import changed to HTTPOk.