mirror of
https://github.com/jazzband/django-categories.git
synced 2026-03-16 22:30:24 +00:00
Moved path to category code into its own function to make reuse easier.
This commit is contained in:
parent
1070cb7dae
commit
0b201156e0
1 changed files with 19 additions and 17 deletions
|
|
@ -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):
|
||||
|
||||
|
|
@ -48,30 +62,18 @@ if django.VERSION[0] >= 1 and django.VERSION[1] >= 3:
|
|||
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue