Example #1
0
def	home(request):
	logger.info("Loading home page")
	access_token = request.GET.get('access_token')
	userid = request.GET.get('userid')
	res = {}
	res['valid'] = False

	#Update the access token for the user in the database
	try:
		user = fetch_fb_user(userid)
	except IndexError:
		#New user, add user to database
		try:
			content = fetch_user_info_from_fb(userid, access_token)
		except:
			logger.debug("Error fetching user info from facebook for userid "+str(userid)+"\n");
			return render(request, 'polls/error.html', {"message": "Something went wrong. Please logout and login again!"})

		#confirm userid is equal to id returned from graph api
		if 'id' not in content or userid != content['id']:
			return render(request, 'polls/error.html', {"message": "Something went wrong. Please logout and login again!"})
		content['access_token'] = access_token
		logger.debug("Trying to save user")
		try:
			res = save_fb_user(content)
			logger.debug("Saved user info succesfully"+str(res))
		except:
			logger.exception("Unexpected error while saving user: "******"application/json")		
	
	if res['valid']:
		user = fetch_fb_user(userid)
		
	logger.debug('Got user '+str(user)+' for userid '+str(userid)+str(access_token))
	if user:
		user.access_token = access_token
		user.save()
	return render(request, 'polls/home.html')
Example #2
0
def retrieve_tagged_places(request):
	userid = request.POST['user']
	access_token = request.POST['access_token']
	res = {}
	res['valid'] = False
	try:
		user = fetch_fb_user(userid)
	except:
		res['valid'] = False
		res['msg'] = 'Exception while retrieving user '+str(sys.exc_info()[0])
		logger.exception("Unexpected error while user: "******"application/json")		

	try:
		res = store_tagged_places(user)
	except:
		res['valid'] = False
		res['msg'] = 'Exception while retrieving user data '+str(sys.exc_info()[0])
		logger.exception("Unexpected error while user's tagged places: ", sys.exc_info()[0])
		return HttpResponse(json.dumps(res), content_type="application/json")		

	return HttpResponse(json.dumps(res), content_type="application/json")