def bookings_handler():
	api = CobotAPI()
	user = UserController().get_item(session['email']) if 'email' in session else None
	third_party_user_controller = ThirdPartyUserController()
	tp_user = third_party_user_controller.get_item(session['email'], 'cobot')
	from_time = '2015-10-13 00:00:00' #datetime.datetime.strptime(date+' '+start, '%Y-%m-%d %H:%M')
	to_time = '2015-10-13 12:00:00' #datetime.datetime.strptime(date+' '+end, '%Y-%m-%d %H:%M')
	bookings = api.list_bookings(tp_user['id'], from_time, to_time)
	print '***Bookings***'
	bookings_list = bookings.json()
	for booking in bookings_list:
		for key in booking:
				print key+' :: '+str(booking[key])
	return render_template("bookings.html", bookings=bookings_list, user=user)
def booking_create_handler():
    print "In booking create"
    user = UserController().get_item(session['email']) if 'email' in session else None
    if user:
        form = request.form
        space_id = form['space_id']
        room_id = form['room_id']
        date = form['date']
        start = form['start']
        end = form['end']
        print date, start, end
        controller = BookingController()
        account = Account.get(user['email'])
        account.billing_info = BillingInfo(token_id = form['recurly-token'])
        account.save()
        transaction = Transaction(
          amount_in_cents=int(form['amount'])*100,
          currency='INR',
          account=account
        )
        transaction.save()
        success = False
        if transaction.status == 'success':
            third_party_user_controller = ThirdPartyUserController()
            tp_user = third_party_user_controller.get_item(user['email'], 'cobot')
            from_time = datetime.datetime.strptime(date+' '+start, '%Y-%m-%d %H:%M')
            to_time = datetime.datetime.strptime(date+' '+end, '%Y-%m-%d %H:%M')
            print from_time, to_time
            type='room'
            api = CobotAPI()
            result = api.create_booking(tp_user['id'], from_time, to_time, type+'_'+space_id+'_'+room_id+'_'+str(from_time)+'_'+str(to_time))
            print result
            #create_booking(type='room', space_id=space_id, room_id=room_id, date=date, start_time=start, end_time=end)
            success = True
            bookings = api.list_bookings(tp_user['id'], from_time, to_time)
            print bookings
        return json.dumps({'success':success})
def create_third_party_user(user, network, access_token=None, **kwargs):
    third_party_user_controller = ThirdPartyUserController()
    tp_user = third_party_user_controller.get_item(user['email'], network)
    if not tp_user:
        third_party_user = third_party_user_controller.create_item(email=user['email'], network=network, access_token=access_token, **kwargs)