Fixed #29903 -- Added error message for invalid WeekArchiveView week_format.

This commit is contained in:
Hasan Ramezani 2018-10-29 15:56:04 +01:00 committed by Tim Graham
parent 7f2b27e95c
commit 4f8f1b2f24
3 changed files with 16 additions and 4 deletions

View File

@ -485,10 +485,14 @@ class BaseWeekArchiveView(YearMixin, WeekMixin, BaseDateListView):
date_field = self.get_date_field()
week_format = self.get_week_format()
week_start = {
'%W': '1',
'%U': '0',
}[week_format]
week_choices = {'%W': '1', '%U': '0'}
try:
week_start = week_choices[week_format]
except KeyError:
raise ValueError('Unknown week format %r. Choices are: %s' % (
week_format,
', '.join(sorted(week_choices)),
))
date = _date_from_string(year, self.get_year_format(),
week_start, '%w',
week, week_format)

View File

@ -528,6 +528,10 @@ class WeekArchiveViewTests(TestDataMixin, TestCase):
self.assertEqual(res.status_code, 200)
self.assertEqual(res.context['week'], datetime.date(2008, 9, 29))
def test_unknown_week_format(self):
with self.assertRaisesMessage(ValueError, "Unknown week format '%T'. Choices are: %U, %W"):
self.client.get('/dates/books/2008/week/39/unknown_week_format/')
def test_datetime_week_view(self):
BookSigning.objects.create(event_date=datetime.datetime(2008, 4, 2, 12, 0))
res = self.client.get('/dates/booksignings/2008/week/13/')

View File

@ -182,6 +182,10 @@ urlpatterns = [
path('dates/books/<int:year>/week/<int:week>/paginated/', views.BookWeekArchive.as_view(paginate_by=30)),
path('dates/books/<int:year>/week/no_week/', views.BookWeekArchive.as_view()),
path('dates/books/<int:year>/week/<int:week>/monday/', views.BookWeekArchive.as_view(week_format='%W')),
path(
'dates/books/<int:year>/week/<int:week>/unknown_week_format/',
views.BookWeekArchive.as_view(week_format='%T'),
),
path('dates/booksignings/<int:year>/week/<int:week>/', views.BookSigningWeekArchive.as_view()),
# DayArchiveView