diff --git a/tenants/menus.py b/tenants/menus.py index 1f5330b..c80f0b7 100644 --- a/tenants/menus.py +++ b/tenants/menus.py @@ -1,5 +1,7 @@ """Generates Tenant-specific menus""" from django.conf import settings +from django.db.models.signals import post_save +from django.dispatch import receiver from django.urls import reverse from django.utils.translation import gettext_lazy as _ @@ -13,6 +15,31 @@ from tenants.utils import create_tenant_string +def create_menus(tenant): + """Generate menus specific for each tenant""" + + Menu.add_item( + create_tenant_string(tenant, "files"), + CacheMenuItem( + title=_("Files"), + url=reverse("backend:index"), + generate_function=distinct_requests, + key=create_tenant_string(tenant, settings.PDF_CACHE_KEY), + timeout=60 * 60, + ), + ) + Menu.add_item( + create_tenant_string(tenant, "songbook"), + CacheMenuItem( + title=_("Categories"), + url=reverse("backend:index"), + generate_function=categories, + key=create_tenant_string(tenant, settings.CATEGORY_CACHE_KEY), + timeout=60 * 60 * 24 * 7, + ), + ) + + def categories(request): """Returns MenuItems for all Categories""" items = [ @@ -47,23 +74,12 @@ def distinct_requests(request): for tenant in Tenant.objects.all(): - Menu.add_item( - create_tenant_string(tenant, "files"), - CacheMenuItem( - title=_("Files"), - url=reverse("backend:index"), - generate_function=distinct_requests, - key=create_tenant_string(tenant, settings.PDF_CACHE_KEY), - timeout=60 * 60, - ), - ) - Menu.add_item( - create_tenant_string(tenant, "songbook"), - CacheMenuItem( - title=_("Categories"), - url=reverse("backend:index"), - generate_function=categories, - key=create_tenant_string(tenant, settings.CATEGORY_CACHE_KEY), - timeout=60 * 60 * 24 * 7, - ), - ) + create_menus(tenant) + + +# pylint: disable=unused-argument +@receiver(post_save, sender=Tenant) +def generate_menus(sender, instance, created, **kwargs): + """Generates a menu for new tenants""" + if created: + create_menus(instance) diff --git a/tenants/models.py b/tenants/models.py index 069476d..7088018 100644 --- a/tenants/models.py +++ b/tenants/models.py @@ -4,8 +4,6 @@ from django.db.models import Model, CharField, ManyToManyField, BooleanField, ImageField from django.utils.translation import gettext_lazy as _ -# Create your models here. - def only_png(value): """Raises validation error if the file is not a PNG image"""