From 0a6ed6ffe2c5922211766f1125b51ce076f8230d Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Sun, 30 Jun 2024 21:05:34 -0500 Subject: [PATCH] generalize the view generation into a factory --- franklincce/explorer/urls.py | 4 ++-- franklincce/explorer/views.py | 25 +++++++++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/franklincce/explorer/urls.py b/franklincce/explorer/urls.py index 68dd743..efa00ce 100644 --- a/franklincce/explorer/urls.py +++ b/franklincce/explorer/urls.py @@ -10,6 +10,6 @@ urlpatterns = [ path("conference//", views.view_conference, name="viewconf"), path("topics//", views.get_all_classified_by_id, name="classificationview"), path("topics/", views.get_all_classifications, name="classificationsview"), - path("schools//", views.get_all_by_school, name="schoolview"), - path("countries//", views.get_all_by_country, name="countryview"), + path("schools//", views.get_all_by_school, name="schoolview"), + path("countries//", views.get_all_by_country, name="countryview"), ] diff --git a/franklincce/explorer/views.py b/franklincce/explorer/views.py index 9dcb793..cdcc968 100644 --- a/franklincce/explorer/views.py +++ b/franklincce/explorer/views.py @@ -81,18 +81,15 @@ 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_by_country(request, country_id): - country = get_object_or_404(Country, pk=country_id) - - return render(request, "explorer/results.html", { - "result_name": "All legislation by country {}".format(country.name), - "legislation": country.legislativetext_set.all() - }) +get_all_by_school = get_all_by_x(School) +get_all_by_country = get_all_by_x(Country) \ No newline at end of file