Esempio n. 1
0
def record_facebook_invitation(request):
    """Expects a post request formatted just as the Facebook App
    request response sent through the wire encoded by jQuery.

    If you need to do processing after the invitation has been created
    then use FacebookInvitationCreateView.

    POST keys:
      request: The Facebook request id
      to[]: A list of Facebook UIDs that the request was sent to.
      next: Optional argument for where the user is to be redirected after
            the invitation has been accepted.

    Returns:
      400 when no request has been set
      201 when invitations has been successfully created
    """
    request_id = request.POST.get('request')
    if not request_id: return HttpResponseBadRequest('No request set.')

    profile = request.user.get_profile()
    fb_user = profile.facebook
    if not fb_user:
        HttpResponse(json.dumps({'result': 'error',
                                 'error': 'no friendface user'}),
                     content_type='application/json',
                     status=403)
    application = fb_user.application

    for recipient in request.POST.getlist('to[]'):
        FacebookInvitation.create_with_receiver(
            receiver=recipient,
            request_id=request_id,
            application=application,
            sender=fb_user,
            next=request.POST.get('next', ''))

    return HttpResponse(json.dumps({'result': 'ok'}),
                        content_type='application/json',
                        status=201)
Esempio n. 2
0
    def get_context_data(self, **kwargs):
        """
        Expects a post request formatted just as the Facebook App
        request response sent through the wire encoded by jQuery.

        POST keys:
          request: The Facebook request id
          to[]: A list of Facebook UIDs that the request was sent to.
          next: Optional argument for where the user is to be redirected after
                the invitation has been accepted.

        Raises:
           ValueError when request is not available
        """
        context = {}
        request = self.request.POST.get('request')
        if not request: raise ValueError('No request id specified')
        fb_user = self.request.user.get_profile().facebook

        context.update({
            'request_id': request,
            'sender': fb_user,
            'application': self.request.facebook,
            'next': self.request.POST.get('next', ''),
            'invitations': []
        })

        for recipient in self.request.POST.getlist('to[]'):
            invitation = FacebookInvitation.create_with_receiver(
                receiver=recipient,
                request_id=context['request_id'],
                application=context['application'],
                sender=context['sender'],
                next=context['next'],
            )

            context['invitations'].append(invitation)
            self.handle_invitation(invitation)

        return context