def _get_record(recid, uid, result, fresh_record=False): """Retrieve record structure. """ record = None mtime = None cache_dirty = None record_status = record_exists(recid) existing_cache = cache_exists(recid, uid) if record_status == 0: result['resultCode'], result[ 'resultText'] = 1, 'Non-existent record: %s' % recid elif record_status == -1: result['resultCode'], result[ 'resultText'] = 1, 'Deleted record: %s' % recid elif not existing_cache and record_locked_by_other_user(recid, uid): result['resultCode'], result[ 'resultText'] = 1, 'Record %s locked by user' % recid elif existing_cache and cache_expired(recid, uid) and \ record_locked_by_other_user(recid, uid): result['resultCode'], result[ 'resultText'] = 1, 'Record %s locked by user' % recid elif record_locked_by_queue(recid): result['resultCode'], result[ 'resultText'] = 1, 'Record %s locked by queue' % recid else: if fresh_record: delete_cache(recid, uid) existing_cache = False if not existing_cache: record_revision, record = create_cache(recid, uid) mtime = get_cache_mtime(recid, uid) cache_dirty = False else: tmpRes = get_cache_contents(recid, uid) cache_dirty, record_revision, record = tmpRes[0], tmpRes[ 1], tmpRes[2] touch_cache(recid, uid) mtime = get_cache_mtime(recid, uid) if not latest_record_revision(recid, record_revision): result['cacheOutdated'] = True result['resultCode'], result['resultText'], result[ 'cacheDirty'], result[ 'cacheMTime'] = 0, 'Record OK', cache_dirty, mtime record_order_subfields(record) return record
def _get_record(recid, uid, result, fresh_record=False): """Retrieve record structure. """ record = None mtime = None cache_dirty = None record_status = record_exists(recid) existing_cache = cache_exists(recid, uid) if record_status == 0: result['resultCode'], result['resultText'] = 1, 'Non-existent record: %s' % recid elif record_status == -1: result['resultCode'], result['resultText'] = 1, 'Deleted record: %s' % recid elif not existing_cache and record_locked_by_other_user(recid, uid): result['resultCode'], result['resultText'] = 1, 'Record %s locked by user' % recid elif existing_cache and cache_expired(recid, uid) and \ record_locked_by_other_user(recid, uid): result['resultCode'], result['resultText'] = 1, 'Record %s locked by user' % recid elif record_locked_by_queue(recid): result['resultCode'], result['resultText'] = 1, 'Record %s locked by queue' % recid else: if fresh_record: delete_cache(recid, uid) existing_cache = False if not existing_cache: record_revision, record = create_cache(recid, uid) mtime = get_cache_mtime(recid, uid) cache_dirty = False else: tmpRes = get_cache_contents(recid, uid) cache_dirty, record_revision, record = tmpRes[0], tmpRes[1], tmpRes[2] touch_cache(recid, uid) mtime = get_cache_mtime(recid, uid) if not latest_record_revision(recid, record_revision): result['cacheOutdated'] = True result['resultCode'], result['resultText'], result['cacheDirty'], result['cacheMTime'] = 0, 'Record OK', cache_dirty, mtime record_order_subfields(record) return record
def index(self, req, form): """ Display live bibz39 queue """ referer = '/admin2/bibz39/' navtrail = ' <a class="navtrail" href=\"%s/help/admin\">Admin Area</a> ' % CFG_SITE_URL auth_code, auth_message = acc_authorize_action(req, 'runbibedit') if auth_code != 0: return page_not_authorized(req=req, referer=referer, text=auth_message, navtrail=navtrail) argd = wash_urlargd(form, { 'search': (str, ''), 'marcxml': (str, ''), 'server': (list, []), 'search_type': (str, ''), }) if argd['marcxml']: uid = getUid(req) new_recid = reserve_record_id() record = create_record(argd["marcxml"])[0] record_delete_field(record, '001') record_delete_field(record, '005') record_add_field(record, '001', controlfield_value=str(new_recid)) create_cache(new_recid, uid, record, True) redirect_to_url(req, '{0}/record/edit/#state=edit&recid={1}'.format(CFG_SITE_SECURE_URL, new_recid)) body_content = '' body_content += self.generate_request_form(argd) if "search" in argd and argd["search"] and 'search_type' in argd and argd["search_type"] in \ self._request_type_dict: conn = None list_of_record = [] errors = {} res = [] err = False for server in argd["server"]: try: errors[server] = {"internal": [], "remote": []} conn = zoom.Connection(CFG_Z39_SERVER[server]["address"], CFG_Z39_SERVER[server]["port"], user=CFG_Z39_SERVER[server].get("user", None), password=CFG_Z39_SERVER[server].get("password", None)) conn.databaseName = CFG_Z39_SERVER[server]["databasename"] conn.preferredRecordSyntax = CFG_Z39_SERVER[server]["preferredRecordSyntax"] value = argd["search"].replace("-", "") if argd["search_type"] == "ISBN" else \ argd["search"] query = zoom.Query('CCL', u'{0}="{1}"'.format( self._request_type_dict[argd["search_type"]], value)) body_content += "" try: server_answer = conn.search(query) if len(server_answer) < 100: nb_to_browse = len(server_answer) else: nb_to_browse = 100 errors[server]["remote"].append( "The server {0} returned too many results. {1}/{2} are printed.".format( server, nb_to_browse, len(server_answer))) for result in server_answer[0:nb_to_browse]: res.append({"value": result, "provider": server}) except zoom.Bib1Err as e: errors[server]["remote"].append("{0}".format(e)) err = True conn.close() except Exception as e: register_exception() errors[server]["internal"].append("{0}".format(e)) if conn: conn.close() p_err = False warnings = '<div class="error">' for server in errors: if errors[server]["internal"] or errors[server]["remote"]: warnings += "<b>{0}</b><ul>".format(server) for error in errors[server]["internal"]: warnings += "<li>(internal) {0}</li>".format(error) for error in errors[server]["remote"]: warnings += "<li>(remote) {0}</li>".format(error) warnings += "</ul>" p_err = True if p_err: body_content += "{0}</div>".format(warnings) if res: body_content += "<table id='result_area' class='fullwidth tablesorter'>" body_content += "<tr><th class='bibz39_titles_th' >Title</th><th class='bibz39_sources_th'>Authors</th><th>Publisher</th><th class='bibz39_sources_th'>Source</th><th><div class='bibz39_button_td'>View XML</div></th><th><div class='bibz39_button_td'>Import</div></th></tr>" for identifier, rec in enumerate(res): list_of_record.append( create_record( self.interpret_string(zmarc.MARC( rec["value"].data, strict=0).toMARCXML()))[0]) title = '' authors = '' publishers = '' if "100" in list_of_record[identifier]: for author in list_of_record[identifier]["100"]: for tag in author[0]: if tag[0] == 'a': if authors != "": authors += " / " + tag[1].strip(",;.") else: authors += tag[1].strip(",;.") + " " if "700" in list_of_record[identifier]: for author in list_of_record[identifier]["700"]: for tag in author[0]: if tag[0] == 'a': if authors != "": authors += " / " + tag[1].strip(",;.") else: authors += tag[1].strip(",;.") + " " if "260" in list_of_record[identifier]: for publisher in list_of_record[identifier]["260"][0][0]: publishers += publisher[1] + " " if "245" in list_of_record[identifier]: for title_constituant in list_of_record[identifier]["245"][0][0]: title += title_constituant[1] + " " body_content += "<tr><td><div class='bibz39_titles' onclick='showxml({6})'>{0}<div><td>{4}</td><td>{5}</td</td><td><div>{2}</div></td><td><div class='bibz39_button_td'>{3}</div></td><td><div class='bibz39_button_td'>{1}</div></td></tr>".format( title, '<form method="post" action="/admin2/bibz39/"><input type="hidden" name="marcxml" value="{0}"><input type="submit" value="Import" /></form>'.format( cgi.escape(record_xml_output(list_of_record[identifier])).replace( "\"", """).replace("\'", """)), rec["provider"], '<button onclick="showxml({0})">View</button>'.format(identifier), authors, publishers, identifier) body_content += "</table>" body_content += '<script type="text/javascript">' body_content += "var gAllMarcXml= {" for i, rec in enumerate(list_of_record): body_content += "{0}:{1},".format(i, json.dumps(record_xml_output(rec))) body_content += "};" body_content += '</script>' else: if not err: body_content += "<p class='bibz39_button_td spinning_wheel'> No result</p>" body_content += '<div id="dialog-message" title="XML Preview"></div></div>' return page(title="Z39.50 Search", body=body_content, errors=[], warnings=[], metaheaderadd=get_head(), req=req)