Background Image
Table of Contents Table of Contents
Previous Page  271 / 478 Next Page
Information
Show Menu
Previous Page 271 / 478 Next Page
Page Background

The first thing I did was take a look at an existing Django-Persona integration called

Django-BrowserID ,

but unfortunately it didn’t really support Python 3. I’m sure it will

by the time you read this, but I was quietly relieved since I was rather looking forward

to writing my own code for this!

It took me about three hours of hacking about, using a combination of code stolen from

Dan’s talk and the example code on the

Persona site

, but by the end I had something

which just about works. I’ll take you on a tour, and then we’ll go through and “de-spike”

the implementation.

You should go ahead and add this code to your own site too, and then you can have a

play with it, try logging in with your own email address, and convince yourself that it

really does work.

Starting a Branch for the Spike

Before embarking on a spike, it’s a good idea to start a new branch, so you can still use

your VCS without worrying about your spike commits getting mixed up with your

production code:

$

git checkout -b persona-spike

Frontend and JavaScript Code

Let’s start with the frontend. I was able to cut and paste code from the Persona site and

Dan’s slides with minimal modification:

lists/templates/base.html (ch15l001).

<script

src=

"http://code.jquery.com/jquery.min.js

"

></script>

<script

src=

"/static/list.js"

></script>

<script

src=

"https://login.persona.org/include.js

"

></script>

<script>

$

(

document

).

ready

(

function

() {

var

loginLink

=

document

.

getElementById

(

'login'

);

if

(

loginLink

) {

loginLink

.

onclick

=

function

() {

navigator

.

id

.

request

(); };

}

var

logoutLink

=

document

.

getElementById

(

'logout'

);

if

(

logoutLink

) {

logoutLink

.

onclick

=

function

() {

navigator

.

id

.

logout

(); };

}

var

currentUser

=

'{{ user.email }}'

||

null

;

var

csrf_token

=

'{{ csrf_token }}'

;

console

.

log

(

currentUser

);

navigator

.

id

.

watch

({

loggedInUser

:

currentUser

,

onlogin

:

function

(

assertion

) {

$

.

post

(

'/accounts/login'

, {

assertion

:

assertion

,

csrfmiddlewaretoken

:

csrf_token

})

.

done

(

function

() {

window

.

location

.

reload

(); })

Exploratory Coding, aka “Spiking”

|

243