Esempio n. 1
0
def process_data_tables(ctx, data_record_query, first_data_id,
                        data_table=None):
    data_table_metadata = OrderedDict()
    ctx['show_upload_area'] = False

    if ctx['show_upload_widget'] and data_record_query.count() == 0:
        ctx['show_upload_area'] = True
    elif data_record_query.count() > 0:
        record_submissions = data_record_query.all()
        for submission_record in record_submissions:
            processed_name = "".join(submission_record.name.split())
            data_table_metadata[submission_record.id] = {
                "id": submission_record.id, "processed_name": processed_name,
                "name": submission_record.name,
                "location": submission_record.location_in_publication,
                "doi": submission_record.doi,
                "description": sanitize_html(
                    truncate_string(submission_record.description, 20),
                    tags=[],
                    strip=True
                )
            }

            if first_data_id == -1:
                first_data_id = submission_record.id

            if data_table:
                if submission_record.name == data_table:
                    first_data_id = submission_record.id

    return data_table_metadata, first_data_id
Esempio n. 2
0
def generate_table_structure(table_contents):
    """
    Creates a renderable structure from the table structure we've defined.

    :param table_contents:
    :return: a dictionary encompassing the qualifiers, headers and values
    """

    record = {"name": table_contents["name"], "doi": table_contents["doi"],
              "location": table_contents["location"],
              "qualifiers": {},
              "qualifier_order": [], "headers": [],
              "review": table_contents["review"],
              "associated_files": table_contents["associated_files"],
              "keywords": {},
              "values": []}

    record["description"] = sanitize_html(table_contents["title"])

    # add in keywords
    if table_contents['keywords'] is not None:
        for keyword in table_contents['keywords']:
            if keyword.name not in record['keywords']:
                record['keywords'][keyword.name] = []

            if keyword.value not in record['keywords'][keyword.name]:
                record['keywords'][keyword.name].append(keyword.value)

    tmp_values = {}
    x_axes = OrderedDict()
    x_headers = []
    process_independent_variables(table_contents, x_axes, x_headers)
    record["x_count"] = len(x_headers)
    record["headers"] += x_headers

    group_count = 0
    yheaders = []

    process_dependent_variables(group_count, record, table_contents,
                                tmp_values, x_axes, yheaders)

    # attempt column merge
    last_yheader = None
    for counter, yheader in enumerate(yheaders):
        if not last_yheader:
            last_yheader = yheader
        else:
            if last_yheader["name"] == yheader["name"]:
                last_yheader["colspan"] += 1
            else:
                record["headers"].append(last_yheader)
                last_yheader = yheader
        if counter == len(yheaders) - 1:
            record["headers"].append(last_yheader)

    for tmp_value in tmp_values:
        record["values"].append(tmp_values[tmp_value])

    return record
