Exemple #1
0
    def post(self):
        #get login agruments. Arguments include facebook accessToken, expiration date, and user fb_id
        from wigi.models.models import User, WigiTokens
        assert(self.get_argument("wigi_accessToken"))
        assert(self.get_argument("wigi_expr_token"))
        assert(self.get_argument("wigi_fb_id"))

        #check if user exists
        if(User.doesUserExist(int(self.get_argument("wigi_fb_id")))):
            #if user exists, check accesstoken
	    user = User.getUserForFbId(int(self.get_argument("wigi_fb_id")))
	    if user.access_token == self.get_argument("wigi_accessToken"):
	        print "user's accesstoken matches"
        	#if accesstoken matches, get secure wigi token
		userToken = WigiTokens.getTokenForUser(user)
		#if no token exists for user, create one
		if not userToken:
		    userToken = WigiTokens.createNewToken(user)
		#write token to response
	        self.write(tornado.escape.json_encode({'wigi_token':userToken.wigi_token,
							'wigi_id':user.id}))
	        self.finish()
            else:
		#Most likely new access token was given 
		#create new token for user based on new access token
		userToken = WigiTokens.createNewToken(user)
		#write token to response
	        self.write(tornado.escape.json_encode({'wigi_token':userToken.wigi_token,
							'wigi_id':user.id}))
                self.finish()
        else:
           print "user doesn't exist"
	   #if user doesn't exist, determine if accesstoken is valid and create user, then create wigi token and write to response
           self.accessToken = self.get_argument("wigi_accessToken")
	   self.facebook_request("/me",
                                access_token=self.get_argument("wigi_accessToken"),
                                callback=self.async_callback(self.__is_access_token_valid))
Exemple #2
0
    def __is_access_token_valid(self,response):
        from wigi.models.models import User, WigiTokens
        if not response:
            print "here"
	    #invalid access token
            self.finish(tornado.escape.json_encode({'error':'Access token invalid'}))
            return
        else:
            #create user and wigi token, write token to response
            #verify user is who they say they are
            if(response.get('id') == self.get_argument('wigi_fb_id')):
	        print "creating new user id: %s token: %s" %(response.get('id'), self.get_argument('wigi_accessToken'))
            	newUser = User.createNewUser(int(response.get('id')), self.get_argument('wigi_accessToken'))
                #create wigi token
                userToken = WigiTokens.createNewToken(newUser)
                print userToken.wigi_token
                self.write(tornado.escape.json_encode({'wigi_token':userToken.wigi_token,
							'wigi_id':newUser.id}))
                self.finish() 
Exemple #3
0
    def post(self,user_id):
        print "in itemhandler post for user: "******"user found!"
           # print self.get_argument('wigi_access_token')
            token = WigiTokens.getTokenForUser(user)
            if token:
                if self.get_argument('wigi_access_token') == token.wigi_token :
	            print "access token matches"
		    saveDir = "%s%s/items/" % (options.media_dir,str(user_id))
		    print "save directory : " + saveDir
                    if (not os.path.exists(saveDir)):
                        #directory doesn't exist
			#create, then save image
			os.makedirs(saveDir)
		    try:
		        im = cStringIO.StringIO(self.request.files["wigi_item_image"][0]['body'])	
 	   	        img = Image.open(im)
			print img.format, img. size, img.mode
		        imageFilePath= saveDir + str(datetime.datetime.now()).replace(" ","_") +"_" + str(user_id) + ".jpeg"
		   	img.save(imageFilePath)
			#save item, image tags and comments to db
                        newItem = WigiItems.addNewItemForUser(user, imageFilePath,'want_it', self.get_argument('wigi_item_comment'))	
                        ItemTags.addTagForItem(newItem,self.get_argument('wigi_item_tag'))
                    except IOError as e:
		        print "CANNOT CONVERT FILE: " + e.__str__()
                 
 	            self.finish("thanks")
            else:
                self.finish("Error retrieving Token")
	else:
	    #error retrieving user
            self.finish("Error adding item to wigi")