Skip to content

Commit

Permalink
Merge pull request #2040 from laws-africa/validate-year
Browse files Browse the repository at this point in the history
validate year for year mixins
  • Loading branch information
longhotsummer authored Sep 17, 2024
2 parents 3feadb7 + f797957 commit 4773ac6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions peachjam/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def test_court_year_listing(self):
self.assertContains(response, "/judgments/ECOWASCJ/2016/")
self.assertNotIn("years", response.context["facet_data"], [2016, 2018])

def test_court_year_listing_bad_year(self):
self.assertEqual(self.client.get("/judgments/ECOWASCJ/0/").status_code, 404)
self.assertEqual(
self.client.get("/judgments/ECOWASCJ/999999/").status_code, 404
)

def test_judgment_detail(self):
response = self.client.get(
"/akn/aa-au/judgment/ecowascj/2018/17/eng@2018-06-29"
Expand Down
11 changes: 11 additions & 0 deletions peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools

from django.http import Http404
from django.http.response import HttpResponse
from django.middleware.csrf import get_token
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -449,6 +450,16 @@ def get(self, request, *args, **kwargs):


class YearMixin:
def dispatch(self, request, *args, **kwargs):
# validate year
try:
year = int(kwargs["year"])
if year < 1 or year > 9999:
raise ValueError()
except ValueError:
raise Http404()
return super().dispatch(request, *args, **kwargs)

@property
def year(self):
return self.kwargs["year"]
Expand Down

0 comments on commit 4773ac6

Please sign in to comment.