Skip to content

Commit

Permalink
Moved path to category code into its own function to make reuse easier.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgecho authored and coordt committed May 8, 2011
1 parent 1070cb7 commit 0b20115
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions categories/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,23 @@ def category_detail(request, path,
return HttpResponse(select_template(templates).render(context))


def get_category_for_path(path, queryset=Category.objects.all()):
path_items = path.strip('/').split('/')
if len(path_items) >= 2:
queryset = queryset.filter(
slug__iexact = path_items[-1],
level = len(path_items)-1,
parent__slug__iexact=path_items[-2])
else:
queryset = queryset.filter(
slug__iexact = path_items[-1],
level = len(path_items)-1)
return queryset.get()


import django
if django.VERSION[0] >= 1 and django.VERSION[1] >= 3:
from django.views.generic import DetailView
from django.views.generic import DetailView, ListView

class CategoryDetailView(DetailView):

Expand All @@ -48,30 +62,18 @@ def get_object(self, **kwargs):
u"a %s." % self.__class__.__name__, self.path_field)
if self.queryset is None:
queryset = self.get_queryset()

self.path_items = self.kwargs[self.path_field].strip('/').split('/')
if len(self.path_items) >= 2:
queryset = queryset.filter(
slug__iexact = self.path_items[-1],
level = len(self.path_items)-1,
parent__slug__iexact=self.path_items[-2])
else:
queryset = queryset.filter(
slug__iexact = self.path_items[-1],
level = len(self.path_items)-1)

try:
obj = queryset.get()
return get_category_for_path(self.kwargs[self.path_field])
except ObjectDoesNotExist:
raise Http404(_(u"No %(verbose_name)s found matching the query") %
{'verbose_name': queryset.model._meta.verbose_name})
return obj

def get_template_names(self):
names = []
path_items = self.path_items
path_items = self.kwargs[self.path_field].strip('/').split('/')
while path_items:
names.append('categories/%s.html' % '_'.join(path_items))
path_items.pop()
names.extend(super(CategoryDetailView, self).get_template_names())
return names
return names

0 comments on commit 0b20115

Please sign in to comment.