add automatic generation of country and school listing

This commit is contained in:
stupidcomputer 2024-06-30 21:56:33 -05:00
parent 0a6ed6ffe2
commit bfbea9e855
6 changed files with 71 additions and 4 deletions

View File

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from .leglib import HSYIG24, HSMUN23 from .leglib import HSYIG24, HSMUN23
import io import io
@ -26,6 +27,10 @@ class School(models.Model):
def __str__(self): def __str__(self):
return self.name 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): class Country(models.Model):
name = models.CharField(max_length=256) name = models.CharField(max_length=256)
@ -35,6 +40,10 @@ class Country(models.Model):
def __str__(self): def __str__(self):
return self.name 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 LegislationBook(models.Model):
class Meta: class Meta:
verbose_name = "Book" verbose_name = "Book"

View File

@ -18,7 +18,7 @@
</div> </div>
<div id="rightnav"> <div id="rightnav">
<a href="/explorer/all">all</a> <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/search">search</a>
<a href="/explorer/stats">stats</a> <a href="/explorer/stats">stats</a>
</div> </div>

View File

@ -0,0 +1,14 @@
{% 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>
</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

@ -10,6 +10,11 @@ urlpatterns = [
path("conference/<int:conference_id>/", views.view_conference, name="viewconf"), 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/<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:model_id>/", views.get_all_by_school, name="schoolview"),
path("countries/<int:model_id>/", views.get_all_by_country, name="countryview"), # 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

@ -91,5 +91,29 @@ def get_all_by_x(model):
return wrapped return wrapped
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"
plural = plural.lower()
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_school = get_all_by_x(School)
get_all_by_country = get_all_by_x(Country) get_all_by_country = get_all_by_x(Country)
get_all_schools = get_all_xs(School)
get_all_countries = get_all_xs(Country)