Example #1
0
def perform_request_init(uid, ln, req, lastupdated):
    """Handle the initial request by adding menu and JavaScript to the page."""
    errors = []
    warnings = []
    body = ""

    # Add script data.
    record_templates = get_record_templates()
    record_templates.sort()
    tag_names = get_name_tags_all()
    protected_fields = ["001"]
    protected_fields.extend(CFG_BIBEDIT_PROTECTED_FIELDS.split(","))
    history_url = '"' + CFG_SITE_URL + '/admin/bibedit/bibeditadmin.py/history"'
    cern_site = "false"

    if not simplejson_available:
        title = "Record Editor"
        body = """Sorry, the record editor cannot operate when the
                `simplejson' module is not installed.  Please see the INSTALL
                file."""
        return page(
            title=title,
            body=body,
            errors=[],
            warnings=[],
            uid=uid,
            language=ln,
            navtrail="",
            lastupdated=lastupdated,
            req=req,
        )

    if CFG_CERN_SITE:
        cern_site = "true"
    data = {
        "gRECORD_TEMPLATES": record_templates,
        "gTAG_NAMES": tag_names,
        "gPROTECTED_FIELDS": protected_fields,
        "gSITE_URL": '"' + CFG_SITE_URL + '"',
        "gHISTORY_URL": history_url,
        "gCERN_SITE": cern_site,
        "gHASH_CHECK_INTERVAL": CFG_BIBEDIT_JS_HASH_CHECK_INTERVAL,
        "gCHECK_SCROLL_INTERVAL": CFG_BIBEDIT_JS_CHECK_SCROLL_INTERVAL,
        "gSTATUS_ERROR_TIME": CFG_BIBEDIT_JS_STATUS_ERROR_TIME,
        "gSTATUS_INFO_TIME": CFG_BIBEDIT_JS_STATUS_INFO_TIME,
        "gCLONED_RECORD_COLOR": '"' + CFG_BIBEDIT_JS_CLONED_RECORD_COLOR + '"',
        "gCLONED_RECORD_COLOR_FADE_DURATION": CFG_BIBEDIT_JS_CLONED_RECORD_COLOR_FADE_DURATION,
        "gNEW_ADD_FIELD_FORM_COLOR": '"' + CFG_BIBEDIT_JS_NEW_ADD_FIELD_FORM_COLOR + '"',
        "gNEW_ADD_FIELD_FORM_COLOR_FADE_DURATION": CFG_BIBEDIT_JS_NEW_ADD_FIELD_FORM_COLOR_FADE_DURATION,
        "gNEW_CONTENT_COLOR": '"' + CFG_BIBEDIT_JS_NEW_CONTENT_COLOR + '"',
        "gNEW_CONTENT_COLOR_FADE_DURATION": CFG_BIBEDIT_JS_NEW_CONTENT_COLOR_FADE_DURATION,
        "gNEW_CONTENT_HIGHLIGHT_DELAY": CFG_BIBEDIT_JS_NEW_CONTENT_HIGHLIGHT_DELAY,
        "gTICKET_REFRESH_DELAY": CFG_BIBEDIT_JS_TICKET_REFRESH_DELAY,
        "gRESULT_CODES": CFG_BIBEDIT_AJAX_RESULT_CODES,
        "gAUTOSUGGEST_TAGS": CFG_BIBEDIT_AUTOSUGGEST_TAGS,
        "gAUTOCOMPLETE_TAGS": CFG_BIBEDIT_AUTOCOMPLETE_TAGS_KBS.keys(),
        "gKEYWORD_TAG": '"' + CFG_BIBEDIT_KEYWORD_TAG + '"',
    }
    body += '<script type="text/javascript">\n'
    for key in data:
        body += "    var %s = %s;\n" % (key, data[key])
    body += "    </script>\n"

    # Adding the information about field templates
    fieldTemplates = get_available_fields_templates()
    body += "<script>\n" + "   var fieldTemplates = %s\n" % (json.dumps(fieldTemplates),) + "</script>\n"
    # Add scripts (the ordering is NOT irrelevant).
    scripts = [
        "jquery.min.js",
        "jquery.effects.core.min.js",
        "jquery.effects.highlight.min.js",
        "jquery.autogrow.js",
        "jquery.jeditable.mini.js",
        "jquery.hotkeys.min.js",
        "json2.js",
        "bibedit_display.js",
        "bibedit_engine.js",
        "bibedit_keys.js",
        "bibedit_menu.js",
        "bibedit_holdingpen.js",
        "marcxml.js",
        "bibedit_clipboard.js",
    ]

    for script in scripts:
        body += '    <script type="text/javascript" src="%s/js/%s">' "</script>\n" % (CFG_SITE_URL, script)

    # Build page structure and menu.
    # rec = create_record(format_record(235, "xm"))[0]
    # oaiId = record_extract_oai_id(rec)

    body += bibedit_templates.menu()
    body += '    <div id="bibEditContent"></div>\n'

    return body, errors, warnings
Example #2
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