Skip to content

Commit

Permalink
Merge pull request #2801 from uktrade/feat/move-analysis-accordion
Browse files Browse the repository at this point in the history
Feat: copy code snippets to detail/preview page
  • Loading branch information
ian-leggett authored Oct 19, 2023
2 parents 9f232a1 + 37f72c7 commit 2198db8
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 14 deletions.
4 changes: 2 additions & 2 deletions dataworkspace/dataworkspace/apps/datasets/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
path(
"<uuid:dataset_uuid>/grid",
login_required(views.ReferenceDatasetGridView.as_view()),
name="reference_dataset_grid",
name="reference_dataset_detail",
),
path(
"<uuid:dataset_uuid>/reference/download",
Expand Down Expand Up @@ -111,7 +111,7 @@
),
path(
"<uuid:dataset_uuid>/table/<uuid:object_id>/grid",
login_required(views.DataCutSourceDetailView.as_view()),
login_required(views.DataSourcesetDetailView.as_view()),
{"model_class": models.SourceTable},
name="source_table_detail",
),
Expand Down
108 changes: 105 additions & 3 deletions dataworkspace/dataworkspace/apps/datasets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ def _get_context_data_for_reference_dataset(self, ctx, **kwargs):
"DATA_GRID_REFERENCE_DATASET_FLAG": settings.DATA_GRID_REFERENCE_DATASET_FLAG,
"code_snippets": code_snippets,
"columns": columns,
"tools_links": get_tools_links_for_user(self.request.user, self.request.scheme),
"subscription": {
"current_user_is_subscribed": subscription.exists()
and subscription.first().is_active(),
Expand Down Expand Up @@ -924,6 +925,15 @@ def get(self, request, dataset_uuid):
class ReferenceDatasetGridView(View):
def get(self, request, dataset_uuid):
dataset = find_dataset(dataset_uuid, request.user, ReferenceDataset)
code_snippets = get_code_snippets_for_reference_table(dataset)
columns = None
if dataset.external_database:
columns = datasets_db.get_columns(
dataset.external_database.memorable_name,
schema="public",
table=dataset.table_name,
include_types=True,
)
log_event(
self.request.user,
EventLog.TYPE_DATA_TABLE_VIEW,
Expand All @@ -936,8 +946,13 @@ def get(self, request, dataset_uuid):
)
return render(
request,
"datasets/reference_dataset_grid.html",
context={"model": dataset},
"datasets/data-preview/reference_dataset_preview.html",
context={
"model": dataset,
"code_snippets": code_snippets,
"columns": columns,
"tools_links": get_tools_links_for_user(self.request.user, self.request.scheme),
},
)


Expand Down Expand Up @@ -1089,8 +1104,95 @@ def get(self, request, dataset_uuid, **kwargs):
)


class DataSourcesetDetailView(DetailView):
template_name = "datasets/data-preview/data_sourceset_preview.html"

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["model"] = self.object
columns = datasets_db.get_columns(
self.object.database.memorable_name,
schema=self.object.schema,
table=self.object.table,
include_types=True,
)

ctx.update(
{
"has_access": self.object.dataset.user_has_access(self.request.user),
"tools_links": get_tools_links_for_user(self.request.user, self.request.scheme),
"code_snippets": get_code_snippets_for_table(self.object),
"columns": columns,
}
)
return ctx

def dispatch(self, request, *args, **kwargs):
source = self.get_object()
if not source.data_grid_enabled:
raise DatasetPreviewDisabledError(source.dataset)

if not source.dataset.user_has_access(self.request.user):
raise DatasetPermissionDenied(source.dataset)

log_event(
self.request.user,
EventLog.TYPE_DATA_TABLE_VIEW,
source,
extra={
"path": self.request.get_full_path(),
"data_table_name": source.name,
"data_table_id": source.id,
"dataset": source.dataset.name,
**(
{"data_table_tablename": f"{source.schema}.{source.table}"}
if hasattr(source, "schema")
else {
"data_table_sourcetables": [
f"{s.schema}.{s.table}" for s in source.tables.all()
]
}
),
},
)

return super().dispatch(request, *args, **kwargs)

def get_object(self, queryset=None):
dataset = find_dataset(self.kwargs["dataset_uuid"], self.request.user)
table_object = get_object_or_404(
self.kwargs["model_class"],
dataset=dataset,
pk=self.kwargs["object_id"],
)

return table_object


class DataCutSourceDetailView(DetailView):
template_name = "datasets/data_cut_source_detail.html"
template_name = "datasets/data-preview/data_cut_source_preview.html"

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["model"] = self.object

code_snippet = get_code_snippets_for_query(self.object.query)

columns = datasets_db.get_columns(
database_name=self.object.database.memorable_name,
query=self.object.query,
include_types=True,
)

ctx.update(
{
"has_access": self.object.dataset.user_has_access(self.request.user),
"tools_links": get_tools_links_for_user(self.request.user, self.request.scheme),
"code_snippets": code_snippet,
"columns": columns,
}
)
return ctx

