Skip to content

Commit

Permalink
fixup: move some logic in to view
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Jan 9, 2024
1 parent a19f252 commit c7506e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
4 changes: 0 additions & 4 deletions hub/templates/hub/new_area_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ <h2 class="mb-3 text-primary">Place</h2>
<div class="area-data-grid">

{% for dataset in categories.place %}
{% if country not in dataset.excluded_countries %}
{% if dataset.is_range and dataset.data|length > 9 %}
<div class="card dataset-card area-data--lg{% if dataset.featured %} area-data--featured{% endif %}">
{% elif dataset.is_range or dataset.data_type == "json" %}
Expand Down Expand Up @@ -350,7 +349,6 @@ <h3 class="h5">{{ dataset.label }}</h3>
<p class="card-text">{{ dataset.source }}</p>
</div>
</div>
{% endif %}
{% endfor %}
</div>

Expand All @@ -369,7 +367,6 @@ <h2 class="mb-3 text-primary">Movement</h2>
<div class="area-data-grid">

{% for dataset in categories.movement %}
{% if dataset.db_name not in is_related_category %}
{% if dataset.is_range and dataset.data|length > 9 %}
<div class="card dataset-card area-data--lg{% if dataset.featured %} area-data--featured{% endif %}">
{% elif dataset.is_range or dataset.data_type == "json" %}
Expand Down Expand Up @@ -446,7 +443,6 @@ <h3 class="h5">{{ dataset.label }}</h3>
<p class="card-text">{{ dataset.source }}</p>
</div>
</div>
{% endif %}
{% endfor %}

</div>
Expand Down
45 changes: 28 additions & 17 deletions hub/views/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def get_context_data(self, **kwargs):
pass

categories = defaultdict(list)
indexed_categories = defaultdict(dict)
for data_set in DataSet.objects.order_by("order", "label").all():
data = self.process_dataset(data_set)

Expand All @@ -178,6 +179,8 @@ def get_context_data(self, **kwargs):
else:
categories["place"].append(data)

indexed_categories[data["db_name"]] = data

context["related_categories"] = {
"constituency_christian_aid_group_count": "constituency_christian_aid_groups",
"constituency_foe_groups_count": "constituency_foe_groups",
Expand All @@ -188,29 +191,37 @@ def get_context_data(self, **kwargs):

context["is_related_category"] = context["related_categories"].values()

indexed_categories = defaultdict(dict)
for category_name, category_items in categories.items():
for data_item in category_items:
if (
data_item["db_name"] in context["is_related_category"]
or data_item["db_name"] == "country"
):
indexed_categories[data_item["db_name"]] = data_item

for data_set in categories["movement"]:
if context["related_categories"].get(data_set["db_name"], None) is not None:
data_set["related_category"] = indexed_categories[
context["related_categories"][data_set["db_name"]]
]

context["categories"] = categories
context["indexed_categories"] = indexed_categories
categories_to_remove = defaultdict(list)

try:
context["country"] = indexed_categories["country"]["data"].value()
except (ValueError, KeyError):
context["country"] = None

if context["country"] is not None:
for category, items in categories.items():
for data_set in items:
if (
context["related_categories"].get(data_set["db_name"], None)
is not None
):
data_item = indexed_categories[
context["related_categories"][data_set["db_name"]]
]
if len(data_item) > 0:
data_set["related_category"] = data_item
categories_to_remove["movement"].append(data_item)

if context["country"] in data_set["excluded_countries"]:
categories_to_remove[category].append(data_set)

for category_name, items in categories_to_remove.items():
for item in items:
categories[category_name].remove(item)

context["categories"] = categories
context["indexed_categories"] = indexed_categories

return context


Expand Down

0 comments on commit c7506e2

Please sign in to comment.