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 | from django.contrib.auth.models import User
from django.http import get_host
from django.shortcuts import render_to_response as render
from django.utils.html import escape
import gdata.contacts.service
GOOGLE_CONTACTS_URI = 'http://www.google.com/m8/feeds/'
def get_url_host(request):
if request.is_secure():
protocol = 'https'
else:
protocol = 'http'
host = escape(get_host(request))
return '%s://%s' % (protocol, host)
def get_full_url(request):
return get_url_host(request) + request.get_full_path()
def get_auth_sub_url(next):
scope = GOOGLE_CONTACTS_URI
secure = False
session = True
contacts_service = gdata.contacts.service.ContactsService()
return contacts_service.GenerateAuthSubURL(next, scope, secure, session);
def get_contact_emails(authsub_token):
contacts_service = gdata.contacts.service.ContactsService()
contacts_service.auth_token = authsub_token
contacts_service.UpgradeToSessionToken()
emails = []
feed = contacts_service.GetContactsFeed()
emails.extend(sum([[email.address for email in entry.email] for entry in feed.entry], []))
next_link = feed.GetNextLink()
while next_link:
feed = contacts_service.GetContactsFeed(uri=next_link.href)
emails.extend(sum([[email.address for email in entry.email] for entry in feed.entry], []))
next_link = feed.GetNextLink()
return emails
def import_contacts(request):
if request.GET.get('token', ''):
emails = get_contact_emails(request.GET['token'])
users = User.objects.filter(email__in=emails)
return render('google_contacts/results.html', {
'users': users
})
else:
next = get_full_url(request)
return render('google_contacts/login.html', {
'auth_sub_url': get_auth_sub_url(next)
})
|
Comments
Hi, I don't understand why the variables host and protocol are computed inside get_ful_url(), because they are not used. Is there any reason ?
#
No idea. I actually lifted that straight from django_openidconsumer.views and it worked ... so I kept it. :)
#
Err, on second look, that was probably a paste error. Edited. Thanks!
#