Пример #1
0
def _fb_user_to_auth(request, fb_user_cookie):
  prof.start('profile-save-fb-get-me')
  graph = facebook.GraphAPI(access_token=fb_user_cookie['access_token'])
  try:
    fb_user = graph.get_object("me")
  except facebook.GraphAPIError:
    # Clear cookies
    return None
  prof.stop('profile-save-fb-get-me')

  log.debug("fb_user_cookie %s fb_user_me: %s" % (str(fb_user_cookie), str(fb_user)))
  prof.start('profile-save-fb-new-user')
  (user, profile, new_user) = _check_and_save_user_atomic(fb_user)
  prof.stop('profile-save-fb-new-user')
  if new_user:
    # A new user, we also need to save to profile
    fb_user['photo'] = graph.request("me/picture", args={'type': 'large'}, binary=True)
    _populate_user_profile(user, profile, fb_user, fb_user_cookie)

    # Make friends in facebook for fingo too
    _save_friends(user, graph.get_connections("me", "friends")['data'])

    # Post it to wall
    # FIXME: this will fail if it comes from fb_app
    FacebookSystem.post_new_user_to_wall(request.COOKIES)

  return user
Пример #2
0
def _add(request, form):
  # Check if the user is active or not
  if not request.user.is_active:
    return

  # Save news
  prof.start('news-add')
  news = form.save(commit=False)
  news.user = request.user
  news.save()
  prof.stop('news-add')

  # Publish on wall?
  if not('anonymous' in form.cleaned_data and form.cleaned_data['anonymous']):
    if 'fb_wall' in form.cleaned_data and form.cleaned_data['fb_wall']:
        try:
          FacebookSystem.post_gossip_to_wall(request.COOKIES,
                                             news,
                                             'me')
        except facebook.GraphAPIError:
          # TODO: Error when the user logs out from facebook between
          # loading /news/add and submiting it
          pass

  # Save images if any
  prof.start('news-add-save-images')
  for image in request.FILES.getlist('images'):
    img = Image(news=news, image=image)
    img.save()
  prof.stop('news-add-save-images')


  # Send e-mail
  if news.about_id:
    NotificationSystem.create(news.about_id, 
                              const.NotifConst.NEW_GOSSIP_ME,
                              {'username': request.user.username,
                              'anonymous': news.anonymous,
                              'name': prettyuser.user_or_first_name(request.user),
                              })