get_page_header()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
dl 0
loc 28
rs 8.7413
c 1
b 0
f 0
1
import logging
2
3
from mezzanine.pages.models import RichTextPage
4
5
from fullcalendar.views import OccurrenceView
6
7
from website.jdpages.models import HomePage
8
from website.jdpages.models import SidebarSocial
9
from website.jdpages.models import PageHeaderImage
10
11
logger = logging.getLogger(__name__)
12
13
14
def get_homepage_id():
15
    homepage = HomePage.objects.values_list('id').first()
16
    if homepage is not None:
17
        return homepage[0]
18
    pages = RichTextPage.objects.filter(slug='/')
19
    if pages.exists():
20
        return pages[0].id
21
    return None
22
23
24
def get_page_header(page):
25
    """
26
    :returns: the page header for a given page.
27
    Uses parent page image when none is defined.
28
    Top parent is the HomePage.
29
    None when there is no Hompage or when the HomePage has no header.
30
    """
31
32
    # Get a random page header, or the first one if there is only one
33
    image = PageHeaderImage.objects.filter(page=page).order_by('?').first()
34
    if image is not None:
35
        return image
36
37
    # No image found! If we received an integer, then do not search further
38
    if isinstance(page, int):
39
        return None
40
41
    # Try from parent
42
    if page.parent:
43
        return get_page_header(page.parent)
44
45
    # Try from first homepage
46
    homepage = get_homepage_id()
47
    if homepage is not None:
48
        # Since we give get_page_header an integer, there is no infinite recursion here...
49
        return get_page_header(homepage)
50
51
    return None
52
53
54
def get_homepage_header():
55
    """ Returns the page header image of the homepage """
56
    homepage_id = get_homepage_id()
57
    if homepage_id is None:
58
        return None
59
    return get_page_header(homepage_id)
60
61
62
class OccuranceJDView(OccurrenceView):
63
64
    def get_context_data(self, **kwargs):
65
        context = super().get_context_data(**kwargs)
66
        context['page_for_sidebars'] = HomePage.objects.all().first()
67
        return context
68