Ticket #667: extra_lookup_kwargs_callback_r1217.patch

File extra_lookup_kwargs_callback_r1217.patch, 5.1 KB (added by Tom Tobin <korpios@…>, 10 years ago)

Patch against r1217 to implement optional callback for extra_lookup_kwargs

  • views/generic/date_based.py

     
    2121    """
    2222    mod = get_module(app_label, module_name)
    2323    lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()}
    24     lookup_kwargs.update(extra_lookup_kwargs)
     24    if callable(extra_lookup_kwargs):
     25        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     26    else:
     27        lookup_kwargs.update(extra_lookup_kwargs)
    2528    date_list = getattr(mod, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1]
    2629    if not date_list:
    2730        raise Http404("No %s.%s available" % (app_label, module_name))
     
    6871    # Only bother to check current date if the year isn't in the past.
    6972    if int(year) >= now.year:
    7073        lookup_kwargs['%s__lte' % date_field] = now
    71     lookup_kwargs.update(extra_lookup_kwargs)
     74    if callable(extra_lookup_kwargs):
     75        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     76    else:
     77        lookup_kwargs.update(extra_lookup_kwargs)
    7278    date_list = getattr(mod, "get_%s_list" % date_field)('month', **lookup_kwargs)
    7379    if not date_list:
    7480        raise Http404
     
    120126    # Only bother to check current date if the month isn't in the past.
    121127    if last_day >= now.date():
    122128        lookup_kwargs['%s__lte' % date_field] = now
    123     lookup_kwargs.update(extra_lookup_kwargs)
     129    if callable(extra_lookup_kwargs):
     130        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     131    else:
     132        lookup_kwargs.update(extra_lookup_kwargs)
    124133    object_list = mod.get_list(**lookup_kwargs)
    125134    if not object_list:
    126135        raise Http404
     
    169178    # Only bother to check current date if the date isn't in the past.
    170179    if date >= now.date():
    171180        lookup_kwargs['%s__lte' % date_field] = now
    172     lookup_kwargs.update(extra_lookup_kwargs)
     181    if callable(extra_lookup_kwargs):
     182        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     183    else:
     184        lookup_kwargs.update(extra_lookup_kwargs)
    173185    object_list = mod.get_list(**lookup_kwargs)
    174186    if not allow_empty and not object_list:
    175187        raise Http404
     
    233245        lookup_kwargs['%s__exact' % slug_field] = slug
    234246    else:
    235247        raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield")
    236     lookup_kwargs.update(extra_lookup_kwargs)
     248    if callable(extra_lookup_kwargs):
     249        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     250    else:
     251        lookup_kwargs.update(extra_lookup_kwargs)
    237252    try:
    238253        object = mod.get_object(**lookup_kwargs)
    239254    except ObjectDoesNotExist:
  • views/generic/list_detail.py

     
    3636            number of objects, total
    3737    """
    3838    mod = models.get_module(app_label, module_name)
    39     lookup_kwargs = extra_lookup_kwargs.copy()
     39    if callable(extra_lookup_kwargs):
     40        lookup_kwargs = extra_lookup_kwargs(request)
     41    else:
     42        lookup_kwargs = extra_lookup_kwargs.copy()
    4043    if paginate_by:
    4144        paginator = ObjectPaginator(mod, lookup_kwargs, paginate_by)
    4245        page = request.GET.get('page', 0)
     
    98101        lookup_kwargs['%s__exact' % slug_field] = slug
    99102    else:
    100103        raise AttributeError("Generic detail view must be called with either an object_id or a slug/slug_field")
    101     lookup_kwargs.update(extra_lookup_kwargs)
     104    if callable(extra_lookup_kwargs):
     105        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     106    else:
     107        lookup_kwargs.update(extra_lookup_kwargs)
    102108    try:
    103109        object = mod.get_object(**lookup_kwargs)
    104110    except ObjectDoesNotExist:
  • views/generic/create_update.py

     
    9292        lookup_kwargs['%s__exact' % slug_field] = slug
    9393    else:
    9494        raise AttributeError("Generic edit view must be called with either an object_id or a slug/slug_field")
    95     lookup_kwargs.update(extra_lookup_kwargs)
     95    if callable(extra_lookup_kwargs):
     96        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     97    else:
     98        lookup_kwargs.update(extra_lookup_kwargs)
    9699    try:
    97100        object = mod.get_object(**lookup_kwargs)
    98101    except ObjectDoesNotExist:
     
    168171        lookup_kwargs['%s__exact' % slug_field] = slug
    169172    else:
    170173        raise AttributeError("Generic delete view must be called with either an object_id or a slug/slug_field")
    171     lookup_kwargs.update(extra_lookup_kwargs)
     174    if callable(extra_lookup_kwargs):
     175        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     176    else:
     177        lookup_kwargs.update(extra_lookup_kwargs)
    172178    try:
    173179        object = mod.get_object(**lookup_kwargs)
    174180    except ObjectDoesNotExist:
Back to Top