def dispatch(self, request, *args, **kwargs):
source = self.get_object()
Expand Down
6 changes: 4 additions & 2 deletions dataworkspace/dataworkspace/templates/_base_no_footer.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load static core_filters waffle_tags %}
<!DOCTYPE html>
<html lang="en-GB" class="govuk-template" style="overflow: hidden; height: 100%">
<html lang="en-GB" class="govuk-template" style="overflow: scroll; height: 100%">

<head>
<meta charset="utf-8">
Expand Down Expand Up @@ -45,7 +45,7 @@
{% block head %}{% endblock %}
</head>

<body id="body" class="govuk-template__body" style="overflow: hidden; height: 100%">
<body id="body" class="govuk-template__body">
{% include 'partials/gtm_body.html' %}

<script nonce="{{ request.csp_nonce }}">
Expand Down Expand Up @@ -74,6 +74,8 @@
<script nonce="{{ request.csp_nonce }}">
window.GOVUKFrontend.initAll();
</script>
<script src="{% static 'assets/vendor/highlight/highlight.pack.js' %}"></script>
<script nonce="{{ request.csp_nonce }}">hljs.initHighlightingOnLoad();</script>

{% block footer_scripts %}
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{% extends '_main_no_container.html' %}
{% load static core_tags humanize waffle_tags datasets_tags %}

{% block page_title %}View {{ object.name }} - {{ block.super }}{% endblock %}data-grid

