|
37 | 37 | from django.core.exceptions import ValidationError |
38 | 38 | from django.core.files.storage.filesystem import FileSystemStorage |
39 | 39 | from django.core.serializers.json import DjangoJSONEncoder |
| 40 | +from django.db.models import Exists |
| 41 | +from django.db.models import OuterRef |
40 | 42 | from django.db.models import Prefetch |
41 | 43 | from django.db.models.manager import Manager |
42 | 44 | from django.http import FileResponse |
@@ -2567,3 +2569,35 @@ def get_node(self, package): |
2567 | 2569 | if children: |
2568 | 2570 | node["children"] = children |
2569 | 2571 | return node |
| 2572 | + |
| 2573 | + |
| 2574 | +class CodebaseResourceTreeView(ConditionalLoginRequired, generic.DetailView): |
| 2575 | + template_name = "scanpipe/resource_tree.html" |
| 2576 | + |
| 2577 | + def get(self, request, *args, **kwargs): |
| 2578 | + slug = self.kwargs.get("slug") |
| 2579 | + project = get_object_or_404(Project, slug=slug) |
| 2580 | + path = request.GET.get("path", None) |
| 2581 | + |
| 2582 | + base_qs = ( |
| 2583 | + CodebaseResource.objects.filter(project=project, parent_path=path) |
| 2584 | + .only("path", "name", "type") |
| 2585 | + .order_by("path") |
| 2586 | + ) |
| 2587 | + |
| 2588 | + subdirs = CodebaseResource.objects.filter( |
| 2589 | + project=project, |
| 2590 | + parent_path=OuterRef("path"), |
| 2591 | + ) |
| 2592 | + |
| 2593 | + children = base_qs.annotate(has_children=Exists(subdirs)) |
| 2594 | + |
| 2595 | + context = { |
| 2596 | + "project": project, |
| 2597 | + "path": path, |
| 2598 | + "children": children, |
| 2599 | + } |
| 2600 | + |
| 2601 | + if request.GET.get("tree") == "true": |
| 2602 | + return render(request, "scanpipe/panels/file_tree_panel.html", context) |
| 2603 | + return render(request, self.template_name, context) |
0 commit comments