Compare commits

...

3 Commits

6 changed files with 91 additions and 27 deletions

View File

@ -1,5 +1,6 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from .leglib import HSYIG24, HSMUN23
import io
@ -26,6 +27,10 @@ class School(models.Model):
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})
class Country(models.Model):
name = models.CharField(max_length=256)
@ -35,6 +40,10 @@ class Country(models.Model):
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})
class LegislationBook(models.Model):
class Meta:
verbose_name = "Book"
@ -159,8 +168,8 @@ class LegislativeText(models.Model):
class LegislationClassification(models.Model):
class Meta:
verbose_name = "Classification"
verbose_name_plural = "Classifications"
verbose_name = "Topic"
verbose_name_plural = "Topics"
name = models.CharField(max_length=256, help_text="Name of this classification.")
text_to_match = models.CharField(
@ -170,3 +179,7 @@ class LegislationClassification(models.Model):
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})

View File

@ -18,7 +18,7 @@
</div>
<div id="rightnav">
<a href="/explorer/all">all</a>
<a href="/explorer/topics">by topic</a>
<a href="/explorer/groups">by group</a>
<a href="/explorer/search">search</a>
<a href="/explorer/stats">stats</a>
</div>

View File

@ -0,0 +1,15 @@
{% extends "explorer/base.html" %}
{% block content %}
<link rel="stylesheet" type="text/css" href="/static/tn.css" />
<div class="boxed">
<h1>View legislation</h1>
<ul>
<li><a href="/explorer/countries/">By country</a></li>
<li><a href="/explorer/schools/">By school</a></li>
<li><a href="/explorer/topics/">By topic</a></li>
</ul>
</div>
{% endblock content %}

View File

@ -0,0 +1,15 @@
{% extends "explorer/base.html" %}
{% block content %}
<link rel="stylesheet" type="text/css" href="/static/tn.css" />
<div class="boxed">
<h1>{{ result_name }}</h1>
<ul>
{% for instance in instances %}
<li><a href="{{ instance.get_absolute_url }}">{{ instance.name }}</a></li>
{% endfor %}
</ul>
</div>
{% endblock content %}

View File

@ -8,8 +8,13 @@ urlpatterns = [
path("stats/", views.stats, name="stats"),
path("legislation/<int:legislation_id>/", views.view_legislation, name="viewleg"),
path("conference/<int:conference_id>/", views.view_conference, name="viewconf"),
path("topics/<int:classification_id>/", views.get_all_classified_by_id, name="classificationview"),
path("topics/", views.get_all_classifications, name="classificationsview"),
path("schools/<int:school_id>/", views.get_all_by_school, name="schoolview"),
path("countries/<int:country_id>/", views.get_all_by_country, name="countryview"),
path("topics/<int:model_id>/", views.get_all_classified_by_id, name="LegislationClassification.detail"),
path("topics/", views.get_all_classifications, name="LegislationClassification"),
# these are named weirdly -- see models.py School and Country definitions
path("schools/<int:model_id>/", views.get_all_by_school, name="School.detail"),
path("countries/<int:model_id>/", views.get_all_by_country, name="Country.detail"),
path("schools/", views.get_all_schools, name="School"),
path("countries/", views.get_all_countries, name="Country"),
path("groups/", views.return_groups, name="Groups")
]

View File

@ -54,14 +54,8 @@ def stats(request):
}
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)
def get_all_classified_by_id(request, model_id):
classification = get_object_or_404(LegislationClassification, pk=model_id)
# this is very expensive; make a way for this to be cached please?
all_texts = LegislativeText.objects.all()
@ -81,18 +75,40 @@ def get_all_classified_by_id(request, classification_id):
"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)
def get_all_by_x(model):
def wrapped(request, model_id):
instance = get_object_or_404(model, pk=model_id)
return render(request, "explorer/results.html", {
"result_name": "All legislation by {}".format(instance.name),
"legislation": instance.legislativetext_set.all()
})
return wrapped
return render(request, "explorer/results.html", {
"result_name": "All legislation by {}".format(school.name),
"legislation": school.legislativetext_set.all()
})
def get_all_xs(model):
def wrapper(request):
instances = model.objects.all()
try:
# what the heck, django?????
plural = model._meta.verbose_name_plural
except:
plural = model.__name__ + "s"
def get_all_by_country(request, country_id):
country = get_object_or_404(Country, pk=country_id)
plural = plural.lower()
return render(request, "explorer/results.html", {
"result_name": "All legislation by country {}".format(country.name),
"legislation": country.legislativetext_set.all()
})
return render(request, "explorer/listing.html", {
"result_name": "All {}".format(plural),
"instances": instances,
})
return wrapper
def return_groups(request):
return render(request, "explorer/by_group.html", {})
get_all_by_school = get_all_by_x(School)
get_all_by_country = get_all_by_x(Country)
get_all_schools = get_all_xs(School)
get_all_countries = get_all_xs(Country)
get_all_classifications = get_all_xs(LegislationClassification)