Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow adding data sets in range data types as columns #363

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion hub/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def columns(self, mp_name=False):
"name": dataset.name,
"value_col": dataset.value_col,
"label": dataset.label,
"header_label": dataset.label,
}
)
except DataSet.DoesNotExist:
Expand All @@ -99,6 +100,7 @@ def columns(self, mp_name=False):
"name": datatype.name,
"value_col": datatype.value_col,
"label": datatype.label,
"header_label": f"{datatype.data_set.label} - {datatype.label}",
}
)
except DataType.DoesNotExist:
Expand Down Expand Up @@ -130,7 +132,9 @@ def format_value(self, type, value):
def data(self, as_dict=False, mp_name=False):
headers = ["Constituency Name"]
headers += map(lambda f: f["dataset"].label, self.filters())
headers += map(lambda f: f["label"], self.columns(mp_name=mp_name))
headers += map(
lambda f: f.get("header_label", f["label"]), self.columns(mp_name=mp_name)
)

data = [headers]

Expand Down
10 changes: 9 additions & 1 deletion hub/static/js/explore.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ const app = createApp({
addColumn(datasetName) {
const dataset = this.getDataset(datasetName)

if (!dataset.selectedType && dataset.types) {
dataset.selectedType = dataset.types[0].name
}

this.columns.push(dataset)

trackEvent('explore_column_added', {
Expand Down Expand Up @@ -238,7 +242,11 @@ const app = createApp({
if (this.columns.length) {
var cols = []
this.columns.forEach(function(d) {
cols.push(d.name)
if (d.selectedType) {
cols.push(d.selectedType)
} else {
cols.push(d.name)
}
})

state['columns'] = cols.join(',')
Expand Down
5 changes: 5 additions & 0 deletions hub/templates/hub/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ <h2 class="h5 mt-4 mt-lg-5 mb-3 mb-lg-4 text-primary">Step 3: Add extra columns
<h3 class="h6 mb-0 me-auto">${ column.title }</h3>
<button @click="removeColumn(column.name)" type="button" class="btn-close p-0" aria-label="Remove column"></button>
</div>
<div :class="{ 'filter__body': true, 'filter__body--expanded': true }">
<select v-if="column.is_range" v-model="column.selectedType" class="form-select form-select-sm flex-grow-0 flex-shrink-1">
<option v-for="ds_type in column.types" :value="ds_type.name">${ ds_type.title }</option>
</select>
</div>
</div>
</div>

Expand Down
6 changes: 3 additions & 3 deletions hub/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_explore_view_with_many_to_one(self):

def test_explore_view_extra_column(self):
output_csv = str.encode(
"Constituency Name,APPG membership,Wind support\r\nSouth Borsetshire,MadeUpAPPG; MadeUpAPPG2,70.0\r\n"
"Constituency Name,APPG membership,Constituency Polling Data - Wind support\r\nSouth Borsetshire,MadeUpAPPG; MadeUpAPPG2,70.0\r\n"
)

url = f"{reverse('explore_csv')}?mp_appg_membership__exact=MadeUpAPPG&columns=wind_support"
Expand All @@ -92,7 +92,7 @@ def test_explore_view_extra_column(self):

def test_explore_view_multiple_extra_columns(self):
output_csv = str.encode(
"Constituency Name,APPG membership,Wind support,Constituency Fuel Poverty\r\nSouth Borsetshire,MadeUpAPPG; MadeUpAPPG2,70.0,12.4321\r\n"
"Constituency Name,APPG membership,Constituency Polling Data - Wind support,Constituency Fuel Poverty - Constituency Fuel Poverty\r\nSouth Borsetshire,MadeUpAPPG; MadeUpAPPG2,70.0,12.4321\r\n"
)

url = f"{reverse('explore_csv')}?mp_appg_membership__exact=MadeUpAPPG&columns=wind_support,fuel_poverty"
Expand All @@ -105,7 +105,7 @@ def test_explore_view_multiple_extra_columns(self):

def test_explore_view_extra_columns_multiset(self):
output_csv = str.encode(
"Constituency Name,APPG membership,Wind support,Constituency Age 0-9 %age\r\nSouth Borsetshire,MadeUpAPPG; MadeUpAPPG2,70.0,10.1234\r\n"
"Constituency Name,APPG membership,Constituency Polling Data - Wind support,Constituency Age Distribution - Constituency Age 0-9 %age\r\nSouth Borsetshire,MadeUpAPPG; MadeUpAPPG2,70.0,10.1234\r\n"
)

url = f"{reverse('explore_csv')}?mp_appg_membership__exact=MadeUpAPPG&columns=wind_support,ages_0-9"
Expand Down
Loading