Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Jul 3, 2024
1 parent f0c6fdc commit 62abc94
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 62 deletions.
3 changes: 1 addition & 2 deletions hub/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ class PersonAdmin(admin.ModelAdmin):
list_display = (
"__str__",
"person_type",
"area",
)
search_fields = (
"name",
"area__name",
"areas__name",
)
list_filter = ("person_type",)
36 changes: 30 additions & 6 deletions hub/fixtures/duplicate_mps.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"name": "James Madeupname",
"external_id": "1",
"person_type": "MP",
"id_type": "parlid",
"area": 1
"id_type": "parlid"
}
},
{
Expand All @@ -17,8 +16,7 @@
"name": "Juliet Madeupname",
"external_id": "2",
"person_type": "MP",
"id_type": "parlid",
"area": 2
"id_type": "parlid"
}
},
{
Expand All @@ -28,8 +26,34 @@
"name": "Juliet Replacement",
"external_id": "3",
"person_type": "MP",
"id_type": "parlid",
"area": 2
"id_type": "parlid"
}
},
{
"model": "hub.personarea",
"pk": 1,
"fields": {
"person": 1,
"area": 1,
"person_type": "MP"
}
},
{
"model": "hub.personarea",
"pk": 2,
"fields": {
"person": 2,
"area": 2,
"person_type": "MP"
}
},
{
"model": "hub.personarea",
"pk": 3,
"fields": {
"person": 3,
"area": 2,
"person_type": "MP"
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions hub/fixtures/elections.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"last_update": "2022-10-10T13:20:30+00:00",
"source": "https://wikipedia.org",
"areas_available": [1],
"table": "person__persondata",
"table": "people__persondata",
"is_public": "True"
}
},
Expand All @@ -21,7 +21,7 @@
"last_update": "2022-10-10T13:20:30+00:00",
"source": "https://wikipedia.org",
"areas_available": [1],
"table": "person__persondata"
"table": "people__persondata"
}
},
{
Expand All @@ -33,7 +33,7 @@
"last_update": "2022-10-10T13:20:30+00:00",
"source": "https://wikipedia.org",
"areas_available": [1],
"table": "person__persondata"
"table": "people__persondata"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion hub/fixtures/mp_memberships.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"model": "hub.dataset",
"pk": 10,
"fields": {
"table": "person__persondata",
"table": "people__persondata",
"name": "mp_appg_membership",
"label": "APPG membership",
"last_update": "2022-10-10T13:20:30+00:00",
Expand Down
22 changes: 20 additions & 2 deletions hub/fixtures/mps.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"external_id": "1",
"person_type": "MP",
"id_type": "parlid",
"area": 1
"areas": [1]
}
},
{
Expand All @@ -18,7 +18,25 @@
"external_id": "2",
"person_type": "MP",
"id_type": "parlid",
"area": 2
"areas": [2]
}
},
{
"model": "hub.personarea",
"pk": 1,
"fields": {
"person": 1,
"area": 1,
"person_type": "MP"
}
},
{
"model": "hub.personarea",
"pk": 2,
"fields": {
"person": 2,
"area": 2,
"person_type": "MP"
}
},
{
Expand Down
24 changes: 20 additions & 4 deletions hub/fixtures/mps_23.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"name": "James Madeupname",
"external_id": "1",
"person_type": "MP",
"id_type": "parlid",
"area": 5
"id_type": "parlid"
}
},
{
Expand All @@ -17,8 +16,25 @@
"name": "Juliet Madeupname",
"external_id": "2",
"person_type": "MP",
"id_type": "parlid",
"area": 4
"id_type": "parlid"
}
},
{
"model": "hub.personarea",
"pk": 1,
"fields": {
"person": 1,
"area": 5,
"person_type": "MP"
}
},
{
"model": "hub.personarea",
"pk": 2,
"fields": {
"person": 2,
"area": 4,
"person_type": "MP"
}
},
{
Expand Down
16 changes: 8 additions & 8 deletions hub/management/commands/import_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ def import_mps(self):
id_type="twfyid",
defaults={
"name": name,
"area": area,
},
)
person.areas.add(area, through_defaults={"person_type": "MP"})
except DataError as e:
print(f"Failed to create/update mp {name}: {e}")
continue
Expand Down Expand Up @@ -304,7 +304,7 @@ def import_mp_images(self):
print("Importing MP Images")
for mp in tqdm(
PersonData.objects.filter(
data_type__name="parlid", person__person_type="MP"
data_type__name="parlid", person__personarea__person_type="MP"
)
.select_related("person")
.all(),
Expand All @@ -322,10 +322,10 @@ def import_mp_images(self):

def check_for_duplicate_mps(self):
duplicates = (
Person.objects.filter(person_type="MP")
Person.objects.filter(personarea__person_type="MP")
.distinct()
.values("area_id")
.annotate(area_count=Count("area_id"))
.values("personarea__area_id")
.annotate(area_count=Count("personarea__area_id"))
.filter(area_count__gt=1)
)
if duplicates.count() > 0:
Expand All @@ -337,9 +337,9 @@ def check_for_duplicate_mps(self):

# Remove all duplicate MPs
for area in duplicates:
duplicate_mps = Person.objects.filter(area=area["area_id"]).values_list(
"external_id", flat=True
)
duplicate_mps = Person.objects.filter(
areas=area["personarea__area_id"]
).values_list("external_id", flat=True)
mps_to_delete = list(duplicate_mps)
try:
least_recent_mp = (
Expand Down
50 changes: 50 additions & 0 deletions hub/migrations/0066_personarea_person_areas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 4.2.11 on 2024-07-03 09:47

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("hub", "0065_alter_areatype_area_type"),
]

operations = [
migrations.CreateModel(
name="PersonArea",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("start_date", models.DateField(blank=True, null=True)),
("end_date", models.DateField(blank=True, null=True)),
("person_type", models.CharField(max_length=10)),
(
"area",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="hub.area"
),
),
(
"person",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="hub.person"
),
),
],
),
migrations.AddField(
model_name="person",
name="areas",
field=models.ManyToManyField(
related_name="people", through="hub.PersonArea", to="hub.area"
),
),
]
26 changes: 26 additions & 0 deletions hub/migrations/0067_populate_person_areas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.11 on 2024-07-03 09:49

