Skip to content

Commit

Permalink
Fix menus generation after Tenant creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Jan 23, 2024
1 parent 7d6067d commit 0071a4d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
56 changes: 36 additions & 20 deletions tenants/menus.py
Original file line number Diff line number Diff line change
@@ -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 _

Expand All @@ -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 = [
Expand Down Expand Up @@ -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)
2 changes: 0 additions & 2 deletions tenants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit 0071a4d

Please sign in to comment.