def Test_AddMasterTagsToDataStore(self):

        u1 = MyUser(None, "Bill")
        u1.put()
        u2 = MyUser(None, "Ted")
        u2.put()

        file = open("E:\\eclipse\\testworkspace\\zardoztestzone\\src\\DeliciousTagExplorer\\UnitTests\\shortTagDag\\shorttaglist.json", "r")
        dag = TagDAGBuilderClass()
        dict = dag.StoreMasterTagList(u1, file.read())
        file.close()
        
        # add an extra vertex to represent the second user's data
        tv = TagVertex(u2, "wild thing")
        tv.put()

        # check that the returned dictionary is complete
        check.ok_(dict.has_key("28mm") == True)
        check.ok_(dict.has_key("2mm") == True)
        check.ok_(dict.has_key("6mm") == True)
        check.ok_(dict.has_key("accessories") == True)
        check.ok_(dict.has_key("actionscript") == True)
        check.ok_(dict.has_key("activities") == True)
        check.ok_(dict.has_key("d'oh!") == False)
        check.ok_(dict.has_key("wild thing") == False)
        
        #check that the complete set of vertices is correct
        dict2 = {}
        alist = dag.GetCompleteVertexSet()
        for tag in alist:
            dict2[tag.key().name()] = tag.ttlCount
        #endfor    
        check.ok_(dict2.has_key("28mm") == True)
        check.ok_(dict2.has_key("2mm") == True)
        check.ok_(dict2.has_key("6mm") == True)
        check.ok_(dict2.has_key("accessories") == True)
        check.ok_(dict2.has_key("actionscript") == True)
        check.ok_(dict2.has_key("activities") == True)
        check.ok_(dict2.has_key("d'oh!") == False)
        check.ok_(dict2.has_key("wild thing") == True)
        
        #check we can get a set of vertices for each user
        dict3 = {}
        alist = dag.GetCompleteVertexSetForNamedUser("Bill")
        for tag in alist:
            dict3[tag.key().name()] = tag.ttlCount
        #endfor    
        check.ok_(dict3.has_key("28mm") == True)
        check.ok_(dict3.has_key("2mm") == True)
        check.ok_(dict3.has_key("6mm") == True)
        check.ok_(dict3.has_key("accessories") == True)
        check.ok_(dict3.has_key("actionscript") == True)
        check.ok_(dict3.has_key("activities") == True)
        check.ok_(dict3.has_key("d'oh!") == False)
        check.ok_(dict3.has_key("wild thing") == False)

        dict4 = {}
        alist = dag.GetCompleteVertexSetForNamedUser("Ted")
        for tag in alist:
            dict4[tag.key().name()] = tag.ttlCount
        #endfor    
        check.ok_(dict4.has_key("28mm") == False)
        check.ok_(dict4.has_key("2mm") == False)
        check.ok_(dict4.has_key("6mm") == False)
        check.ok_(dict4.has_key("accessories") == False)
        check.ok_(dict4.has_key("actionscript") == False)
        check.ok_(dict4.has_key("activities") == False)
        check.ok_(dict4.has_key("d'oh!") == False)
        check.ok_(dict4.has_key("wild thing") == True)

        testVertexKey = db.Key.from_path("MyUser", "Bill", "TagVertex", "28mm")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex.key().name() == "28mm")
        check.ok_(testVertex.ttlCount == 62)
        check.ok_(testVertex.parent().key() == u1.key())

        testVertexKey = db.Key.from_path("MyUser", "Bill", "TagVertex", "2mm")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex.key().name() == "2mm")
        check.ok_(testVertex.ttlCount == 1)

        testVertexKey = db.Key.from_path("MyUser", "Bill", "TagVertex", "6mm")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex.key().name() == "6mm")
        check.ok_(testVertex.ttlCount == 29)

        testVertexKey = db.Key.from_path("MyUser", "Bill", "TagVertex", "accessories")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex.key().name() == "accessories")
        check.ok_(testVertex.ttlCount == 52)

        testVertexKey = db.Key.from_path("MyUser", "Bill", "TagVertex", "actionscript")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex.key().name() == "actionscript")
        check.ok_(testVertex.ttlCount == 1)

        testVertexKey = db.Key.from_path("MyUser", "Bill", "TagVertex", "activities")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex.key().name() == "activities")
        check.ok_(testVertex.ttlCount == 3)

        #try to get an existing tag with the wrong user
        testVertexKey = db.Key.from_path("MyUser", "Ted", "TagVertex", "activities")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex == None)

        #try to get a tag that does not exist
        testVertexKey = db.Key.from_path("MyUser", "Bill", "TagVertex", "d'oh!")
        testVertex = db.get(testVertexKey)
        check.ok_(testVertex == None)

        # test that an empty tag string does not cause an exception
        dict = dag.StoreMasterTagList(u2, "")
        check.ok_(len(dict) == 0)
        s = None
        dict = dag.StoreMasterTagList(u2, s)
        check.ok_(len(dict) == 0)
