From f76e811403110b592d904aa3a4d7b5e876a75ed1 Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Thu, 4 Oct 2018 08:43:02 +1300 Subject: [PATCH] Improve handling of invalid branch in layer CSV export view On layers.openembedded.org we're seeing requests from some search engine crawlers requesting the CSV export URL with an invalid branch for the layer. I couldn't see the referer anywhere in the logs but I suspect it has to do with some recent cleanup work I did in the database where I deleted some invalid LayerBranch records - they were probably following links in a cached version of the webpage. In any event we want to return 404 in this situation rather than an internal server error. Signed-off-by: Paul Eggleton --- layerindex/views.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/layerindex/views.py b/layerindex/views.py index 5ac7ffa..af6ad99 100644 --- a/layerindex/views.py +++ b/layerindex/views.py @@ -1323,6 +1323,8 @@ def layer_export_recipes_csv_view(request, branch, slug): import csv layer = get_object_or_404(LayerItem, name=slug) layerbranch = layer.get_layerbranch(branch) + if not layerbranch: + raise Http404 response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="recipes_%s_%s.csv"' % (layer.name, layerbranch.branch.name)