def api(): """Handle AJAX requests.""" from invenio.ext.login import current_user from invenio.utils.json import json_unicode_to_utf8 from invenio.legacy.bibedit.utils import user_can_edit_record_collection from invenio.legacy.bibedit.engine import perform_request_ajax uid = current_user.get_id() json_data = json.loads(request.form['jsondata'].encode("utf-8")) json_data = json_unicode_to_utf8(json_data) json_response = {'resultCode': 0, 'ID': json_data['ID']} recid = None if 'recID' in json_data: recid = int(json_data['recID']) json_response.update({'recID': recid}) if json_data['requestType'] == "getRecord": # Authorize access to record. if not user_can_edit_record_collection(request, recid): json_response.update({'resultCode': 101}) return json.dumps(json_response) # Handle AJAX request. json_response.update(perform_request_ajax(request, recid, uid, json_data)) return jsonify(json_response)
def create(label, plugin, parameters, update_redirection=False): """Register redirection page.""" from invenio.utils.json import json, json_unicode_to_utf8 from .api import register_redirection parameters = json_unicode_to_utf8(json.loads(parameters)) register_redirection(label, plugin, parameters, update_redirection)
def update(label, plugin, parameters=None): """Update an existing redirection.""" from invenio.utils.json import json, json_unicode_to_utf8 from .api import update_redirection parameters = parameters or '{}' parameters = json_unicode_to_utf8(json.loads(parameters)) update_redirection(label, plugin, parameters)
def get_redirection_data(label): """ Returns all information about a given redirection identified by label. @param label: the label identifying the redirection @type label: string @returns: a dictionary with the following keys: * label: the label * plugin: the name of the plugin * parameters: the parameters that are passed to the plugin (deserialized from JSON) * creation_date: datetime object on when the redirection was first created. * modification_date: datetime object on when the redirection was last modified. @rtype: dict @raises ValueError: in case the label does not exist. """ res = run_sql("SELECT label, plugin, parameters, creation_date, modification_date FROM goto WHERE label=%s", (label, )) if res: return {'label': res[0][0], 'plugin': REDIRECT_METHODS[res[0][1]], 'parameters': json_unicode_to_utf8(json.loads(res[0][2])), 'creation_date': res[0][3], 'modification_date': res[0][4]} else: raise ValueError("%s label does not exist" % label)
def query_fundref_api(query, **kwargs): """ Perform a request to the Fundref API. """ sleep(1) req = urllib2.Request("http://api.crossref.org%s?%s" % (query, urllib.urlencode(kwargs)), headers={'content-type': 'application/vnd.crossref-api-message+json; version=1.0'}) res = json_unicode_to_utf8(json.load(CROSSREF_OPENER.open(req))) return res['message']
def manage(self, req, form): """ Web interface for the management of the info space """ uid = getUid(req) argd = wash_urlargd(form, {'ln': (str, CFG_SITE_LANG)}) # If it is an Ajax request, extract any JSON data. ajax_request = False if 'jsondata' in form: json_data = json.loads(str(form['jsondata'])) json_data = json_unicode_to_utf8(json_data) ajax_request = True json_response = {} # Authorization. user_info = collect_user_info(req) if user_info['email'] == 'guest': # User is not logged in. if not ajax_request: # Do not display the introductory recID selection box to guest # users (as it used to be with v0.99.0): dummy_auth_code, auth_message = acc_authorize_action( req, 'runinfomanager') referer = '/info' return page_not_authorized(req=req, referer=referer, text=auth_message) else: # Session has most likely timed out. json_response.update({'status': "timeout"}) return json.dumps(json_response) # Handle request. if not ajax_request: body, errors, warnings = perform_request_init_info_interface() title = 'Info Space Manager' return page(title=title, body=body, errors=errors, warnings=warnings, uid=uid, language=argd['ln'], req=req) else: # Handle AJAX request. if json_data["action"] == "listFiles": json_response.update( perform_request_edit_file(json_data["filename"])) try: return json.dumps(json_response) except UnicodeDecodeError: # Error decoding, the file can be a pdf, image or any kind # of file non-editable return json.dumps({"status": "error_file_not_readable"}) if json_data["action"] == "saveContent": return json.dumps( perform_request_save_file(json_data["filename"], json_data["filecontent"]))
def manage(self, req, form): """ Web interface for the management of the info space """ uid = getUid(req) argd = wash_urlargd(form, {'ln': (str, CFG_SITE_LANG)}) # If it is an Ajax request, extract any JSON data. ajax_request = False if 'jsondata' in form: json_data = json.loads(str(form['jsondata'])) json_data = json_unicode_to_utf8(json_data) ajax_request = True json_response = {} # Authorization. user_info = collect_user_info(req) if user_info['email'] == 'guest': # User is not logged in. if not ajax_request: # Do not display the introductory recID selection box to guest # users (as it used to be with v0.99.0): dummy_auth_code, auth_message = acc_authorize_action(req, 'runinfomanager') referer = '/info' return page_not_authorized(req=req, referer=referer, text=auth_message) else: # Session has most likely timed out. json_response.update({'status': "timeout"}) return json.dumps(json_response) # Handle request. if not ajax_request: body, errors, warnings = perform_request_init_info_interface() title = 'Info Space Manager' return page(title=title, body=body, errors=errors, warnings=warnings, uid=uid, language=argd['ln'], req=req) else: # Handle AJAX request. if json_data["action"] == "listFiles": json_response.update(perform_request_edit_file(json_data["filename"])) try: return json.dumps(json_response) except UnicodeDecodeError: # Error decoding, the file can be a pdf, image or any kind # of file non-editable return json.dumps({"status": "error_file_not_readable"}) if json_data["action"] == "saveContent": return json.dumps(perform_request_save_file(json_data["filename"], json_data["filecontent"]))
def __extract_attribute(self, req): """ Load from the request the given assertion, extract all the attribute to properly login the user, and verify that the data are actually both well formed and signed correctly. """ from invenio.ext.legacy.handler import wash_urlargd args = wash_urlargd( req.form, { 'assertion': (str, ''), 'robot': (str, ''), 'digest': (str, ''), 'login_method': (str, '') }) assertion = args['assertion'] digest = args['digest'] robot = args['robot'] login_method = args['login_method'] shared_key = load_robot_keys().get(login_method, {}).get(robot) if shared_key is None: raise InvenioWebAccessExternalAuthError( "A key does not exist for robot: %s, login_method: %s" % (robot, login_method)) if not self.verify(shared_key, assertion, digest): raise InvenioWebAccessExternalAuthError( "The provided assertion does not validate against the digest %s for robot %s" % (repr(digest), repr(robot))) if self.use_zlib: try: ## Workaround to Perl implementation that does not add ## any padding to the base64 encoding. needed_pad = (4 - len(assertion) % 4) % 4 assertion += needed_pad * '=' assertion = decompress(base64.urlsafe_b64decode(assertion)) except: raise InvenioWebAccessExternalAuthError( "The provided assertion is corrupted") data = json_unicode_to_utf8(json.loads(assertion)) if not isinstance(data, dict): raise InvenioWebAccessExternalAuthError( "The provided assertion is invalid") timeout = data[self.timeout_attribute_name] if timeout < time.time(): raise InvenioWebAccessExternalAuthError( "The provided assertion is expired") userip = data.get(self.userip_attribute_name) if not self.check_user_ip or (normalize_ip( userip, self.check_user_ip) == normalize_ip( req.remote_ip, self.check_user_ip)): return data else: raise InvenioWebAccessExternalAuthError( "The provided assertion has been issued for a different IP address (%s instead of %s)" % (userip, req.remote_ip))
def get_limited_access_data_from_orcid(orcid_id, access_token): """ Since we are dealing with orcid we can fetch tons of information from the user profile. """ from invenio.modules.access.local_config import CFG_OAUTH2_CONFIGURATIONS profile = requests.get(CFG_OAUTH2_CONFIGURATIONS['orcid']['request_url'].format(id=orcid_id), headers={'Accept': 'application/orcid+json', 'Authorization': 'Bearer %s' % access_token}) if profile.status_code != 200: raise NotImplementedError() orcid_data = json_unicode_to_utf8(profile.json())['orcid-profile'] return orcid_data
def query_fundref_api(query, **kwargs): """ Perform a request to the Fundref API. """ sleep(1) req = urllib2.Request( "http://api.crossref.org%s?%s" % (query, urllib.urlencode(kwargs)), headers={ 'content-type': 'application/vnd.crossref-api-message+json; version=1.0' }) res = json_unicode_to_utf8(json.load(CROSSREF_OPENER.open(req))) return res['message']
def _get_user_email_and_id_from_orcid(req): """ Since we are dealing with orcid we can fetch tons of information from the user profile. """ from invenio.modules.access.local_config import CFG_OAUTH2_CONFIGURATIONS profile = requests.get(CFG_OAUTH2_CONFIGURATIONS['orcid']['request_url'].format(id=req.g['oauth2_orcid']), headers={'Accept': 'application/orcid+json', 'Authorization': 'Bearer %s' % req.g['oauth2_access_token']}) orcid_record = req.g['orcid_record'] = json_unicode_to_utf8(profile.json)['orcid-profile'] id = orcid_record['orcid']['value'] emails = orcid_record['orcid-bio'].get('contact-details', {}).get('email', []) if emails: return emails[0], id else: return None, id
def get_limited_access_data_from_orcid(orcid_id, access_token): """ Since we are dealing with orcid we can fetch tons of information from the user profile. """ from invenio.modules.access.local_config import CFG_OAUTH2_CONFIGURATIONS profile = requests.get( CFG_OAUTH2_CONFIGURATIONS['orcid']['request_url'].format( id=orcid_id), headers={ 'Accept': 'application/orcid+json', 'Authorization': 'Bearer %s' % access_token }) if profile.status_code != 200: raise NotImplementedError() orcid_data = json_unicode_to_utf8(profile.json())['orcid-profile'] return orcid_data
def __extract_attribute(self, req): """ Load from the request the given assertion, extract all the attribute to properly login the user, and verify that the data are actually both well formed and signed correctly. """ from invenio.ext.legacy.handler import wash_urlargd args = wash_urlargd(req.form, { 'assertion': (str, ''), 'robot': (str, ''), 'digest': (str, ''), 'login_method': (str, '')}) assertion = args['assertion'] digest = args['digest'] robot = args['robot'] login_method = args['login_method'] shared_key = load_robot_keys().get(login_method, {}).get(robot) if shared_key is None: raise InvenioWebAccessExternalAuthError("A key does not exist for robot: %s, login_method: %s" % (robot, login_method)) if not self.verify(shared_key, assertion, digest): raise InvenioWebAccessExternalAuthError("The provided assertion does not validate against the digest %s for robot %s" % (repr(digest), repr(robot))) if self.use_zlib: try: ## Workaround to Perl implementation that does not add ## any padding to the base64 encoding. needed_pad = (4 - len(assertion) % 4) % 4 assertion += needed_pad * '=' assertion = decompress(base64.urlsafe_b64decode(assertion)) except: raise InvenioWebAccessExternalAuthError("The provided assertion is corrupted") data = json_unicode_to_utf8(json.loads(assertion)) if not isinstance(data, dict): raise InvenioWebAccessExternalAuthError("The provided assertion is invalid") timeout = data[self.timeout_attribute_name] if timeout < time.time(): raise InvenioWebAccessExternalAuthError("The provided assertion is expired") userip = data.get(self.userip_attribute_name) if not self.check_user_ip or (normalize_ip(userip, self.check_user_ip) == normalize_ip(req.remote_ip, self.check_user_ip)): return data else: raise InvenioWebAccessExternalAuthError("The provided assertion has been issued for a different IP address (%s instead of %s)" % (userip, req.remote_ip))
def _get_user_email_and_id_from_orcid(req): """ Since we are dealing with orcid we can fetch tons of information from the user profile. """ from invenio.modules.access.local_config import CFG_OAUTH2_CONFIGURATIONS profile = requests.get( CFG_OAUTH2_CONFIGURATIONS['orcid']['request_url'].format( id=req.g['oauth2_orcid']), headers={ 'Accept': 'application/orcid+json', 'Authorization': 'Bearer %s' % req.g['oauth2_access_token'] }) orcid_record = req.g['orcid_record'] = json_unicode_to_utf8( profile.json())['orcid-profile'] id = orcid_record['orcid']['value'] emails = orcid_record['orcid-bio'].get('contact-details', {}).get('email', []) if emails: return emails[0], id else: return None, id
def _process_json_request(self, form, req): """Takes care about the json requests.""" argd = wash_urlargd(form, { self._JSON_DATA_KEY: (str, ""), }) # load json data json_data_string = argd[self._JSON_DATA_KEY] json_data_unicode = json.loads(json_data_string) json_data = json_unicode_to_utf8(json_data_unicode) language = json_data["language"] search_criteria = json_data["searchCriteria"] output_tags = json_data["outputTags"] output_tags = output_tags.split(',') output_tags = [tag.strip() for tag in output_tags] action_type = json_data["actionType"] current_record_id = json_data["currentRecordID"] commands = json_data["commands"] output_format = json_data["outputFormat"] page_to_display = json_data["pageToDisplay"] collection = json_data["collection"] compute_modifications = json_data["compute_modifications"] checked_records = json_data["checked_records"] json_response = {} if action_type == self._action_types.test_search: json_response.update(multi_edit_engine.perform_request_test_search( search_criteria, [], output_format, page_to_display, language, output_tags, collection, req=req, checked_records=checked_records)) json_response['display_info_box'] = 1 json_response['info_html'] = "" return json.dumps(json_response) elif action_type == self._action_types.display_detailed_record: json_response.update(multi_edit_engine.perform_request_detailed_record( current_record_id, [], output_format, language)) return json.dumps(json_response) elif action_type == self._action_types.preview_results: commands_list, upload_mode, tag_list = self._create_commands_list(commands) json_response = {} json_response.update(multi_edit_engine.perform_request_test_search( search_criteria, commands_list, output_format, page_to_display, language, output_tags, collection, compute_modifications, upload_mode, req, checked_records)) return json.dumps(json_response) elif action_type == self._action_types.display_detailed_result: commands_list, upload_mode, tag_list = self._create_commands_list(commands) json_response.update(multi_edit_engine.perform_request_detailed_record( current_record_id, commands_list, output_format, language)) return json.dumps(json_response) elif action_type == self._action_types.submit_changes: commands_list, upload_mode, tag_list = self._create_commands_list(commands) json_response.update(multi_edit_engine.perform_request_submit_changes(search_criteria, commands_list, language, upload_mode, tag_list, collection, req, checked_records)) return json.dumps(json_response) # In case we obtain wrong action type we return empty page. return " "
def create_authorpage_websearch(self, req, form, person_id, ln='en', expire_cache=False): recompute_allowed = True oldest_cache_date = get_person_oldest_date(person_id) if oldest_cache_date: delay = datetime.datetime.now() - oldest_cache_date if delay > RECOMPUTE_ALLOWED_DELAY: if expire_cache: recompute_allowed = False expire_all_cache_for_person(person_id) else: recompute_allowed = False if CFG_WEBAUTHORPROFILE_USE_BIBAUTHORID: if person_id < 0: return ("Critical Error. PersonID should never be less than 0!") pubs, pubsStatus = get_pubs(person_id) if not pubs: pubs = [] selfpubs, selfpubsStatus = get_self_pubs(person_id) if not selfpubs: selfpubs = [] namesdict, namesdictStatus = get_person_names_dicts(person_id) if not namesdict: namesdict = {} try: authorname = namesdict['longest'] db_names_dict = namesdict['db_names_dict'] except (IndexError, KeyError): authorname = 'None' db_names_dict = {} #author_aff_pubs, author_aff_pubsStatus = (None, None) author_aff_pubs, author_aff_pubsStatus = get_institute_pub_dict(person_id) if not author_aff_pubs: author_aff_pubs = {} coauthors, coauthorsStatus = get_coauthors(person_id) if not coauthors: coauthors = {} summarize_records, summarize_recordsStatus = get_summarize_records(person_id, 'hcs', ln) if not summarize_records: summarize_records = 'None' totaldownloads, totaldownloadsStatus = get_total_downloads(person_id) if not totaldownloads: totaldownloads = 0 citedbylist, citedbylistStatus = get_cited_by_list(person_id) if not citedbylist: citedbylist = 'None' kwtuples, kwtuplesStatus = get_kwtuples(person_id) if kwtuples: pass #kwtuples = kwtuples[0:MAX_KEYWORD_LIST] else: kwtuples = [] collab, collabStatus = get_collabtuples(person_id) vtuples, venuetuplesStatus = get_venuetuples(person_id) if vtuples: pass #vtuples = venuetuples[0:MAX_VENUE_LIST] else: vtuples = str(vtuples) person_link, person_linkStatus = get_veryfy_my_pubs_list_link(person_id) if not person_link or not person_linkStatus: bibauthorid_data = {"is_baid": True, "pid":person_id, "cid": None} person_link = str(person_id) else: bibauthorid_data = {"is_baid": True, "pid":person_id, "cid": person_link} hepdict, hepdictStatus = get_hepnames_data(person_id) oldest_cache_date = get_person_oldest_date(person_id) #req.write("\nPAGE CONTENT START\n") #req.write(str(time.time())) #eval = [not_empty(x) or y for x, y in beval = [y for _, y in [(authorname, namesdictStatus) , (totaldownloads, totaldownloadsStatus), (author_aff_pubs, author_aff_pubsStatus), (citedbylist, citedbylistStatus), (kwtuples, kwtuplesStatus), (coauthors, coauthorsStatus), (vtuples, venuetuplesStatus), (db_names_dict, namesdictStatus), (person_link, person_linkStatus), (summarize_records, summarize_recordsStatus), (pubs, pubsStatus), (hepdict, hepdictStatus), (selfpubs, selfpubsStatus), (collab, collabStatus)]] #not_complete = False in eval #req.write(str(eval)) if 'jsondata' in form: json_response = {'boxes_info': {}} json_data = json.loads(str(form['jsondata'])) json_data = json_unicode_to_utf8(json_data) # loop to check which boxes need content json_response['boxes_info'].update({'name_variants': {'status':beval[0], 'html_content': webauthorprofile_templates.tmpl_author_name_variants_box(req, db_names_dict, bibauthorid_data, ln, add_box=False, loading=not beval[0])}}) json_response['boxes_info'].update({'combined_papers': {'status':(beval[3] and beval[12]), 'html_content': webauthorprofile_templates.tmpl_papers_with_self_papers_box(req, pubs, selfpubs, bibauthorid_data, totaldownloads, ln, add_box=False, loading=not beval[3])}}) #json_response['boxes_info'].update({'papers': {'status':beval[3], 'html_content': webauthorprofile_templates.tmpl_papers_box(req, pubs, bibauthorid_data, totaldownloads, ln, add_box=False, loading=not beval[3])}}) json_response['boxes_info'].update({'selfpapers': {'status':beval[12], 'html_content': webauthorprofile_templates.tmpl_self_papers_box(req, selfpubs, bibauthorid_data, totaldownloads, ln, add_box=False, loading=not beval[12])}}) json_response['boxes_info'].update({'keywords': {'status':beval[4], 'html_content': webauthorprofile_templates.tmpl_keyword_box(kwtuples, bibauthorid_data, ln, add_box=False, loading=not beval[4])}}) json_response['boxes_info'].update({'affiliations': {'status':beval[2], 'html_content': webauthorprofile_templates.tmpl_affiliations_box(author_aff_pubs, ln, add_box=False, loading=not beval[2])}}) json_response['boxes_info'].update({'coauthors': {'status':beval[5], 'html_content': webauthorprofile_templates.tmpl_coauthor_box(bibauthorid_data, coauthors, ln, add_box=False, loading=not beval[5])}}) json_response['boxes_info'].update({'numpaperstitle': {'status':beval[10], 'html_content': webauthorprofile_templates.tmpl_numpaperstitle(bibauthorid_data, pubs)}}) json_response['boxes_info'].update({'authornametitle': {'status':beval[7], 'html_content': webauthorprofile_templates.tmpl_authornametitle(db_names_dict)}}) json_response['boxes_info'].update({'citations': {'status':beval[9], 'html_content': summarize_records}}) json_response['boxes_info'].update({'hepdata': {'status':beval[11], 'html_content':webauthorprofile_templates.tmpl_hepnames(hepdict, ln, add_box=False, loading=not beval[11])}}) json_response['boxes_info'].update({'collaborations': {'status':beval[13], 'html_content': webauthorprofile_templates.tmpl_collab_box(collab, bibauthorid_data, ln, add_box=False, loading=not beval[13])}}) req.content_type = 'application/json' req.write(json.dumps(json_response)) else: gboxstatus = self.person_id if False not in beval: gboxstatus = 'noAjax' req.write('<script type="text/javascript">var gBOX_STATUS = "%s" </script>' % (gboxstatus)) req.write(webauthorprofile_templates.tmpl_author_page(req, pubs, \ selfpubs, \ authorname, \ totaldownloads, \ author_aff_pubs, \ citedbylist, kwtuples, \ coauthors, vtuples, \ db_names_dict, person_link, \ bibauthorid_data, \ summarize_records, \ hepdict, \ collab, \ ln, \ beval, \ oldest_cache_date, recompute_allowed))
def index(self, req, form): """Handle all BibMerge requests. The responsibilities of this functions are: * JSON decoding and encoding. * Redirection, if necessary. * Authorization. * Calling the appropriate function from the engine. """ # If it is an Ajax request, extract any JSON data. ajax_request, recid1, recid2 = False, None, None argd = wash_urlargd(form, {'ln': (str, CFG_SITE_LANG)}) if 'jsondata' in form: json_data = json.loads(str(form['jsondata'])) # Deunicode all strings (Invenio doesn't have unicode # support). json_data = json_unicode_to_utf8(json_data) ajax_request = True json_response = {} if 'recID1' in json_data: recid1 = json_data['recID1'] if 'recID2' in json_data: recid2 = json_data['recID2'] # Authorization. user_info = collect_user_info(req) if user_info['email'] == 'guest': # User is not logged in. if not ajax_request: # Do not display the introductory recID selection box to guest # users (as it used to be with v0.99.0): auth_code, auth_message = acc_authorize_action(req, 'runbibmerge') referer = '/merge/' return page_not_authorized(req=req, referer=referer, text=auth_message, navtrail=navtrail) else: # Session has most likely timed out. json_response.update({'resultCode': 1, 'resultText': 'Error: Not logged in'}) return json.dumps(json_response) elif self.recid: # Handle RESTful call by storing recid and redirecting to # generic URL. redirect_to_url(req, '%s/%s/merge/' % (CFG_SITE_SECURE_URL, CFG_SITE_RECORD) ) if recid1 is not None: # Authorize access to record 1. auth_code, auth_message = acc_authorize_action(req, 'runbibmerge', collection=guess_primary_collection_of_a_record(recid1)) if auth_code != 0: json_response.update({'resultCode': 1, 'resultText': 'No access to record %s' % recid1}) return json.dumps(json_response) if recid2 is not None: # Authorize access to record 2. auth_code, auth_message = acc_authorize_action(req, 'runbibmerge', collection=guess_primary_collection_of_a_record(recid2)) if auth_code != 0: json_response.update({'resultCode': 1, 'resultText': 'No access to record %s' % recid2}) return json.dumps(json_response) # Handle request. uid = getUid(req) if not ajax_request: # Show BibEdit start page. body, errors, warnings = perform_request_init() metaheaderadd = """<script type="text/javascript" src="%(site)s/js/json2.js"></script> <script type="text/javascript" src="%(url)s"></script>""" % {'site': url_for('merger.static', filename='js/merger/engine.js')} title = 'Record Merger' return page(title = title, metaheaderadd = metaheaderadd, body = body, errors = errors, warnings = warnings, uid = uid, language = argd['ln'], navtrail = navtrail, lastupdated = __lastupdated__, req = req) else: # Handle AJAX request. json_response = perform_request_ajax(req, uid, json_data) return json.dumps(json_response)
def index(self, req, form): """Handle all requests""" uid = getUid(req) argd = wash_urlargd(form, {'ln' : (str, CFG_SITE_LANG), 'state' : (str, '')}) ln = argd['ln'] state = argd['state'] _ = gettext_set_language(ln) # Abort if the simplejson module isn't available if not CFG_JSON_AVAILABLE: title = 'Authorlist Manager' 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 = navtrail, lastupdated = __lastupdated__, req = req) # Extract additional JSON data from form if 'options' in form: options = json.loads(str(form['options'])) # Deunicode all strings (Invenio doesn't have unicode # support). options = json_unicode_to_utf8(options) # Authorization. not_authorized = authorlist_engine.user_authorization(req, ln) if not_authorized: return not_authorized # User is authorized, let's handle different states # if no state parameter, load the main page if state == '': return page(title = _('Author List Manager'), metaheaderadd = authorlist_templates.index_header(), body = authorlist_templates.body(), errors = [], warnings = [], uid = uid, language = ln, navtrail = navtrail, lastupdated = __lastupdated__, req = req) elif state == 'itemize': data = authorlist_db.itemize(uid) req.content_type = 'application/json' req.write(json.dumps(data)) # open paremeter set? initialize a Authorlist instance elif state == 'open': # if 'id' in url, check if user has right to modify this paper try: received = wash_urlargd(form, {'id': (str, None)}) paper_id = received['id'] if authorlist_engine.check_user_rights(uid, paper_id): return page(title = _('Author List Manager'), metaheaderadd = authorlist_templates.list_header(), body = authorlist_templates.body(), errors = [], warnings = [], uid = uid, language = ln, navtrail = navtrail, lastupdated = __lastupdated__, req = req) else: # no rights to modify this paper redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) except: # redirect to the main page if weird stuff happens redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) # On load state we will answer with the JSON encoded data of the passed # paper id. Should usually not be directly surfed by the user. elif state == 'load': try: received = wash_urlargd(form, {'id': (str, None)}) paper_id = received['id'] data = authorlist_db.load(paper_id) req.content_type = 'application/json' req.write(json.dumps(data)) except: # redirect to the main page if weird stuff happens redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) # The save state saves the send data in the database using the passed # paper id. Responds with a JSON object containing the id of the paper # as saved in the database. Should usually not be surfed directly by the # user elif state == 'save': try: received = wash_urlargd(form, {'id': (str, None), 'data': (str, '')}) paper_id = received['id'] in_data = json.loads(received['data']) out_data = authorlist_db.save(paper_id, uid, in_data) req.content_type = 'application/json' req.write(json.dumps(out_data)) except: # redirect to the main page if something weird happens redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) # Clones the paper with the given id in the database and responds with a # JSON object containing the id of the clone. Should usually not surfed # directly by the user. elif state == 'clone': try: received = wash_urlargd(form, {'id': (str, None)}) paper_id = received['id'] data = authorlist_db.clone(paper_id, uid) req.content_type = 'application/json' req.write(json.dumps(data)) except: # redirect to the main page if something weird happens redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) # Transform the sent data into the format passed in the URL using a # authorlist_engine converter. Reponds with the MIME type of the # converter and offers it as a download (content-disposition header). elif state == 'export': try: received = wash_urlargd(form, {'format': (str, None), 'data': (str, '')}) data_format = received['format'] data = received['data'] converter = authorlist_engine.Converters.get(data_format) attachement = 'attachement; filename="%s"' % converter.FILE_NAME req.headers_out['Content-Type'] = converter.CONTENT_TYPE req.headers_out['Content-Disposition'] = attachement #redirect_to_url(req, authorlist_engine.dumps(data, converter)) req.write(authorlist_engine.dumps(data, converter)) except: # throw exception if something weird happens return sys.exc_info() elif state == 'delete': try: received = wash_urlargd(form, {'id': (str, None)}) paper_id = received['id'] data = authorlist_db.delete(paper_id) req.content_type = 'application/json' req.write(json.dumps(data)) except: # redirect to the main page if something weird happens redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) elif state == 'import': try: received = wash_urlargd(form, {'importid': (str, None)}) recID = received['importid'] data = authorlist_engine.retrieve_data_from_record(recID) req.content_type = 'application/json' req.write(json.dumps(data)) except: # redirect to the main page if something weird happens redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) elif state == 'importxml': try: received = wash_urlargd(form, {'xmlfile': (Field, None)}) xml_string = received['xmlfile'].value import_data = authorlist_engine.retrieve_data_from_xml(xml_string) req.content_type = 'application/json' req.write(json.dumps(import_data)) except: # redirect to the main page if something weird happens redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL)) # No state given, just go to the main page. else: redirect_to_url(req, '%s/authorlist/' % (CFG_SITE_URL))
def get_json_parameters_from_cli(option, dummy_opt_str, value, dummy_parser): try: option.parameters = json_unicode_to_utf8(json.loads(value)) except Exception as err: raise optparse.OptionValueError("Cannot parse as a valid JSON serialization the provided parameters: %s. %s" % (value, err))
def templates(self, req, form): """handle a edit/templates request""" uid = current_user.get_id() argd = wash_urlargd(form, {'ln': (str, CFG_SITE_LANG)}) # Abort if the simplejson module isn't available if not CFG_JSON_AVAILABLE: title = 'Record Editor Template Manager' 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 = argd['ln'], navtrail = navtrail_bibedit, lastupdated = __lastupdated__, req = req, body_css_classes = ['bibedit']) # If it is an Ajax request, extract any JSON data. ajax_request = False if 'jsondata' in form: json_data = json.loads(str(form['jsondata'])) # Deunicode all strings (Invenio doesn't have unicode # support). json_data = json_unicode_to_utf8(json_data) ajax_request = True json_response = {'resultCode': 0} # Authorization. if current_user.is_guest: # User is not logged in. if not ajax_request: # Do not display the introductory recID selection box to guest # users (as it used to be with v0.99.0): dummy_auth_code, auth_message = acc_authorize_action(req, 'runbibedit') referer = '/edit' return page_not_authorized(req=req, referer=referer, text=auth_message, navtrail=navtrail) else: # Session has most likely timed out. json_response.update({'resultCode': 100}) return json.dumps(json_response) # Handle request. if not ajax_request: # Show BibEdit template management start page. body, errors, warnings = perform_request_init_template_interface() title = 'Record Editor Template Manager' return page(title = title, body = body, errors = errors, warnings = warnings, uid = uid, language = argd['ln'], navtrail = navtrail_bibedit, lastupdated = __lastupdated__, req = req, body_css_classes = ['bibedit']) else: # Handle AJAX request. json_response.update(perform_request_ajax_template_interface(json_data)) return json.dumps(json_response)
def index(self, req, form): """Handle all BibMerge requests. The responsibilities of this functions are: * JSON decoding and encoding. * Redirection, if necessary. * Authorization. * Calling the appropriate function from the engine. """ # If it is an Ajax request, extract any JSON data. ajax_request, recid1, recid2 = False, None, None argd = wash_urlargd(form, {'ln': (str, CFG_SITE_LANG)}) if 'jsondata' in form: json_data = json.loads(str(form['jsondata'])) # Deunicode all strings (Invenio doesn't have unicode # support). json_data = json_unicode_to_utf8(json_data) ajax_request = True json_response = {} try: if json_data.has_key('recID1'): recid1 = int(json_data['recID1']) json_data['recID1'] = recid1 if json_data.has_key('recID2'): if json_data.get('record2Mode') == "recid": recid2 = int(json_data['recID2']) json_data['recID2'] = recid2 except ValueError: json_response.update({ 'resultCode': 1, 'resultText': 'Invalid record ID!' }) return json.dumps(json_response) if json_data.has_key("duplicate"): if json_data.get('record2Mode') == "recid": json_data["duplicate"] = int(json_data["duplicate"]) # Authorization. user_info = collect_user_info(req) if user_info['email'] == 'guest': # User is not logged in. if not ajax_request: # Do not display the introductory recID selection box to guest # users (as it used to be with v0.99.0): auth_code, auth_message = acc_authorize_action( req, 'runbibmerge') referer = '/merge/' return page_not_authorized(req=req, referer=referer, text=auth_message, navtrail=navtrail) else: # Session has most likely timed out. json_response.update({ 'resultCode': 1, 'resultText': 'Error: Not logged in' }) return json.dumps(json_response) elif self.recid: # Handle RESTful call by storing recid and redirecting to # generic URL. redirect_to_url( req, '%s/%s/merge/' % (CFG_SITE_SECURE_URL, CFG_SITE_RECORD)) if recid1 is not None: # Authorize access to record 1. auth_code, auth_message = acc_authorize_action( req, 'runbibmerge', collection=guess_primary_collection_of_a_record(recid1)) if auth_code != 0: json_response.update({ 'resultCode': 1, 'resultText': 'No access to record %s' % recid1 }) return json.dumps(json_response) if recid2 is not None: # Authorize access to record 2. auth_code, auth_message = acc_authorize_action( req, 'runbibmerge', collection=guess_primary_collection_of_a_record(recid2)) if auth_code != 0: json_response.update({ 'resultCode': 1, 'resultText': 'No access to record %s' % recid2 }) return json.dumps(json_response) # Handle request. uid = getUid(req) if not ajax_request: # Show BibEdit start page. body, errors, warnings = perform_request_init() scripts = ["vendors/json2/json2.js", "js/merger/engine.js"] metaheaderadd = "" for script in scripts: metaheaderadd += '<script type="text/javascript" src="%s/%s"></script>' % ( CFG_SITE_URL, auto_version_url(script)) return page(title='Record Merger', metaheaderadd=metaheaderadd, body=body, errors=errors, warnings=warnings, uid=uid, language=argd['ln'], navtrail=navtrail, lastupdated=__lastupdated__, req=req) else: # Handle AJAX request. json_response = perform_request_ajax(req, uid, json_data) return json.dumps(json_response)
def index(self, req, form): """Handle all BibEdit requests. The responsibilities of this functions is: * JSON decoding and encoding. * Redirection, if necessary. * Authorization. * Calling the appropriate function from the engine. """ uid = current_user.get_id() argd = wash_urlargd(form, {'ln': (str, CFG_SITE_LANG)}) # Abort if the simplejson module isn't available if not CFG_JSON_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 = argd['ln'], navtrail = navtrail, lastupdated = __lastupdated__, req = req, body_css_classes = ['bibedit']) # If it is an Ajax request, extract any JSON data. ajax_request, recid = False, None if 'jsondata' in form: json_data = json.loads(str(form['jsondata'])) # Deunicode all strings (Invenio doesn't have unicode # support). json_data = json_unicode_to_utf8(json_data) ajax_request = True if 'recID' in json_data: recid = json_data['recID'] json_response = {'resultCode': 0, 'ID': json_data['ID']} # Authorization. if current_user.is_guest: # User is not logged in. if not ajax_request: # Do not display the introductory recID selection box to guest # users (as it used to be with v0.99.0): dummy_auth_code, auth_message = acc_authorize_action(req, 'runbibedit') referer = '/edit/' if self.recid: referer = '/%s/%s/edit/' % (CFG_SITE_RECORD, self.recid) return page_not_authorized(req=req, referer=referer, text=auth_message, navtrail=navtrail) else: # Session has most likely timed out. json_response.update({'resultCode': 100}) return json.dumps(json_response) elif self.recid: # Handle RESTful calls from logged in users by redirecting to # generic URL. redirect_to_url(req, '%s/%s/edit/#state=edit&recid=%s&recrev=%s' % ( CFG_SITE_SECURE_URL, CFG_SITE_RECORD, self.recid, "")) elif recid is not None: json_response.update({'recID': recid}) if json_data['requestType'] == "getRecord": # Authorize access to record. if not user_can_edit_record_collection(req, recid): json_response.update({'resultCode': 101}) return json.dumps(json_response) # Handle request. if not ajax_request: # Show BibEdit start page. body, errors, warnings = perform_request_init(uid, argd['ln'], req, __lastupdated__) title = 'Record Editor' return page(title = title, body = body, errors = errors, warnings = warnings, uid = uid, language = argd['ln'], navtrail = navtrail, lastupdated = __lastupdated__, req = req, body_css_classes = ['bibedit']) else: # Handle AJAX request. json_response.update(perform_request_ajax(req, recid, uid, json_data)) return json.dumps(json_response)