Exemple #2
0
    def post_worker(self, tagSource):

        userName = cgi.escape(self.request.get('getUserName'))
        if userName is not None:

            tdbc = TagDAGBuilderClass()
            dbm = MyMetaDataManager()
     
            #do we already know this user?
            user = dbm.GetUser(userName)
            if user is None:
                logging.info("Fetching master tag list for NEW user '" + userName + "'")
    
                #see if the user is known to Delicious *and* has some tags
                tagsetString = tagSource.FetchMasterTagList(userName)
                if tagsetString is None or len(tagsetString) < 2:
                    logging.error("NEW user '" + userName + "' has no tags!")
                    raise Exception("The user '" + userName + "' has no tags!")
                #endif
                
                #new user, with tags, if we get here...
                user = dbm.NewUser(userName)
                tdbc.StoreMasterTagList(user, tagsetString)
            else:
                #else we have the tags for them already...    
                logging.info("Regenerating master tag list for KNOWN user '" + userName + "'")
            #endif
            
            #reset the session even if it is the same "user"
            session = get_current_session()
            if session is not None:
                if session.is_active():
                    session.terminate()
                #endif   
                session['user'] = user
                session['name'] = userName
            #endif         
            
            taglist = tdbc.GetCompleteVertexSetForNamedUser(user.key().name())
            #sort alphabetically
            taglist.sort(key=lambda tag: unicode.lower(tag.key().name()))
    
            # compute the average count of tags and only show above-average ones
            # in the cloud.
            for tag in taglist:
                user.IncTagCount(tag.ttlCount)
            #endfor
            user.GenerateTagStats()
            
            #form up the expected tag cloud json
            '''
            var word_list = [{text: "Lorem", weight: 15},
                             {text: "Ipsum", weight: 9, url: "http://jquery.com/", title: "jQuery Rocks!"},
                             {text: "Dolor", weight: 6},
                             {text: "Sit", weight: 7},
                             {text: "Amet", weight: 5}
                           ...other words
            '''
            word_list = "["
            autocomplete_list = "["
            
            filteredCount = 0
            for tag in taglist:
                autocomplete_list = autocomplete_list + '"' + tag.key().name() + '",\n'
                if tag.ttlCount >= user.tagCountAverage:
                    filteredCount = filteredCount + 1
                    word_list = word_list + '{text: "' + tag.key().name() + ' ", ' 
                    word_list = word_list + "weight: " + str(tag.ttlCount) + "}, " + "\n"
                #endif    
            #endfor
            word_list = word_list.rstrip(", \n") + "]"
            autocomplete_list = autocomplete_list.rstrip(", \n") + "]" 
    
            logging.info("Found " + str(user.numTags) + " for user '" + userName + "'. ")
            logging.info("Returned " + str(filteredCount) + " tags for the cloud.")
        
            return (word_list, autocomplete_list)
        else:
            logging.error("Bad or unknown user passed into StartTagSelectorClass::post() '" + userName + "'")
            raise Exception("Bad or unknown user passed into StartTagSelectorClass::post() '" + userName + "'")
        #endif
        
        return (None, None)
    
    #end post_worker    
    
#end class StartTagSelectorClass
#eof