Skip to content

Commit

Permalink
WIP: Add info based on AreaType
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-griffen authored and zarino committed Oct 27, 2023
1 parent ca7fdd1 commit 5565933
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
34 changes: 32 additions & 2 deletions hub/templates/hub/area.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,40 @@ <h1 class="m-0">{{ area.name }}</h1>
</div>
</div>
<div class="col-lg-9 offset-xl-1">

{% if area_type == "WMC" %}
<div class="card flex-grow-1 dataset-card">
<div class="dataset-card-header">
<h5>This is a 2010 constituency</h5>
</div>
<div class="card-body">
<p>At the next election, people from this constituency will be divided into <b>{{ overlap_constituencies|length|apnumber }} constituenc{% if overlap_constituencies|length_is:1 %}y{% else %}ies{% endif %}:</b></p>
<table class="table mb-0">
<tbody>
{% for overlap_constituency in overlap_constituencies %}
<tr>
<th><a href="{% url 'area' area_type=overlap_constituency.area.area_type.code name=overlap_constituency.area.name %}" class="text-decoration-none">{{ overlap_constituency.area }}</a></th>
<td class="text-muted">Covers approximately {{ overlap_constituency.pop_overlap }}% of this constituency's population, and {{ overlap_constituency.area_overlap }}% of this constituency's area.</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}
<section id="mp" class="area-section">
<h2 class="mb-3 text-primary">MP</h2>
{% include 'hub/area/_mp_data.html' with mp_data=mp %}
{% if area_type == "WMC23" %}
<div class="card flex-grow-1 dataset-card">
<div class="dataset-card-header">
<h5>This is a 2023 constituency</h5>
</div>
<div class="card-body">
<p>At the next election, an MP will be voted into this constituency, however until then, there is no MP.</p>
</div>
</div>
{% else %}
{% include 'hub/area/_mp_data.html' with mp_data=mp %}
{% endif %}
</section>

{% if categories.opinion %}
Expand Down
31 changes: 31 additions & 0 deletions hub/views/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.shortcuts import get_object_or_404, redirect
from django.views.generic import DetailView, TemplateView, View

from mysoc_dataset import get_dataset_df

from hub.mixins import TitleMixin
from hub.models import (
Area,
Expand All @@ -16,6 +18,7 @@
UserDataSets,
)
from utils import is_valid_postcode
from utils.constituency_mapping import get_overlap_df
from utils.mapit import (
BadRequestException,
ForbiddenException,
Expand Down Expand Up @@ -176,8 +179,36 @@ def get_tags(self, mp_data, area_data):

return tags

def get_overlap_info(self, **kwargs):
# Get lookup between short code and GSS
constituency_lookup = (
get_dataset_df(
repo_name="2025-constituencies",
package_name="parliament_con_2025",
version_name="latest",
file_name="parl_constituencies_2025.csv",
)
.set_index("short_code")["gss_code"]
.to_dict()
)

df = get_overlap_df("PARL10", "PARL25")
overlap_constituencies = df.query("PARL10 == @self.object.gss")
overlap_constituencies = [
{
"area": Area.objects.get(gss=constituency_lookup[row["PARL25"]]),
"pop_overlap": int(row["percentage_overlap_pop"] * 100),
"area_overlap": int(row["percentage_overlap_area"] * 100),
}
for index, row in overlap_constituencies.iterrows()
]
return overlap_constituencies

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

context["overlap_constituencies"] = self.get_overlap_info()
context["area_type"] = str(self.object.area_type)
try:
context["mp"] = {"person": Person.objects.get(area=self.object)}

Expand Down

0 comments on commit 5565933

Please sign in to comment.