2018-01-21 12:44:00 +00:00
|
|
|
from django.utils.translation import get_language
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
|
|
|
import os
|
|
|
|
|
2018-01-27 22:12:09 +00:00
|
|
|
|
2018-01-21 12:44:00 +00:00
|
|
|
class Element(models.Model):
|
2018-01-27 22:12:09 +00:00
|
|
|
parent = models.ForeignKey(
|
2019-07-24 17:07:41 +00:00
|
|
|
"Element",
|
2018-01-27 22:12:09 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
2018-01-29 21:34:05 +00:00
|
|
|
blank=True,
|
2019-07-24 17:07:41 +00:00
|
|
|
limit_choices_to={"parent": None},
|
2018-01-27 22:12:09 +00:00
|
|
|
)
|
2018-01-21 12:44:00 +00:00
|
|
|
link = models.CharField(max_length=512, blank=True)
|
|
|
|
new_window = models.BooleanField(default=False)
|
2018-02-04 19:40:41 +00:00
|
|
|
add_locale = models.BooleanField(default=False)
|
2018-01-27 22:12:09 +00:00
|
|
|
icon = models.FilePathField(
|
2019-07-24 17:07:41 +00:00
|
|
|
path="navbar/static/navbar/icons", match=".png", null=True, blank=True
|
2018-01-27 22:12:09 +00:00
|
|
|
)
|
2018-01-21 12:44:00 +00:00
|
|
|
weight = models.PositiveSmallIntegerField()
|
|
|
|
|
|
|
|
def icon_path(self):
|
2019-07-24 17:07:41 +00:00
|
|
|
return os.path.join("navbar/icons", os.path.basename(self.icon))
|
2018-01-21 12:44:00 +00:00
|
|
|
|
|
|
|
def children(self):
|
2018-01-29 21:34:05 +00:00
|
|
|
return sorted(
|
2019-07-24 17:07:41 +00:00
|
|
|
list(Element.objects.filter(parent=self.id).order_by("weight"))
|
|
|
|
+ list(ElementSeparator.objects.filter(parent=self.id).order_by("weight")),
|
|
|
|
key=lambda c: c.weight,
|
2018-01-29 21:34:05 +00:00
|
|
|
)
|
2018-01-21 12:44:00 +00:00
|
|
|
|
|
|
|
def description(self):
|
2018-02-25 15:15:29 +00:00
|
|
|
lang = get_language()
|
|
|
|
if lang is None:
|
|
|
|
lang = settings.LANGUAGE_CODE
|
2019-07-24 17:07:41 +00:00
|
|
|
return ElementDescription.objects.filter(element=self.id, language=lang).first()
|
2018-01-21 12:44:00 +00:00
|
|
|
|
2018-02-25 15:15:29 +00:00
|
|
|
def localized_link(self):
|
|
|
|
link = self.link
|
|
|
|
desc = self.description()
|
2020-02-06 11:39:44 +00:00
|
|
|
if desc and desc.link_override:
|
2018-02-25 15:15:29 +00:00
|
|
|
link = desc.link_override
|
|
|
|
return link
|
|
|
|
|
2018-01-21 12:44:00 +00:00
|
|
|
def __str__(self):
|
|
|
|
desc = self.description()
|
|
|
|
return desc.short_name if desc is not None else self.link
|
|
|
|
|
2018-01-27 22:12:09 +00:00
|
|
|
|
2018-01-21 12:44:00 +00:00
|
|
|
class ElementDescription(models.Model):
|
|
|
|
element = models.ForeignKey(Element, on_delete=models.CASCADE)
|
|
|
|
language = models.CharField(max_length=10, choices=settings.LANGUAGES)
|
|
|
|
short_name = models.CharField(max_length=32)
|
|
|
|
full_name = models.CharField(max_length=64, blank=True)
|
|
|
|
description = models.CharField(max_length=512, blank=True)
|
2018-02-25 15:15:29 +00:00
|
|
|
link_override = models.CharField(max_length=512, blank=True)
|
2018-01-29 21:34:05 +00:00
|
|
|
|
|
|
|
def is_separator(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class ElementSeparator(models.Model):
|
|
|
|
parent = models.ForeignKey(
|
2019-07-24 17:07:41 +00:00
|
|
|
Element, on_delete=models.CASCADE, limit_choices_to={"parent": None}
|
2018-01-29 21:34:05 +00:00
|
|
|
)
|
|
|
|
weight = models.PositiveSmallIntegerField()
|
|
|
|
|
|
|
|
def is_separator(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-07-24 17:07:41 +00:00
|
|
|
return "{} ({})".format(self.parent, self.weight)
|