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