Exemplo n.º 1
0
def _get_profile_form(request, form=None, non_xhr=False):
    """
  Helper method to render the profile form.
  """
    try:
        fb_user = facebook.get_user_from_cookie(request.COOKIES,
                                                settings.FACEBOOK_APP_ID,
                                                settings.FACEBOOK_SECRET_KEY)
    except:
        fb_user = None

    fb_id = None
    facebook_photo = None
    if fb_user:
        try:
            graph = facebook.GraphAPI(fb_user["access_token"])
            graph_profile = graph.get_object("me")
            fb_id = graph_profile["id"]
            facebook_photo = "http://graph.facebook.com/%s/picture?type=large" % fb_id
        except facebook.GraphAPIError:
            return HttpResponse(json.dumps({
                "contents":
                "Facebook is not available at the moment, please try later",
            }),
                                mimetype='application/json')

    if not form:
        form = ProfileForm(
            initial={
                "display_name": request.user.get_profile().name,
                "facebook_photo": facebook_photo,
            })

    response = render_to_string("home/first-login/profile.html", {
        "form": form,
        "fb_id": fb_id,
    },
                                context_instance=RequestContext(request))

    if non_xhr:
        return HttpResponse('<textarea>' +
                            json.dumps({
                                "title": "Introduction: Step 4 of 7",
                                "contents": cgi.escape(response),
                            }) + '</textarea>',
                            mimetype='text/html')
    else:
        return HttpResponse(json.dumps({
            "title": "Introduction: Step 4 of 7",
            "contents": response,
        }),
                            mimetype='application/json')
Exemplo n.º 2
0
def get_facebook_photo(request):
    """
  Connect to Facebook to get the user's facebook photo..
  """
    if request.is_ajax():
        fb_user = facebook.get_user_from_cookie(request.COOKIES,
                                                settings.FACEBOOK_APP_ID,
                                                settings.FACEBOOK_SECRET_KEY)
        fb_id = None
        fb_error = None
        if not fb_user:
            return HttpResponse(json.dumps({
                "error":
                "We could not access your info.  Please log in again."
            }),
                                mimetype="application/json")

        try:
            graph = facebook.GraphAPI(fb_user["access_token"])
            graph_profile = graph.get_object("me")
            fb_id = graph_profile["id"]
        except facebook.GraphAPIError:
            return HttpResponse(json.dumps({
                "contents":
                "Facebook is not available at the moment, please try later",
            }),
                                mimetype='application/json')

        # Insert the form into the response.
        form = FacebookPictureForm(
            initial={
                "facebook_photo":
                "http://graph.facebook.com/%s/picture?type=large" % fb_id
            })

        response = render_to_string("makahiki_avatar/avatar_facebook.html", {
            "fb_error": fb_error,
            "fb_id": fb_id,
            "fb_form": form,
        },
                                    context_instance=RequestContext(request))

        return HttpResponse(json.dumps({
            "contents": response,
        }),
                            mimetype='application/json')

    raise Http404
Exemplo n.º 3
0
    def create_or_update_from_fb_user(user, fb_user):
        """
    Saves the data retrieved by the cookie so that the we don't need to 
    constantly retrieve data from Facebook. Returns None if the user 
    cannot be retrieved.
    """

        if not user or not fb_user:
            return None

        try:
            graph = facebook.GraphAPI(fb_user["access_token"])
            graph_profile = graph.get_object("me")
        except facebook.GraphAPIError:
            return None

        fb_profile = None
        try:
            fb_profile = user.facebookprofile
        except FacebookProfile.DoesNotExist:
            fb_profile = FacebookProfile(user=user)

        for key in graph_profile.keys():
            value = graph_profile[key]

            if key == "about":
                fb_profile.about = value
            elif key == "last_name":
                fb_profile.last_name = value
            elif key == "first_name":
                fb_profile.first_name = value
            elif key == "name":
                fb_profile.name = value
            elif key == "gender":
                fb_profile.gender = value
            elif key == "link":
                fb_profile.profile_link = value
            elif key == "id":
                fb_profile.profile_id = value

        fb_profile.save()
        return fb_profile
Exemplo n.º 4
0
def change(request, extra_context={}, next_override=None):
    file_error = None
    avatars = Avatar.objects.filter(user=request.user).order_by('-primary')
    if avatars.count() > 0:
        avatar = avatars[0]
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        avatar = None
        kwargs = {}
    primary_avatar_form = PrimaryAvatarForm(request.POST or None,
                                            user=request.user,
                                            **kwargs)
    if request.method == "POST":
        updated = False
        if 'avatar' in request.FILES:
            try:
                __handle_uploaded_file(request.FILES['avatar'], request.user)
                updated = True
                return HttpResponseRedirect(
                    reverse("profile_index") + "?changed_avatar=True")
            except Exception:
                file_error = "Uploaded file is larger than 1 MB."

        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(
                id=primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True

            return HttpResponseRedirect(
                reverse("profile_index") + "?changed_avatar=True")

    fb_user = facebook.get_user_from_cookie(request.COOKIES,
                                            settings.FACEBOOK_APP_ID,
                                            settings.FACEBOOK_SECRET_KEY)
    fb_id = None

    if fb_user:
        try:
            graph = facebook.GraphAPI(fb_user["access_token"])
            graph_profile = graph.get_object("me")
            fb_id = graph_profile["id"]
        except facebook.GraphAPIError:
            pass

    fb_form = FacebookPictureForm(
        initial={
            "facebook_photo":
            "http://graph.facebook.com/%s/picture?type=large" % fb_id
        })
    return render_to_response(
        'makahiki_avatar/change.html',
        extra_context,
        context_instance=RequestContext(
            request, {
                'avatar': avatar,
                'avatars': avatars,
                'primary_avatar_form': primary_avatar_form,
                'next': next_override or _get_next(request),
                'fb_id': fb_id,
                'fb_form': fb_form,
                'file_error': file_error,
            }))