{% block footer_scripts %}
<script src="{% static 'ag-grid-community.min.js' %}"></script>
<script src="{% static 'dayjs.min.js' %}"></script>
<script src="{% static 'js/grid-utils.js' %}"></script>
<script src="{% static 'data-grid.js' %}"></script>
{# We *have* to include the stylesheet after the ag-grid js as it overrides the default styles #}
<link rel="stylesheet" type="text/css" href="{% static 'data-grid.css' %}"/>
{{ object.get_column_config|json_script:"column_data" }}
{% saved_grid_config user object as grid_config %}
{{ grid_config|json_script:"grid-config" }}
<script nonce="{{ request.csp_nonce }}">
window.initDataGrid(
JSON.parse(document.getElementById('column_data').textContent),
'{{ object.get_grid_data_url }}',
'?download=1',
null,
'{{ object.name }}-custom-export.csv',
{% flag CHART_BUILDER_BUILD_CHARTS_FLAG %}
'{% url 'datasets:create_chart_from_grid' object.dataset.id object.id %}',
{% else %}
null,
{% endflag %}
null,
'{{ object.dataset.id }}',
'{{ object.dataset.name }}',
'{{ object.dataset.get_type_display }}',
null,
'{% url 'datasets:log_data_preview_load_time' object.dataset.id object.id %}'
);
</script>
{% endblock footer_scripts %}
{% block content %}
{% with object.get_metadata_row_count as row_count %}
<div class="govuk-breadcrumbs govuk-!-padding-left-2 govuk-!-padding-bottom-2 govuk-!-margin-bottom-0 app-grid-breadcrumbs">
<ol class="govuk-breadcrumbs__list">
<li class="govuk-breadcrumbs__list-item">
<a class="govuk-breadcrumbs__link" href="">Home</a>
</li>
<li class="govuk-breadcrumbs__list-item">
<a class="govuk-breadcrumbs__link"
href="{% url "datasets:dataset_detail" dataset_uuid=object.dataset.id %}#{{ object.dataset.slug }}">{{ object.dataset }}</a>
</li>
<li class="govuk-breadcrumbs__list-item">{{ object.name }}</li>
{% flag SECURITY_CLASSIFICATION_FLAG %}
<div style="float: right" class="govuk-!-padding-right-3">
{% if not object.dataset.government_security_classification %}
<strong class="govuk-tag govuk-tag--yellow">Awaiting classification</strong>
{% else %}
{% if object.dataset.get_government_security_classification_display == "OFFICIAL" %}
<strong
class="govuk-tag govuk-tag--blue">{{ object.dataset.get_government_security_classification_display }}</strong>
{% else %}
<strong
class="govuk-tag govuk-tag--red">{{ object.dataset.get_government_security_classification_display }}
{% if object.dataset.sensitivity.all %}
{% for sensitivity in object.dataset.sensitivity.all %}
{% if not forloop.first %}and{% endif %}</span> {{ sensitivity }}
{% endfor %}
{% endif %} </strong>
{% endif %}
{% endif %}
</div>
{% endflag %}
</ol>
</div>
<div id="collapsible-header" class="govuk-!-margin-top-4">
{% if object.disable_data_grid_interaction %}
{% include "datasets/partials/grid-interaction-disabled.html" %}
{% endif %}
{% include "datasets/partials/grid-view-saved.html" %}
{% if object.dataset.enquiries_contact %}
<a class="govuk-link govuk-link--no-visited-state govuk-!-padding-right-3" style="float: right"
href="mailto:{{ object.dataset.enquiries_contact.email }}?subject=Reporting an issue - {{ object.name }}">
{% include "partials/report_an_issue_icon.html" %}
</a>
{% endif %}
<div class="govuk-!-padding-top-3 govuk-!-padding-left-2">
<span class="govuk-caption-xl">
Data table{% if object.schema %} ({{ object.schema }}.{{ object.table }}){% endif %}
</span>
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<h2 class="govuk-heading-l">{{ object.name }}</h2>
</div>
<div class="govuk-grid-column-full">
{% if columns or code_snippets and has_access %}
{% if columns %}
{% include "partials/column_list.html" with columns=columns dataset=dataset source=datacut_link %}
{% endif %}
{% if code_snippets and has_access %}
<details class="govuk-details govuk-!-margin-bottom-2" data-module="govuk-details">
<summary class="govuk-details__summary">
<span class="govuk-details__summary-text">
Use this data for analysis<span
class="govuk-visually-hidden"> "{{ source_table.schema }}"."{{ source_table.table }}"</span>
</span>
</summary>
<div class="govuk-details__text">
{% include 'partials/code_snippets.html' with code_snippets=code_snippets source_table=datacut_link tools_links=tools_links %}
</div>
</details>
{% endif %}
{% endif %}
</div>
<div class="govuk-grid-column-full">
<p class="govuk-body govuk-!-padding-top-0 govuk-!-padding-right-3 govuk-!-margin-bottom-2 govuk-!-text-align-right">
<span class="govuk-!-font-weight-bold">Data last updated:</span>
{{ object.get_data_last_updated_date|gmt_date|default_if_none:"N/A" }} {{ object.get_data_last_updated_date|time_with_gmt_offset|default_if_none:"N/A" }}
</p>
</div>
</div>
</div>
</div>
<div class="app-compressed-grid">
<div class="app-grid-header">
<div class="grid-header-left">
<div class="govuk-button-group">
<p class="govuk-body govuk-!-font-weight-bold" id="data-grid-rowcount">
{% if row_count is not None %}
{% if row_count > 5000 %}Over 5,000{% else %}{{ row_count|intcomma }}{% endif %} rows
{% else %}
Loading data...
{% endif %}
</p>
{% flag CHART_BUILDER_BUILD_CHARTS_FLAG %}
<button class="govuk-button" id="data-grid-create-chart">Create a chart</button>
{% endflag %}
<button class="govuk-button govuk-button--secondary" data-module="govuk-button" id="data-grid-save-view" data-prevent-double-click="true">
Save view
</button>
<button class="govuk-button govuk-button--secondary" data-module="govuk-button" id="data-grid-reset-view">
Reset view
</button>
{% if object.data_grid_download_enabled %}
<button class="govuk-button govuk-button--secondary" data-module="govuk-button" id="downloadData">
Download this data
</button>
{% endif %}
{# We only support data dictionaries for master and reference datasets currently #}
{# reference datagrid uses reference_dataset_grid.html btw #}
{% if object.type == 1 %}
{% if object.dataset.dictionary_published or request.user|can_edit_dataset:object.dataset %}
<a href="{% url "datasets:data_dictionary" source_uuid=object.id %}?dataset_uuid={{ object.dataset.id }}" target="_blank" class="govuk-link govuk-!-margin-top-1 govuk-!-font-size-16">
Data dictionary
</a>
{% endif %}
{% endif %}
</div>
</div>
<div class="grid-header-right govuk-!-padding-right-3">
<button id="increase-grid-button" class="govuk-button govuk-button--secondary" data-module="govuk-button">Show more rows</button>
</div>
</div>
<div
id="data-grid"
class="ag-theme-alpine"
data-save-view-url="{{ object.get_save_grid_view_url }}"
{% if row_count is not None %}
data-initial-row-count="{{ row_count }}"
{% endif %}
data-source-name= "{{ object.name }}{% if object.schema %} ({{ object.schema }}.{{ object.table }}){% endif %}"
{% if object.disable_data_grid_interaction %}
data-disable-interaction="true"
{% endif %}
></div>
</div>
{% include "datasets/partials/dialog-data-cut.html" %}
{% endwith %}
{% csrf_token %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,27 @@
</span>
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<h2 class="govuk-heading-l govuk-!-margin-bottom-0">{{ object.name }} </h2>
<h2 class="govuk-heading-l">{{ object.name }}</h2>
</div>
<div class="govuk-grid-column-full">
{% if columns or code_snippets and has_access %}
{% if columns %}
{% include "partials/column_list.html" with columns=columns dataset=dataset source=datacut_link %}
{% endif %}
{% if code_snippets and has_access %}
<details class="govuk-details govuk-!-margin-bottom-2" data-module="govuk-details">
<summary class="govuk-details__summary">
<span class="govuk-details__summary-text">
Use this data for analysis<span
class="govuk-visually-hidden"> "{{ source_table.schema }}"."{{ source_table.table }}"</span>
</span>
</summary>
<div class="govuk-details__text">
{% include 'partials/code_snippets.html' with code_snippets=code_snippets source_table=datacut_link tools_links=tools_links %}
</div>
</details>
{% endif %}
{% endif %}
</div>
<div class="govuk-grid-column-full">
<p class="govuk-body govuk-!-padding-top-0 govuk-!-padding-right-3 govuk-!-margin-bottom-2 govuk-!-text-align-right">
Expand Down
Loading

0 comments on commit 2198db8

Please sign in to comment.