diff --git a/polisplexity/settings.py b/polisplexity/settings.py index 073ae2e..6505f1d 100644 --- a/polisplexity/settings.py +++ b/polisplexity/settings.py @@ -67,6 +67,7 @@ TEMPLATES = [ "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", + "pxy_dashboard.context_processors.sidebar_context", ], }, }, diff --git a/pxy_dashboard/admin.py b/pxy_dashboard/admin.py index 8c38f3f..dcba8f5 100644 --- a/pxy_dashboard/admin.py +++ b/pxy_dashboard/admin.py @@ -1,3 +1,10 @@ from django.contrib import admin +from .models import SidebarMenuItem -# Register your models here. + +@admin.register(SidebarMenuItem) +class SidebarMenuAdmin(admin.ModelAdmin): + list_display = ("label", "type", "url", "order", "parent") + list_filter = ("type", "parent") + search_fields = ("label", "url") + ordering = ("order",) diff --git a/pxy_dashboard/context_processors.py b/pxy_dashboard/context_processors.py new file mode 100644 index 0000000..8fbbfe9 --- /dev/null +++ b/pxy_dashboard/context_processors.py @@ -0,0 +1,21 @@ +from .models import SidebarMenuItem + +def build_menu_tree(parent=None): + items = SidebarMenuItem.objects.filter(parent=parent).order_by("order") + tree = [] + + for item in items: + tree.append({ + "type": item.type, + "label": item.label, + "icon": item.icon, + "url": item.url, + "badge": item.badge, + "children": build_menu_tree(parent=item) + }) + + return tree + +def sidebar_context(request): + sidebar_menu = build_menu_tree() + return {"sidebar_menu": sidebar_menu} diff --git a/pxy_dashboard/management/__init__.py b/pxy_dashboard/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pxy_dashboard/management/commands/__init__.py b/pxy_dashboard/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pxy_dashboard/management/commands/load_sidebar_menu.py b/pxy_dashboard/management/commands/load_sidebar_menu.py new file mode 100644 index 0000000..1665baf --- /dev/null +++ b/pxy_dashboard/management/commands/load_sidebar_menu.py @@ -0,0 +1,26 @@ +import json +from django.core.management.base import BaseCommand +from pxy_dashboard.models import SidebarMenuItem + +class Command(BaseCommand): + help = "Carga el menú lateral desde un archivo JSON" + + def add_arguments(self, parser): + parser.add_argument("json_file", type=str, help="Ruta al archivo JSON") + + def handle(self, *args, **kwargs): + json_path = kwargs["json_file"] + with open(json_path, "r", encoding="utf-8") as f: + data = json.load(f) + created = self.create_menu_items(data) + self.stdout.write(self.style.SUCCESS(f"{created} ítems de menú creados.")) + + def create_menu_items(self, items, parent=None): + count = 0 + for item in items: + children = item.pop("children", []) + menu_item = SidebarMenuItem.objects.create(parent=parent, **item) + count += 1 + if children: + count += self.create_menu_items(children, parent=menu_item) + return count diff --git a/pxy_dashboard/migrations/0001_initial.py b/pxy_dashboard/migrations/0001_initial.py new file mode 100644 index 0000000..c218424 --- /dev/null +++ b/pxy_dashboard/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# Generated by Django 5.0.3 on 2025-05-15 20:58 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='SidebarMenuItem', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('type', models.CharField(choices=[('title', 'Título de sección'), ('link', 'Enlace simple'), ('submenu', 'Submenú (con hijos)')], max_length=10)), + ('label', models.CharField(max_length=100)), + ('icon', models.CharField(blank=True, max_length=100, null=True)), + ('url', models.CharField(blank=True, max_length=200, null=True)), + ('badge', models.CharField(blank=True, max_length=50, null=True)), + ('order', models.PositiveIntegerField(default=0)), + ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='pxy_dashboard.sidebarmenuitem')), + ], + options={ + 'ordering': ['order'], + }, + ), + ] diff --git a/pxy_dashboard/migrations/0002_sidebarmenuitem_open_in_new_tab.py b/pxy_dashboard/migrations/0002_sidebarmenuitem_open_in_new_tab.py new file mode 100644 index 0000000..1562b91 --- /dev/null +++ b/pxy_dashboard/migrations/0002_sidebarmenuitem_open_in_new_tab.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.3 on 2025-05-16 07:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('pxy_dashboard', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='sidebarmenuitem', + name='open_in_new_tab', + field=models.BooleanField(default=False), + ), + ] diff --git a/pxy_dashboard/models.py b/pxy_dashboard/models.py index 71a8362..e99be20 100644 --- a/pxy_dashboard/models.py +++ b/pxy_dashboard/models.py @@ -1,3 +1,24 @@ from django.db import models -# Create your models here. +class SidebarMenuItem(models.Model): + MENU_TYPES = [ + ("title", "Título de sección"), + ("link", "Enlace simple"), + ("submenu", "Submenú (con hijos)"), + ] + + type = models.CharField(max_length=10, choices=MENU_TYPES) + label = models.CharField(max_length=100) + icon = models.CharField(max_length=100, blank=True, null=True) + url = models.CharField(max_length=200, blank=True, null=True) + badge = models.CharField(max_length=50, blank=True, null=True) + parent = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE, related_name="children") + order = models.PositiveIntegerField(default=0) + open_in_new_tab = models.BooleanField(default=False) + + + class Meta: + ordering = ["order"] + + def __str__(self): + return self.label diff --git a/pxy_dashboard/templates/pxy_dashboard/partials/left-sidebar.html b/pxy_dashboard/templates/pxy_dashboard/partials/left-sidebar.html index 010a9f3..b4bd3b0 100644 --- a/pxy_dashboard/templates/pxy_dashboard/partials/left-sidebar.html +++ b/pxy_dashboard/templates/pxy_dashboard/partials/left-sidebar.html @@ -50,6 +50,11 @@ + {% load sidebar_menu %} +