31 lines
1.3 KiB
Python
31 lines
1.3 KiB
Python
from django.utils.translation import get_language
|
|
from django.conf import settings
|
|
from django.db import models
|
|
import os
|
|
|
|
class Element(models.Model):
|
|
parent = models.ForeignKey('Element', on_delete=models.CASCADE, null=True, blank=True)
|
|
link = models.CharField(max_length=512, blank=True)
|
|
new_window = models.BooleanField(default=False)
|
|
icon = models.FilePathField(path=os.path.join(settings.BASE_DIR, 'navbar/static/icons'), match=".png", null=True, blank=True)
|
|
weight = models.PositiveSmallIntegerField()
|
|
|
|
def icon_path(self):
|
|
return os.path.join('icons', os.path.basename(self.icon))
|
|
|
|
def children(self):
|
|
return Element.objects.filter(parent=self.id).order_by('weight')
|
|
|
|
def description(self):
|
|
return ElementDescription.objects.filter(element=self.id, language=get_language()).first()
|
|
|
|
def __str__(self):
|
|
desc = self.description()
|
|
return desc.short_name if desc is not None else self.link
|
|
|
|
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)
|