Ejemplo n.º 1
0
 def test_kb_attribute_update(self):
     """bibknowledge - attribute modifications persist in database"""
     from invenio.modules.knowledge.api import update_kb_attributes, get_kb_id
     from invenio.modules.knowledge.dblayer import get_kb_description
     # 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)
Ejemplo n.º 2
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)
Ejemplo n.º 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)