diff --git a/franklincce/explorer/admin.py b/franklincce/explorer/admin.py index be6d5dc..8b27863 100644 --- a/franklincce/explorer/admin.py +++ b/franklincce/explorer/admin.py @@ -11,7 +11,8 @@ class LegislationBookAdmin(admin.ModelAdmin): to_register = [ [models.LegislativeText, LegislativeTextAdmin], [models.LegislationBook, LegislationBookAdmin], - [models.LegislationClassification] + [models.LegislationClassification], + [models.School], ] for i in to_register: admin.site.register(*i) \ No newline at end of file diff --git a/franklincce/explorer/migrations/0008_school_alter_legislativetext_school.py b/franklincce/explorer/migrations/0008_school_alter_legislativetext_school.py new file mode 100644 index 0000000..0dd47e4 --- /dev/null +++ b/franklincce/explorer/migrations/0008_school_alter_legislativetext_school.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.12 on 2024-06-30 03:57 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('explorer', '0007_legislativetext_category_legislativetext_country'), + ] + + operations = [ + migrations.CreateModel( + name='School', + 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='school', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='explorer.school'), + ), + ] diff --git a/franklincce/explorer/models.py b/franklincce/explorer/models.py index 8d2cfb9..e1f1f64 100644 --- a/franklincce/explorer/models.py +++ b/franklincce/explorer/models.py @@ -7,6 +7,25 @@ import fitz from collections import namedtuple +def ForeignInstantiateIfNone(model, name): + """ + Search the model for instances by name. + If there's none, then create one. + """ + filtered = model.objects.filter(name__exact=name) + try: + return filtered[0] + except IndexError: + obj = model(name=name) + obj.save() + return obj + +class School(models.Model): + name = models.CharField(max_length=256) + + def __str__(self): + return self.name + class LegislationBook(models.Model): class ConferenceType(models.TextChoices): MIDDLE = "M", _("Middle School") @@ -47,6 +66,7 @@ class LegislationBook(models.Model): return for text in parsed.output: + text["school"] = ForeignInstantiateIfNone(School, text["school"]) text = LegislativeText(**text, from_book=self) text.save() @@ -78,7 +98,7 @@ class LegislativeText(models.Model): committee = models.IntegerField() category = models.CharField(max_length=256) docket_order = models.IntegerField() - school = models.CharField(max_length=256) + school = models.ForeignKey(School, on_delete=models.SET_NULL, null=True) sponsors = models.CharField(max_length=256) from_book = models.ForeignKey(LegislationBook, on_delete=models.CASCADE) legislation_title = models.CharField(max_length=512) diff --git a/franklincce/explorer/templates/explorer/legislation.html b/franklincce/explorer/templates/explorer/legislation.html index e10acd7..d9421da 100644 --- a/franklincce/explorer/templates/explorer/legislation.html +++ b/franklincce/explorer/templates/explorer/legislation.html @@ -13,7 +13,7 @@

{{ legislation.assembly }}/{{ legislation.committee }}/{{ legislation.docket_order }}

-

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

+

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

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

diff --git a/franklincce/explorer/templates/explorer/school.html b/franklincce/explorer/templates/explorer/school.html new file mode 100644 index 0000000..1237684 --- /dev/null +++ b/franklincce/explorer/templates/explorer/school.html @@ -0,0 +1,14 @@ +{% extends "explorer/base.html" %} + +{% block content %} + +
+

All bills by {{ school_name }}

+ + +
+{% endblock content %} diff --git a/franklincce/explorer/urls.py b/franklincce/explorer/urls.py index 7771702..3e18c72 100644 --- a/franklincce/explorer/urls.py +++ b/franklincce/explorer/urls.py @@ -9,5 +9,6 @@ urlpatterns = [ path("legislation//", views.view_legislation, name="viewleg"), path("conference//", views.view_conference, name="viewconf"), path("topics//", views.get_all_classified_by_id, name="classificationview"), - path("topics/", views.get_all_classifications, name="classificationview"), + path("topics/", views.get_all_classifications, name="classificationsview"), + path("schools//", views.get_all_by_school, name="schoolview"), ] diff --git a/franklincce/explorer/views.py b/franklincce/explorer/views.py index ba0ded4..7c92a25 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 +from .models import LegislativeText, LegislationBook, LegislationClassification, School from random import sample @@ -80,3 +80,12 @@ def get_all_classified_by_id(request, classification_id): "legislation": matches, "result_name": "All legislation in topic {}".format(classification.name) }) + + +def get_all_by_school(request, school_id): + school = get_object_or_404(School, pk=school_id) + + return render(request, "explorer/school.html", { + "school_name": school.name, + "legislation": school.legislativetext_set.all() + })