2024-06-19 01:29:05 -05:00
|
|
|
from django.db import models
|
2024-06-19 05:41:13 -05:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-06-30 21:56:33 -05:00
|
|
|
from django.urls import reverse
|
2024-06-19 01:29:05 -05:00
|
|
|
|
2024-06-29 13:52:56 -05:00
|
|
|
from .leglib import HSYIG24, HSMUN23
|
2024-06-19 12:41:41 -05:00
|
|
|
import io
|
|
|
|
import fitz
|
|
|
|
|
2024-06-21 03:02:03 -05:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2024-06-29 23:41:33 -05:00
|
|
|
def InstantiateIfNone(model, name):
|
2024-06-29 23:19:39 -05:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
2024-06-30 21:56:33 -05:00
|
|
|
def get_absolute_url(self):
|
|
|
|
our_name = __class__.__name__
|
|
|
|
return reverse("{}.detail".format(our_name), kwargs={"model_id": self.id})
|
|
|
|
|
2024-06-29 23:41:33 -05:00
|
|
|
class Country(models.Model):
|
|
|
|
name = models.CharField(max_length=256)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = "Countries"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2024-06-30 21:56:33 -05:00
|
|
|
def get_absolute_url(self):
|
|
|
|
our_name = __class__.__name__
|
|
|
|
return reverse("{}.detail".format(our_name), kwargs={"model_id": self.id})
|
|
|
|
|
2024-07-23 23:51:37 -05:00
|
|
|
class Sponsor(models.Model):
|
|
|
|
name = models.CharField(max_length=256)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
our_name = __class__.__name__
|
|
|
|
return reverse("{}.detail".format(our_name), kwargs={"model_id": self.id})
|
|
|
|
|
2024-07-24 13:13:07 -05:00
|
|
|
class Category(models.Model):
|
|
|
|
name = models.CharField(max_length=256)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = "Categories"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{}".format(self.name)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
our_name = __class__.__name__
|
|
|
|
return reverse("{}.detail".format(our_name), kwargs={"model_id": self.id})
|
|
|
|
|
2024-06-19 05:41:13 -05:00
|
|
|
class LegislationBook(models.Model):
|
2024-06-29 23:54:25 -05:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Book"
|
|
|
|
verbose_name_plural = "Books"
|
|
|
|
|
2024-06-19 05:41:13 -05:00
|
|
|
class ConferenceType(models.TextChoices):
|
|
|
|
MIDDLE = "M", _("Middle School")
|
|
|
|
HIGH = "H", _("High School")
|
|
|
|
|
2024-06-19 12:41:41 -05:00
|
|
|
class ImportStrategy(models.TextChoices):
|
|
|
|
HSYIGA = "HSYIGBookParser", _("High School YIG Book Parser 1")
|
|
|
|
HSMUNA = "HSMUNBookParser", _("High School MUN Book Parser 1")
|
|
|
|
|
2024-06-19 05:41:13 -05:00
|
|
|
conference_type = models.CharField(
|
|
|
|
max_length=1,
|
|
|
|
choices=ConferenceType.choices,
|
|
|
|
default=ConferenceType.HIGH,
|
|
|
|
)
|
|
|
|
pdf = models.FileField(upload_to="uploads/")
|
|
|
|
name = models.CharField(max_length=256)
|
2024-06-19 12:41:41 -05:00
|
|
|
import_strategy = models.CharField(
|
|
|
|
max_length=128,
|
|
|
|
choices=ImportStrategy.choices,
|
|
|
|
default=ImportStrategy.HSYIGA
|
|
|
|
)
|
|
|
|
has_performed_export = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
def save(self, **kwargs):
|
|
|
|
if not self.has_performed_export:
|
|
|
|
self.has_performed_export = True
|
|
|
|
super().save(**kwargs)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
the_file = io.BytesIO(self.pdf.file.file.read())
|
|
|
|
the_document = fitz.open(stream=the_file)
|
|
|
|
if self.import_strategy == "HSYIGBookParser":
|
2024-06-29 13:52:56 -05:00
|
|
|
parsed = HSYIG24(the_document)
|
2024-06-19 12:41:41 -05:00
|
|
|
elif self.import_strategy == "HSMUNBookParser":
|
2024-06-29 13:52:56 -05:00
|
|
|
parsed = HSMUN23(the_document)
|
2024-06-19 12:41:41 -05:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
for text in parsed.output:
|
2024-06-29 23:41:33 -05:00
|
|
|
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"])
|
2024-07-23 23:51:37 -05:00
|
|
|
|
2024-07-24 13:13:07 -05:00
|
|
|
if not text["category"] or text["category"] == "Select One--":
|
|
|
|
text["category"] = "No category"
|
|
|
|
text["category"] = InstantiateIfNone(Category, text["category"])
|
|
|
|
|
2024-07-23 23:51:37 -05:00
|
|
|
sponsors = text["sponsors"].split(', ')
|
2024-07-24 00:13:30 -05:00
|
|
|
sponsors = [InstantiateIfNone(Sponsor, sponsor) for sponsor in sponsors]
|
2024-07-23 23:51:37 -05:00
|
|
|
|
|
|
|
del text["sponsors"]
|
|
|
|
|
2024-06-29 22:27:05 -05:00
|
|
|
text = LegislativeText(**text, from_book=self)
|
2024-06-19 12:41:41 -05:00
|
|
|
text.save()
|
2024-06-19 05:41:13 -05:00
|
|
|
|
2024-07-23 23:51:37 -05:00
|
|
|
for sponsor in sponsors:
|
2024-07-24 00:13:30 -05:00
|
|
|
text.sponsors.add(sponsor)
|
2024-07-23 23:51:37 -05:00
|
|
|
|
2024-06-19 05:41:13 -05:00
|
|
|
def __str__(self):
|
|
|
|
return "{}".format(self.name)
|
|
|
|
|
|
|
|
class LegislativeText(models.Model):
|
2024-06-29 23:54:25 -05:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Legislation"
|
|
|
|
verbose_name_plural = "Legislation"
|
|
|
|
|
2024-06-19 05:41:13 -05:00
|
|
|
class Assemblies(models.TextChoices):
|
|
|
|
RGA = "RGA", _("Red General Assembly")
|
|
|
|
BGA = "BGA", _("Blue General Assembly")
|
|
|
|
WGA = "WGA", _("White General Assembly")
|
|
|
|
RHB = "RHB", _("Red House")
|
|
|
|
BHB = "BHB", _("Blue House")
|
|
|
|
WHB = "WHB", _("White House")
|
|
|
|
RSB = "RSB", _("Red Senate")
|
|
|
|
BSB = "BSB", _("Blue Senate")
|
|
|
|
WSB = "WSB", _("White Senate")
|
|
|
|
SEN = "SEN", _("Senate")
|
|
|
|
HOU = "HOU", _("House")
|
|
|
|
GEN = "GEN", _("General Assembly")
|
|
|
|
|
|
|
|
assembly = models.CharField(
|
|
|
|
max_length=3,
|
|
|
|
choices=Assemblies.choices,
|
|
|
|
default=Assemblies.GEN
|
|
|
|
)
|
|
|
|
text = models.TextField()
|
|
|
|
year = models.IntegerField()
|
|
|
|
committee = models.IntegerField()
|
2024-07-24 13:13:07 -05:00
|
|
|
category = models.ForeignKey(Category, on_delete=models.CASCADE)
|
2024-06-19 05:41:13 -05:00
|
|
|
docket_order = models.IntegerField()
|
2024-06-29 23:54:25 -05:00
|
|
|
school = models.ForeignKey(School, on_delete=models.CASCADE)
|
2024-07-24 00:13:30 -05:00
|
|
|
sponsors = models.ManyToManyField(Sponsor, blank=True)
|
2024-06-19 05:41:13 -05:00
|
|
|
from_book = models.ForeignKey(LegislationBook, on_delete=models.CASCADE)
|
|
|
|
legislation_title = models.CharField(max_length=512)
|
2024-06-29 23:41:33 -05:00
|
|
|
country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True)
|
2024-06-19 05:41:13 -05:00
|
|
|
|
|
|
|
def __str__(self):
|
2024-06-19 12:41:41 -05:00
|
|
|
return "{}/{}-{}-{}".format(
|
2024-06-19 05:41:13 -05:00
|
|
|
self.assembly,
|
2024-06-19 12:41:41 -05:00
|
|
|
str(self.year),
|
2024-06-19 05:41:13 -05:00
|
|
|
self.committee,
|
|
|
|
self.docket_order,
|
|
|
|
)
|
2024-06-21 03:02:03 -05:00
|
|
|
|
|
|
|
def get_lines(self):
|
|
|
|
cls = namedtuple('LegLine', ['linenumber', 'linetext'])
|
|
|
|
return [cls(i + 1, j) for i, j in enumerate(self.text.split('\n'))]
|
2024-06-21 22:14:07 -05:00
|
|
|
|
|
|
|
def is_bill(self):
|
|
|
|
if self.assembly in [
|
|
|
|
"RHB",
|
|
|
|
"BHB",
|
|
|
|
"WHB",
|
|
|
|
"RSB",
|
|
|
|
"BSB",
|
|
|
|
"WSB",
|
|
|
|
"SEN",
|
|
|
|
"HOU",
|
|
|
|
]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_resolution(self):
|
|
|
|
if self.assembly in ["RGA", "BGA", "WGA", "GEN"]:
|
|
|
|
return True
|
|
|
|
return False
|
2024-06-28 16:36:06 -05:00
|
|
|
|
|
|
|
class LegislationClassification(models.Model):
|
2024-06-29 23:54:25 -05:00
|
|
|
class Meta:
|
2024-06-30 22:01:55 -05:00
|
|
|
verbose_name = "Topic"
|
|
|
|
verbose_name_plural = "Topics"
|
2024-06-29 23:54:25 -05:00
|
|
|
|
2024-06-28 16:36:06 -05:00
|
|
|
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)
|
2024-06-30 22:01:55 -05:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
our_name = __class__.__name__
|
2024-07-24 01:03:19 -05:00
|
|
|
return reverse("{}.detail".format(our_name), kwargs={"model_id": self.id})
|
|
|
|
|
2024-07-24 13:13:07 -05:00
|
|
|
models_in_index = [LegislationClassification, School, Country, Sponsor, Category]
|