Skip to content

Commit

Permalink
Added a view that list items of specific model that are related to th…
Browse files Browse the repository at this point in the history
…e current category.
  • Loading branch information
tgecho authored and coordt committed May 8, 2011
1 parent 0b20115 commit c89c954
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions categories/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,31 @@ def get_template_names(self):
names.extend(super(CategoryDetailView, self).get_template_names())
return names


class CategoryRelatedList(ListView):

path_field = 'category_path'

def get_queryset(self):
queryset = super(CategoryRelatedList, self).get_queryset()
category = get_category_for_path(self.kwargs['category_path'])
return queryset.filter(category=category)

def get_template_names(self):
names = []
if hasattr(self.object_list, 'model'):
opts = self.object_list.model._meta
path_items = self.kwargs[self.path_field].strip('/').split('/')
while path_items:
names.append( '%s/category_%s_%s%s.html' % (opts.app_label,
'_'.join(path_items),
opts.object_name.lower(),
self.template_name_suffix)
)
path_items.pop()
names.append('%s/category_%s%s.html' % (opts.app_label,
opts.object_name.lower(),
self.template_name_suffix)
)
names.extend(super(CategoryRelatedList, self).get_template_names())
return names

0 comments on commit c89c954

Please sign in to comment.