from django.db import migrations


def populate_person_areas(apps, schema_editor):
Person = apps.get_model("hub", "Person")
PersonArea = apps.get_model("hub", "PersonArea")
for p in Person.objects.all():
pa = PersonArea(person=p, area=p.area, person_type=p.person_type)
pa.save()


def reverse_noop(apps, schema_editor): # pragma: no cover
pass


class Migration(migrations.Migration):

dependencies = [
("hub", "0066_personarea_person_areas"),
]

operations = [
migrations.RunPython(populate_person_areas, reverse_code=reverse_noop)
]
24 changes: 24 additions & 0 deletions hub/migrations/0068_add_person_old_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.11 on 2024-07-03 10:36

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("hub", "0067_populate_person_areas"),
]

operations = [
migrations.AddField(
model_name="person",
name="old_area",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="hub.area",
),
),
]
30 changes: 30 additions & 0 deletions hub/migrations/0069_populate_person_old_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.11 on 2024-07-03 13:01

from django.db import migrations


def populate_person_old_area(apps, schema_editor):
Person = apps.get_model("hub", "Person")
for p in Person.objects.all():
p.old_area = p.area
p.save()


def populate_person_area(apps, schema_editor):
Person = apps.get_model("hub", "Person")
for p in Person.objects.all():
p.area = p.old_area
p.save()


class Migration(migrations.Migration):

dependencies = [
("hub", "0068_add_person_old_area"),
]

operations = [
migrations.RunPython(
populate_person_old_area, reverse_code=populate_person_area
)
]
18 changes: 18 additions & 0 deletions hub/migrations/0070_remove_person_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.11 on 2024-07-03 10:36

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("hub", "0069_populate_person_old_area"),
]

operations = [
migrations.RemoveField(
model_name="person",
name="area",
),
]
Loading

0 comments on commit 62abc94

Please sign in to comment.