Ticket #3585: gv_datebased_object_date.2.diff

File gv_datebased_object_date.2.diff, 8.7 KB (added by Per Jonsson <poj@…>, 9 years ago)
  • django/views/generic/date_based.py

     
    106106        month:
    107107            (date) this month
    108108        next_month:
    109             (date) the first day of the next month, or None if the next month is in the future
     109            (date) the first day of the next month, or None if the next month is in the future and allow_future is False
    110110        previous_month:
    111111            (date) the first day of the previous month
    112112        object_list:
    113113            list of objects published in the given month
     114        next_object_date:
     115            (date) the date for the first object after the beginning of the next month, or None if there is no such object or if the date is in the future and allow_future is False
     116        previous_object_date:
     117            (date) the date for the last object before the beginning of the this month, or None if there is no such object
    114118    """
    115119    if extra_context is None: extra_context = {}
    116120    try:
     
    144148    else:
    145149        next_month = None
    146150
     151    # Get the closest past object's date, if it exists.
     152    lookup_kwargs = {'%s__lt' % date_field: first_day}
     153    previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
     154    if previous_list:
     155        previous_object_date = getattr(previous_list[0], date_field)
     156    else:
     157        previous_object_date = None
     158
     159    # Get the closest future object's date, if it exists and is allowed
     160    if last_day >= now.date() and not allow_future:
     161        next_object_date = None
     162    else:
     163        lookup_kwargs = {'%s__gte' % date_field: last_day}
     164        next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
     165        if next_list:
     166            next_object_date = getattr(next_list[0], date_field)
     167        else:
     168            next_object_date = None
     169
    147170    if not template_name:
    148171        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    149172    t = template_loader.get_template(template_name)
     
    152175        'month': date,
    153176        'next_month': next_month,
    154177        'previous_month': first_day - datetime.timedelta(days=1),
     178        'next_%s_date' % template_object_name: next_object_date,
     179        'previous_%s_date' % template_object_name: previous_object_date,
    155180    }, context_processors)
    156181    for key, value in extra_context.items():
    157182        if callable(value):
     
    225250        previous_day
    226251            (datetime) the previous day
    227252        next_day
    228             (datetime) the next day, or None if the current day is today
     253            (datetime) the next day, or None if the current day is today and allow_future is False
     254        next_object_date:
     255            (date) the date for the first object after the beginning of the next month, or None if there is no such object or if the date is in the future and allow_future is False
     256        previous_object_date:
     257            (date) the date for the last object before the beginning of the this month, or None if there is no such object
    229258    """
    230259    if extra_context is None: extra_context = {}
    231260    try:
     
    256285    else:
    257286        next_day = None
    258287
     288    # Get the closest past object's date, if it exists.
     289    if isinstance(model._meta.get_field(date_field), DateTimeField):
     290        lookup_kwargs = {'%s__lt' % date_field: datetime.datetime.combine(date, datetime.time.min)}
     291    else:
     292        lookup_kwargs = {'%s__lt' % date_field: date}
     293    previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
     294    if previous_list:
     295        previous_object_date = getattr(previous_list[0], date_field)
     296    else:
     297        previous_object_date = None
     298
     299    # Get the closest future object's date, if it exists and is allowed
     300    if date >= now.date() and not allow_future:
     301        next_object_date = None
     302    else:
     303        if isinstance(model._meta.get_field(date_field), DateTimeField):
     304            lookup_kwargs = {'%s__gt' % date_field: datetime.datetime.combine(date, datetime.time.min)}
     305        else:
     306            lookup_kwargs = {'%s__gt' % date_field: date}
     307        next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
     308        if next_list:
     309            next_object_date = getattr(next_list[0], date_field)
     310        else:
     311            next_object_date = None
     312
     313
    259314    if not template_name:
    260315        template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
    261316    t = template_loader.get_template(template_name)
     
    264319        'day': date,
    265320        'previous_day': date - datetime.timedelta(days=1),
    266321        'next_day': next_day,
     322        'next_%s_date' % template_object_name: next_object_date,
     323        'previous_%s_date' % template_object_name: previous_object_date,
    267324    }, context_processors)
    268325    for key, value in extra_context.items():
    269326        if callable(value):
  • docs/generic_views.txt

     
    384384    * ``month``: A ``datetime.date`` object representing the given month.
    385385
    386386    * ``next_month``: A ``datetime.date`` object representing the first day of
    387       the next month. If the next month is in the future, this will be
    388       ``None``.
     387      the next month. If the next month is in the future and ``allow_future`` is
     388      ``False``, this will be ``None``.
    389389
    390390    * ``previous_month``: A ``datetime.date`` object representing the first day
    391391      of the previous month. Unlike ``next_month``, this will never be
    392392      ``None``.
    393393
     394    * ``previous_month``: A ``datetime.date`` object representing the day of the
     395      last post of the previous month. If there is no such post this will be
     396      ``None``.
     397
    394398    * ``object_list``: A list of objects available for the given month. This
    395399      variable's name depends on the ``template_object_name`` parameter, which
    396400      is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
    397401      this variable's name will be ``foo_list``.
    398402
     403    * ``next_object_date``: A ``datetime.date`` object representing the date of the
     404      first object dated after the start of next month. If there is no such object
     405      or if the date is in the future and ``allow_future`` is ``False``, this will
     406      be ``None``. This variable's name depends on the ``template_object_name``
     407      parameter, which is ``'object'`` by default. If ``template_object_name`` is
     408      ``'foo'``, this variable's name will be ``next_foo_date``.
     409
     410    * ``previous_object_date``: A ``datetime.date`` object representing the date of
     411      the last object dated before the start of the given month. If there is no such
     412      object, this will be ``None``. This variable's name depends on the
     413      ``template_object_name`` parameter, which is ``'object'`` by default.
     414      If ``template_object_name`` is ``'foo'``, this variable's name will be
     415      ``previous_foo_date``.
     416
    399417.. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
    400418
    401419``django.views.generic.date_based.archive_week``
     
    550568    * ``day``: A ``datetime.date`` object representing the given day.
    551569
    552570    * ``next_day``: A ``datetime.date`` object representing the next day. If
    553       the next day is in the future, this will be ``None``.
     571      the next day is in the future, and ``allow_future`` is ``False``, this
     572      will be ``None``.
    554573
    555574    * ``previous_day``: A ``datetime.date`` object representing the given day.
    556575      Unlike ``next_day``, this will never be ``None``.
     
    560579      is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
    561580      this variable's name will be ``foo_list``.
    562581
     582    * ``next_object_date``: A ``datetime.date`` object representing the date of the
     583      first object dated after the given day. If there is no such object or if the
     584      date is in the future and ``allow_future`` is ``False``, this will be ``None``.
     585      This variable's name depends on the ``template_object_name`` parameter, which
     586      is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
     587      this variable's name will be ``next_foo_date``.
     588
     589    * ``previous_object_date``: A ``datetime.date`` object representing the date of
     590      the last object dated before the given day. If there is no such object, this
     591      will be ``None``. This variable's name depends on the ``template_object_name``
     592      parameter, which is ``'object'`` by default. If ``template_object_name`` is
     593      ``'foo'``, this variable's name will be ``previous_foo_date``.
     594
     595
    563596``django.views.generic.date_based.archive_today``
    564597-------------------------------------------------
    565598
Back to Top