Skip to content

Commit

Permalink
Merge pull request #18 from ministryofjustice/connect-catalogue
Browse files Browse the repository at this point in the history
Query search results from the catalogue library
  • Loading branch information
MatMoore authored Jan 24, 2024
2 parents 6ce66d0 + 9bfd3b4 commit 4166849
Show file tree
Hide file tree
Showing 10 changed files with 3,123 additions and 94 deletions.
9 changes: 0 additions & 9 deletions READ.md

This file was deleted.

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Quick start

Project contains a poetry dependency file for installation with `poetry install`
To run `poetry run python manage.py runserver`

## Configuration

The following should be set using environment variables:

- CATALOGUE_URL
- CATALOGUE_TOKEN

## Current Endpoints

/search

![Screenshot of the service showing the search page](image.png)
3 changes: 3 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
with open(SAMPLE_SEARCH_RESULTS_FILENAME) as f:
SAMPLE_SEARCH_RESULTS = yaml.safe_load(f)

DATAHUB_TOKEN = os.environ["CATALOGUE_TOKEN"]
DATAHUB_URL = os.environ["CATALOGUE_URL"]

try:
from local import * # type: ignore[reportMissingImports]
except ImportError:
Expand Down
9 changes: 9 additions & 0 deletions home/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from data_platform_catalogue.client import BaseCatalogueClient
from data_platform_catalogue.client.datahub import DataHubCatalogueClient
from django.conf import settings


def get_catalogue_client() -> BaseCatalogueClient:
return DataHubCatalogueClient(
jwt_token=settings.DATAHUB_TOKEN, api_url=settings.DATAHUB_URL
)
Empty file added home/templatetags/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions home/templatetags/markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from markdown import markdown as markdown_func

register = template.Library()


@register.filter
@stringfilter
def markdown(value):
return mark_safe(markdown_func(value))
13 changes: 11 additions & 2 deletions home/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.conf import settings
from django.shortcuts import render

from .services import get_catalogue_client


# Create your views here.
def home_view(request):
Expand All @@ -10,7 +11,15 @@ def home_view(request):


def search_view(request):
query = request.GET.get("query", "")
page = request.GET.get("page", None)

client = get_catalogue_client()
search_results = client.search(query=query, page=page)

context = {}
context["query"] = query
context["service_name"] = "Daap Data Catalogue"
context.update(settings.SAMPLE_SEARCH_RESULTS)
context["results"] = search_results.page_results
context["total_results"] = search_results.total_results
return render(request, "search.html", context)
3,094 changes: 3,044 additions & 50 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ django = "^5.0.1"
pyyaml = "^6.0.1"
gunicorn = "^21.2.0"
whitenoise = "^6.6.0"
ministryofjustice-data-platform-catalogue = "^0.5.0"
markdown = "^3.5.2"

[tool.poetry.group.dev] # dev group definition

[tool.poetry.group.dev.dependencies]
black = "^23.12.1"



[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
56 changes: 25 additions & 31 deletions templates/search.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "base/base.html" %}
{% load static %}
{% load markdown %}

{% block content %}
<div class="govuk-width-container">
Expand All @@ -11,7 +12,7 @@
<div class="govuk-form-group">
<h1 class="govuk-heading-xl">Find MOJ Data</h1>
<div class="search-container">
<input name="search" class="govuk-input search-input" id="searchForm-search" type="search" value="">
<input name="query" class="govuk-input search-input" id="searchForm-search" type="search" value="{{query}}">
<button class="search-button" type="submit">
<svg aria-hidden="true" class="search-icon" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" width="40" height="40"><path d="M25.7 24.8L21.9 21c.7-1 1.1-2.2 1.1-3.5 0-3.6-2.9-6.5-6.5-6.5S10 13.9 10 17.5s2.9 6.5 6.5 6.5c1.6 0 3-.6 4.1-1.5l3.7 3.7 1.4-1.4zM12 17.5c0-2.5 2-4.5 4.5-4.5s4.5 2 4.5 4.5-2 4.5-4.5 4.5-4.5-2-4.5-4.5z" fill="currentColor"></path>
</svg><span class="govuk-visually-hidden">Search</span>
Expand Down Expand Up @@ -41,54 +42,47 @@ <h2 class="govuk-heading-l govuk-!-display-inline-block">236 Results</h2>
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<h3 class="govuk-heading-m govuk-!-margin-bottom-2">
<a href="/dataset">{{result.title}}</a>
<a href="/dataset/{{result.id}}"></a>{{result.name}}</a>
{% if result.result_type.name == "DATA_PRODUCT" %}
<strong class="govuk-tag govuk-!-margin-left-2">
Data product
</strong>
{% elif result.result_type.name == "TABLE" %}
<strong class="govuk-tag govuk-!-margin-left-2">
Table
</strong>
{% endif %}
</h3>

<p class="govuk-body-m">{{result.summary}}</p>
{% if result.description %}
<div class="govuk-body-m">{{result.description|markdown}}</div>
{% endif %}

<dl class="govuk-summary-list">

{% if result.metadata.domain %}
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Registered by</dt>
<dd class="govuk-summary-list__value">
{{result.registered_by}}
</dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Database Name</dt>
<dd class="govuk-summary-list__value">
{{result.database_name}}
</dd>
</div>


<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">First created</dt>
<dd class="govuk-summary-list__value">
{{result.first_created}}
</dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Refresh period</dt>
<dt class="govuk-summary-list__key">Domain</dt>
<dd class="govuk-summary-list__value">
{{result.refresh_period}}
{{result.metadata.domain.properties.name}}
</dd>
</div>

{% endif %}
{% if result.metadata.owner %}
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Retention period</dt>
<dt class="govuk-summary-list__key">Registered by</dt>
<dd class="govuk-summary-list__value">
{{result.retention_period}}
{{result.metadata.owner}}
</dd>
</div>
{% endif %}
{% if result.tags %}
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Tags</dt>
<dd class="govuk-summary-list__value">
{{ result.tags |join:", " }}
</dd>
</div>
{% endif %}
</div>
</div>
<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible">
Expand Down

0 comments on commit 4166849

Please sign in to comment.