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, limit_choices_to={'parent': None} ) link = models.CharField(max_length=512, blank=True) new_window = models.BooleanField(default=False) add_locale = 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 sorted( 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 ) 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) def is_separator(self): return False class ElementSeparator(models.Model): parent = models.ForeignKey( Element, on_delete=models.CASCADE, limit_choices_to={'parent': None} ) weight = models.PositiveSmallIntegerField() def is_separator(self): return True def __str__(self): return '{} ({})'.format(self.parent, self.weight)