Ticket #10793: paginator_doc.diff

File paginator_doc.diff, 3.0 KB (added by Christopher Medrela, 13 years ago)

Added documentation to previous patch, moved checking code to separate method.

  • django/contrib/sitemaps/__init__.py

    diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py
    index 747cd1d..c14473a 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 timedelta
     6from django.utils.timezone import now
    57
    68PING_URL = "http://www.google.com/webmasters/tools/ping"
    79
    class Sitemap(object):  
    3941    # This limit is defined by Google. See the index documentation at
    4042    # http://sitemaps.org/protocol.php#index.
    4143    limit = 50000
     44    counter_delta = timedelta(seconds = 60)
     45    reload_delta = timedelta(seconds = 600)
    4246
    4347    # If protocol is None, the URLs in the sitemap will use the protocol
    4448    # with which the sitemap was requested.
    class Sitemap(object):  
    5963    def location(self, obj):
    6064        return obj.get_absolute_url()
    6165
     66    def _paginator_reload(self, __now):
     67        """
     68        Determines whether to recreate paginator. Called from _get_paginator,
     69        can be overriden in child classes.
     70
     71        Arguments:
     72        __now -- current timestamp
     73        """
     74        return (
     75            # paginator very old or
     76            (self.__paginator_validate_timestamp > __now) or
     77            # paginator not so new and items count in DB changed
     78            (self.__counter_validate_timestamp > __now and self.__counter != len(self.items())))
     79
    6280    def _get_paginator(self):
    63         if not hasattr(self, "_paginator"):
     81        __now = now()
     82        if not hasattr(self, "_paginator") or self._paginator_reload(__now):
     83            self.__counter_validate_timestamp = __now + self.counter_delta
     84            self.__paginator_validate_timestamp = __now + self.reload_delta
     85            self.__counter = len(self.items())
    6486            self._paginator = paginator.Paginator(self.items(), self.limit)
    6587        return self._paginator
    6688    paginator = property(_get_paginator)
  • docs/ref/contrib/sitemaps.txt

    diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt
    index ac9f8ab..d9b99c6 100644
    a b Sitemap class reference  
    131131
    132132    A ``Sitemap`` class can define the following methods/attributes:
    133133
     134    .. attribute:: Sitemap.reload_delta
     135
     136        Timedelta between recreation paginator storing objects from database.
     137        Default 600 seconds.
     138
     139    .. attribute:: Sitemap.counter_delta
     140
     141        Timedelta between checks whether number of database objects stored
     142        in paginator differs from number in database. Difference means
     143        paginator recreation. Default 60 seconds.
     144
     145    .. attribute:: Sitemap._paginator_reload
     146
     147        Function governing whether to create new paginator replacing existing one.
     148        Can be overridden in child classes.
     149
    134150    .. attribute:: Sitemap.items
    135151
    136152        **Required.** A method that returns a list of objects. The framework
Back to Top