├── MANIFEST.in ├── README.rst ├── django_contact_importer ├── __init__.py ├── contacts │ ├── __init__.py │ ├── models.py │ ├── tests.py │ ├── urls.py │ └── views.py └── templates │ └── contacts │ ├── index.html │ └── invite.html ├── setup.cfg └── setup.py /MANIFEST.in: -------------------------------------------------------------------------------- 1 | global-exclude *.pyc 2 | 3 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | About django_contact_importer 2 | ------------------------- 3 | 4 | Import contacts from Gmail, Yahoo and Live accounts within your Django application. 5 | 6 | Installing 7 | ------------------------------- 8 | 9 | Clone the repository and run the following command.:: 10 | 11 | python setup.py install 12 | 13 | 14 | Configure Django Application 15 | ---------------------------- 16 | 17 | 1. Add `django_contact_importer` to your `INSTALLED_APPS` in `settings.py`. 18 | 2. Obtain a client id and client secret from providers like Google, Yahoo and Live. 19 | 3. Set your client id and client secret informations in settings.py. 20 | 21 | :: 22 | 23 | GOOGLE_CLIENT_ID = your_google_client_id 24 | GOOGLE_CLIENT_SECRET = your_google_client_secret 25 | YAHOO_CLIENT_ID = your_yahoo_client_id 26 | YAHOO_CLIENT_SECRET = your_yahoo_client_secret" 27 | LIVE_CLIENT_ID = your_live_client_id 28 | LIVE_CLIENT_SECRET = your_live_client_secret 29 | 30 | 3. Open your urls.py and add the following line: 31 | 32 | :: 33 | 34 | url(r'^contacts/', include("django_contact_importer.contacts.urls")), 35 | 36 | 37 | You can now reach the application from `http://127.0.0.1/contacts`. 38 | 39 | Templates 40 | --------- 41 | There are two templates that you can override from your application. 42 | 43 | 1. contacts/index.html - Lists the providers. 44 | 45 | 2. contacts/invite.html - Lists the contacts retrieved from providers. 46 | -------------------------------------------------------------------------------- /django_contact_importer/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | -------------------------------------------------------------------------------- /django_contact_importer/contacts/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengu/django_contact_importer/c2c95d168a5c0c077ee7184651fb79b73a9d589e/django_contact_importer/contacts/__init__.py -------------------------------------------------------------------------------- /django_contact_importer/contacts/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /django_contact_importer/contacts/tests.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file demonstrates writing tests using the unittest module. These will pass 3 | when you run "manage.py test". 4 | 5 | Replace this with more appropriate tests for your application. 6 | """ 7 | 8 | from django.test import TestCase 9 | 10 | 11 | class SimpleTest(TestCase): 12 | def test_basic_addition(self): 13 | """ 14 | Tests that 1 + 1 always equals 2. 15 | """ 16 | self.assertEqual(1 + 1, 2) 17 | -------------------------------------------------------------------------------- /django_contact_importer/contacts/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import patterns, include, url 2 | 3 | urlpatterns = patterns('', 4 | url(r'^$', 'django_contact_importer.contacts.views.index', name='contacts_index'), 5 | url(r'^invite$', 'django_contact_importer.contacts.views.invite', name='invite_contacts'), 6 | ) -------------------------------------------------------------------------------- /django_contact_importer/contacts/views.py: -------------------------------------------------------------------------------- 1 | # Create your views here. 2 | 3 | from django.shortcuts import redirect, render_to_response 4 | from django.conf import settings 5 | from django.core.urlresolvers import reverse 6 | from contact_importer.providers.google import GoogleContactImporter 7 | from contact_importer.providers.yahoo import YahooContactImporter 8 | from contact_importer.providers.live import LiveContactImporter 9 | 10 | providers = { 11 | "google": GoogleContactImporter, 12 | "live": LiveContactImporter, 13 | "yahoo": YahooContactImporter 14 | } 15 | 16 | def index(request): 17 | provider = request.GET.get('provider') 18 | if provider: 19 | provider_instance = _get_provider_instance(provider, _get_redirect_url(request)) 20 | if provider == "yahoo": 21 | provider_instance.get_request_token() 22 | request.session["oauth_token_secret"] = provider_instance.oauth_token_secret 23 | return redirect(provider_instance.request_authorization()) 24 | return render_to_response("contacts/index.html") 25 | 26 | def invite(request): 27 | provider = request.GET.get('provider') 28 | 29 | if not provider: 30 | return redirect(reverse("contacts_index")) 31 | 32 | code = request.GET.get('code') 33 | oauth_token = request.GET.get('oauth_token') 34 | oauth_verifier = request.GET.get('oauth_verifier') 35 | redirect_url = _get_redirect_url(request) 36 | provider_instance = _get_provider_instance(provider, redirect_url) 37 | 38 | if provider == "yahoo": 39 | provider_instance.oauth_token = oauth_token 40 | provider_instance.oauth_verifier = oauth_verifier 41 | provider_instance.oauth_token_secret = request.session["oauth_token_secret"] 42 | provider_instance.get_token() 43 | contacts = provider_instance.import_contacts() 44 | del request.session["oauth_token_secret"] 45 | else: 46 | access_token = provider_instance.request_access_token(code) 47 | contacts = provider_instance.import_contacts(access_token) 48 | 49 | return render_to_response("contacts/invite.html", {"contacts": contacts}) 50 | 51 | def _get_redirect_url(request): 52 | provider = request.GET.get('provider') 53 | invite_url = "%s?provider=%s" % (reverse("invite_contacts"), provider) 54 | request_scheme = "https" if request.is_secure() else "http" 55 | redirect_url = "%s://%s%s" % (request_scheme, request.META["HTTP_HOST"], invite_url) 56 | return redirect_url 57 | 58 | def _get_provider_instance(provider, redirect_url): 59 | if provider not in providers: 60 | raise Exception("The provider %s is not supported." % provider) 61 | 62 | client_id = getattr(settings, "%s_CLIENT_ID" % provider.upper(), None) 63 | client_secret = getattr(settings, "%s_CLIENT_SECRET" % provider.upper(), None) 64 | 65 | if not client_id: 66 | raise Exception("The provider %s is not supported." % provider) 67 | 68 | provider_class = providers[provider] 69 | return provider_class(client_id, client_secret, redirect_url) -------------------------------------------------------------------------------- /django_contact_importer/templates/contacts/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /django_contact_importer/templates/contacts/invite.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | {% for contact in contacts %} 9 |