Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwood committed Sep 16, 2024
1 parent 2121022 commit 307d240
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 87 deletions.
90 changes: 54 additions & 36 deletions grantnav/csv_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
("Amount Disbursed", "result.amountDisbursed"),
("Award Date", "result.awardDateDateOnly"),
("URL", "result.recipientOrganization.0.url"),

("Planned Dates:Start Date", "result.plannedDates.0.startDateDateOnly"),
("Planned Dates:End Date", "result.plannedDates.0.endDateDateOnly"),
("Planned Dates:Duration (months)", "result.plannedDates.0.duration"),
("Actual Dates:Start Date", "result.actualDates.0.startDate"),
("Actual Dates:End Date", "result.actualDates.0.endDateDateOnly"),
("Actual Dates:Duration (months)", "result.actualDates.0.duration"),

("Recipient Org:Identifier", "result.recipientOrganization.0.id"),
("Recipient Org:Name", "result.recipientOrganization.0.name"),
("Recipient Org:Charity Number", "result.recipientOrganization.0.charityNumber"),
Expand All @@ -30,13 +32,27 @@
("Recipient Org:Location:2:Geographic Code Type", "result.recipientOrganization.0.location.2.geoCodeType"),
("Recipient Org:Location:2:Geographic Code", "result.recipientOrganization.0.location.2.geoCode"),
("Recipient Org:Location:2:Name", "result.recipientOrganization.0.location.2.name"),

("Recipient Individual Id", "result.recipientIndividual.id"),
("Recipient Individual Details:Primary Grant Reason", "result.toIndividualsDetails.primaryGrantReason"),
("Recipient Individual Details:Secondary Grant Reason", "result.toIndividualsDetails.secondaryGrantReason"),
("Recipient Individual Details:Grant Purpose", "result.toIndividualsDetails.grantPurpose"),

("Funding Org:Identifier", "result.fundingOrganization.0.id"),
("Funding Org:Name", "result.fundingOrganization.0.name"),
("Funding Org:Postal Code", "result.fundingOrganization.0.postalCode"),
("Funding Org:Charity Number", "result.fundingOrganization.0.charityNumber"),
("Funding Org:Company Number", "result.fundingOrganization.0.companyNumber"),


("Grant Programme:Code", "result.grantProgramme.0.code"),
("Grant Programme:Title", "result.grantProgramme.0.title"),
("Grant Programme:URL", "result.grantProgramme.0.url"),
("Funding Type:Title", "result.fundingType.0.title"),

("Regrant Type", "result.regrantType"),

("Funding Type Title", "result.fundingType.0.title"),

("Beneficiary Location:0:Name", "result.beneficiaryLocation.0.name"),
("Beneficiary Location:0:Country Code", "result.beneficiaryLocation.0.countryCode"),
("Beneficiary Location:0:Geographic Code", "result.beneficiaryLocation.0.geoCode"),
Expand Down Expand Up @@ -70,45 +86,47 @@
("Beneficiary Location:7:Geographic Code", "result.beneficiaryLocation.7.geoCode"),
("Beneficiary Location:7:Geographic Code Type", "result.beneficiaryLocation.7.geoCodeType"),
("From An Open Call?", "result.fromOpenCall"),
("The following fields are not in the 360 Giving Standard and are added by GrantNav.", ""),
# ("#comment The following fields are not in the 360 Giving Standard and are added by GrantNav.", ""),
# Additional data
("Data Source", "dataset.distribution.0.downloadURL"),
("Publisher:Name", "dataset.publisher.name"),

("Best Available Recipient Region", "result.additional_data.recipientRegionName"),
("Best Available Recipient District", "result.additional_data.recipientDistrictName"),
("Best Available Recipient District Geographic Code", "result.additional_data.recipientDistrictGeoCode"),
("Best Available Recipient Ward", "result.additional_data.recipientWardName"),
("Best Available Recipient Ward Geographic Code", "result.additional_data.recipientWardNameGeoCode"),

("Recipient Region", "result.additional_data.GNRecipientOrgRegionName"),
("Recipient Region Geographic code", "result.additional_data.GNRecipientOrgRegionGeoCode"),

("Recipient District", "result.additional_data.GNRecipientOrgDistrictName"),
("Recipient District Geographic code", "result.additional_data.GNRecipientOrgDistrictGeoCode"),

("Beneficiary Region", "result.additional_data.GNBeneficiaryRegionName"),
("Beneficiary Region Geographic code", "result.additional_data.GNBeneficiaryRegionGeoCode"),

