Skip to content

Commit

Permalink
WIP displaying data set columns
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Dec 5, 2023
1 parent 48e0e90 commit 87eb010
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
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

0 comments on commit 87eb010

Please sign in to comment.