Built-in Fields

Search Fields take raw search metadata from Globus Search and expose them for use by templates. Commonly, raw data from Globus Search needs a bit more processing before it can be viewed in templates. Examples include parsing dates or generating links to the Globus Webapp.

The Django Globus Portal Framework includes some built-in templates which will automatically be rendered if given the right field names. Some of these include:

  • Title – A title for a given subject, shown on the search results page and the detail page.

  • Globus App Link – A link to the file on https://app.globus.org.

  • HTTPS URL – A direct-download link to the file.

First, let’s take a look at the metadata once more:

{
    "ingest_type": "GMetaList",
    "ingest_data": {
        "gmeta": [
            {
                "id": "metadata",
                "subject": "globus://ddb59af0-6d04-11e5-ba46-22000b92c6ec/share/godata/file1.txt",
                "visible_to": ["public"],
                "content": {
                    "title": "File Number 1",
                    "url": "globus://ddb59af0-6d04-11e5-ba46-22000b92c6ec/share/godata/file1.txt",
                    "author": "Data Researcher",
                    "tags": ["globus", "tutorial", "file"],
                    "date": "2022-11-15T12:31:28.560098",
                    "times_accessed": 23974,
                    "original_collection_name": "Globus Tutorial Endpoint 1"
                }
            }
        ]
    }
}

Create an empty myportal/fields.py file next to settings.py, and copy-paste the following code.

import os
from urllib.parse import urlsplit, urlunsplit, urlencode


def title(result):
    """The title for this Globus Search subject"""
    return result[0]["title"]


def globus_app_link(result):
    """A Globus Webapp link for the transfer/sync button on the detail page"""
    url = result[0]["url"]
    parsed = urlsplit(url)
    query_params = {
        "origin_id": parsed.netloc,
        "origin_path": os.path.dirname(parsed.path),
    }
    return urlunsplit(
        ("https", "app.globus.org", "file-manager", urlencode(query_params), "")
    )


def https_url(result):
    """Add a direct download link to files over HTTPS"""
    path = urlsplit(result[0]["url"]).path
    return urlunsplit(("https", "g-71c9e9.10bac.8443.data.globus.org", path, "", ""))

Here the result[0] variable encapsulates the information of a given search record, and can be used to access any component of the metadata content such as title and url.

To propagate myportal/fields.py throughout your portal, configure fields for your search index by adding fields to your SEARCH_INDEXES:

from myportal import fields

SEARCH_INDEXES = {
    "index-slug": {
        "uuid": "my-search-index-uuid",
        ...  # Previous fields hidden for brevity
        "fields": [
            # Calls a function with your search record as a parameter
            ("title", fields.title),
            ("globus_app_link", fields.globus_app_link),
            ("https_url", fields.https_url)
        ],
    }
}

You should notice the following changes the next time you run your server:

Continue on to cover custom Templates.