def test_kb_attribute_update(self):
     """bibknowledge - attribute modifications persist in database"""
     # NB: Tested here because with the exception of get_kb_description,
     #     these are exported by module bibknowledge. This mostly is
     #     exercising bibknowledge_dblayer, though.
     dyn_kb_oldname = self.dyn_kbname
     dyn_kb_newname = self._name_a_db()
     junk_desc = self._randomstring()
     dyn_kb_id = get_kb_id(dyn_kb_oldname)
     # we created it so we know it has no desc
     self.assertEqual('', get_kb_description(dyn_kb_oldname))
     # now check that we can rename it
     update_kb_attributes(dyn_kb_oldname, dyn_kb_newname)
     self.assertEqual(None, get_kb_id(dyn_kb_oldname))
     self.assertEqual(dyn_kb_id, get_kb_id(dyn_kb_newname))
     # now check that we can tag it with a description
     update_kb_attributes(dyn_kb_newname, dyn_kb_newname, junk_desc)
     self.assertEqual(junk_desc, get_kb_description(dyn_kb_newname))
     update_kb_attributes(dyn_kb_newname, dyn_kb_oldname)
 def test_kb_attribute_update(self):
     """bibknowledge - attribute modifications persist in database"""
     # NB: Tested here because with the exception of get_kb_description,
     #     these are exported by module bibknowledge. This mostly is
     #     exercising bibknowledge_dblayer, though.
     dyn_kb_oldname = self.dyn_kbname
     dyn_kb_newname = self._name_a_db()
     junk_desc      = self._randomstring()
     dyn_kb_id      = get_kb_id(dyn_kb_oldname)
     # we created it so we know it has no desc
     self.assertEqual('', get_kb_description(dyn_kb_oldname))
     # now check that we can rename it
     update_kb_attributes(dyn_kb_oldname, dyn_kb_newname)
     self.assertEqual(None, get_kb_id(dyn_kb_oldname))
     self.assertEqual(dyn_kb_id, get_kb_id(dyn_kb_newname))
     # now check that we can tag it with a description
     update_kb_attributes(dyn_kb_newname, dyn_kb_newname, junk_desc)
     self.assertEqual(junk_desc, get_kb_description(dyn_kb_newname))
     update_kb_attributes(dyn_kb_newname, dyn_kb_oldname)
Example #3
0
def kb_update_attributes(req, kb="", name="", description="", sortby="to",
                         ln=CFG_SITE_LANG, chosen_option=None, kb_type=None):
    """
    Update the attributes of the kb

    @param ln language
    @param kb the kb id to update
    @param sortby the sorting criteria ('from' or 'to')
    @param name the new name of the kn
    @param description the new description of the kb
    @param chosen_option set to dialog box value
    """

    ln = wash_language(ln)
    _ = gettext_set_language(ln)

    navtrail_previous_links = ''' &gt; <a class="navtrail" href="%s/kb?ln=%s">%s</a>''' % (CFG_SITE_SECURE_URL, ln, _("Manage Knowledge Bases"))

    try:
        dummy = getUid(req)
    except:
        return error_page('Error', req)

    (auth_code, auth_msg) = check_user(req, 'cfgbibknowledge')
    if not auth_code:
        kb_id = wash_url_argument(kb, 'int')
        if chosen_option is not None:
            # Update could not be performed.
            # Redirect to kb attributes page
            redirect_to_url(req, "kb?ln=%(ln)s&amp;action=attributes&amp;kb=%(kb)s&sortby=%(sortby)s&kb_type=%(kb_type)s" % {'ln':ln, 'kb':kb_id, 'sortby':sortby, 'kb_type':kb_type})


        kb_name = bibknowledge.get_kb_name(kb_id)

        if kb_name is None:
            return page(title=_("Unknown Knowledge Base"),
                        body = "",
                        language=ln,
                        navtrail = navtrail_previous_links,
                        errors = [("ERR_KB_ID_UNKNOWN", kb)],
                        lastupdated=__lastupdated__,
                        req=req)

        new_name = wash_url_argument(name, 'str')
        if kb_name != new_name and bibknowledge.kb_exists(new_name):
            #A knowledge base with that name already exist
            #Do not update
            return dialog_box(req=req,
                              ln=ln,
                              title="Name already in use",
                              message="""<i>%s</i> cannot be renamed to %s:
                                        Another knowledge base already has that name.
                                        <br/>Please choose another name.""" % (kb_name,
                                                                             new_name),
                              navtrail=navtrail_previous_links,
                              options=[ _("Ok")])

        new_desc = wash_url_argument(description, 'str')
        bibknowledge.update_kb_attributes(kb_name, new_name, new_desc)
        redirect_to_url(req, "kb?ln=%(ln)s&kb=%(kb)s&sortby=%(sortby)s" % {'ln':ln, 'kb':kb_id, 'sortby':sortby})
    else:
        return page_not_authorized(req=req,
                                   text=auth_msg,
                                   navtrail=navtrail_previous_links)
