From e0d26e8a7a3283e5e9c68f7fd389d6d4f29c7365 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Mon, 29 Jan 2018 22:34:05 +0100 Subject: [PATCH] Add separators within the navbar Implement #2 --- navbar/admin.py | 3 ++- navbar/migrations/0002_elementseparator.py | 22 ++++++++++++++++ navbar/models.py | 30 ++++++++++++++++++++-- navbar/templates/navbar/navbar.html | 4 +++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 navbar/migrations/0002_elementseparator.py diff --git a/navbar/admin.py b/navbar/admin.py index f64690a..1dd8f38 100644 --- a/navbar/admin.py +++ b/navbar/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from .models import Element, ElementDescription +from .models import Element, ElementDescription, ElementSeparator class ElementDescriptionInline(admin.StackedInline): @@ -12,3 +12,4 @@ class ElementAdmin(admin.ModelAdmin): admin.site.register(Element, ElementAdmin) +admin.site.register(ElementSeparator) diff --git a/navbar/migrations/0002_elementseparator.py b/navbar/migrations/0002_elementseparator.py new file mode 100644 index 0000000..3dfbe54 --- /dev/null +++ b/navbar/migrations/0002_elementseparator.py @@ -0,0 +1,22 @@ +# Generated by Django 2.0.1 on 2018-01-29 21:12 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('navbar', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='ElementSeparator', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('weight', models.PositiveSmallIntegerField()), + ('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='navbar.Element')), + ], + ), + ] diff --git a/navbar/models.py b/navbar/models.py index 07a7132..c0e72ca 100644 --- a/navbar/models.py +++ b/navbar/models.py @@ -9,7 +9,8 @@ class Element(models.Model): 'Element', on_delete=models.CASCADE, null=True, - blank=True + blank=True, + limit_choices_to={'parent': None} ) link = models.CharField(max_length=512, blank=True) new_window = models.BooleanField(default=False) @@ -25,7 +26,14 @@ class Element(models.Model): return os.path.join('icons', os.path.basename(self.icon)) def children(self): - return Element.objects.filter(parent=self.id).order_by('weight') + 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( @@ -44,3 +52,21 @@ class ElementDescription(models.Model): 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) diff --git a/navbar/templates/navbar/navbar.html b/navbar/templates/navbar/navbar.html index b96b0f7..6045375 100644 --- a/navbar/templates/navbar/navbar.html +++ b/navbar/templates/navbar/navbar.html @@ -16,9 +16,13 @@