Browse Source

fix(context): exclu les pages déjà épinglées; fix #62

master
François Poulain 10 months ago
parent
commit
936921b4dc
2 changed files with 27 additions and 1 deletions
  1. +6
    -1
      biohdf/base/models.py
  2. +21
    -0
      biohdf/base/utils.py

+ 6
- 1
biohdf/base/models.py View File

@@ -289,7 +289,12 @@ class SitePage(mixins.SetSiteTheme, Page):
def get_context(self, request):
context = super().get_context(request)

excluded = [self.id]
if hasattr(self, 'contenu4') and self.contenu4:
excluded = utils.get_streamfield_pined(self.contenu4) + [self.id]
elif hasattr(self, 'contenu') and self.contenu:
excluded = utils.get_streamfield_pined(self.contenu) + [self.id]
else:
excluded = [self.id]

for idx, key in [
[self.get_current_actualite_index(), 'last_articles'],

+ 21
- 0
biohdf/base/utils.py View File

@@ -1,6 +1,7 @@
import re

from django.utils.html import strip_tags
from wagtail.core.blocks.stream_block import StreamValue


def resume(x):
@@ -20,3 +21,23 @@ def resume(x):
def streamfield_to_resume(stream):
s = " | ".join([resume(stream.stream_data) for x in stream])
return re.sub(r'\s+', ' ', strip_tags(s)).strip()


def get_streamfield_pined(x):
'''
Prend des datas StreamField et en extrait la liste des pages épinglées.
'''
if isinstance(x, StreamValue):
return get_streamfield_pined(x.stream_data)
elif isinstance(x, list):
return [
item for sublist in
[get_streamfield_pined(item) for item in x]
for item in sublist
]
elif not isinstance(x, dict):
return []
elif x.get('type', None) == 'page':
return [x['value']['page']]
else:
return get_streamfield_pined([v for v in x.values()])

Loading…
Cancel
Save