From 3fa153ee592fae0e5a22c2d2e834ee031946f376 Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Fri, 28 Jun 2024 16:36:06 -0500 Subject: [PATCH] add a model for legislation classification, making topics now you can make topics! --- franklincce/explorer/admin.py | 12 ++++++-- .../0004_legislationclassification.py | 21 ++++++++++++++ ...egislationclassification_obvious_change.py | 19 ++++++++++++ ...egislationclassification_obvious_change.py | 17 +++++++++++ franklincce/explorer/models.py | 12 ++++++-- .../templates/explorer/classifications.html | 15 ++++++++++ .../explorer/templates/explorer/results.html | 15 ++++++++++ franklincce/explorer/urls.py | 2 ++ franklincce/explorer/views.py | 29 ++++++++++++++++++- 9 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 franklincce/explorer/migrations/0004_legislationclassification.py create mode 100644 franklincce/explorer/migrations/0005_legislationclassification_obvious_change.py create mode 100644 franklincce/explorer/migrations/0006_remove_legislationclassification_obvious_change.py create mode 100644 franklincce/explorer/templates/explorer/classifications.html create mode 100644 franklincce/explorer/templates/explorer/results.html diff --git a/franklincce/explorer/admin.py b/franklincce/explorer/admin.py index 3e404c0..0c86b04 100644 --- a/franklincce/explorer/admin.py +++ b/franklincce/explorer/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import LegislativeText, LegislationBook +from explorer import models class LegislativeTextAdmin(admin.ModelAdmin): list_display = ('__str__', 'legislation_title', 'school') @@ -8,5 +8,11 @@ class LegislativeTextAdmin(admin.ModelAdmin): class LegislationBookAdmin(admin.ModelAdmin): exclude = ("has_performed_export",) -admin.site.register(LegislativeText, LegislativeTextAdmin) -admin.site.register(LegislationBook, LegislationBookAdmin) +to_register = [ + [models.LegislativeText, LegislativeTextAdmin], + [models.LegislationBook, LegislationBookAdmin], + [models.LegislationClassification] +] +for i in to_register: + admin.site.register(*i) + print(i) diff --git a/franklincce/explorer/migrations/0004_legislationclassification.py b/franklincce/explorer/migrations/0004_legislationclassification.py new file mode 100644 index 0000000..acd0d38 --- /dev/null +++ b/franklincce/explorer/migrations/0004_legislationclassification.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.12 on 2024-06-28 20:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('explorer', '0003_legislationbook_has_performed_export_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='LegislationClassification', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(help_text='Name of this classification.', max_length=256)), + ('text_to_match', models.CharField(help_text='a comma seperated list of keywords to include in the classification. spaces and dashes are discluded.', max_length=256)), + ], + ), + ] diff --git a/franklincce/explorer/migrations/0005_legislationclassification_obvious_change.py b/franklincce/explorer/migrations/0005_legislationclassification_obvious_change.py new file mode 100644 index 0000000..208a48c --- /dev/null +++ b/franklincce/explorer/migrations/0005_legislationclassification_obvious_change.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.12 on 2024-06-28 21:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('explorer', '0004_legislationclassification'), + ] + + operations = [ + migrations.AddField( + model_name='legislationclassification', + name='obvious_change', + field=models.CharField(default='test', help_text='Name of this classification.', max_length=256), + preserve_default=False, + ), + ] diff --git a/franklincce/explorer/migrations/0006_remove_legislationclassification_obvious_change.py b/franklincce/explorer/migrations/0006_remove_legislationclassification_obvious_change.py new file mode 100644 index 0000000..33b97cb --- /dev/null +++ b/franklincce/explorer/migrations/0006_remove_legislationclassification_obvious_change.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.12 on 2024-06-28 21:08 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('explorer', '0005_legislationclassification_obvious_change'), + ] + + operations = [ + migrations.RemoveField( + model_name='legislationclassification', + name='obvious_change', + ), + ] diff --git a/franklincce/explorer/models.py b/franklincce/explorer/models.py index 1357bb6..cd312ed 100644 --- a/franklincce/explorer/models.py +++ b/franklincce/explorer/models.py @@ -31,8 +31,6 @@ class LegislationBook(models.Model): has_performed_export = models.BooleanField(default=False) def save(self, **kwargs): - super().save(**kwargs) - if not self.has_performed_export: self.has_performed_export = True super().save(**kwargs) @@ -136,3 +134,13 @@ class LegislativeText(models.Model): if self.assembly in ["RGA", "BGA", "WGA", "GEN"]: return True return False + +class LegislationClassification(models.Model): + name = models.CharField(max_length=256, help_text="Name of this classification.") + text_to_match = models.CharField( + max_length=256, + help_text="a comma seperated list of keywords to include in the classification. spaces and dashes are discluded." + ) + + def __str__(self): + return "{}".format(self.name) diff --git a/franklincce/explorer/templates/explorer/classifications.html b/franklincce/explorer/templates/explorer/classifications.html new file mode 100644 index 0000000..96ddf1c --- /dev/null +++ b/franklincce/explorer/templates/explorer/classifications.html @@ -0,0 +1,15 @@ +{% extends "explorer/base.html" %} + +{% block content %} + + +
+

All topics

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

{{ result_name }}

+ + +
+{% endblock content %} diff --git a/franklincce/explorer/urls.py b/franklincce/explorer/urls.py index a2580d2..7771702 100644 --- a/franklincce/explorer/urls.py +++ b/franklincce/explorer/urls.py @@ -8,4 +8,6 @@ urlpatterns = [ path("stats/", views.stats, name="stats"), 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"), ] diff --git a/franklincce/explorer/views.py b/franklincce/explorer/views.py index feb0db8..ba0ded4 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 +from .models import LegislativeText, LegislationBook, LegislationClassification from random import sample @@ -53,3 +53,30 @@ def stats(request): "white_ga": len(LegislativeText.objects.filter(assembly="WGA")), } return render(request, "explorer/stats.html", context) + +def get_all_classifications(request): + classifications = LegislationClassification.objects.all() + return render(request, "explorer/classifications.html", { + "classifications": classifications, + }) + +def get_all_classified_by_id(request, classification_id): + classification = get_object_or_404(LegislationClassification, pk=classification_id) + # this is very expensive; make a way for this to be cached please? + + all_texts = LegislativeText.objects.all() + all_terms = classification.text_to_match.split(',') + all_terms = [i.lower() for i in all_terms] + + matches = [] + + for text in all_texts: + for term in all_terms: + if term in text.text.lower(): + matches.append(text) + break + + return render(request, "explorer/results.html", { + "legislation": matches, + "result_name": "All legislation in topic {}".format(classification.name) + })