Commit 66580fe9 authored by Tim Graham's avatar Tim Graham
Browse files

Fixed #23403 -- Fixed crash in contrib.sitemaps if lastmod returned a date rather than datetime.

Thanks igorcc for the report.
parent c5b4145f
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -82,6 +82,21 @@ class HTTPSitemapTests(SitemapTestsBase):
        response = self.client.get('/lastmod/sitemap.xml')
        self.assertEqual(response['Last-Modified'], 'Wed, 13 Mar 2013 10:00:00 GMT')

    def test_sitemap_last_modified_date(self):
        """
        The Last-Modified header should be support dates (without time).
        """
        response = self.client.get('/lastmod/date-sitemap.xml')
        self.assertEqual(response['Last-Modified'], 'Wed, 13 Mar 2013 00:00:00 GMT')

    def test_sitemap_last_modified_tz(self):
        """
        The Last-Modified header should be converted from timezone aware dates
        to GMT.
        """
        response = self.client.get('/lastmod/tz-sitemap.xml')
        self.assertEqual(response['Last-Modified'], 'Wed, 13 Mar 2013 15:00:00 GMT')

    def test_sitemap_last_modified_missing(self):
        "Tests that Last-Modified header is missing when sitemap has no lastmod"
        response = self.client.get('/generic/sitemap.xml')
+14 −1
Original line number Diff line number Diff line
from datetime import datetime
from datetime import date, datetime
from django.conf.urls import url
from django.conf.urls.i18n import i18n_patterns
from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views
from django.http import HttpResponse
from django.utils import timezone
from django.views.decorators.cache import cache_page

from django.contrib.sitemaps.tests.base import I18nTestModel, TestModel
@@ -53,6 +54,14 @@ class FixedLastmodMixedSitemap(Sitemap):
        return [o1, o2]


class DateSiteMap(SimpleSitemap):
    lastmod = date(2013, 3, 13)


class TimezoneSiteMap(SimpleSitemap):
    lastmod = datetime(2013, 3, 13, 10, 0, 0, tzinfo=timezone.get_fixed_timezone(-300))


def testmodelview(request, id):
    return HttpResponse()

@@ -99,6 +108,10 @@ urlpatterns = [
    url(r'^empty/sitemap\.xml$', views.sitemap, {'sitemaps': empty_sitemaps}),
    url(r'^lastmod/sitemap\.xml$', views.sitemap, {'sitemaps': fixed_lastmod_sitemaps}),
    url(r'^lastmod-mixed/sitemap\.xml$', views.sitemap, {'sitemaps': fixed_lastmod__mixed_sitemaps}),
    url(r'^lastmod/date-sitemap.xml$', views.sitemap,
        {'sitemaps': {'date-sitemap': DateSiteMap}}),
    url(r'^lastmod/tz-sitemap.xml$', views.sitemap,
        {'sitemaps': {'tz-sitemap': TimezoneSiteMap}}),
    url(r'^generic/sitemap\.xml$', views.sitemap, {'sitemaps': generic_sitemaps}),
    url(r'^flatpages/sitemap\.xml$', views.sitemap, {'sitemaps': flatpage_sitemaps}),
    url(r'^cached/index\.xml$', cache_page(1)(views.index),
+7 −1
Original line number Diff line number Diff line
from calendar import timegm
import datetime
from functools import wraps

from django.contrib.sites.shortcuts import get_current_site
@@ -74,6 +75,11 @@ def sitemap(request, sitemaps, section=None,
    if hasattr(site, 'latest_lastmod'):
        # if latest_lastmod is defined for site, set header so as
        # ConditionalGetMiddleware is able to send 304 NOT MODIFIED
        lastmod = site.latest_lastmod
        response['Last-Modified'] = http_date(
            timegm(site.latest_lastmod.utctimetuple()))
            timegm(
                lastmod.utctimetuple() if isinstance(lastmod, datetime.datetime)
                else lastmod.timetuple()
            )
        )
    return response
+3 −0
Original line number Diff line number Diff line
@@ -40,3 +40,6 @@ Bugfixes

* Empty ``index_together`` or ``unique_together`` model options no longer
  results in infinite migrations (:ticket:`23452`).

* Fixed crash in ``contrib.sitemaps`` if ``lastmod`` returned a ``date`` rather
  than a ``datetime`` (:ticket:`23403`).