Ticket #450: dynamic_generics.diff

File dynamic_generics.diff, 6.5 KB (added by slashzero, 10 years ago)

patch to do dynamic keyword lookups

  • generic/date_based.py

     
    22from django.core.exceptions import Http404, ObjectDoesNotExist
    33from django.core.extensions import DjangoContext as Context
    44from django.core.xheaders import populate_xheaders
     5from django.views.generic import get_dynamic_lookup
    56from django.models import get_module
    67from django.utils.httpwrappers import HttpResponse
    78import datetime, time
    8 
     9           
    910def archive_index(request, app_label, module_name, date_field, num_latest=15,
    10                   template_name=None, extra_lookup_kwargs={}, extra_context={}):
     11                  template_name=None, extra_lookup_kwargs={}, extra_context={}, **keywds):
    1112    """
    1213    Generic top-level archive of date-based objects.
    1314
     
    2122    mod = get_module(app_label, module_name)
    2223    lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()}
    2324    lookup_kwargs.update(extra_lookup_kwargs)
     25    lookup_kwargs.update(get_dynamic_lookup(keywds))
     26   
    2427    date_list = getattr(mod, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1]
    2528    if not date_list:
    2629        raise Http404("No %s.%s available" % (app_label, module_name))
     
    4952    return HttpResponse(t.render(c))
    5053
    5154def archive_year(request, year, app_label, module_name, date_field,
    52                  template_name=None, extra_lookup_kwargs={}, extra_context={}):
     55                 template_name=None, extra_lookup_kwargs={}, extra_context={},**keywds):
    5356    """
    5457    Generic yearly archive view.
    5558
     
    6770    if int(year) >= now.year:
    6871        lookup_kwargs['%s__lte' % date_field] = now
    6972    lookup_kwargs.update(extra_lookup_kwargs)
     73    lookup_kwargs.update(get_dynamic_lookup(keywds))
     74   
    7075    date_list = getattr(mod, "get_%s_list" % date_field)('month', **lookup_kwargs)
    7176    if not date_list:
    7277        raise Http404
     
    8691
    8792def archive_month(request, year, month, app_label, module_name, date_field,
    8893                  month_format='%b', template_name=None, extra_lookup_kwargs={},
    89                   extra_context={}):
     94                  extra_context={},**keywds):
    9095    """
    9196    Generic monthly archive view.
    9297
     
    119124    if last_day >= now.date():
    120125        lookup_kwargs['%s__lte' % date_field] = now
    121126    lookup_kwargs.update(extra_lookup_kwargs)
     127    lookup_kwargs.update(get_dynamic_lookup(keywds))
    122128    object_list = mod.get_list(**lookup_kwargs)
    123129    if not object_list:
    124130        raise Http404
     
    138144
    139145def archive_day(request, year, month, day, app_label, module_name, date_field,
    140146                month_format='%b', day_format='%d', template_name=None,
    141                 extra_lookup_kwargs={}, extra_context={}, allow_empty=False):
     147                extra_lookup_kwargs={}, extra_context={}, allow_empty=False,**keywds):
    142148    """
    143149    Generic daily archive view.
    144150
     
    167173    if date >= now.date():
    168174        lookup_kwargs['%s__lte' % date_field] = now
    169175    lookup_kwargs.update(extra_lookup_kwargs)
     176    lookup_kwargs.update(get_dynamic_lookup(keywds))
    170177    object_list = mod.get_list(**lookup_kwargs)
    171178    if not allow_empty and not object_list:
    172179        raise Http404
     
    201208def object_detail(request, year, month, day, app_label, module_name, date_field,
    202209                  month_format='%b', day_format='%d', object_id=None, slug=None,
    203210                  slug_field=None, template_name=None, template_name_field=None,
    204                   extra_lookup_kwargs={}, extra_context={}):
     211                  extra_lookup_kwargs={}, extra_context={},**keywds):
    205212    """
    206213    Generic detail view from year/month/day/slug or year/month/day/id structure.
    207214
     
    230237    else:
    231238        raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield")
    232239    lookup_kwargs.update(extra_lookup_kwargs)
     240    lookup_kwargs.update(get_dynamic_lookup(keywds))   
    233241    try:
    234242        object = mod.get_object(**lookup_kwargs)
    235243    except ObjectDoesNotExist:
  • generic/list_detail.py

     
    55from django.core.extensions import DjangoContext as Context
    66from django.core.paginator import ObjectPaginator, InvalidPage
    77from django.core.exceptions import Http404, ObjectDoesNotExist
     8from django.views.generic import get_dynamic_lookup
    89
    910def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False,
    10                 template_name=None, extra_lookup_kwargs={}, extra_context={}):
     11                template_name=None, extra_lookup_kwargs={}, extra_context={},**kwargs):
    1112    """
    1213    Generic list of objects.
    1314
     
    3435    """
    3536    mod = models.get_module(app_label, module_name)
    3637    lookup_kwargs = extra_lookup_kwargs.copy()
     38    lookup_kwargs.update(get_dynamic_lookup(keywds))
     39   
    3740    if paginate_by:
    3841        paginator = ObjectPaginator(mod, lookup_kwargs, paginate_by)
    3942        page = request.GET.get('page', 0)
     
    7376
    7477def object_detail(request, app_label, module_name, object_id=None, slug=None,
    7578                  slug_field=None, template_name=None, template_name_field=None,
    76                   extra_lookup_kwargs={}, extra_context={}):
     79                  extra_lookup_kwargs={}, extra_context={},**kwargs):
    7780    """
    7881    Generic list of objects.
    7982
     
    9194    else:
    9295        raise AttributeError("Generic detail view must be called with either an object_id or a slug/slug_field")
    9396    lookup_kwargs.update(extra_lookup_kwargs)
     97    lookup_kwargs.update(get_dynamic_lookup(keywds))
     98   
    9499    try:
    95100        object = mod.get_object(**lookup_kwargs)
    96101    except ObjectDoesNotExist:
  • generic/__init__.py

     
     1def get_dynamic_lookup(keywords):
     2   lookups = {}
     3   
     4   if keywords.has_key("dynamic_lookup_kwargs"):
     5       dynamic_lookup_kwargs = keywords["dynamic_lookup_kwargs"]
     6
     7       del keywords["dynamic_lookup_kwargs"]
     8
     9       for lookup, url_keyword in dynamic_lookup_kwargs.items():
     10           if keywords.has_key(url_keyword):
     11               lookups[lookup] = keywords[url_keyword]
     12           else:
     13               raise Exception, "dynamic variable %s, was needed but is not defined in the url pattern" % (url_keyword)
     14
     15   return lookups
Back to Top