manager: optimize Page.get_as_reordered_flat_hierarchy (#21798)

This commit is contained in:
Frédéric Péters 2018-02-12 15:58:27 +01:00
parent be8d736860
commit c680590d44
1 changed files with 7 additions and 2 deletions

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import collections
import copy
import feedparser
import hashlib
@ -239,13 +240,17 @@ class Page(models.Model):
@classmethod
def get_as_reordered_flat_hierarchy(cls, object_list):
reordered = []
parenting = collections.defaultdict(list)
for page in object_list:
parenting[page.parent_id].append(page)
def fill_list(object_sublist, level=0, parent=None):
for page in object_sublist:
page._children = [x for x in object_list if x.parent_id == page.id]
page._children = parenting.get(page.id)
if page.parent == parent:
page.level = level
reordered.append(page)
fill_list(object_sublist, level=level+1, parent=page)
if page.id in parenting:
fill_list(object_sublist, level=level+1, parent=page)
fill_list(object_list)
return reordered