diff --git a/hepdata/config.py b/hepdata/config.py index ce7f4efce..e1085c28e 100644 --- a/hepdata/config.py +++ b/hepdata/config.py @@ -188,6 +188,7 @@ def _(x): CLIENT_TIMEOUT = 298 # Client-side timeout in s (should be slightly smaller than server timeout) SIZE_LOAD_CHECK_THRESHOLD = 1 * (1024 * 1024) # Size (bytes) threshold for immediate loading of a table on the records page. ADDITIONAL_SIZE_LOAD_CHECK_THRESHOLD = 1 * (1024 * 1024) # Size (bytes) threshold for disallowing render of additional res files +OBSERVER_KEY_LENGTH = 8 # The length that the UUID4 observer key is trimmed down to. CFG_PUB_TYPE = 'publication' CFG_DATA_TYPE = 'datatable' diff --git a/hepdata/modules/converter/views.py b/hepdata/modules/converter/views.py index 130c813d2..ba556474d 100644 --- a/hepdata/modules/converter/views.py +++ b/hepdata/modules/converter/views.py @@ -31,7 +31,7 @@ from hepdata.config import CFG_CONVERTER_URL, CFG_SUPPORTED_FORMATS, CFG_CONVERTER_TIMEOUT from hepdata_converter_ws_client import convert, Error -from hepdata.modules.permissions.api import user_allowed_to_perform_action +from hepdata.modules.permissions.api import user_allowed_to_perform_action, verify_observer_key from hepdata.modules.converter import convert_zip_archive from hepdata.modules.submission.api import get_latest_hepsubmission from hepdata.modules.submission.models import HEPSubmission, DataResource, DataSubmission @@ -133,6 +133,8 @@ def download_submission_with_recid(*args, **kwargs): :return: download_submission """ recid = kwargs.pop('recid') + observer_key = request.args.get('observer_key') + key_verified = verify_observer_key(recid, observer_key) version_count, version_count_all = get_version_count(recid) if 'version' in kwargs: @@ -142,7 +144,7 @@ def download_submission_with_recid(*args, **kwargs): version = version_count if version_count else 1 # Check for a user trying to access a version of a publication record where they don't have permissions. - if version_count < version_count_all and version == version_count_all: + if version_count < version_count_all and version == version_count_all and not key_verified: abort(403) submission = HEPSubmission.query.filter_by(publication_recid=recid, version=version).first() @@ -352,6 +354,8 @@ def download_data_table_by_recid(*args, **kwargs): recid = kwargs.pop('recid') table_name = kwargs.pop('table_name') rivet = kwargs.pop('rivet', '') + observer_key = request.args.get('observer_key') + key_verified = verify_observer_key(recid, observer_key) version_count, version_count_all = get_version_count(recid) if 'version' in kwargs: @@ -361,9 +365,12 @@ def download_data_table_by_recid(*args, **kwargs): version = version_count if version_count else 1 # Check for a user trying to access a version of a publication record where they don't have permissions. - if version_count < version_count_all and version == version_count_all: + if version_count < version_count_all and version == version_count_all and not key_verified: abort(403) + if not key_verified: + observer_key = None + datasubmission = None original_table_name = table_name try: @@ -389,7 +396,7 @@ def download_data_table_by_recid(*args, **kwargs): return download_datatable(datasubmission, kwargs.pop('file_format'), submission_id='{0}'.format(recid), table_name=datasubmission.name, - rivet_analysis_name=rivet, + rivet_analysis_name=rivet, observer_key=observer_key, yoda_keep_qualifiers=bool(request.args.get('qualifiers', False))) @@ -419,8 +426,13 @@ def download_datatable(datasubmission, file_format, *args, **kwargs): """ if file_format == 'json': - return redirect('/record/data/{0}/{1}/{2}'.format(datasubmission.publication_recid, - datasubmission.id, datasubmission.version)) + redirect_url = '/record/data/{0}/{1}/{2}'.format(datasubmission.publication_recid, + datasubmission.id, datasubmission.version) + observer_key = kwargs.get("observer_key") + if observer_key: + redirect_url += f"?observer_key={observer_key}" + return redirect(redirect_url) + elif file_format not in CFG_SUPPORTED_FORMATS: return display_error( title="The " + file_format + " output format is not supported", diff --git a/hepdata/modules/dashboard/assets/js/hepdata_dashboard_manage_scripts.js b/hepdata/modules/dashboard/assets/js/hepdata_dashboard_manage_scripts.js index 692ba2ed1..7abbd016e 100644 --- a/hepdata/modules/dashboard/assets/js/hepdata_dashboard_manage_scripts.js +++ b/hepdata/modules/dashboard/assets/js/hepdata_dashboard_manage_scripts.js @@ -185,10 +185,31 @@ $(document).ready(function() { }); } + function set_observer_key(recid) { + /** + * Sets the observer key value on the manage submission dashboard widget. + * + * @param {number} recid - The recid to get observer_key for. + */ + + // Call for observer key, with the flag to retrieve + let observer_promise = HEPDATA.get_observer_key_data(recid, 1); + + observer_promise.then(function(observer_key) { + if(observer_key) { + // Unhide container, set value and text on the input + $('#data_link_container').removeAttr('hidden'); + $('#direct_data_link').val(observer_key); + } + }); + HEPDATA.setup_clipboard("#dashboard_button"); + } + $(document).on('click', '.manage-submission-trigger', function (event) { window.recid = $(this).attr('data-recid'); HEPDATA.load_all_review_messages("#conversation", $(this).attr('data-recid')); process_reviews($(this).attr('data-recid')); + set_observer_key($(this).attr('data-recid')); }); $(document).on('click', '.conversation-trigger', function (event) { diff --git a/hepdata/modules/dashboard/templates/hepdata_dashboard/dashboard.html b/hepdata/modules/dashboard/templates/hepdata_dashboard/dashboard.html index e240d34aa..81cd874be 100644 --- a/hepdata/modules/dashboard/templates/hepdata_dashboard/dashboard.html +++ b/hepdata/modules/dashboard/templates/hepdata_dashboard/dashboard.html @@ -7,6 +7,7 @@ {%- block additional_assets %} {{ webpack['hepdata-info.css'] }} + {{ webpack['toastr.css'] }} {%- endblock additional_assets %} diff --git a/hepdata/modules/dashboard/templates/hepdata_dashboard/manager-widget.html b/hepdata/modules/dashboard/templates/hepdata_dashboard/manager-widget.html index 23e90dbae..78c37e022 100644 --- a/hepdata/modules/dashboard/templates/hepdata_dashboard/manager-widget.html +++ b/hepdata/modules/dashboard/templates/hepdata_dashboard/manager-widget.html @@ -29,10 +29,19 @@
- + - diff --git a/hepdata/modules/email/api.py b/hepdata/modules/email/api.py index b6b7f5a78..8eddd53c8 100644 --- a/hepdata/modules/email/api.py +++ b/hepdata/modules/email/api.py @@ -34,7 +34,8 @@ from hepdata.modules.permissions.models import CoordinatorRequest from hepdata.modules.submission.api import get_latest_hepsubmission, \ - get_primary_submission_participants_for_record, get_submission_participants_for_record + get_primary_submission_participants_for_record, get_submission_participants_for_record, \ + get_or_create_submission_observer from hepdata.modules.submission.models import HEPSubmission, DataSubmission, DataReview from hepdata.utils.users import get_user_from_id from invenio_accounts.models import User @@ -460,6 +461,12 @@ def notify_submission_created(record, coordinator_id, uploader, reviewer): collaboration = _get_collaboration(coordinator_id) + # Get any SubmissionObserver object entry for this ID + submission_observer = get_or_create_submission_observer(record['recid']) + + # Generate the observer access URL with key in + observer_url = f"{site_url}/record/{record['recid']}?observer_key={submission_observer.observer_key}" + message_body = render_template('hepdata_theme/email/created.html', name=name, actor=coordinator.email, @@ -469,7 +476,8 @@ def notify_submission_created(record, coordinator_id, uploader, reviewer): article=record['recid'], title=record['title'], site_url=site_url, - link=site_url + "/record/{0}".format(record['recid'])) + link=site_url + "/record/{0}".format(record['recid']), + observer_url=observer_url) create_send_email_task(coordinator.email, '[HEPData] Submission {0} has been created'.format(record['recid']), diff --git a/hepdata/modules/permissions/api.py b/hepdata/modules/permissions/api.py index 8391e9b61..230f75d48 100644 --- a/hepdata/modules/permissions/api.py +++ b/hepdata/modules/permissions/api.py @@ -19,7 +19,7 @@ # In applying this license, CERN does not # waive the privileges and immunities granted to it by virtue of its status # as an Intergovernmental Organization or submit itself to any jurisdiction. - +import logging from functools import partial from operator import is_not @@ -27,11 +27,15 @@ from flask_login import current_user from sqlalchemy import or_ +from hepdata.config import OBSERVER_KEY_LENGTH from hepdata.modules.permissions.models import SubmissionParticipant, CoordinatorRequest from hepdata.modules.records.utils.common import get_record_contents -from hepdata.modules.submission.models import HEPSubmission +from hepdata.modules.submission.models import HEPSubmission, SubmissionObserver from hepdata.utils.users import get_user_from_id, user_is_admin +logging.basicConfig() +log = logging.getLogger(__name__) + def get_records_participated_in_by_user(user): _current_user_id = user.id @@ -194,3 +198,40 @@ def write_submissions_to_files(): csvfile.close() csvfile1.close() + + +def verify_observer_key(submission_id, observer_key): + """ + Verifies the access key used to access a submission without + login requirement. + :param int submission_id: The requested HEPSubmission for access + :param str observer_key: The access key used to access the submission + :returns: Bool representing match status against database + """ + + # Validate inputs + if submission_id is None or observer_key is None: + return False + + try: + submission_id = int(submission_id) + except (ValueError, TypeError): + log.warning(f"Invalid submission_id format: {submission_id}") + return False + + if len(observer_key) != OBSERVER_KEY_LENGTH: + log.warning(f"Invalid observer_key length for submission {submission_id}") + return False + try: + submission_observer = SubmissionObserver.query.filter_by( + publication_recid=submission_id, + observer_key=observer_key + ).first() + + result = submission_observer is not None + + return result + + except Exception as e: + log.error(f"Database error in verify_observer_key: {e}") + return False diff --git a/hepdata/modules/records/api.py b/hepdata/modules/records/api.py index 2e99b6c2e..d9f4ac737 100644 --- a/hepdata/modules/records/api.py +++ b/hepdata/modules/records/api.py @@ -41,7 +41,7 @@ from hepdata.modules.converter import convert_oldhepdata_to_yaml from hepdata.modules.email.api import send_cookie_email from hepdata.modules.email.utils import create_send_email_task -from hepdata.modules.permissions.api import user_allowed_to_perform_action +from hepdata.modules.permissions.api import user_allowed_to_perform_action, verify_observer_key from hepdata.modules.permissions.models import SubmissionParticipant from hepdata.modules.records.subscribers.api import is_current_user_subscribed_to_record from hepdata.modules.records.utils.common import decode_string, find_file_in_directory, allowed_file, \ @@ -51,7 +51,8 @@ from hepdata.modules.records.utils.json_ld import get_json_ld from hepdata.modules.records.utils.submission import process_submission_directory, \ create_data_review, cleanup_submission, clean_error_message_for_display -from hepdata.modules.submission.api import get_latest_hepsubmission, get_submission_participants_for_record +from hepdata.modules.submission.api import get_latest_hepsubmission, get_submission_participants_for_record, \ + get_or_create_submission_observer from hepdata.modules.records.utils.users import get_coordinators_in_system, has_role from hepdata.modules.records.utils.workflow import update_action_for_submission_participant from hepdata.modules.records.utils.yaml_utils import split_files @@ -62,7 +63,8 @@ HEPSubmission, RecordVersionCommitMessage, RelatedRecid, - RelatedTable + RelatedTable, + SubmissionObserver ) from hepdata.utils.file_extractor import extract from hepdata.utils.miscellaneous import sanitize_html, get_resource_data @@ -98,7 +100,7 @@ def decorated_function(*args, **kwargs): def format_submission(recid, record, version, version_count, hepdata_submission, - data_table=None): + data_table=None, observer_view=None): """ Performs all the processing of the record to be displayed. @@ -108,6 +110,7 @@ def format_submission(recid, record, version, version_count, hepdata_submission, :param version_count: :param hepdata_submission: :param data_table: + :param observer_view: :return: """ ctx = {} @@ -165,9 +168,14 @@ def format_submission(recid, record, version, version_count, hepdata_submission, if not (ctx['show_review_widget'] or ctx['show_upload_widget'] or ctx['is_submission_coordinator_or_admin']): - # we show the latest approved version. - ctx["version"] -= 1 - ctx["version_count"] -= 1 + + if not observer_view: + # we show the latest approved version. + ctx["version"] -= 1 + ctx["version_count"] -= 1 + else: + ctx["version_count"] += 1 + ctx['additional_resources'] = submission_has_resources(hepdata_submission) ctx['resources_with_doi'] = [] @@ -370,7 +378,7 @@ def extract_journal_info(record): record['journal_info'] = "Conference Paper" -def render_record(recid, record, version, output_format, light_mode=False): +def render_record(recid, record, version, output_format, light_mode=False, observer_key=None): # Count number of all versions and number of finished versions of a publication record. version_count_all = HEPSubmission.query.filter(HEPSubmission.publication_recid == recid, @@ -385,20 +393,24 @@ def render_record(recid, record, version, output_format, light_mode=False): if version == -1: version = version_count if version_count else 1 - # Check for a user trying to access a version of a publication record where they don't have permissions. - if version_count < version_count_all and version == version_count_all: - # Prompt the user to login if they are not authenticated then redirect, otherwise return a 403 error. - if not current_user.is_authenticated: - redirect_url_after_login = '%2Frecord%2F{0}%3Fversion%3D{1}%26format%3D{2}'.format(recid, version, output_format) - if 'table' in request.args: - redirect_url_after_login += '%26table%3D{0}'.format(request.args['table']) - if output_format.startswith('yoda') and 'rivet' in request.args: - redirect_url_after_login += '%26rivet%3D{0}'.format(request.args['rivet']) - if output_format.startswith('yoda') and 'qualifiers' in request.args: - redirect_url_after_login += '%26qualifiers%3D{0}'.format(request.args['qualifiers']) - return redirect('/login/?next={0}'.format(redirect_url_after_login)) - else: - abort(403) + key_verified = verify_observer_key(recid, observer_key) + + # We skip the version check if the access key matches + if not key_verified: + # Check for a user trying to access a version of a publication record where they don't have permissions. + if version_count < version_count_all and version == version_count_all: + # Prompt the user to login if they are not authenticated then redirect, otherwise return a 403 error. + if not current_user.is_authenticated: + redirect_url_after_login = '%2Frecord%2F{0}%3Fversion%3D{1}%26format%3D{2}'.format(recid, version, output_format) + if 'table' in request.args: + redirect_url_after_login += '%26table%3D{0}'.format(request.args['table']) + if output_format.startswith('yoda') and 'rivet' in request.args: + redirect_url_after_login += '%26rivet%3D{0}'.format(request.args['rivet']) + if output_format.startswith('yoda') and 'qualifiers' in request.args: + redirect_url_after_login += '%26qualifiers%3D{0}'.format(request.args['qualifiers']) + return redirect('/login/?next={0}'.format(redirect_url_after_login)) + else: + abort(403) hepdata_submission = get_latest_hepsubmission(publication_recid=recid, version=version) @@ -409,10 +421,20 @@ def render_record(recid, record, version, output_format, light_mode=False): return render_template('hepdata_records/publication_processing.html', ctx=ctx) elif not hepdata_submission.overall_status.startswith('sandbox'): - ctx = format_submission(recid, record, version, version_count, hepdata_submission) + ctx = format_submission(recid, record, version, version_count, hepdata_submission, observer_view=key_verified) ctx['record_type'] = 'publication' ctx['related_recids'] = get_record_data_list(hepdata_submission, "related") ctx['related_to_this_recids'] = get_record_data_list(hepdata_submission, "related_to_this") + ctx['overall_status'] = hepdata_submission.overall_status + + + if key_verified and observer_key: + ctx['observer_key'] = observer_key + elif hepdata_submission.overall_status == 'todo': + observer = get_or_create_submission_observer(hepdata_submission.publication_recid) + + if observer and has_coordinator_permissions(recid, current_user): + ctx['observer_key'] = observer.observer_key increment(recid) @@ -541,7 +563,12 @@ def create_new_version(recid, user, notify_uploader=True, uploader_message=None) inspire_id=hepsubmission.inspire_id, coordinator=hepsubmission.coordinator, version=hepsubmission.version + 1) + + # Gets a generated or new SubmissionObserver object + observer_key = get_or_create_submission_observer(_rev_hepsubmission.publication_recid, regenerate=True) + db.session.add(_rev_hepsubmission) + db.session.add(observer_key) db.session.commit() if notify_uploader: diff --git a/hepdata/modules/records/assets/js/hepdata_common.js b/hepdata/modules/records/assets/js/hepdata_common.js index 833ecd320..5c250fc26 100644 --- a/hepdata/modules/records/assets/js/hepdata_common.js +++ b/hepdata/modules/records/assets/js/hepdata_common.js @@ -17,9 +17,14 @@ HEPDATA.current_record_id = undefined; HEPDATA.current_table_id = undefined; HEPDATA.current_table_name = undefined; HEPDATA.current_table_version = undefined; +HEPDATA.current_submission_status = undefined; +HEPDATA.current_observer_key = undefined; HEPDATA.clipboard = undefined; +// Stores a list of CSS selectors to track added Clipboards +HEPDATA.clipboard_list = []; HEPDATA.selected = {}; HEPDATA.site_url = "https://www.hepdata.net"; +HEPDATA.OBSERVER_KEY_LENGTH = 8; HEPDATA.current_filters = { "text": "", @@ -236,31 +241,84 @@ HEPDATA.render_associated_files = function (associated_files, placement) { } }; -HEPDATA.setup_clipboard = function () { +HEPDATA.setup_default_clipboards = function () { + /* + Sets up the default clipboards for the base HEPData webpage, and triggers + adding associated success/error event listeners. + */ if (HEPDATA.clipboard == undefined) { - HEPDATA.clipboard = new ClipboardJS('.copy-btn'); - HEPDATA.cite_clipboard = new ClipboardJS('.cite-copy-btn') - - const clipboards = [HEPDATA.clipboard, HEPDATA.cite_clipboard]; + HEPDATA.clipboard = HEPDATA.setup_clipboard('.copy-btn'); + HEPDATA.observer_clipboard = HEPDATA.setup_clipboard('.observer-copy-btn'); + HEPDATA.cite_clipboard = HEPDATA.setup_clipboard('.cite-copy-btn'); toastr.options.timeOut = 3000; + } +}; - for (var i in clipboards) { +HEPDATA.setup_clipboard = function(selector) { + /* + Sets up a ClipboardJS object from a CSS selector. - clipboards[i].on('success', function (e) { - toastr.success(e.text + ' copied to clipboard.') - }); + @param {string} selector - A CSS selector used to select objects for clipboard + */ - clipboards[i].on('error', function (e) { - if (navigator.userAgent.indexOf("Safari") > -1) { - toastr.success('Press ⌘ + C to finalise copy'); - } else { - toastr.error('There was a problem copying the link.'); - } - }) - } + toastr.options.timeOut = 3000; + + // If we have already set this clipboard up, we return null + if(HEPDATA.clipboard_list.includes(selector)) { + return null; + } + else { + // Push selector to the list to avoid adding duplicate events + HEPDATA.clipboard_list.push(selector); + } + + // Select all buttons in the document based on the selector + let button_objects = document.querySelectorAll(selector); + + // Get the ClipboardJS object from the objects. + let clipboard = new ClipboardJS(button_objects); + + // Add the clipboard's success and error toasts. + clipboard.on('success', function (e) { + toastr.success(e.text + ' copied to clipboard.') + }); + + clipboard.on('error', function (e) { + if (navigator.userAgent.indexOf("Safari") > -1) { + toastr.success('Press ⌘ + C to finalise copy'); + } else { + toastr.error('There was a problem copying the link.'); } + }) + + return clipboard; } +HEPDATA.get_observer_key_data = function(recid, as_url) { + /** + * Requests the observer key data from a recid for URL display purposes. + * Optionally can be returned as the record URL. + * + * @param {number} recid - The recid to get observer_key for. + * @param {number} as_url - When set to "1", will request response as a full access URL + * @return {Promise} - Returns the request promise to later retrieve the observer key + */ + + let request_url = '/record/coordinator/observer_key/' + recid; + // Setting the request flag if required + if (as_url === 1) request_url += '/1'; + + return $.ajax({ + dataType: 'json', + url: request_url, + processData: false, + cache: true + }).then(function (data) { + HEPDATA.current_observer_key = data['observer_exists'] ? data['observer_key'] : null; + return HEPDATA.current_observer_key; + }); +}; + window.HEPDATA = HEPDATA; export default HEPDATA; diff --git a/hepdata/modules/records/assets/js/hepdata_record_js.js b/hepdata/modules/records/assets/js/hepdata_record_js.js index 7982e0003..97f769920 100644 --- a/hepdata/modules/records/assets/js/hepdata_record_js.js +++ b/hepdata/modules/records/assets/js/hepdata_record_js.js @@ -171,6 +171,14 @@ HEPDATA.hepdata_record = (function () { $("#revise-confirm").hide(); $("#revise-success").removeClass('hidden'); + let observer_promise = HEPDATA.get_observer_key_data(HEPDATA.current_record_id, 1); + observer_promise.then(function(observer_key) { + if (observer_key) { + $('#revision_data_link').val(observer_key); + HEPDATA.setup_clipboard("#revision_button"); + } + }); + var count = 5; setInterval(function () { count -= 1; diff --git a/hepdata/modules/records/assets/js/hepdata_resources.js b/hepdata/modules/records/assets/js/hepdata_resources.js index 248b53b77..494891e0e 100644 --- a/hepdata/modules/records/assets/js/hepdata_resources.js +++ b/hepdata/modules/records/assets/js/hepdata_resources.js @@ -70,32 +70,64 @@ HEPDATA.hepdata_resources = (function () { return d['doi'] == null ? '' : '' + d['doi'] + ''; }); - // Add the landing page button - resource_item.append("a") - .attr('target', '_new') - .attr("class", "btn btn-primary btn-sm") - .attr("href", function(d) { - return '/record/resource/' + d.id + '?landing_page=true'; - }) - .text("Landing Page"); + let todo = HEPDATA.current_submission_status === 'todo'; + let observer_key_promise; + + // If unfinished, we can just resolve the promise with null + if (!todo) { + observer_key_promise = Promise.resolve(null); + } else if (HEPDATA.current_observer_key) { + // If unfinished, and the key has been set, we can continue, + observer_key_promise = Promise.resolve(HEPDATA.current_observer_key); + } else { + // Unfinished, and no key: We need to request this key, if we can. + observer_key_promise = HEPDATA.get_observer_key_data(HEPDATA.current_record_id); + } - resource_item.append("a") - .attr('target', '_new') - .attr("class", "btn btn-primary btn-sm") - .attr("href", function (d) { - var download_location = d.location; + // Wait for the promise to resolve, as we cannot continue without the append + observer_key_promise.then(function(observer_key) { + let key_append = ""; + let should_append = todo && observer_key; + + if(should_append) { + key_append = '&observer_key=' + observer_key; + } + + // Add the landing page button + resource_item.append("a") + .attr('target', '_new') + .attr("class", "btn btn-primary btn-sm") + .attr("href", function(d) { + let landing_page_url = '/record/resource/' + d.id + '?landing_page=true'; + if(should_append) { + landing_page_url += key_append; + } + return landing_page_url; + }) + .text("Landing Page"); + + resource_item.append("a") + .attr('target', '_new') + .attr("class", "btn btn-primary btn-sm") + .attr("href", function (d) { + var download_location = d.location; + if (d.location.indexOf('http') == -1) { + let view_page_url = '/record/resource/' + d.id + '?view=true'; + if(should_append) { + view_page_url += key_append; + } + download_location = view_page_url; + } + return download_location; + }).text(function (d) { if (d.location.indexOf('http') == -1) { - download_location = '/record/resource/' + d.id + '?view=true'; + return "Download" } - return download_location; - }).text(function (d) { - if (d.location.indexOf('http') == -1) { - return "Download" - } - return "View Resource"; - }); + return "View Resource"; + }); - HEPDATA.typeset([d3.select("#resourceModal").node()]); + HEPDATA.typeset([d3.select("#resourceModal").node()]); + }); }; var create_modal_view = function (recid, version) { @@ -105,9 +137,6 @@ HEPDATA.hepdata_resources = (function () { dataType: 'json', url: '/record/resources/' + recid + '/' + version, success: function (data) { - - - var resource_list = d3.select("#resource-filter ul"); resource_list.html(''); diff --git a/hepdata/modules/records/assets/js/hepdata_tables.js b/hepdata/modules/records/assets/js/hepdata_tables.js index 26a244861..d8056db73 100644 --- a/hepdata/modules/records/assets/js/hepdata_tables.js +++ b/hepdata/modules/records/assets/js/hepdata_tables.js @@ -50,7 +50,7 @@ HEPDATA.switch_table = function (listId, table_requested, table_name, status) { $("#direct_data_link").val(direct_link); $(".copy-btn").attr('data-clipboard-text', direct_link); - HEPDATA.setup_clipboard(); + HEPDATA.setup_default_clipboards(); // Reset the table loading section $("#hepdata_table_loading").removeClass("hidden"); @@ -87,18 +87,62 @@ HEPDATA.switch_table = function (listId, table_requested, table_name, status) { HEPDATA.table_renderer.get_and_display_table(data_url); }); - $(".data_download_link").each(function () { - var data_format = $(this).text().toLowerCase(); - var data_url = '/download/table/' + _recid + '/' + encoded_name + '/' + HEPDATA.current_table_version + '/' + data_format; - $(this).attr('href', data_url); - }); - - if (HEPDATA.current_record_type == 'table') { - $("#json_link").hide(); - } else { - $("#json_link").attr('href', '/download/table/' + _recid + '/' + encoded_name + '/' + HEPDATA.current_table_version + '/json'); - $("#json_link").show(); + // Some logic to decide which key to pass, or if to query for the observer key + let observer_key_promise; + if(HEPDATA.current_submission_status === 'todo') { + // If to-do, we either pass through the key we have, or execute as normal. + if(HEPDATA.current_observer_key) { + observer_key_promise = $.Deferred().resolve(HEPDATA.current_observer_key).promise(); + } + else { + observer_key_promise = HEPDATA.get_observer_key_data(HEPDATA.current_record_id); } + } + else { + observer_key_promise = $.Deferred().resolve(null).promise(); } + + observer_key_promise.then(function(requested_key) { + // If it doesn't match the correct length, we null it. + // If yes, create the append string. + let observer_append; + if(requested_key && requested_key.length === HEPDATA.OBSERVER_KEY_LENGTH) { + observer_append = '?observer_key=' + requested_key; + + direct_link += observer_append; + direct_link = direct_link.replace('?observer', '&observer'); + $("#direct_data_link").val(direct_link); + $(".copy-btn").attr('data-clipboard-text', direct_link); + } else { + // Null it if unexpected length + requested_key = null; + } + + $(".data_download_link").each(function () { + var data_format = $(this).text().toLowerCase(); + var data_url = '/download/table/' + _recid + '/' + encoded_name + '/' + HEPDATA.current_table_version + '/' + data_format; + + if(requested_key) { + data_url += observer_append; + } + + $(this).attr('href', data_url); + }); + + let json_link = $("#json_link"); + + if (HEPDATA.current_record_type === 'table') { + json_link.hide(); + } else { + let json_url = '/download/table/' + _recid + '/' + encoded_name + '/' + HEPDATA.current_table_version + '/json'; + + if(requested_key) { + json_url += observer_append; + } + + json_link.attr('href', json_url); + json_link.show(); + } + }); }; HEPDATA.table_renderer = { @@ -107,6 +151,14 @@ HEPDATA.table_renderer = { Render only the main table information (name, details, etc.) at the top, then decides whether to trigger render of the table or not. */ + const params = new URLSearchParams(document.location.search); + const observer_key = params.get('observer_key'); + + // Add the observer key + if(observer_key && observer_key.length === HEPDATA.OBSERVER_KEY_LENGTH) { + url += "?observer_key=" + observer_key; + } + $.ajax({ dataType: "json", url: url, diff --git a/hepdata/modules/records/templates/hepdata_records/components/record-breadcrumbs.html b/hepdata/modules/records/templates/hepdata_records/components/record-breadcrumbs.html index 57939fadf..1a6d3b28a 100644 --- a/hepdata/modules/records/templates/hepdata_records/components/record-breadcrumbs.html +++ b/hepdata/modules/records/templates/hepdata_records/components/record-breadcrumbs.html @@ -53,7 +53,12 @@ {% endif %} -