Esempio n. 3
0
def test_sanitize_html(app):
    with app.app_context():
        test_cases = [
            ("<b>Here is some bold text</b> and <script>here is a dodgy script</script>",
             "<b>Here is some bold text</b> and &lt;script&gt;here is a dodgy script&lt;/script&gt;"
             ),
            ("<i>Dphi</i> correlation functions for 0.15<pT<4 GEV/c and 4<p_T^trig<6 GEV/c.",
             "<i>Dphi</i> correlation functions for 0.15&lt;pT&lt;4 GEV/c and 4&lt;p_T^trig&lt;6 GEV/c."
             ),
            ("Dphi correlation functions for 2 < pT < 4  GEV/c and 4 < p_T^trig < 6 GEV/c",
             "Dphi correlation functions for 2 &lt; pT &lt; 4  GEV/c and 4 &lt; p_T^trig &lt; 6 GEV/c"
             ),
            ("Dphi correlation functions for pT<0.15 GEV/c",
             "Dphi correlation functions for pT&lt;0.15 GEV/c"),
            ("Dphi correlation functions for pT>4 GEV/c",
             "Dphi correlation functions for pT&gt;4 GEV/c"),
            ("Variation of Tkin with <β> for different energies and centralities.",
             "Variation of Tkin with &lt;β&gt; for different energies and centralities."
             ),
            ("""- - - - - - - - Overview of HEPData Record - - - - - - - - <br/><br/>
<b>Background Fit results:</b> <ul> <li><a href=\"89413?version=1&table=Backgroundfit1\">CRs</a>
<li><a href=\"89413?version=1&table=Backgroundfit2\">VRs</a> <li><a href=\"89413?version=1&table=Backgroundfit5\">inclusive
DF-0J SRs</a> <li><a href=\"89413?version=1&table=Backgroundfit6\">inclusive DF-1J
SRs</a> <li><a href=\"89413?version=1&table=Backgroundfit3\">inclusive SF-0J SRs</a>
<li><a href=\"89413?version=1&table=Backgroundfit4\">inclusive SF-1J SRs</a> </ul>""",
             """- - - - - - - - Overview of HEPData Record - - - - - - - - <br><br>
<b>Background Fit results:</b> <ul> <li><a href=\"89413?version=1&amp;table=Backgroundfit1\">CRs</a>
</li><li><a href=\"89413?version=1&amp;table=Backgroundfit2\">VRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit5\">inclusive
DF-0J SRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit6\">inclusive DF-1J
SRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit3\">inclusive SF-0J SRs</a>
</li><li><a href=\"89413?version=1&amp;table=Backgroundfit4\">inclusive SF-1J SRs</a> </li></ul>"""
             ), (None, None)
        ]

        # Test default params
        for original, sanitized in test_cases:
            assert (sanitize_html(original) == sanitized)

        # Test other params
        assert (sanitize_html(test_cases[0][0], strip=True) ==
                "<b>Here is some bold text</b> and here is a dodgy script")
        assert (sanitize_html(test_cases[0][0],
                              tags=["script", "b"]) == test_cases[0][0])
        assert (
            sanitize_html(test_cases[0][0], tags=["i"]) ==
            "&lt;b&gt;Here is some bold text&lt;/b&gt; and &lt;script&gt;here is a dodgy script&lt;/script&gt;"
        )
        assert (sanitize_html(
            "<a href=\"89413?version=1&amp;table=Backgroundfit3\">inclusive SF-0J SRs</a>",
            attributes=["title"]) == "<a>inclusive SF-0J SRs</a>")
        assert (
            sanitize_html(test_cases[1][0], tags=[], strip=True) ==
            "Dphi correlation functions for 0.15&lt;pT&lt;4 GEV/c and 4&lt;p_T^trig&lt;6 GEV/c."
        )
Esempio n. 4
0
def format_submission(recid, record, version, version_count, hepdata_submission,
                      data_table=None):
    """
    Performs all the processing of the record to be displayed.

    :param recid:
    :param record:
    :param version:
    :param version_count:
    :param hepdata_submission:
    :param data_table:
    :return:
    """
    ctx = {}
    if hepdata_submission is not None:

        ctx['site_url'] = current_app.config.get('SITE_URL', 'https://www.hepdata.net')
        ctx['record'] = record
        ctx["version_count"] = version_count

        if version is not -1:
            ctx["version"] = version
        else:
            # we get the latest version by default
            ctx["version"] = version_count

        if record is not None:
            if "collaborations" in record and type(record['collaborations']) is not list:
                collaborations = [x.strip() for x in record["collaborations"].split(",")]
                ctx['record']['collaborations'] = collaborations

            authors = record.get('authors', None)

            create_breadcrumb_text(authors, ctx, record)
            get_commit_message(ctx, recid)

            if authors:
                truncate_author_list(record)

            determine_user_privileges(recid, ctx)

        else:
            ctx['record'] = {}
            determine_user_privileges(recid, ctx)
            ctx['show_upload_widget'] = True
            ctx['show_review_widget'] = False

        ctx['participant_count'] = SubmissionParticipant.query \
            .filter_by(publication_recid=recid, status="primary") \
            .filter(SubmissionParticipant.role.in_(["reviewer", "uploader"])) \
            .count()
        ctx['reviewers_notified'] = hepdata_submission.reviewers_notified

        ctx['record']['last_updated'] = hepdata_submission.last_updated
        ctx['record']['hepdata_doi'] = "{0}".format(hepdata_submission.doi)

        if ctx['version'] > 1:
            ctx['record']['hepdata_doi'] += ".v{0}".format(ctx['version'])

        ctx['recid'] = recid
        ctx["status"] = hepdata_submission.overall_status
        ctx['record']['data_abstract'] = sanitize_html(decode_string(hepdata_submission.data_abstract))

        extract_journal_info(record)

        if hepdata_submission.overall_status != 'finished' and ctx["version_count"] > 0:
            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

        ctx['additional_resources'] = submission_has_resources(hepdata_submission)

        # query for a related data submission
        data_record_query = DataSubmission.query.filter_by(
            publication_recid=recid,
            version=ctx["version"]).order_by(DataSubmission.id.asc())

        format_tables(ctx, data_record_query, data_table, recid)

        ctx['access_count'] = get_count(recid)
        ctx['mode'] = 'record'
        ctx['coordinator'] = hepdata_submission.coordinator
        ctx['coordinators'] = get_coordinators_in_system()
        ctx['record'].pop('authors', None)

    return ctx