Beispiel #1
0
def fblogin(request):
	if request.user.is_authenticated():
		return HttpResponseRedirect('/')
	
	user = facebook.get_user_from_cookie(request.COOKIES, settings.FACEBOOK_APP_ID, settings.FACEBOOK_SECRET_KEY)
	
	if user:
		graph = facebook.GraphAPI(user["access_token"])
		profile = graph.get_object("me")
		
		if 'first_name' in profile and 'last_name' in profile and 'email' in profile:
			login_or_register(request, profile['email'], profile['first_name']+ ' ' + profile['last_name'])
			
			request.session['facebook'] = True
			
			if 'next' in request.GET:
				url = request.GET['next']
			
			
			if 'url' in request.POST and request.POST['url'] != '':
				return HttpResponseRedirect('/' + request.POST['url'][1:])
			else:
				return HttpResponseRedirect('/')
		else:
			return HttpResponse('Napaka pri prijavi!')
	else:
		return HttpResponseRedirect('/')
Beispiel #2
0
    def current_user(self):
        if users.get_current_user():
            user = users.get_current_user()
            localUser = db.GqlQuery("select * from User where userID = :1", user.user_id()).get()
            if not localUser:
                user = User(userID=user.user_id(),
                            nickname=user.nickname())
                user.put()
                self.redirect('/user/edit?userKey=%s' % user.key())
            else:
                user = localUser
            self._current_user = user
        elif not hasattr(self, "_current_user"):
            self._current_user = None
            cookie = facebook.get_user_from_cookie(self.request.cookies, 
                                                   facebook.FACEBOOK_APP_ID, 
                                                   facebook.FACEBOOK_APP_SECRET)
            if cookie:
                # Store a local instance of the user data so we don't need
                # a round-trip to Facebook on every request
                user = User.get_by_key_name(cookie["uid"])
                if not user:
                    graph = facebook.GraphAPI(cookie["access_token"])
                    try:
                        profile = graph.get_object("me")
                    except facebook.GraphAPIError:
                        self._current_user = None
                        return None
                    
                    user = User(key_name=str(profile["id"]),
                                userID=str(profile["id"]),
                                nickname=profile["name"],
                                fbProfile_url=profile["link"],
                                email=profile["email"],
                                access_token=cookie["access_token"])
                    logging.info("New User object created for %s (%s)" % (profile["name"],profile["id"]))
                    user.put()
                    
                    # log event
                    event.createEvent(event.EVENT_USER_ADD, user, None, profile["id"])

                elif user.access_token != cookie["access_token"]:
                    user.access_token = cookie["access_token"]
                    user.put()
                self._current_user = user
        return self._current_user