Projet

Général

Profil

0002-general-create-page-hierarchy-beforehand-24238.patch

Frédéric Péters, 02 juillet 2018 08:30

Télécharger (7,14 ko)

Voir les différences:

Subject: [PATCH 2/2] general: create page hierarchy beforehand (#24238)

 combo/apps/momo/utils.py | 17 +++++------------
 combo/data/models.py     | 31 ++++++++++++++++++++++++++-----
 combo/public/menu.py     | 17 +++++++++++++----
 tests/test_public.py     |  3 ++-
 4 files changed, 46 insertions(+), 22 deletions(-)
combo/apps/momo/utils.py
95 95
        if page_dict.get('external') and icon_cells[0].embed_page:
96 96
            page_dict['external'] = False
97 97

  
98
    if hasattr(page, '_children') and page._children:
99
        children = page._children
98
    if page.slug == 'index' and page.parent_id is None: # home
99
        children = page.get_siblings()[1:]
100 100
    else:
101 101
        children = page.get_children()
102 102

  
......
143 143
    # - the application pages are created from the homepage siblings and their
144 144
    #   children
145 145
    # - the application menu is created from direct children of the homepage
146
    children = []
147
    homepage = None
148
    for page in level0_pages:
149
        if page.slug == 'index':
150
            homepage = page
151
        else:
152
            children.append(page)
153

  
154
    if not homepage:
146
    try:
147
        homepage = Page.objects.get(slug='index', parent_id=None)
148
    except Page.DoesNotExist:
155 149
        raise GenerationError(_('The homepage needs to be created first.'))
156 150

  
157
    homepage._children = children
158 151
    manifest.update(get_page_dict(request, homepage, manifest))
159 152

  
160 153
    # footer
combo/data/models.py
145 145
    related_cells = JSONField(blank=True)
146 146

  
147 147
    _level = None
148
    _children = None
149 148

  
150 149
    class Meta:
151 150
        ordering = ['order']
......
185 184
        pages = [self]
186 185
        page = self
187 186
        while page.parent_id:
188
            page = page.parent
187
            page = getattr(page, '_parent', page.parent)
189 188
            pages.append(page)
190 189
        return list(reversed(pages))
191 190

  
......
202 201
        parts = [self]
203 202
        page = self
204 203
        while page.parent_id:
205
            page = page.parent
204
            page = getattr(page, '_parent', page.parent)
206 205
            parts.append(page)
207 206
        parts.reverse()
208 207
        try:
......
211 210
            return None
212 211

  
213 212
    def get_siblings(self):
214
        return Page.objects.filter(parent=self.parent)
213
        if hasattr(self, '_parent'):
214
            if self._parent:
215
                return self._parent._children
216
        return Page.objects.filter(parent_id=self.parent_id)
215 217

  
216 218
    def get_children(self):
219
        if hasattr(self, '_children'):
220
            return self._children
217 221
        return Page.objects.filter(parent_id=self.id)
218 222

  
219 223
    def has_children(self):
224
        if hasattr(self, '_children'):
225
            return bool(self._children)
220 226
        return Page.objects.filter(parent_id=self.id).exists()
221 227

  
222 228
    def get_template_display_name(self):
......
282 288
            parenting[page.parent_id].append(page)
283 289
        def fill_list(object_sublist, level=0, parent=None):
284 290
            for page in object_sublist:
285
                page._children = parenting.get(page.id)
286 291
                if page.parent == parent:
287 292
                    page.level = level
288 293
                    reordered.append(page)
......
291 296
        fill_list(object_list)
292 297
        return reordered
293 298

  
299
    @classmethod
300
    def get_with_hierarchy_attributes(cls):
301
        pages = cls.objects.all()
302
        pages_by_id = {}
303
        for page in pages:
304
            pages_by_id[page.id] = page
305
            page._parent = None
306
            page._children = []
307
        for page in pages:
308
            page._parent = pages_by_id[page.parent_id] if page.parent_id else None
309
            if page._parent:
310
                page._parent._children.append(page)
311
        for page in pages:
312
            page._children.sort(key=lambda x: x.order)
313
        return pages_by_id
314

  
294 315
    def visibility(self):
295 316
        if self.public:
296 317
            return _('Public')
combo/public/menu.py
16 16

  
17 17
from django.template.loader import get_template
18 18

  
19
from combo.data.models import Page
20

  
21

  
19 22
def render_menu(context, level=0, root_page=None, depth=1):
20 23
    context['root_page'] = root_page
21 24
    if root_page:
......
25 28
    return template.render(context)
26 29

  
27 30
def get_menu_context(context, level=0, current_page=None, depth=1):
31
    if 'hierarchical_pages_by_id' not in context:
32
        context['hierarchical_pages_by_id'] = Page.get_with_hierarchy_attributes()
33
    pages_by_id = context['hierarchical_pages_by_id']
28 34
    context['depth'] = depth
29 35
    if current_page is None:
30
        current_page = context['page']
36
        current_page = pages_by_id.get(context['page'].id, context['page'])
37
    else:
38
        current_page = pages_by_id.get(current_page.id, current_page)
31 39

  
32 40
    if 'root_page' in context:
41
        root_page = pages_by_id[context['root_page'].id]
33 42
        # if the menu is anchored at a specific root page, we make sure the
34 43
        # current page is in the right path, otherwise we fall back to using
35 44
        # the root page as base.
36 45
        ariane = current_page.get_parents_and_self()
37
        if not context.get('root_page') in ariane:
38
            page_of_level = context['root_page']
46
        if not root_page in ariane:
47
            page_of_level = root_page
39 48
        else:
40 49
            page_of_level = current_page.get_page_of_level(level)
41 50
    else:
......
51 60
            context['menuitems'] = []
52 61
            return context
53 62
    else:
54
        if page_of_level is context.get('root_page'):
63
        if page_of_level == context.get('root_page'):
55 64
            elements = page_of_level.get_children()
56 65
        else:
57 66
            elements = page_of_level.get_siblings()
tests/test_public.py
116 116
        assert resp.body.count('BARFOO') == 1
117 117
        assert resp.body.count('BAR2FOO') == 1
118 118
        queries_count_fourth = len(ctx.captured_queries)
119
        assert queries_count_fourth == queries_count_second + 2
119
        # +2 for parent cells, +1 for get_parents_and_self()
120
        assert queries_count_fourth == queries_count_second + 3
120 121

  
121 122
    # check footer doesn't get duplicated in real index children
122 123
    page6 = Page(title='Sixth', slug='sixth', template_name='standard', parent=page_index)
123
-