("Beneficiary District", "result.additional_data.GNBeneficiaryDistrictName"),
("Beneficiary District Geographic code", "result.additional_data.GNBeneficiaryDistrictGeoCode"),

("Retrieved for use in GrantNav", "dataset.additional_data.datagetter_metadata.datetime_downloaded"),
("Funding Org: Org Type", "result.additional_data.TSGFundingOrgType"),
("Funding Org: Canonical Org ID", "result.additional_data.GNCanonicalFundingOrgId"),
("Funding Org: Canonical Name", "result.additional_data.GNCanonicalFundingOrgName"),
("Recipient Org: Date Registered", "result.additional_data.recipientOrgInfos.0.dateRegistered"),
("Recipient Org: Date Removed", "result.additional_data.recipientOrgInfos.0.dateRemoved"),
("Recipient Org: Org ID(s)", "result.additional_data.recipientOrgInfos.0.orgIDs"),
("Recipient Org: Latest Income", "result.additional_data.recipientOrgInfos.0.latestIncome"),
("Recipient Org: Latest Income Date", "result.additional_data.recipientOrgInfos.0.latestIncomeDate"),
("Recipient Org: Org Type", "result.additional_data.recipientOrgInfos.0.organisationTypePrimary"),
("Recipient Org: Registered Postcode", "result.additional_data.recipientOrgInfos.0.postalCode"),
("Recipient Org: Data Source", "result.additional_data.recipientOrgInfos.0.source"),
("Recipient Org: Canonical Org ID", "result.additional_data.GNCanonicalRecipientOrgId"),
("Recipient Org: Canonical Name", "result.additional_data.GNCanonicalRecipientOrgName"),
("License (see note)", "dataset.license"),
("Note, this file also contains OS data © Crown copyright and database right 2016, Royal Mail data © Royal Mail copyright and Database right 2016, National Statistics data © Crown copyright and database right 2015 & 2016, see http://grantnav.threesixtygiving.org/datasets/ for more information.", ""),
("Best Available Recipient Region (additional data)", "result.additional_data.recipientRegionName"),
("Best Available Recipient District (additional data)", "result.additional_data.recipientDistrictName"),
("Best Available Recipient District Geographic Code (additional data)", "result.additional_data.recipientDistrictGeoCode"),
("Best Available Recipient Ward (additional data)", "result.additional_data.recipientWardName"),
("Best Available Recipient Ward Geographic Code (additional data)", "result.additional_data.recipientWardNameGeoCode"),

("Recipient Region (additional data)", "result.additional_data.GNRecipientOrgRegionName"),
("Recipient Region Geographic code (additional data)", "result.additional_data.GNRecipientOrgRegionGeoCode"),

("Recipient District (additional data)", "result.additional_data.GNRecipientOrgDistrictName"),
("Recipient District Geographic code (additional data)", "result.additional_data.GNRecipientOrgDistrictGeoCode"),

("Beneficiary Region (additional data)", "result.additional_data.GNBeneficiaryRegionName"),
("Beneficiary Region Geographic code (additional data)", "result.additional_data.GNBeneficiaryRegionGeoCode"),

("Beneficiary District (additional data)", "result.additional_data.GNBeneficiaryDistrictName"),
("Beneficiary District Geographic code (additional data)", "result.additional_data.GNBeneficiaryDistrictGeoCode"),

("Retrieved for use in GrantNav (additional data)", "dataset.additional_data.datagetter_metadata.datetime_downloaded"),
("Funding Org: Org Type (additional data)", "result.additional_data.TSGFundingOrgType"),
("Funding Org: Canonical Org ID (additional data)", "result.additional_data.GNCanonicalFundingOrgId"),
("Funding Org: Canonical Name (additional data)", "result.additional_data.GNCanonicalFundingOrgName"),
("Recipient Org: Date Registered (additional data)", "result.additional_data.recipientOrgInfos.0.dateRegistered"),
("Recipient Org: Date Removed (additional data)", "result.additional_data.recipientOrgInfos.0.dateRemoved"),
("Recipient Org: Org ID(s) (additional data)", "result.additional_data.recipientOrgInfos.0.orgIDs"),
("Recipient Org: Latest Income (additional data)", "result.additional_data.recipientOrgInfos.0.latestIncome"),
("Recipient Org: Latest Income Date (additional data)", "result.additional_data.recipientOrgInfos.0.latestIncomeDate"),
("Recipient Org: Org Type (additional data)", "result.additional_data.recipientOrgInfos.0.organisationTypePrimary"),
("Recipient Org: Registered Postcode (additional data)", "result.additional_data.recipientOrgInfos.0.postalCode"),
("Recipient Org: Data Source (additional data)", "result.additional_data.recipientOrgInfos.0.source"),
("Recipient Org: Canonical Org ID (additional data)", "result.additional_data.GNCanonicalRecipientOrgId"),
("Recipient Org: Canonical Name (additional data)", "result.additional_data.GNCanonicalRecipientOrgName"),

