Add country model

This commit is contained in:
stupidcomputer 2024-06-29 23:41:33 -05:00
parent de4575a2f0
commit 39e81d5727
6 changed files with 58 additions and 8 deletions

View File

@ -13,6 +13,7 @@ to_register = [
[models.LegislationBook, LegislationBookAdmin], [models.LegislationBook, LegislationBookAdmin],
[models.LegislationClassification], [models.LegislationClassification],
[models.School], [models.School],
[models.Country],
] ]
for i in to_register: for i in to_register:
admin.site.register(*i) admin.site.register(*i)

View File

@ -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'),
),
]

View File

@ -7,7 +7,7 @@ import fitz
from collections import namedtuple from collections import namedtuple
def ForeignInstantiateIfNone(model, name): def InstantiateIfNone(model, name):
""" """
Search the model for instances by name. Search the model for instances by name.
If there's none, then create one. If there's none, then create one.
@ -26,6 +26,15 @@ class School(models.Model):
def __str__(self): def __str__(self):
return self.name 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 LegislationBook(models.Model):
class ConferenceType(models.TextChoices): class ConferenceType(models.TextChoices):
MIDDLE = "M", _("Middle School") MIDDLE = "M", _("Middle School")
@ -66,7 +75,12 @@ class LegislationBook(models.Model):
return return
for text in parsed.output: 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 = LegislativeText(**text, from_book=self)
text.save() text.save()
@ -102,11 +116,7 @@ class LegislativeText(models.Model):
sponsors = models.CharField(max_length=256) sponsors = models.CharField(max_length=256)
from_book = models.ForeignKey(LegislationBook, on_delete=models.CASCADE) from_book = models.ForeignKey(LegislationBook, on_delete=models.CASCADE)
legislation_title = models.CharField(max_length=512) legislation_title = models.CharField(max_length=512)
country = models.CharField( country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True)
max_length=512,
null=True,
blank=True
)
def __str__(self): def __str__(self):
return "{}/{}-{}-{}".format( return "{}/{}-{}-{}".format(

View File

@ -15,6 +15,10 @@
<p>Sponsored by {{ legislation.sponsors }} of <a href="/explorer/schools/{{ legislation.school.id}}">{{ legislation.school }}</a></p> <p>Sponsored by {{ legislation.sponsors }} of <a href="/explorer/schools/{{ legislation.school.id}}">{{ legislation.school }}</a></p>
{% if legislation.country %}
<p>The delegates above represented the <a href="/explorer/countries/{{ legislation.country.id }}">Delegation of {{ legislation.country }}</a>.</p>
{% endif %}
<p>Presented as part of the <a href="/explorer/conference/{{ legislation.from_book.id }}">{{ legislation.from_book.name }}</a> conference</p> <p>Presented as part of the <a href="/explorer/conference/{{ legislation.from_book.id }}">{{ legislation.from_book.name }}</a> conference</p>
</div> </div>

View File

@ -11,4 +11,5 @@ urlpatterns = [
path("topics/<int:classification_id>/", views.get_all_classified_by_id, name="classificationview"), path("topics/<int:classification_id>/", views.get_all_classified_by_id, name="classificationview"),
path("topics/", views.get_all_classifications, name="classificationsview"), path("topics/", views.get_all_classifications, name="classificationsview"),
path("schools/<int:school_id>/", views.get_all_by_school, name="schoolview"), path("schools/<int:school_id>/", views.get_all_by_school, name="schoolview"),
path("countries/<int:country_id>/", views.get_all_by_country, name="countryview"),
] ]

View File

@ -1,7 +1,7 @@
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse from django.http import HttpResponse
from .models import LegislativeText, LegislationBook, LegislationClassification, School from .models import LegislativeText, LegislationBook, LegislationClassification, School, Country
from random import sample from random import sample
@ -89,3 +89,11 @@ def get_all_by_school(request, school_id):
"school_name": school.name, "school_name": school.name,
"legislation": school.legislativetext_set.all() "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()
})