Procházet zdrojové kódy

Drop custom get_ancestors() & introduce get_[ancestors|descendants]_and_itself()

Templates need access to both ancestors and descendants of a
category, and ideally both including the category itself or not.
Previously, get_ancestors used to default to including the category
itself, which required slicing in templates. Also, get_ancestors is a
treebeard function and I wouldn't bet that it doesn't interact with
treebeard logic.

Now, the two treebeard functions get_ancestors and get_descendants
return a queryset, and get_[ancestors|descendants]_and_itself return a
list that includes the category itself.
master
Maik Hoepfel před 11 roky
rodič
revize
88a5ce77a0

+ 5
- 0
docs/source/releases/v0.8.rst Zobrazit soubor

@@ -330,6 +330,11 @@ Misc
330 330
   expected. There's currently no frontend or dashboard support for it, as there
331 331
   is no good default behaviour.
332 332
 
333
+* ``Category.get_ancestors`` used to return a list of ancestors and would
334
+  default to include itself. For consistency with get_descendants and to avoid
335
+  having to slice the results in templates, it now returns a queryset of the
336
+  ancestors; use ``Category.get_ancestors_and_itself`` for the old behaviour.
337
+
333 338
 .. _rewritten: https://github.com/tangentlabs/django-oscar/commit/d8b4dbfed17be90846ea4bc47b5f7b39ad944c24
334 339
 
335 340
 Basket line stockrecords

+ 15
- 5
oscar/apps/catalogue/abstract_models.py Zobrazit soubor

@@ -149,11 +149,21 @@ class AbstractCategory(MP_Node):
149 149
         reloaded_self.update_slug()
150 150
         reloaded_self.update_children_slugs()
151 151
 
152
-    def get_ancestors(self, include_self=True):
153
-        ancestors = list(super(AbstractCategory, self).get_ancestors())
154
-        if include_self:
155
-            ancestors.append(self)
156
-        return ancestors
152
+    def get_ancestors_and_self(self):
153
+        """
154
+        Gets ancestors and includes itself. Use treebeard's get_ancestors
155
+        if you don't want to include the category itself. It's a separate
156
+        function as it's commonly used in templates.
157
+        """
158
+        return list(self.get_ancestors()) + [self]
159
+
160
+    def get_descendants_and_self(self):
161
+        """
162
+        Gets descendants and includes itself. Use treebeard's get_descendants
163
+        if you don't want to include the category itself. It's a separate
164
+        function as it's commonly used in templates.
165
+        """
166
+        return list(self.get_descendants()) + [self]
157 167
 
158 168
     def get_absolute_url(self):
159 169
         return reverse('catalogue:category',

+ 2
- 4
oscar/apps/catalogue/views.py Zobrazit soubor

@@ -121,9 +121,7 @@ class FacetedProductCategoryView(ListView):
121 121
             request, *args, **kwargs)
122 122
 
123 123
     def get_queryset(self):
124
-        # We build a list of this category and all descendents (TODO put this
125
-        # method onto the category itself as it's needed in a few places).
126
-        categories = list(self.category.get_descendants()) + [self.category]
124
+        categories = self.category.get_descendants_and_self()
127 125
         # We use 'narrow' API to ensure Solr's 'fq' filtering is used as
128 126
         # opposed to filtering using 'q'.
129 127
         pattern = ' OR '.join(['"%s"' % c.full_name for c in categories])
@@ -196,7 +194,7 @@ class ProductCategoryView(ListView):
196 194
         """
197 195
         Return a list of the current category and it's ancestors
198 196
         """
199
-        return list(self.category.get_descendants()) + [self.category]
197
+        return self.category.get_descendants_and_self()
200 198
 
201 199
     def get_summary(self):
202 200
         """

+ 1
- 1
oscar/apps/dashboard/catalogue/views.py Zobrazit soubor

@@ -403,7 +403,7 @@ class CategoryDetailListView(generic.DetailView):
403 403
         ctx = super(CategoryDetailListView, self).get_context_data(*args,
404 404
                                                                    **kwargs)
405 405
         ctx['child_categories'] = self.object.get_children()
406
-        ctx['ancestors'] = self.object.get_ancestors()
406
+        ctx['ancestors'] = self.object.get_ancestors_and_self()
407 407
         return ctx
408 408
 
409 409
 

+ 1
- 1
oscar/templates/oscar/catalogue/browse.html Zobrazit soubor

@@ -24,7 +24,7 @@
24 24
         {% if not category %}
25 25
             <li class="active">{% trans "All products" %}</li>
26 26
         {% else %}
27
-            {% for category in category.get_ancestors|slice:":-1" %}
27
+            {% for category in category.get_ancestors %}
28 28
                 <li>
29 29
                     <a href="{{ category.get_absolute_url }}">{{ category.name }}</a>
30 30
                     <span class="divider">/</span>

+ 1
- 1
oscar/templates/oscar/catalogue/category.html Zobrazit soubor

@@ -24,7 +24,7 @@
24 24
         {% if not category %}
25 25
             <li class="active">{% trans "All products" %}</li>
26 26
         {% else %}
27
-            {% for category in category.get_ancestors|slice:":-1" %}
27
+            {% for category in category.get_ancestors %}
28 28
                 <li>
29 29
                     <a href="{{ category.get_absolute_url }}">{{ category.name }}</a>
30 30
                     <span class="divider">/</span>

+ 1
- 1
oscar/templates/oscar/catalogue/detail.html Zobrazit soubor

@@ -24,7 +24,7 @@
24 24
         <span class="divider">/</span>
25 25
     </li>
26 26
     {% with category=product.categories.all.0 %}
27
-        {% for c in category.get_ancestors %}
27
+        {% for c in category.get_ancestors_and_self %}
28 28
         <li>
29 29
             <a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
30 30
             <span class="divider">/</span>

+ 1
- 1
oscar/templates/oscar/catalogue/reviews/review_detail.html Zobrazit soubor

@@ -12,7 +12,7 @@
12 12
             <span class="divider">/</span>
13 13
         </li>
14 14
         {% with category=product.categories.all.0 %}
15
-            {% for c in category.get_ancestors %}
15
+            {% for c in category.get_ancestors_and_self %}
16 16
                 <li>
17 17
                     <a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
18 18
                     <span class="divider">/</span>

+ 1
- 1
oscar/templates/oscar/catalogue/reviews/review_list.html Zobrazit soubor

@@ -14,7 +14,7 @@
14 14
             <span class="divider">/</span>
15 15
         </li>
16 16
         {% with category=product.categories.all.0 %}
17
-            {% for c in category.get_ancestors %}
17
+            {% for c in category.get_ancestors_and_self %}
18 18
                 <li>
19 19
                     <a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
20 20
                     <span class="divider">/</span>

Načítá se…
Zrušit
Uložit