Commits

Anonymous committed 43dd5d2

do not show archive entries for the date before the first post

  • Participants
  • Parent commits 0671267

Comments (0)

Files changed (1)

apps/blog/views.py

 def archive_month(request, year, month):
     if int(year) < 1901: # to exclude all troubles with strftime
         raise Http404
+    first_post = Post.objects.order_by('date')[0]
+    min_year  = first_post.date.date().year
+    min_month = first_post.date.date().month
+    try:
+        if dt(int(year), int(month), 1) < dt(int(min_year), int(min_month), 1):
+            return HttpResponseRedirect('/%s' % settings.BLOG_URLCONF_ROOT)
+    except (ValueError):
+        return HttpResponseRedirect('/%s' % settings.BLOG_URLCONF_ROOT)
     qs = Post.objects.all()
     return date_based.archive_month(request, year, month, queryset=qs,
                                     month_format='%m',
 def archive_year(request, year):
     if int(year) < 1901:
         raise Http404
+    first_post = Post.objects.order_by('date')[0]
+    min_year = first_post.date.date().year
+    if int(year) < min_year:
+        return HttpResponseRedirect('/%s' % settings.BLOG_URLCONF_ROOT)
     qs = Post.objects.all()
     return date_based.archive_year(request, year, make_object_list=True,
                                    queryset=qs,
 def archive_day(request, year, month, day):
     if int(year) < 1901:
         raise Http404
+    first_post = Post.objects.order_by('date')[0]
+    min_year  = first_post.date.date().year
+    min_month = first_post.date.date().month
+    min_day   = first_post.date.date().day
+    try:
+        if dt(int(year), int(month), int(day)) < dt(int(min_year), int(min_month), int(min_day)):
+            return HttpResponseRedirect('/%s' % settings.BLOG_URLCONF_ROOT)
+    except (ValueError):
+        return HttpResponseRedirect('/%s' % settings.BLOG_URLCONF_ROOT)
     qs = Post.objects.all()
     return date_based.archive_day(request, year, month, day, month_format='%m',
                                   template_name='blog/post_archive_year.html',