Example #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# InstitutionsCollection_dynamic_KB.py
"""Create a dynamic KB from the instutions collection, and annotate it."""

import sys

from invenio import bibknowledge

kbname = 'InstitutionsCollection'
collection = 'Institutions'
display_field = '110__u'
search_field = '371__a:"*%*" | 110__u:"*%*"'
kbdesc = "A dynamic KB which searches all 371a and 110u fields in the " +\
         "institutions collection, and returns those records' corresponding " + \
         "110u fields."

if __name__ == "__main__":
    bibknowledge.add_dynamic_kb(kbname, display_field, collection,
                                search_field)
    bibknowledge.update_kb_attributes(kbname, kbname, kbdesc)
Example #5
0
def kb_update_attributes(req,
                         kb="",
                         name="",
                         description="",
                         sortby="to",
                         ln=CFG_SITE_LANG,
                         chosen_option=None,
                         kb_type=None):
    """
    Update the attributes of the kb

    @param ln language
    @param kb the kb id to update
    @param sortby the sorting criteria ('from' or 'to')
    @param name the new name of the kn
    @param description the new description of the kb
    @param chosen_option set to dialog box value
    """

    ln = wash_language(ln)
    _ = gettext_set_language(ln)

    navtrail_previous_links = ''' &gt; <a class="navtrail" href="%s/kb?ln=%s">%s</a>''' % (
        CFG_SITE_SECURE_URL, ln, _("Manage Knowledge Bases"))

    try:
        dummy = getUid(req)
    except MySQLdb.Error:
        return error_page(req)

    (auth_code, auth_msg) = check_user(req, 'cfgbibknowledge')
    if not auth_code:
        kb_id = wash_url_argument(kb, 'int')
        if chosen_option is not None:
            # Update could not be performed.
            # Redirect to kb attributes page
            redirect_to_url(
                req,
                "kb?ln=%(ln)s&amp;action=attributes&amp;kb=%(kb)s&sortby=%(sortby)s&kb_type=%(kb_type)s"
                % {
                    'ln': ln,
                    'kb': kb_id,
                    'sortby': sortby,
                    'kb_type': kb_type
                })

        kb_name = bibknowledge.get_kb_name(kb_id)

        if kb_name is None:
            return page(title=_("Unknown Knowledge Base"),
                        body="",
                        language=ln,
                        navtrail=navtrail_previous_links,
                        errors=[("ERR_KB_ID_UNKNOWN", kb)],
                        lastupdated=__lastupdated__,
                        req=req)

        new_name = wash_url_argument(name, 'str')
        if kb_name != new_name and bibknowledge.kb_exists(new_name):
            #A knowledge base with that name already exist
            #Do not update
            return dialog_box(req=req,
                              ln=ln,
                              title="Name already in use",
                              message="""<i>%s</i> cannot be renamed to %s:
                                        Another knowledge base already has that name.
                                        <br/>Please choose another name.""" %
                              (kb_name, new_name),
                              navtrail=navtrail_previous_links,
                              options=[_("Ok")])

        new_desc = wash_url_argument(description, 'str')
        bibknowledge.update_kb_attributes(kb_name, new_name, new_desc)
        redirect_to_url(
            req, "kb?ln=%(ln)s&kb=%(kb)s&sortby=%(sortby)s" % {
                'ln': ln,
                'kb': kb_id,
                'sortby': sortby
            })
    else:
        return page_not_authorized(req=req,
                                   text=auth_msg,
                                   navtrail=navtrail_previous_links)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ExperimentsCollection_dynamic_KB.py
"""Create a dynamic KB from the experiments collection, and annotate it."""

import sys

from invenio import bibknowledge

kbname = 'ExperimentsCollection'
collection = 'Experiments'
display_field = '119__a'
search_field = '119__a:"*%*" | 119__u:"*%*" | 419__a:"*%*" | 245__a:"*%*"'
kbdesc = "A dynamic KB which searches all 119a fields in the " +\
         "experiments collection, and returns those records' corresponding " + \
         "119a fields."


if __name__ == "__main__":
    bibknowledge.add_dynamic_kb(kbname, display_field, collection, search_field)
    bibknowledge.update_kb_attributes(kbname, kbname, kbdesc)