From 39e81d5727ea888da71b2519eb815b8863cccd4a Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Sat, 29 Jun 2024 23:41:33 -0500 Subject: [PATCH] Add country model --- franklincce/explorer/admin.py | 1 + ...9_country_alter_legislativetext_country.py | 26 +++++++++++++++++++ franklincce/explorer/models.py | 24 ++++++++++++----- .../templates/explorer/legislation.html | 4 +++ franklincce/explorer/urls.py | 1 + franklincce/explorer/views.py | 10 ++++++- 6 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 franklincce/explorer/migrations/0009_country_alter_legislativetext_country.py diff --git a/franklincce/explorer/admin.py b/franklincce/explorer/admin.py index 8b27863..441718f 100644 --- a/franklincce/explorer/admin.py +++ b/franklincce/explorer/admin.py @@ -13,6 +13,7 @@ to_register = [ [models.LegislationBook, LegislationBookAdmin], [models.LegislationClassification], [models.School], + [models.Country], ] for i in to_register: admin.site.register(*i) \ No newline at end of file diff --git a/franklincce/explorer/migrations/0009_country_alter_legislativetext_country.py b/franklincce/explorer/migrations/0009_country_alter_legislativetext_country.py new file mode 100644 index 0000000..64afa22 --- /dev/null +++ b/franklincce/explorer/migrations/0009_country_alter_legislativetext_country.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.12 on 2024-06-30 04:26 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('explorer', '0008_school_alter_legislativetext_school'), + ] + + operations = [ + migrations.CreateModel( + name='Country', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=256)), + ], + ), + migrations.AlterField( + model_name='legislativetext', + name='country', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='explorer.country'), + ), + ] diff --git a/franklincce/explorer/models.py b/franklincce/explorer/models.py index e1f1f64..d857014 100644 --- a/franklincce/explorer/models.py +++ b/franklincce/explorer/models.py @@ -7,7 +7,7 @@ import fitz from collections import namedtuple -def ForeignInstantiateIfNone(model, name): +def InstantiateIfNone(model, name): """ Search the model for instances by name. If there's none, then create one. @@ -26,6 +26,15 @@ class School(models.Model): def __str__(self): return self.name +class Country(models.Model): + name = models.CharField(max_length=256) + + class Meta: + verbose_name_plural = "Countries" + + def __str__(self): + return self.name + class LegislationBook(models.Model): class ConferenceType(models.TextChoices): MIDDLE = "M", _("Middle School") @@ -66,7 +75,12 @@ class LegislationBook(models.Model): return for text in parsed.output: - text["school"] = ForeignInstantiateIfNone(School, text["school"]) + text["school"] = InstantiateIfNone(School, text["school"]) + if text["country"]: + # there's sometimes "Dominican Republic" and "Dominican Republic 2" + # handle that gracefully + text["country"] = text["country"].replace(" 2", "") + text["country"] = InstantiateIfNone(Country, text["country"]) text = LegislativeText(**text, from_book=self) text.save() @@ -102,11 +116,7 @@ class LegislativeText(models.Model): sponsors = models.CharField(max_length=256) from_book = models.ForeignKey(LegislationBook, on_delete=models.CASCADE) legislation_title = models.CharField(max_length=512) - country = models.CharField( - max_length=512, - null=True, - blank=True - ) + country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True) def __str__(self): return "{}/{}-{}-{}".format( diff --git a/franklincce/explorer/templates/explorer/legislation.html b/franklincce/explorer/templates/explorer/legislation.html index d9421da..95a0ee4 100644 --- a/franklincce/explorer/templates/explorer/legislation.html +++ b/franklincce/explorer/templates/explorer/legislation.html @@ -15,6 +15,10 @@

Sponsored by {{ legislation.sponsors }} of {{ legislation.school }}

+ {% if legislation.country %} +

The delegates above represented the Delegation of {{ legislation.country }}.

+ {% endif %} +

Presented as part of the {{ legislation.from_book.name }} conference

diff --git a/franklincce/explorer/urls.py b/franklincce/explorer/urls.py index 3e18c72..68dd743 100644 --- a/franklincce/explorer/urls.py +++ b/franklincce/explorer/urls.py @@ -11,4 +11,5 @@ urlpatterns = [ path("topics//", views.get_all_classified_by_id, name="classificationview"), path("topics/", views.get_all_classifications, name="classificationsview"), path("schools//", views.get_all_by_school, name="schoolview"), + path("countries//", views.get_all_by_country, name="countryview"), ] diff --git a/franklincce/explorer/views.py b/franklincce/explorer/views.py index 7c92a25..75c730d 100644 --- a/franklincce/explorer/views.py +++ b/franklincce/explorer/views.py @@ -1,7 +1,7 @@ from django.shortcuts import get_object_or_404, render from django.http import HttpResponse -from .models import LegislativeText, LegislationBook, LegislationClassification, School +from .models import LegislativeText, LegislationBook, LegislationClassification, School, Country from random import sample @@ -89,3 +89,11 @@ def get_all_by_school(request, school_id): "school_name": school.name, "legislation": school.legislativetext_set.all() }) + +def get_all_by_country(request, country_id): + country = get_object_or_404(Country, pk=country_id) + + return render(request, "explorer/results.html", { + "result_name": "All bills by country {}".format(country.name), + "legislation": country.legislativetext_set.all() + })