from django import template

from blog.models import Sponsor, Footer

from wagtail.wagtailcore.models import Page

register = template.Library()


@register.inclusion_tag('lib/tags/sponsor_homepage.html', takes_context=True)
def sponsor_homepage(context):
    return {
        'sponsor': Sponsor.objects.filter(primary_sponsor=True).first(),
        'request': context['request'],
    }


@register.inclusion_tag('lib/tags/sponsor_sidebar.html', takes_context=True)
def sponsor_sidebar(context):
    return {
        'sponsor': Sponsor.objects.filter(primary_sponsor=True).first(),
        'request': context['request'],
    }


@register.assignment_tag(takes_context=True)
def get_site_root(context):
    # NB this returns a core.Page, not the implementation-specific model used
    # so object-comparison to self will return false as objects would differ
    return context['request'].site.root_page


# Retrieves the top menu items
@register.inclusion_tag('lib/tags/top_menu.html', takes_context=True)
def top_menu(context, parent, calling_page=None):
    menuitems = parent.get_children().live().in_menu()
    for menuitem in menuitems:
        # We don't directly check if calling_page is None since the template
        # engine can pass an empty string to calling_page
        # if the variable passed as calling_page does not exist.
        menuitem.active = (calling_page.startswith(menuitem.url)
                           if calling_page else False)
    return {
        'calling_page': calling_page,
        'menuitems': menuitems,
        'is_home': calling_page == u'/',
        # required by the pageurl tag that we want to use within this template
        'request': context['request'],
    }


# Retrieves the footer sitemap items
@register.inclusion_tag('lib/tags/footer.html', takes_context=True)
def footer(context, parent):
    return {
        'menuitems': parent.get_children().live().in_menu(),
        'facebook_url': Footer.objects.first().facebook_url,
        'twitch_url': Footer.objects.first().twitch_url,
        'twitter_url': Footer.objects.first().twitter_url,
        # required by the pageurl tag that we want to use within this template
        'request': context['request'],
    }


@register.inclusion_tag('lib/tags/breadcrumbs.html', takes_context=True)
def breadcrumbs(context):
    self = context.get('self')
    if self is None or self.depth <= 2:
        # When on the home page, displaying breadcrumbs is irrelevant.
        ancestors = ()
    else:
        ancestors = Page.objects.ancestor_of(
            self, inclusive=True).filter(depth__gt=2)
    return {
        'ancestors': ancestors,
        'request': context['request'],
    }

@@ -1834,24 +1834,59 @@
 t, parent):%0A
+    if Footer.objects.first():%0A    
     return %7B
@@ -1878,32 +1878,36 @@
        return %7B%0A
+    
         'menuite
@@ -1947,24 +1947,28 @@
 .in_menu(),%0A
+    
         'fac
@@ -2020,24 +2020,28 @@
 rl,%0A        
+    
 'twitch_url'
@@ -2081,24 +2081,28 @@
 rl,%0A        
+   
+ 
 'twitter_url
@@ -2136,24 +2136,28 @@
 witter_url,%0A
+    
         # re
@@ -2223,32 +2223,36 @@
 emplate%0A        
+  
+  
 'request': conte
@@ -2258,32 +2258,199 @@
 ext%5B'request'%5D,%0A
+        %7D%0A    else:%0A        return %7B%0A            # required by the pageurl tag that we want to use within this template%0A            'request': context%5B'request'%5D,%0A    
     %7D%0A%0A%0A@registe

Fix issue where footer fields weren't defined