Sfoglia il codice sorgente

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 11 anni fa
parent
commit
88a5ce77a0

+ 5
- 0
docs/source/releases/v0.8.rst Vedi File

330
   expected. There's currently no frontend or dashboard support for it, as there
330
   expected. There's currently no frontend or dashboard support for it, as there
331
   is no good default behaviour.
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
 .. _rewritten: https://github.com/tangentlabs/django-oscar/commit/d8b4dbfed17be90846ea4bc47b5f7b39ad944c24
338
 .. _rewritten: https://github.com/tangentlabs/django-oscar/commit/d8b4dbfed17be90846ea4bc47b5f7b39ad944c24
334
 
339
 
335
 Basket line stockrecords
340
 Basket line stockrecords

+ 15
- 5
oscar/apps/catalogue/abstract_models.py Vedi File

149
         reloaded_self.update_slug()
149
         reloaded_self.update_slug()
150
         reloaded_self.update_children_slugs()
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
     def get_absolute_url(self):
168
     def get_absolute_url(self):
159
         return reverse('catalogue:category',
169
         return reverse('catalogue:category',

+ 2
- 4
oscar/apps/catalogue/views.py Vedi File

121
             request, *args, **kwargs)
121
             request, *args, **kwargs)
122
 
122
 
123
     def get_queryset(self):
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
         # We use 'narrow' API to ensure Solr's 'fq' filtering is used as
125
         # We use 'narrow' API to ensure Solr's 'fq' filtering is used as
128
         # opposed to filtering using 'q'.
126
         # opposed to filtering using 'q'.
129
         pattern = ' OR '.join(['"%s"' % c.full_name for c in categories])
127
         pattern = ' OR '.join(['"%s"' % c.full_name for c in categories])
196
         """
194
         """
197
         Return a list of the current category and it's ancestors
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
     def get_summary(self):
199
     def get_summary(self):
202
         """
200
         """

+ 1
- 1
oscar/apps/dashboard/catalogue/views.py Vedi File

403
         ctx = super(CategoryDetailListView, self).get_context_data(*args,
403
         ctx = super(CategoryDetailListView, self).get_context_data(*args,
404
                                                                    **kwargs)
404
                                                                    **kwargs)
405
         ctx['child_categories'] = self.object.get_children()
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
         return ctx
407
         return ctx
408
 
408
 
409
 
409
 

+ 1
- 1
oscar/templates/oscar/catalogue/browse.html Vedi File

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

+ 1
- 1
oscar/templates/oscar/catalogue/category.html Vedi File

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

+ 1
- 1
oscar/templates/oscar/catalogue/detail.html Vedi File

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

+ 1
- 1
oscar/templates/oscar/catalogue/reviews/review_detail.html Vedi File

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

+ 1
- 1
oscar/templates/oscar/catalogue/reviews/review_list.html Vedi File

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

Loading…
Annulla
Salva