def index(): # print get_home() sys.stdout.write(str(request.args.get('code', None))) sys.stdout.write(str(request.args)) access_token = fb_lib.get_token(request) channel_url = url_for('get_channel', _external=True) channel_url = channel_url.replace('http:', '').replace('https:', '') sys.stdout.write(str(access_token)) if access_token: me = fb_lib.fb_call('me', args={'access_token': access_token}) if FBUser.query.filter(FBUser.facebook_id == me['id']).count() == 0: create_user(me, access_token) elif FBUser.query.filter(FBUser.facebook_id == me['id']).first(): user = FBUser.query.filter(FBUser.facebook_id == me['id']).first() user.access_token = access_token[0] db.session.commit() fb_app = fb_lib.fb_call(FB_APP_ID, args={'access_token': access_token}) likes = fb_lib.fb_call('me/likes', args={'access_token': access_token, 'limit': 4}) friends = fb_lib.fb_call('me/friends', args={'access_token': access_token, 'limit': 4}) photos = fb_lib.fb_call('me/photos', args={'access_token': access_token, 'limit': 16}) host = request.host redir = fb_lib.get_home(host) + 'close/' POST_TO_WALL = ("https://www.facebook.com/dialog/feed?redirect_uri=%s&" "display=popup&app_id=%s" % (redir, FB_APP_ID)) app_friends = fb_lib.fql( "SELECT uid, name, is_app_user, pic_square " "FROM user " "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND " " is_app_user = 1", access_token) SEND_TO = ('https://www.facebook.com/dialog/send?' 'redirect_uri=%s&display=popup&app_id=%s&link=%s' % (redir, FB_APP_ID, fb_lib.get_home(host))) url = request.url return render_template( 'index.html', app_id=FB_APP_ID, token=access_token, likes=likes, friends=friends, photos=photos, app_friends=app_friends, app=fb_app, me=me, POST_TO_WALL=POST_TO_WALL, SEND_TO=SEND_TO, url=url, channel_url=channel_url, name=FB_APP_NAME) else: return render_template('login.html', app_id=FB_APP_ID, token=access_token, url=request.url, channel_url=channel_url, name=FB_APP_NAME)
def update_user(): token = fb_lib.get_token(request) zipcode = request.form['zipcode'] me = fb_lib.fb_call('me', args={'access_token': token}) app.logger.info("Updating User: %s" % me) record = FBUser.query.filter(FBUser.facebook_id == me['id']).first() record.zipcode = zipcode db.session.commit() if Location.query.filter(Location.zipcode == zipcode).count() == 0: date = None new_location = Location(request.form['zipcode'], date) db.session.add(new_location) db.session.commit() return render_template('results.html')