File: webpump.rst

package info (click to toggle)
python-pypump 0.7-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 560 kB
  • sloc: python: 3,153; makefile: 134
file content (107 lines) | stat: -rw-r--r-- 3,805 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Web Development using PyPump
============================

.. warning::

    This section needs to be updated.

One of the problem with PyPump and Web development is that you often have
a view which is called and then must return a function. While it is possible
it may be difficult to use the regular PyPump callback routines. WebPump is a
subclassed version of PyPump which handles that for you.

The only real difference is you don't specify a `verifier_callback` (if you do
it will be ignored). Once the instanciation has completed you can guarantee
that the URL for the callback has been created.

Django
------

This is an example of a very basic django view which uses WebPump::

    from pypump import WebPump
    from app.models import PumpModel
    from django.shortcuts import redirect
    from django.exceptions import ObjectDoesNotExist

    def pump_view(request, webfinger):
        try:
            webfinger = PumpModel.objects.get(webfinger=webfinger)
        except ObjectDoesNotExist:
            webfinger = PumpModel.objects.create(webfinger=webfinger)
            webfinger.save()

        # make the WebPump object
        if webfinger.oauth_credentials:
            pump = WebPump(
                    webfinger.webfinger,
                    client_type="web",
                    client_name="DjangoApp",
                    key=webfinger.key,
                    secret=webfinger.secret
                    token=webfinger.token,
                    token_secret=token_secret,
                    callback_uri="http://my_app.com/oauth/authorize"
                    )
        else:
            pump = WebPump(
                    webfinger.webfinger,
                    client_type="web",
                    client_name="DjangoApp",
                    callback_uri="http://my_app.com/oauth/authorize"
                    )

        # save the client credentials as they won't change
        webfinger.key, webfinger.secret, webfinger.expirey = pump.get_registeration()
        
        # save the request tokens so we can identify the authorize callback
        webfinger.token, webfinger.secret = pump.get_registrat()

        # save the model back to db
        webfinger.save()

        if pump.url is not None:
            # The user must go to this url and will get bounced back to our
            # callback_uri we specified above and add the webfinger as a
            # session cookie.
            request.session["webfinger"] = webfinger
            return redirect(pump.url)

        # okay oauth completed successfully, we can just save the oauth
        # credentials and redirect.
        webfinger.token, webfinger.token_secret = pump.get_registration()
        webfinger.save()

        # redirect to profile!
        return redirect("/profile/{webfinger}".format(webfinger))

    def authorize_view(request):
        """ This is the redirect when authorization is complete """
        webfinger = request.session.get("webfinger", None)
        token, verifier = request.GET["token"], request.GET["verifier"]
        
        try:
            webfinger = PumpModel.objects.get(
                    webfiger=webfinger,
                    token=token
                    )

        except ObjectDoesNotExist:
            return redirect("/error") # tell them this is a invalid request

        pump = WebPump(
                webfinger.webfinger,
                client_name="DjangoApp",
                client_type="web",
                key=pump.key,
                secret=pump.secret,
                )

        pump.verifier(verifier)

        # Save the access tokens back now.
        webfinger.token, webfinger.token_secret = pump.get_registration()
        webfinger.save()

        # and redirect to their profile
        return redirect("/profile")