Ticket #10793: paginator.diff

File paginator.diff, 1.7 KB (added by Christopher Medrela, 13 years ago)

Dynamic paginator creation, checking for DB changes and time limit

  • django/contrib/sitemaps/__init__.py

    diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py
    index 747cd1d..fc7f219 100644
    a b from django.contrib.sites.models import Site  
    22from django.core import urlresolvers, paginator
    33from django.core.exceptions import ImproperlyConfigured
    44import urllib
     5from datetime import datetime, timedelta
    56
    67PING_URL = "http://www.google.com/webmasters/tools/ping"
    78
    class Sitemap(object):  
    3940    # This limit is defined by Google. See the index documentation at
    4041    # http://sitemaps.org/protocol.php#index.
    4142    limit = 50000
     43    counter_delta = timedelta(seconds = 60)
     44    reload_delta = timedelta(seconds = 600)
    4245
    4346    # If protocol is None, the URLs in the sitemap will use the protocol
    4447    # with which the sitemap was requested.
    class Sitemap(object):  
    6063        return obj.get_absolute_url()
    6164
    6265    def _get_paginator(self):
    63         if not hasattr(self, "_paginator"):
     66        __now = datetime.now()
     67        # Do now have paginator or
     68        if (not hasattr(self, "_paginator") or
     69        # paginator very old or
     70                (self.__paginator_validate_timestamp > __now) or
     71        # paginator not so new and items count in DB changed
     72                (self.__counter_validate_timestamp > __now and self.__counter != len(self.items()))):
     73
     74            self.__counter_validate_timestamp = __now + self.counter_delta
     75            self.__paginator_validate_timestamp = __now + self.reload_delta
     76            self.__counter = len(self.items())
    6477            self._paginator = paginator.Paginator(self.items(), self.limit)
    6578        return self._paginator
    6679    paginator = property(_get_paginator)
Back to Top