forked from jazzband/django-categories
/
views.py
146 lines (125 loc) · 5.98 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import django
from django.shortcuts import get_object_or_404
from django.template import RequestContext
from django.http import HttpResponse, Http404
from django.views.decorators.cache import cache_page
from django.template.loader import select_template
from categories.models import Category
from settings import CACHE_VIEW_LENGTH
@cache_page(CACHE_VIEW_LENGTH)
def category_detail(request, path,
template_name='categories/category_detail.html', extra_context={}):
path_items = path.strip('/').split('/')
if len(path_items) >= 2:
category = get_object_or_404(Category,
slug__iexact = path_items[-1],
level = len(path_items)-1,
parent__slug__iexact=path_items[-2])
else:
category = get_object_or_404(Category,
slug__iexact = path_items[-1],
level = len(path_items)-1)
templates = []
while path_items:
templates.append('categories/%s.html' % '_'.join(path_items))
path_items.pop()
templates.append(template_name)
context = RequestContext(request)
context.update({'category':category})
if extra_context:
context.update(extra_context)
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()
try:
import cbv
HAS_CBV = True
except ImportError:
HAS_CBV = False
if ((django.VERSION[0] >= 1 and django.VERSION[1] >= 3) or HAS_CBV):
if HAS_CBV:
from cbv import DetailView, ListView
else:
from django.views.generic import DetailView, ListView
class CategoryDetailView(DetailView):
model = Category
path_field = 'path'
def get_object(self, **kwargs):
if self.path_field not in self.kwargs:
raise AttributeError(u"Category detail view %s must be called with "
u"a %s." % self.__class__.__name__, self.path_field)
if self.queryset is None:
queryset = self.get_queryset()
try:
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})
def get_template_names(self):
names = []
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
class CategoryRelatedDetail(DetailView):
path_field = 'category_path'
object_name_field = None
def get_object(self, **kwargs):
queryset = super(CategoryRelatedDetail, self).get_queryset()
category = get_category_for_path(self.kwargs[self.path_field])
return queryset.get(category=category,slug=self.kwargs[self.slug_field])
def get_template_names(self):
names = []
opts = self.object._meta
path_items = self.kwargs[self.path_field].strip('/').split('/')
if self.object_name_field:
path_items.append(getattr(self.object, self.object_name_field))
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(CategoryRelatedDetail, 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