Ticket #667: generic_extra_callbacks_r1263.patch

File generic_extra_callbacks_r1263.patch, 8.0 KB (added by Tom Tobin <korpios@…>, 19 years ago)

Patch against r1263 to implement ticket request (covers both extra_context and 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))
     
    4245        'date_list' : date_list,
    4346        'latest' : latest,
    4447    })
     48    if callable(extra_context):
     49        extra_context = extra_context(request, lookup_kwargs)
    4550    for key, value in extra_context.items():
    4651        if callable(value):
    4752            c[key] = value()
     
    6873    # Only bother to check current date if the year isn't in the past.
    6974    if int(year) >= now.year:
    7075        lookup_kwargs['%s__lte' % date_field] = now
    71     lookup_kwargs.update(extra_lookup_kwargs)
     76    if callable(extra_lookup_kwargs):
     77        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     78    else:
     79        lookup_kwargs.update(extra_lookup_kwargs)
    7280    date_list = getattr(mod, "get_%s_list" % date_field)('month', **lookup_kwargs)
    7381    if not date_list:
    7482        raise Http404
     
    7987        'date_list': date_list,
    8088        'year': year,
    8189    })
     90    if callable(extra_context):
     91        extra_context = extra_context(request, lookup_kwargs)
    8292    for key, value in extra_context.items():
    8393        if callable(value):
    8494            c[key] = value()
     
    120130    # Only bother to check current date if the month isn't in the past.
    121131    if last_day >= now.date():
    122132        lookup_kwargs['%s__lte' % date_field] = now
    123     lookup_kwargs.update(extra_lookup_kwargs)
     133    if callable(extra_lookup_kwargs):
     134        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     135    else:
     136        lookup_kwargs.update(extra_lookup_kwargs)
    124137    object_list = mod.get_list(**lookup_kwargs)
    125138    if not object_list:
    126139        raise Http404
     
    131144        'object_list': object_list,
    132145        'month': date,
    133146    })
     147    if callable(extra_context):
     148        extra_context = extra_context(request, lookup_kwargs)
    134149    for key, value in extra_context.items():
    135150        if callable(value):
    136151            c[key] = value()
     
    169184    # Only bother to check current date if the date isn't in the past.
    170185    if date >= now.date():
    171186        lookup_kwargs['%s__lte' % date_field] = now
    172     lookup_kwargs.update(extra_lookup_kwargs)
     187    if callable(extra_lookup_kwargs):
     188        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     189    else:
     190        lookup_kwargs.update(extra_lookup_kwargs)
    173191    object_list = mod.get_list(**lookup_kwargs)
    174192    if not allow_empty and not object_list:
    175193        raise Http404
     
    182200        'previous_day': date - datetime.timedelta(days=1),
    183201        'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,
    184202    })
     203    if callable(extra_context):
     204        extra_context = extra_context(request, lookup_kwargs)
    185205    for key, value in extra_context.items():
    186206        if callable(value):
    187207            c[key] = value()
     
    233253        lookup_kwargs['%s__exact' % slug_field] = slug
    234254    else:
    235255        raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield")
    236     lookup_kwargs.update(extra_lookup_kwargs)
     256    if callable(extra_lookup_kwargs):
     257        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     258    else:
     259        lookup_kwargs.update(extra_lookup_kwargs)
    237260    try:
    238261        object = mod.get_object(**lookup_kwargs)
    239262    except ObjectDoesNotExist:
     
    248271    c = Context(request, {
    249272        'object': object,
    250273    })
     274    if callable(extra_context):
     275        extra_context = extra_context(request, lookup_kwargs)
    251276    for key, value in extra_context.items():
    252277        if callable(value):
    253278            c[key] = value()
  • 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)
     
    6871        })
    6972        if len(object_list) == 0 and not allow_empty:
    7073            raise Http404
     74    if callable(extra_context):
     75        extra_context = extra_context(request, lookup_kwargs)
    7176    for key, value in extra_context.items():
    7277        if callable(value):
    7378            c[key] = value()
     
    98103        lookup_kwargs['%s__exact' % slug_field] = slug
    99104    else:
    100105        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)
     106    if callable(extra_lookup_kwargs):
     107        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     108    else:
     109        lookup_kwargs.update(extra_lookup_kwargs)
    102110    try:
    103111        object = mod.get_object(**lookup_kwargs)
    104112    except ObjectDoesNotExist:
     
    113121    c = Context(request, {
    114122        'object': object,
    115123    })
     124    if callable(extra_context):
     125        extra_context = extra_context(request, lookup_kwargs)
    116126    for key, value in extra_context.items():
    117127        if callable(value):
    118128            c[key] = value()
  • views/generic/create_update.py

     
    5858    c = Context(request, {
    5959        'form' : form,
    6060    })
     61    if callable(extra_context):
     62        extra_context = extra_context(request, {})
    6163    for key, value in extra_context.items():
    6264        if callable(value):
    6365            c[key] = value()
     
    9294        lookup_kwargs['%s__exact' % slug_field] = slug
    9395    else:
    9496        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)
     97    if callable(extra_lookup_kwargs):
     98        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     99    else:
     100        lookup_kwargs.update(extra_lookup_kwargs)
    96101    try:
    97102        object = mod.get_object(**lookup_kwargs)
    98103    except ObjectDoesNotExist:
     
    130135        'form' : form,
    131136        'object' : object,
    132137    })
     138    if callable(extra_context):
     139        extra_context = extra_context(request, lookup_kwargs)
    133140    for key, value in extra_context.items():
    134141        if callable(value):
    135142            c[key] = value()
     
    168175        lookup_kwargs['%s__exact' % slug_field] = slug
    169176    else:
    170177        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)
     178    if callable(extra_lookup_kwargs):
     179        lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs))
     180    else:
     181        lookup_kwargs.update(extra_lookup_kwargs)
    172182    try:
    173183        object = mod.get_object(**lookup_kwargs)
    174184    except ObjectDoesNotExist:
     
    186196        c = Context(request, {
    187197            'object' : object,
    188198        })
     199        if callable(extra_context):
     200            extra_context = extra_context(request, lookup_kwargs)
    189201        for key, value in extra_context.items():
    190202            if callable(value):
    191203                c[key] = value()
Back to Top