Optimized navigation site_root

This commit is contained in:
Oleg Lavrovsky 2020-02-18 16:51:34 +01:00
parent c2e209f17d
commit eecb2a3446
5 changed files with 27 additions and 22 deletions

View file

@ -6,12 +6,11 @@ from ..models.snippets import Contact, SocialContact
register = template.Library() register = template.Library()
def get_contacts(site_root): def get_contacts(the_site):
if not site_root: return {} if not the_site: return {}
site = site_root.get_site()
# Selected or default contact snippet # Selected or default contact snippet
top_contact = Contact.objects.filter(home_site=site) top_contact = Contact.objects.filter(home_site=the_site)
if top_contact.exists(): if top_contact.exists():
top_contact = top_contact.first() top_contact = top_contact.first()
else: else:
@ -22,7 +21,7 @@ def get_contacts(site_root):
top_contact = Contact.objects.first() top_contact = Contact.objects.first()
# Selected or default social contact snippet # Selected or default social contact snippet
social_contacts = SocialContact.objects.filter(home_site=site) social_contacts = SocialContact.objects.filter(home_site=the_site)
if social_contacts.exists(): if social_contacts.exists():
social_contacts = social_contacts.all() social_contacts = social_contacts.all()
else: else:
@ -38,24 +37,24 @@ def get_contacts(site_root):
# Contact information (footer) # Contact information (footer)
@register.inclusion_tag('tags/contact_info.html') @register.inclusion_tag('tags/contact_info.html')
def contact_info(site_root): def contact_info(the_site):
return get_contacts(site_root) return get_contacts(the_site)
# Contact form (footer) # Contact form (footer)
@register.inclusion_tag('tags/footer_form.html') @register.inclusion_tag('tags/footer_form.html')
def footer_form(site_root): def footer_form(the_site):
cc = get_contacts(site_root) cc = get_contacts(the_site)
if cc['contact']: if cc['contact']:
return { 'form': cc['contact'].contact_form } return { 'form': cc['contact'].contact_form }
return None return None
# Contact links (header) # Contact links (header)
@register.inclusion_tag('tags/contact_links.html') @register.inclusion_tag('tags/contact_links.html')
def contact_links(site_root): def contact_links(the_site):
return get_contacts(site_root) return get_contacts(the_site)
# Styled contact name (header) # Styled contact name (header)
@register.inclusion_tag('tags/contact_name.html') @register.inclusion_tag('tags/contact_name.html')
def contact_name(site_root, html=True): def contact_name(the_site, html=True):
contactname = get_contacts(site_root)['contact'] contactname = get_contacts(the_site)['contact']
return { 'contact': contactname, 'html': html } return { 'contact': contactname, 'html': html }

View file

@ -33,11 +33,15 @@ def language_switcher(context):
'request': context['request'], 'request': context['request'],
} }
@register.simple_tag(takes_context=True)
def get_site(context):
return context['request'].site
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def get_site_root(context): def get_site_root(context):
# NB this returns a core.Page, not the implementation-specific model used # NB this returns a core.Page, not the implementation-specific model used
# so object-comparison to self will return false as objects would differ # so object-comparison to self will return false as objects would differ
return context['request'].site.root_page return get_site(context).root_page
def has_menu_children(page): def has_menu_children(page):
return page.get_children().live().in_menu().exists() return page.get_children().live().in_menu().exists()

View file

@ -1,11 +1,12 @@
{% load compress static wagtailuserbar navigation information %} {% load compress static wagtailuserbar navigation information %}
{% get_site_root as site_root %} {% get_site_root as site_root %}
{% get_site as the_site %}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="{% language_cur %}"> <html lang="{% language_cur %}">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{% block title %}{% if self.seo_title %}{{ self.seo_title }}{% else %}{% if page.trans_title %}{{ page.trans_title }} - {% endif %}{% contact_name site_root=site_root html=False %}{% endif %}{% endblock %}{% block title_suffix %}{% endblock %}</title> <title>{% block title %}{% if self.seo_title %}{{ self.seo_title }}{% else %}{% if page.trans_title %}{{ page.trans_title }} - {% endif %}{% contact_name the_site=the_site html=False %}{% endif %}{% endblock %}{% block title_suffix %}{% endblock %}</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="{% block description %}{% endblock %}"> <meta name="description" content="{% block description %}{% endblock %}">

View file

@ -1,5 +1,5 @@
{% load static wagtailcore_tags navigation information %} {% load static wagtailcore_tags navigation information %}
{% get_site_root as site_root %} {% get_site as the_site %}
<!-- Footer --> <!-- Footer -->
<footer id="footer"> <footer id="footer">
@ -20,18 +20,18 @@
<a name="contact-info"></a> <a name="contact-info"></a>
<div class="col-md-4" id="contact-info"> <div class="col-md-4" id="contact-info">
{% contact_info site_root=site_root %} {% contact_info the_site=the_site %}
</div> </div>
<div class="col-md-4" id="contact-form"> <div class="col-md-4" id="contact-form">
{% footer_form site_root=site_root %} {% footer_form the_site=the_site %}
</div> </div>
</div><!-- /row --> </div><!-- /row -->
</div><!-- /container --> </div><!-- /container -->
</footer> </footer>
<div class="copyright"> <div class="copyright">
{% contact_name site_root=site_root %} {% contact_name the_site=the_site %}
&copy; 2019 &copy; 2020
&bull; <a href="/privacy/">Privacy</a> &bull; <a href="/privacy/">Privacy</a>
&bull; <a href="/impressum/">Impressum</a> &bull; <a href="/impressum/">Impressum</a>
</div> </div>

View file

@ -1,5 +1,6 @@
{% load static wagtailcore_tags navigation information %} {% load static wagtailcore_tags navigation information %}
{% get_site_root as site_root %} {% get_site_root as site_root %}
{% get_site as the_site %}
<nav class="navbar-pre navbar-fixed-top"> <nav class="navbar-pre navbar-fixed-top">
<div class="container"> <div class="container">
@ -9,7 +10,7 @@
<span class="glyphicon glyphicon-user" aria-hidden="true"></span> <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
<span class="sr-only">Contact</span> <span class="sr-only">Contact</span>
</a> </a>
{% contact_links site_root=site_root %} {% contact_links the_site=the_site %}
</span> </span>
<span class="language-nav"> <span class="language-nav">
{% language_switcher %} {% language_switcher %}
@ -28,7 +29,7 @@
</button> </button>
<a class="navbar-brand" href="{% pageurl site_root %}"> <a class="navbar-brand" href="{% pageurl site_root %}">
<img src="{% static 'images/public-health-logo-sign.png' %}" alt="[logo]"> <img src="{% static 'images/public-health-logo-sign.png' %}" alt="[logo]">
<span class="hidden-xs">{% contact_name site_root=site_root %}</span></a> <span class="hidden-xs">{% contact_name the_site=the_site %}</span></a>
</div> </div>
{% block menu %} {% block menu %}