Allow to override a navbar link for each language

ref issue #1
This commit is contained in:
Rodolphe Breard 2018-02-25 16:15:29 +01:00
parent bd0c8ea904
commit 8862f67777
3 changed files with 33 additions and 4 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 2.0.2 on 2018-02-25 13:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('navbar', '0003_auto_20180204_2035'),
]
operations = [
migrations.AddField(
model_name='elementdescription',
name='link_override',
field=models.CharField(blank=True, max_length=512),
),
]

View file

@ -37,11 +37,21 @@ class Element(models.Model):
) )
def description(self): def description(self):
lang = get_language()
if lang is None:
lang = settings.LANGUAGE_CODE
return ElementDescription.objects.filter( return ElementDescription.objects.filter(
element=self.id, element=self.id,
language=get_language() language=lang
).first() ).first()
def localized_link(self):
link = self.link
desc = self.description()
if desc.link_override:
link = desc.link_override
return link
def __str__(self): def __str__(self):
desc = self.description() desc = self.description()
return desc.short_name if desc is not None else self.link return desc.short_name if desc is not None else self.link
@ -53,6 +63,7 @@ class ElementDescription(models.Model):
short_name = models.CharField(max_length=32) short_name = models.CharField(max_length=32)
full_name = models.CharField(max_length=64, blank=True) full_name = models.CharField(max_length=64, blank=True)
description = models.CharField(max_length=512, blank=True) description = models.CharField(max_length=512, blank=True)
link_override = models.CharField(max_length=512, blank=True)
def is_separator(self): def is_separator(self):
return False return False

View file

@ -8,9 +8,9 @@
<div class="collapse navbar-collapse" id="navbarNav"> <div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav"> <ul class="navbar-nav">
{% for e in elems %} {% for e in elems %}
{% if e.link %} {% if e.localized_link %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{{ e.link }}"{% if e.new_window %} target="_blank"{% endif %}>{{ e }}</a> <a class="nav-link" href="{{ e.localized_link }}"{% if e.new_window %} target="_blank"{% endif %}>{{ e }}</a>
</li> </li>
{% elif e.children %} {% elif e.children %}
<li class="nav-item dropdown"> <li class="nav-item dropdown">
@ -22,7 +22,7 @@
{% if c.is_separator %} {% if c.is_separator %}
<hr /> <hr />
{% else %} {% else %}
<a class="dropdown-item" href="{% if c.add_locale %}/{{ current_lang_code }}{% endif %}{{ c.link }}"{% if c.new_window %} target="_blank"{% endif %}{% if c.description.full_name or c.description.description %} data-toggle="tooltip" data-placement="right" data-html="true" title="{% if c.description.full_name %}<b>{{ c.description.full_name }}</b>{% endif %}{% if c.description.full_name and c.description.description %}<hr />{% endif %}{{ c.description.description }}"{% endif %}> <a class="dropdown-item" href="{% if c.add_locale %}/{{ current_lang_code }}{% endif %}{{ c.localized_link }}"{% if c.new_window %} target="_blank"{% endif %}{% if c.description.full_name or c.description.description %} data-toggle="tooltip" data-placement="right" data-html="true" title="{% if c.description.full_name %}<b>{{ c.description.full_name }}</b>{% endif %}{% if c.description.full_name and c.description.description %}<hr />{% endif %}{{ c.description.description }}"{% endif %}>
<img src="{% static c.icon_path %}" alt="" /> {{ c }} <img src="{% static c.icon_path %}" alt="" /> {{ c }}
</a> </a>
{% endif %} {% endif %}