Skip to contentSkip to conversation

layerindex/views: BranchCompareView: fix to_branch check

PATCH New Tim Orling 1 participant 3 messages Oct 17

lore
Share & tools
lore

Tim Orling

author
#0 lore raw

PATCH 1/3 layerindex/views: BranchCompareView: fix to_branch check

The to_branch lookup was guarded by from_branch_id instead of to_branch_id, so a request with only from_branch set tried to fetch a Branch with id None and returned a 404 instead of the empty form.

AI-Generated: Claude Opus 5.5

---
 layerindex/views.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
layerindex/views.py +1 -1
index 4f8e64e..73a4f6a 100644
@@ -1927,7 +1927,7 @@ class BranchCompareView(FormView):
             from_branch = get_object_or_404(Branch, id=from_branch_id)
         context['from_branch'] = from_branch
         to_branch = None
-        if from_branch_id is not None:
+        if to_branch_id is not None:
             to_branch = get_object_or_404(Branch, id=to_branch_id)
         context['to_branch'] = to_branch
         if from_branch and to_branch:
Signature · 1 line
2.56.0
author
#1 lore raw

PATCH 2/3 branchcompare.html: don't submit layer checkboxes

The selected layers are submitted via the hidden 'layers' field, which is filled in from the checkboxes when the layer dialog is closed. The checkboxes themselves were also named 'l', so every comparison URL carried a redundant l=<id> parameter for each layer that the view never used. Drop the name so only 'layers' is submitted.

AI-Generated: Claude Opus 5.5

---
 templates/layerindex/branchcompare.html | 1 -
 1 file changed, 1 deletion(-)
templates/layerindex/branchcompare.html +0 -1
index 83561fd..c9fe5f5 100644
@@ -45,7 +45,6 @@
                                                 <td class="checkboxtd"><input
                                                     type="checkbox"
                                                     class="filterlayercheckbox"
-                                                    name="l"
                                                     value="{{ layer.id }}" id="id_layercheckbox_{{layer.id}}"
                                                     {% if showlayers and layer.id in showlayers %}
                                                         checked
Signature · 1 line
2.56.0
author
#2 lore raw

PATCH 3/3 layerindex/views: BranchCompareView: parse layers as a list of ids

In reply to Tim Orling

The 'layers' parameter is a comma-separated list of layer ids, but the view used the raw string as the list and iterated over it one character at a time. As a result:

* layers=59 compared layers 5 and 9 instead of layer 59
* layers=59,1 tried to look up a layer with id ',' and raised a ValueError (Internal Server Error)
* the "Filter layers" badge showed the length of the string rather than the number of layers (e.g. 3 for "2,1")
* the selected layers were not pre-checked in the layer dialog, since integer ids were being compared against a string

Split the parameter into integer ids (also accepting it repeated, as in layers=59&layers=1), ignore anything that is not a published layer id, and use the cleaned list for the hidden form field, the badge and the text/rST/CSV output. Add tests covering these cases.

Fixes [YOCTO #15332]

AI-Generated: Claude Opus 5.5

---
 layerindex/views.py               |  19 +++++-
 tests/test_branch_compare_view.py | 109 ++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 3 deletions(-)
 create mode 100644 tests/test_branch_compare_view.py
layerindex/views.py +16 -3
index 73a4f6a..dea0a98 100644
@@ -1902,6 +1902,19 @@ class BranchCompareView(FormView):
                 changed.append(recipe)
         return added, changed, removed
 
+    def get_layer_ids(self):
+        # 'layers' is a comma-separated list of layer ids (also accept it
+        # repeated); ignore anything that isn't a selectable layer
+        valid_ids = set(LayerItem.objects.filter(status__in=['P', 'X']).values_list('id', flat=True))
+        values = self.request.GET.getlist('layers') or [str(LayerItem.objects.get(name=settings.CORE_LAYER_NAME).id)]
+        layer_ids = []
+        for value in values:
+            for layer_id in value.split(','):
+                layer_id = layer_id.strip()
+                if layer_id.isdigit() and int(layer_id) in valid_ids and int(layer_id) not in layer_ids:
+                    layer_ids.append(int(layer_id))
+        return layer_ids
+
     def form_valid(self, form):
         return HttpResponseRedirect(reverse_lazy('branch_comparison', args=(form.cleaned_data['from_branch'].name, form.cleaned_data['to_branch'].name)))
 
@@ -1913,7 +1926,7 @@ class BranchCompareView(FormView):
         to_branch_id = self.request.GET.get('to_branch', None)
         if to_branch_id is not None:
             initial['to_branch'] = get_object_or_404(Branch, id=to_branch_id)
-        initial['layers'] = self.request.GET.get('layers', str(LayerItem.objects.get(name=settings.CORE_LAYER_NAME).id))
+        initial['layers'] = ','.join(str(i) for i in self.get_layer_ids())
         return initial
 
     def get_context_data(self, **kwargs):
@@ -1921,7 +1934,7 @@ class BranchCompareView(FormView):
         from_branch_id = self.request.GET.get('from_branch', None)
         to_branch_id = self.request.GET.get('to_branch', None)
 
-        layer_ids = self.request.GET.get('layers', str(LayerItem.objects.get(name=settings.CORE_LAYER_NAME).id))
+        layer_ids = self.get_layer_ids()
         from_branch = None
         if from_branch_id is not None:
             from_branch = get_object_or_404(Branch, id=from_branch_id)
@@ -1936,7 +1949,7 @@ class BranchCompareView(FormView):
         context['layers'] = LayerItem.objects.filter(status__in=['P', 'X']).order_by('name')
         context['showlayers'] = layer_ids
         layerlist = dict(context['layers'].values_list('id', 'name'))
-        context['showlayers_text'] = ', '.join([layerlist[int(i)] for i in layer_ids])
+        context['showlayers_text'] = ', '.join([layerlist[i] for i in layer_ids])
 
         return context
 
tests/test_branch_compare_view.py new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.56.0