Completed
Push — master ( 8081d9...05c9ef )
by Bart
23:30 queued 23:30
created

sidebar()   F

Complexity

Conditions 10

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
dl 0
loc 47
rs 3.4285
c 1
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like sidebar() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import logging
2
3
from django.contrib.sites.models import Site
4
5
from mezzanine.conf import settings
6
7
from website.jdpages.views import get_homepage_header
8
from website.jdpages.models import Footer
9
10
logger = logging.getLogger(__name__)
11
12
13
def site_properties(request):
14
    """ :returns: basic site properties """
15
    if not hasattr(request, '__main_site_url'):
16
        try:
17
            dom = Site.objects.values_list('domain', flat=True).get(pk=1)
18
            request.__main_site_url = 'http://' + dom
19
        except Site.DoesNotExist:
20
            return {}
21
22
    footer = None
23
    if Footer.objects.exists():
24
        footer = Footer.objects.all()[0]
25
26
    properties = {
27
        "site_tagline": settings.SITE_TAGLINE,
28
        "main_site_url": request.__main_site_url,
29
        "footer": footer
30
    }
31
    return properties
32
33
34
def piwik(request):
35
    """ :returns: the the Piwik analytics URL and SITE_ID """
36
    if hasattr(settings, 'PIWIK_URL') and settings.PIWIK_URL != '':
37
        return {"piwik_url": settings.PIWIK_URL, "piwik_site_id": settings.PIWIK_SITE_ID}
38
    else:
39
        return {}
40
41
42
def homepage_header(request):
43
    if not hasattr(request, '__homepage_header'):
44
        request.__homepage_header = get_homepage_header()
45
    return {"homepage_header": request.__homepage_header}
46