|
35 | 35 | from django.db.models import Q
|
36 | 36 | from django.db.models import TextField
|
37 | 37 | from django.db.models.functions import Cast
|
| 38 | +from django.db.models.functions import Lower |
38 | 39 | from django.forms import model_to_dict
|
39 | 40 | from django.urls import reverse
|
40 | 41 | from django.utils import timezone
|
@@ -1160,12 +1161,35 @@ def children(self, codebase=None):
|
1160 | 1161 | Return a QuerySet of direct children CodebaseResource objects using a
|
1161 | 1162 | Database query on this CodebaseResource `path`.
|
1162 | 1163 |
|
| 1164 | + Paths are returned in lower-cased sorted path order to reflect the |
| 1165 | + behavior of `commoncode.resource.Resource.children()` |
| 1166 | + https://github.com/nexB/commoncode/blob/76a03d9c1cd2a582dcec4351c768c3ef646e1b31/src/commoncode/resource.py#L1199 |
| 1167 | +
|
1163 | 1168 | `codebase` is not used in this context but required for compatibility
|
1164 | 1169 | with the commoncode.resource.VirtualCodebase class API.
|
1165 | 1170 | """
|
1166 | 1171 | exactly_one_sub_directory = "[^/]+$"
|
1167 | 1172 | children_regex = rf"^{self.path}/{exactly_one_sub_directory}"
|
1168 |
| - return self.descendants().filter(path__regex=children_regex) |
| 1173 | + return ( |
| 1174 | + self.descendants() |
| 1175 | + .filter(path__regex=children_regex) |
| 1176 | + .order_by(Lower("path")) |
| 1177 | + ) |
| 1178 | + |
| 1179 | + def walk(self, topdown=True): |
| 1180 | + """ |
| 1181 | + Yield all descendant Resources of this Resource. Does not include self. |
| 1182 | +
|
| 1183 | + Walk the tree top-down, depth-first if `topdown` is True, otherwise walk |
| 1184 | + bottom-up. |
| 1185 | + """ |
| 1186 | + for child in self.children().iterator(): |
| 1187 | + if topdown: |
| 1188 | + yield child |
| 1189 | + for subchild in child.walk(topdown=topdown): |
| 1190 | + yield subchild |
| 1191 | + if not topdown: |
| 1192 | + yield child |
1169 | 1193 |
|
1170 | 1194 | def get_absolute_url(self):
|
1171 | 1195 | return reverse("resource_detail", args=[self.project_id, self.pk])
|
|
0 commit comments