Exemplo n.º 1
0
def post_to_facebook_ajax(request):
    success, error, facebook_event_ids = False, '', {}

    event_id = request.POST.get('event_id')
    facebook_owner_type = request.POST.get('owner_type')
    try:
        event = Event.events.get(pk=event_id)
    except Event.DoesNotExist as e:
        event, error = None, e.message

    if event:
        try:
            if facebook_owner_type == 'user':
                facebook_owner_id = facebook_services.get_facebook_user_id(
                    request)
            else:
                facebook_owner_id = request.POST.get('page_id')

            facebook_event_ids = facebook_services.create_facebook_event(
                event, request, facebook_owner_id, facebook_owner_type)
            success = True
        except Exception as e:
            error = e.message
    else:
        error = 'Event does not exists'

    params = {
        'success': success,
        'error': error,
        'facebook_event_ids': facebook_event_ids
    }

    if error and event:
        params['event_link'] = reverse(
            'event_edit',
            kwargs={'authentication_key': event.authentication_key})

    return HttpResponse(json.dumps(params), mimetype='application/json')