示例#1
0
 def test_taxonomy(self):
     """bibknowledge - test a taxonomy (must run as bibsched user)"""
     import mechanize
     from os import remove
     from invenio.bibknowledge import get_kbt_items_for_bibedit, add_kb, \
         get_kb_name, delete_kb, kb_exists
     username = "******"
     password = "******"
     #create a new taxonomy kb
     new_kb_id = add_kb("testtaxonomy", "taxonomy")
     #what was the name?
     new_kb_name = get_kb_name(new_kb_id)
     #get the taxonomy file
     response = mechanize.urlopen(
         "http://invenio-software.org/download/invenio-demo-site-files/HEP.rdf"
     )
     content = response.read()
     f = open(CFG_TMPDIR + "/HEP.rdf", "w")
     f.write(content)
     f.close()
     #upload it to the right destination, but log in first
     browser = mechanize.Browser()
     browser.open(CFG_SITE_SECURE_URL + "/youraccount/login")
     browser.select_form(nr=0)
     browser['nickname'] = username
     browser['password'] = password
     browser.submit()
     #go to upload page
     uploadpage = browser.open(CFG_SITE_URL + "/kb?kb=" + str(new_kb_id))
     #check that we are there
     content = uploadpage.read()
     namethere = content.count("testtaxonomy")
     assert namethere > 0
     #upload
     browser.open(CFG_SITE_URL + "/kb?kb=" + str(new_kb_id))
     browser.select_form(name="upload")
     browser.form["kb"] = str(new_kb_id)  #force the id
     browser.form.add_file(open(CFG_TMPDIR + "/HEP.rdf"),
                           content_type='text/plain',
                           filename="HEP.rdf",
                           name="file")
     browser.submit()
     #check that we can get an item from the kb
     items = get_kbt_items_for_bibedit(new_kb_name, "prefLabel",
                                       "Altarelli")
     #item should contain 1 string: 'Altarelli-Parisi equation'
     self.assertEqual(1, len(items))
     #delete the temp file
     remove(CFG_TMPDIR + "/HEP.rdf")
     #delete the test odf the DB
     delete_kb(new_kb_name)
     still_there = kb_exists(new_kb_name)
     self.assertEqual(False, still_there)
 def test_taxonomy(self):
     """bibknowledge - test a taxonomy (must run as bibsched user)"""
     import mechanize
     from os import remove
     username = "******"
     password = "******"
     #create a new taxonomy kb
     new_kb_id = add_kb("testtaxonomy","taxonomy")
     #what was the name?
     new_kb_name = get_kb_name(new_kb_id)
     #get the taxonomy file
     response = mechanize.urlopen("http://invenio-software.org/download/invenio-demo-site-files/HEP.rdf")
     content = response.read()
     f = open(CFG_TMPDIR+"/HEP.rdf","w")
     f.write(content)
     f.close()
     #upload it to the right destination, but log in first
     browser = mechanize.Browser()
     browser.open(CFG_SITE_SECURE_URL + "/youraccount/login")
     browser.select_form(nr=0)
     browser['p_un'] = username
     browser['p_pw'] = password
     browser.submit()
     #go to upload page
     uploadpage = browser.open(CFG_SITE_URL+"/kb?kb="+str(new_kb_id))
     #check that we are there
     content = uploadpage.read()
     namethere = content.count("testtaxonomy")
     assert namethere > 0
     #upload
     browser.open(CFG_SITE_URL+"/kb?kb="+str(new_kb_id))
     browser.select_form(name="upload")
     browser.form["kb"] = str(new_kb_id) #force the id
     browser.form.add_file(open(CFG_TMPDIR+"/HEP.rdf"), content_type='text/plain', filename="HEP.rdf", name="file")
     browser.submit()
     #check that we can get an item from the kb
     items = get_kbt_items_for_bibedit(new_kb_name, "prefLabel", "Altarelli")
     #item should contain 1 string: 'Altarelli-Parisi equation'
     self.assertEqual(1, len(items))
     #delete the temp file
     remove(CFG_TMPDIR+"/HEP.rdf")
     #delete the test odf the DB
     delete_kb(new_kb_name)
     still_there = kb_exists(new_kb_name)
     self.assertEqual(False, still_there)
示例#3
0
def perform_request_autocomplete(request_type, recid, uid, data):
    """
    Perfrom an AJAX request associated with the retrieval of autocomplete
    data.

    Arguments:
        request_type: Type of the currently served request
        recid: the identifer of the record
        uid: The identifier of the user being currently logged in
        data: The request data containing possibly important additional
              arguments
    """
    response = {}
    # get the values based on which one needs to search
    searchby = data["value"]
    # we check if the data is properly defined
    fulltag = ""
    if data.has_key("maintag") and data.has_key("subtag1") and data.has_key("subtag2") and data.has_key("subfieldcode"):
        maintag = data["maintag"]
        subtag1 = data["subtag1"]
        subtag2 = data["subtag2"]
        u_subtag1 = subtag1
        u_subtag2 = subtag2
        if (not subtag1) or (subtag1 == " "):
            u_subtag1 = "_"
        if (not subtag2) or (subtag2 == " "):
            u_subtag2 = "_"
        subfieldcode = data["subfieldcode"]
        fulltag = maintag + u_subtag1 + u_subtag2 + subfieldcode
    if request_type == "autokeyword":
        # call the keyword-form-ontology function
        if fulltag and searchby:
            items = get_kbt_items_for_bibedit(CFG_BIBEDIT_KEYWORD_TAXONOMY, CFG_BIBEDIT_KEYWORD_RDFLABEL, searchby)
            response["autokeyword"] = items
    if request_type == "autosuggest":
        # call knowledge base function to put the suggestions in an array..
        if fulltag and searchby and len(searchby) > 3:
            suggest_values = get_kbd_values_for_bibedit(fulltag, "", searchby)
            # remove ..
            new_suggest_vals = []
            for sugg in suggest_values:
                if sugg.startswith(searchby):
                    new_suggest_vals.append(sugg)
            response["autosuggest"] = new_suggest_vals
    if request_type == "autocomplete":
        # call the values function with the correct kb_name
        if CFG_BIBEDIT_AUTOCOMPLETE_TAGS_KBS.has_key(fulltag):
            kbname = CFG_BIBEDIT_AUTOCOMPLETE_TAGS_KBS[fulltag]
            # check if the seachby field has semicolons. Take all
            # the semicolon-separated items..
            items = []
            vals = []
            if searchby:
                if searchby.rfind(";"):
                    items = searchby.split(";")
                else:
                    items = [searchby.strip()]
            for item in items:
                item = item.strip()
                kbrvals = get_kbr_values(kbname, item, "", "e")  # we want an exact match
                if kbrvals and kbrvals[0]:  # add the found val into vals
                    vals.append(kbrvals[0])
            # check that the values are not already contained in other
            # instances of this field
            record = get_cache_file_contents(recid, uid)[2]
            xml_rec = print_rec(record)
            record, status_code, dummy_errors = create_record(xml_rec)
            existing_values = []
            if status_code != 0:
                existing_values = record_get_field_values(record, maintag, subtag1, subtag2, subfieldcode)
            # get the new values.. i.e. vals not in existing
            new_vals = vals
            for val in new_vals:
                if val in existing_values:
                    new_vals.remove(val)
            response["autocomplete"] = new_vals
    response["resultCode"] = CFG_BIBEDIT_AJAX_RESULT_CODES_REV["autosuggestion_scanned"]
    return response