Пример #1
0
def contacts(request, form_class=ImportVCardForm,
        template_name="friends_app/contacts.html"):
    if request.method == "POST":
        if request.POST["action"] == "upload_vcard":
            import_vcard_form = form_class(request.POST, request.FILES)
            if import_vcard_form.is_valid():
                imported, total = import_vcard_form.save(request.user)
                request.user.message_set.create(message=_("%(total)s vCards found, %(imported)s contacts imported.") % {'imported': imported, 'total': total})
                import_vcard_form = ImportVCardForm()
        else:
            import_vcard_form = form_class()
            if request.POST["action"] == "import_yahoo":
                bbauth_token = request.session.get('bbauth_token')
                del request.session['bbauth_token']
                if bbauth_token:
                    imported, total = import_yahoo(bbauth_token, request.user)
                    request.user.message_set.create(message=_("%(total)s people with email found, %(imported)s contacts imported.") % {'imported': imported, 'total': total})
            if request.POST["action"] == "import_google":
                authsub_token = request.session.get('authsub_token')
                del request.session['authsub_token']
                if authsub_token:
                    imported, total = import_google(authsub_token, request.user)
                    request.user.message_set.create(message=_("%(total)s people with email found, %(imported)s contacts imported.") % {'imported': imported, 'total': total})
    else:
        import_vcard_form = form_class()
    
    return render_to_response(template_name, {
        "import_vcard_form": import_vcard_form,
        "bbauth_token": request.session.get('bbauth_token'),
        "authsub_token": request.session.get('authsub_token'),
    }, context_instance=RequestContext(request))
Пример #2
0
def contacts(request,
             form_class=ImportVCardForm,
             template_name="friends_app/contacts.html"):
    if request.method == "POST":
        if request.POST["action"] == "upload_vcard":
            import_vcard_form = form_class(request.POST, request.FILES)
            if import_vcard_form.is_valid():
                imported, total = import_vcard_form.save(request.user)
                messages.add_message(
                    request, messages.SUCCESS,
                    ugettext(
                        "%(total)s vCards found, %(imported)s contacts imported."
                    ) % {
                        "imported": imported,
                        "total": total
                    })
                import_vcard_form = ImportVCardForm()
        else:
            import_vcard_form = form_class()
            if request.POST["action"] == "import_yahoo":
                bbauth_token = request.session.get("bbauth_token")
                del request.session["bbauth_token"]
                if bbauth_token:
                    imported, total = import_yahoo(bbauth_token, request.user)
                    messages.add_message(
                        request, messages.SUCCESS,
                        ugettext(
                            "%(total)s people with email found, %(imported)s contacts imported."
                        ) % {
                            "imported": imported,
                            "total": total
                        })
            if request.POST["action"] == "import_google":
                authsub_token = request.session.get("authsub_token")
                del request.session["authsub_token"]
                if authsub_token:
                    imported, total = import_google(authsub_token,
                                                    request.user)
                    messages.add_message(
                        request, messages.SUCCESS,
                        ugettext(
                            "%(total)s people with email found, %(imported)s contacts imported."
                        ) % {
                            "imported": imported,
                            "total": total
                        })
    else:
        import_vcard_form = form_class()

    return render_to_response(
        template_name, {
            "import_vcard_form": import_vcard_form,
            "bbauth_token": request.session.get("bbauth_token"),
            "authsub_token": request.session.get("authsub_token"),
        },
        context_instance=RequestContext(request))
Пример #3
0
def import_google_contacts(request, redirect_to="invite_imported", redirect_field_name=REDIRECT_FIELD_NAME):
    from importer import get_oauth_var
    import gdata.service
    import gdata.gauth
    import gdata.contacts.client
    import pickle
    redirect_to=request.REQUEST.get(redirect_field_name, redirect_to)
    if redirect_to and '/' not in redirect_to:
        redirect_to=reverse(redirect_to)

    
    try:
        token_for_user = GoogleToken.objects.get(user=request.user)
    except:
        cp_scope = gdata.service.lookup_scopes('cp')
        gd_client = gdata.contacts.client.ContactsClient(source='AACE-AcademicExperts-v1')
        if request.GET.has_key('oauth_token'):
            request_token = gdata.gauth.AuthorizeRequestToken(request.session['request_token'], request.build_absolute_uri())
            try:
                access_token = gd_client.GetAccessToken(request_token)
                access_token_string = pickle.dumps(access_token)
                token_for_user = GoogleToken(user=request.user, token=access_token_string, token_secret=request.session['request_token'].token_secret)
                token_for_user.save()
            except gdata.client.RequestError:
                messages.add_message(request, messages.ERROR,"It looks like you didn't allow access to Google, so I couldn't import the contacts.")
                return {}, {'url': redirect_to} 
        else:
            next = "http://%s%s" % (
                    Site.objects.get_current(),
                    reverse('import_google_contacts') 
            )
            request_token = gd_client.GetOAuthToken(
                list(cp_scope), 
                next,
                get_oauth_var('GOOGLE','OAUTH_CONSUMER_KEY'),
                consumer_secret=get_oauth_var('GOOGLE','OAUTH_CONSUMER_SECRET')
            )
            #store the token's secret in a session variable
            request.session['request_token'] = request_token
            url = request_token.generate_authorization_url()
            return HttpResponseRedirect(url)

    if token_for_user:
        try:
            imported, total = import_google(request.user)
        except gdata.service.RequestError:
            if not request.session.has_key('_try_again'):
                request.session["_try_again"]="import_google_contacts"
                GoogleToken.objects.filter(user=request.user).delete()
                if request.session.has_key('request_token'):
                    del(request.session['request_token'])
                return HttpResponseRedirect(
                                        "%s?%s" % (reverse('import_google_contacts'),
                                        urllib.urlencode({ 
                                        redirect_field_name: redirect_to})))
            else:
                del(request.session['_try_again'])
                messages.add_message(request, messages.ERROR,"There was an error posting to Twitter. Please make sure this site isn't blocked in your settings in Twitter.")
                return {}, {'url': redirect_to} 
        if imported < total:
            if imported:
                messages.add_message(request, messages.SUCCESS,'A total of %d emails imported. %d records were already imported.' % (imported, total-imported))
            else:
                messages.add_message(request, messages.SUCCESS,'A total of %d emails were found, but they were already imported.' % (total))
        else:
            messages.add_message(request, messages.SUCCESS,'A total of %d emails imported vs %d.' % (imported,total))
        return {'imported':imported, 'total':total}, {'url': redirect_to}