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

Feature/add party nations #2147

Merged
merged 2 commits into from
Jul 17, 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
1 change: 1 addition & 0 deletions ynr/apps/parties/api/next/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Meta:
"name",
"alternative_name",
"register",
"nations",
"status",
"date_registered",
"date_deregistered",
Expand Down
25 changes: 25 additions & 0 deletions ynr/apps/parties/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def save(self):
"date_deregistered": self.date_deregistered,
"legacy_slug": make_slug(self.ec_id),
"ec_data": self,
"nations": self.nation_list,
},
)

Expand Down Expand Up @@ -351,6 +352,30 @@ def parse_date(self, date_str):
dt = datetime.fromtimestamp(int(timestamp) / 1000.0)
return dt.strftime("%Y-%m-%d")

@property
def nation_list(self):
"""
Generate data for the `nations` field on the party.
For GB parties, return a list of nation codes where candidates are fielded.
For NI parties, return None.
"""

if self.register == "NI":
return None

nation_list = []

if self["FieldingCandidatesInEngland"]:
nation_list.append("ENG")

if self["FieldingCandidatesInScotland"]:
nation_list.append("SCO")

if self["FieldingCandidatesInWales"]:
nation_list.append("WAL")

return nation_list

def mark_inactive_emblems(self):
ec_emblem_id_list = [
emblem["Id"] for emblem in self.get("PartyEmblems", [])
Expand Down
26 changes: 26 additions & 0 deletions ynr/apps/parties/migrations/0022_party_nations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.1.7 on 2023-07-12 16:30

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("parties", "0021_alter_partydescription_options_and_more"),
]

operations = [
migrations.AddField(
model_name="party",
name="nations",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(max_length=3),
help_text='\n Some subset of ["ENG", "WAL", "SCO"],\n depending on where the party fields candidates. \n Nullable as not applicable to NI-based parties.\n ',
max_length=3,
null=True,
size=None,
verbose_name="Party nations",
),
),
]
12 changes: 12 additions & 0 deletions ynr/apps/parties/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.utils import timezone
from django_extensions.db.models import TimeStampedModel
from django.contrib.postgres.fields import ArrayField

from .managers import PartyQuerySet

Expand Down Expand Up @@ -76,6 +77,17 @@ class Party(TimeStampedModel):
current_candidates = models.PositiveSmallIntegerField(default=0)
total_candidates = models.PositiveIntegerField(default=0)
ec_data = models.JSONField(default=dict)
nations = ArrayField(
models.CharField(max_length=3),
max_length=3,
null=True,
verbose_name="Party nations",
help_text="""
Some subset of ["ENG", "WAL", "SCO"],
depending on where the party fields candidates.
Nullable as not applicable to NI-based parties.
""",
)

objects = PartyQuerySet.as_manager()

Expand Down
32 changes: 32 additions & 0 deletions ynr/apps/parties/tests/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"ECRef": "PP01",
"RegisterName": "Great Britain",
"RegistrationStatusName": "Registered",
"FieldingCandidatesInEngland": True,
"FieldingCandidatesInScotland": True,
"FieldingCandidatesInWales": True,
"ApprovedDate": "/Date(1134345600000)/",
"PartyDescriptions": [
{
Expand Down Expand Up @@ -127,6 +130,35 @@ def test_cleaned_name(self):
)
self.assertEqual(party.cleaned_name, cleaned_name)

def test_nations(self):
party = ECParty(
{
"ECRef": "PP01",
"RegulatedEntityName": "The S-Club Party",
"RegisterName": "Northern Ireland",
"FieldingCandidatesInEngland": False,
"FieldingCandidatesInWales": False,
"FieldingCandidatesInScotland": False,
}
)

# Northern Ireland
self.assertEqual(party.nation_list, None)

# Great Britain
party["RegisterName"] = "Great Britain"
# England Only
party["FieldingCandidatesInEngland"] = True
self.assertEqual(party.nation_list, ["ENG"])

# England and Wales
party["FieldingCandidatesInWales"] = True
self.assertEqual(party.nation_list, ["ENG", "WAL"])

# England, Wales and Scotland
party["FieldingCandidatesInScotland"] = True
self.assertEqual(party.nation_list, ["ENG", "SCO", "WAL"])

def test_make_description_text(self):
description_only_plus_en_dash = {
"Description": "Minuses for all - no en–dashes!",
Expand Down