# These two always need to be on the end
("#comment License (see note)", "dataset.license"),
("#comment See http://grantnav.threesixtygiving.org/datasets/ for license information.", ""),
])

grant_csv_titles = list(grants_csv.keys())
Expand Down
92 changes: 45 additions & 47 deletions grantnav/frontend/templates/components/field-chooser-listbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
content: "✓";
}

button[aria-disabled="true"], input[type="submit"][disabled] {
button[aria-disabled="true"],
input[type="submit"][disabled] {
opacity: 0.5;
}
</style>
Expand All @@ -44,12 +45,14 @@
aria-multiselectable="true" style="min-height: 300px; max-height: 300px; overflow: scroll;">
{% for title, fields in export_default_fields.items %}

{% if "#comment" not in title %}

{# These are top-level fields #}
{% if fields|length == 1 %}
<li data-option="{{fields.0|to_json}}" id="field-select-{{fields.0.path}}" class="field-select" role="option"
aria-selected="false" title="[DEBUG {{fields.0.path}}]">
<span class="checkmark" aria-hidden="true"></span>
{{fields.0.title}} {% if "additional_data" in field.path %}(additional data){% endif %}
{{fields.0.title}}
</li>
{% else %}

Expand All @@ -59,7 +62,7 @@
data-toggle-target="dl-group-{{title|cut:" "}}" data-option="{{fields|to_json}}"
title="[DEBUG {{fields.path}}] Expand to show child fields in the {{title}} field">
<span class="checkmark" aria-hidden="true"></span>
&gt; {{title}} ({{fields|length}})
&gt; {{title}} (<a href="#" class="dl-select-all-child-fields">select all {{fields|length}}</a>)
</li>
<div class="dl-group" id="dl-group-{{title|cut:" "}}" {% if fields|length > 1 %}style="display:none" {% endif %}>
{% for field in fields %}
Expand All @@ -73,6 +76,8 @@
</div>
{% endif %}

{% endif %} {# comment in title #}

{% endfor %}
</ul>

Expand Down Expand Up @@ -103,11 +108,12 @@
</div>

<div>
<button type="button" id="dl-remove-fields-btn" class="button button--teal-dark" aria-disabled="true" title="Remove selected field from chosen fields" style="justify-self: start;">
<button type="button" id="dl-remove-fields-btn" class="button button--teal-dark" aria-disabled="true"
title="Remove selected field from chosen fields" style="justify-self: start;">
Remove <span class="dl-selected-counter"></span>
</button>
<button type="button" id="dl-remove-all-fields-btn" class="button button--teal-dark" title="Remove all fields from chosen fields"
aria-disabled="true">
<button type="button" id="dl-remove-all-fields-btn" class="button button--teal-dark"
title="Remove all fields from chosen fields" aria-disabled="true">
Remove all
</button>
</div>
Expand All @@ -117,7 +123,8 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
{% csrf_token %}
<input type="hidden" id="form-data" value="" name="selection" />
<input type="hidden" name="json_query" value="{{json_query}}" />
<input type="submit" id="dl-download-btn" class="button button--teal-dark button--solid" style="width: 100%" value="{{action_label}}" aria-disabled="true" disabled="disabled" title="Start CSV download" />
<input type="submit" id="dl-download-btn" class="button button--teal-dark button--solid" style="width: 100%"
value="{{action_label}}" aria-disabled="true" disabled="disabled" title="Start CSV download" />
</form>

<script id="recommended-fields-list" type="application/json">
Expand Down Expand Up @@ -162,27 +169,27 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>

<script>
class GNSelected extends Array {
constructor (btn) {
constructor(btn) {
super();
this.btn = btn;
}

clear () {
clear() {
this.length = 0;
this._updateDom();
}

add (item) {
add(item) {
this.push(item);
this._updateDom();
}

remove (item) {
remove(item) {
this.pop(item);
this._updateDom();
}

_updateDom () {
_updateDom() {
this.btn.setAttribute('aria-disabled', this.length === 0);
if (this.length > 0) {
this.btn.getElementsByClassName('dl-selected-counter')[0].innerText = `(${this.length})`;
Expand All @@ -193,7 +200,7 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
}

class GNListBox {
constructor () {
constructor() {
this.addBtn = document.getElementById('dl-add-fields-btn');
this.addAllBtn = document.getElementById('dl-add-all-fields-btn');
this.clearBtn = document.getElementById('dl-clear-fields-btn');
Expand All @@ -212,7 +219,7 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
}

/* Filters the list of available fields */
filterCheckboxes (event) {
filterCheckboxes(event) {
const inputText = event.srcElement.value;
const checkboxes = document.querySelectorAll('.field-select');

Expand Down Expand Up @@ -249,12 +256,12 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
}
}

removeItemToRemove (item) {
removeItemToRemove(item) {
this.toRemove.add(item);
this.removeBtn.setAttribute('aria-disabled', this.toRemove.length === 0);
}

select (event) {
select(event) {
const item = event.srcElement;

/* if already selected then toggle this off / remove from selection */
Expand All @@ -275,7 +282,7 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
}
}

updateFieldsList () {
updateFieldsList() {
this.fieldsChosen = Array.from(this.chosenFieldList.children).map((item) => {
return JSON.parse(item.dataset.option);
});
Expand All @@ -293,7 +300,7 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
}
}

setupEvents () {
setupEvents() {
this.clearBtn.addEventListener('click', () => {
this.toAdd.clear();
/* Clear the selection ticks */
Expand All @@ -306,7 +313,7 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
}
});

this.addAllBtn.addEventListener('click', () => {
this.addAllBtn.addEventListener('click', async () => {
for (const item of document.querySelectorAll('#dl-available-fields .field-select[aria-selected=false]')) {
item.click();
}
Expand All @@ -319,46 +326,37 @@ <h4 class="margin-top:1 margin-bottom:1">2. Download the results</h4>
el.addEventListener('click', this.select.bind(this));
}

/* child fields items toggle */
/* child field events toggle */
for (const el of document.getElementsByClassName('parent-field-toggle')) {
el.addEventListener('click', () => {
const childFields = document.getElementById(el.dataset.toggleTarget);

/* Open / Close the child field container */
if (childFields.style.display === 'none') {
childFields.style.display = 'block';
} else {
childFields.style.display = 'none';
}

/* if the current selection has been made and contains our child items
* don't do anything further
*/
for (const itemToAdd of this.toAdd) {
/* If this element being toggled open/close is the parent of an item in the selection
* this means it's now live selection and we don't want to toggle any of the child items
* on/off ourselves.
*/
if (el.dataset.toggleTarget === itemToAdd.parentElement.id) {
return;
}
}
/* select all N * for a parent field */
el.getElementsByClassName('dl-select-all-child-fields')[0].addEventListener('click', (e) => {
e.preventDefault();

const childFields = document.getElementById(el.dataset.toggleTarget);
/* Select/deselect all the fields in this group */
for (const childField of childFields.children) {
if (childField.getAttribute(this.ariaSelected) === 'true') {
continue;
}

const mockSelectEvent = {};

mockSelectEvent.srcElement = childField;
this.select(mockSelectEvent);
}

const currentValue = el.getAttribute(this.ariaSelected);
el.setAttribute(this.ariaSelected, 'true');
});

/* hide show child fields and tick handler */
el.addEventListener('click', () => {
const childFields = document.getElementById(el.dataset.toggleTarget);

/* Toggle the selected value/check mark */
if (currentValue === 'true') {
el.setAttribute(this.ariaSelected, 'false');
/* Open / Close the child field container */
if (childFields.style.display === 'none') {
childFields.style.display = 'block';
} else {
el.setAttribute(this.ariaSelected, 'true');
childFields.style.display = 'none';
}
});
}
Expand Down
4 changes: 0 additions & 4 deletions grantnav/frontend/user_csv_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def process(request):
# Check the incoming data is known to us
for field in fields:
if field["column_title"] not in grant_csv_titles or field["path"] not in grant_csv_paths:
print(field["title"])
print(field["path"])
print(field["column_title"])

raise Http404("The field(s) requested are not available.")

return views.grants_csv_paged(
Expand Down

0 comments on commit 307d240

Please sign in to comment.