mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 20:40:17 +02:00
Merge branch 'middleware-menu' into 'v4.x'
swap to middleware to sync menu hooks See merge request allianceauth/allianceauth!1573
This commit is contained in:
commit
0531da1128
@ -3,6 +3,7 @@ import logging
|
||||
from django.apps import AppConfig
|
||||
from django.db.utils import ProgrammingError, OperationalError
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@ -12,8 +13,7 @@ class MenuConfig(AppConfig):
|
||||
|
||||
def ready(self):
|
||||
try:
|
||||
logger.debug("Syncing MenuItem Hooks")
|
||||
from allianceauth.menu.providers import MenuItem
|
||||
MenuItem.sync_hook_models()
|
||||
from allianceauth.menu.providers import menu_provider
|
||||
menu_provider.clear_synced_flag()
|
||||
except (ProgrammingError, OperationalError):
|
||||
logger.warning("Migrations not completed for MenuItems")
|
||||
|
15
allianceauth/menu/middleware.py
Normal file
15
allianceauth/menu/middleware.py
Normal file
@ -0,0 +1,15 @@
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
|
||||
import logging
|
||||
|
||||
from allianceauth.menu.providers import menu_provider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MenuSyncMiddleware(MiddlewareMixin):
|
||||
|
||||
def __call__(self, request):
|
||||
"""Alliance Auth Menu Sync Middleware"""
|
||||
menu_provider.check_and_sync_menu()
|
||||
return super().__call__(request)
|
@ -1,10 +1,49 @@
|
||||
from django.template.loader import render_to_string
|
||||
import logging
|
||||
|
||||
from allianceauth.hooks import get_hooks
|
||||
from django.core.cache import cache
|
||||
|
||||
from .models import MenuItem
|
||||
from allianceauth.menu.models import MenuItem
|
||||
from allianceauth.utils.django import StartupCommand
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MENU_SYNC_CACHE_KEY = "ALLIANCEAUTH-MENU-SYNCED"
|
||||
MENU_CACHE_KEY = "ALLIANCEAUTH-MENU-CACHE"
|
||||
|
||||
|
||||
class MenuProvider():
|
||||
def __init__(self) -> None:
|
||||
|
||||
def clear_synced_flag(self) -> bool:
|
||||
return cache.delete(MENU_SYNC_CACHE_KEY)
|
||||
|
||||
def set_synced_flag(self) -> bool:
|
||||
return cache.set(MENU_SYNC_CACHE_KEY, True)
|
||||
|
||||
def get_synced_flag(self) -> bool:
|
||||
return cache.get(MENU_SYNC_CACHE_KEY, False)
|
||||
|
||||
def sync_menu_models(self):
|
||||
MenuItem.sync_hook_models()
|
||||
self.set_synced_flag()
|
||||
|
||||
def check_and_sync_menu(self) -> None:
|
||||
if self.get_synced_flag():
|
||||
# performance hit to each page view to ensure tests work.
|
||||
# tests clear DB but not cache.
|
||||
# TODO rethink all of this?
|
||||
if MenuItem.objects.all().count() > 0:
|
||||
logger.debug("Menu Hooks Synced")
|
||||
else:
|
||||
self.sync_menu_models()
|
||||
else:
|
||||
logger.debug("Syncing Menu Hooks")
|
||||
self.sync_menu_models()
|
||||
|
||||
def get_and_cache_menu(self):
|
||||
pass
|
||||
|
||||
def clear_menu_cache(self):
|
||||
pass
|
||||
|
||||
|
||||
menu_provider = MenuProvider()
|
||||
|
@ -3,7 +3,6 @@ from django import template
|
||||
from allianceauth.hooks import get_hooks
|
||||
from allianceauth.menu.models import MenuItem
|
||||
|
||||
from ..providers import MenuProvider
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
@ -3,7 +3,6 @@ from django import urls
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
|
||||
from allianceauth.tests.auth_utils import AuthUtils
|
||||
from allianceauth.menu.models import MenuItem
|
||||
|
||||
class PermissionsToolViewsTestCase(WebTest):
|
||||
def setUp(self):
|
||||
@ -34,8 +33,6 @@ class PermissionsToolViewsTestCase(WebTest):
|
||||
self.member.user_permissions.add(self.permission)
|
||||
AuthUtils.connect_signals()
|
||||
|
||||
# TODO find a nicer way to do this later
|
||||
MenuItem.sync_hook_models()
|
||||
|
||||
def test_menu_item(self):
|
||||
# If we change the side menu again this will fail again.
|
||||
|
@ -72,6 +72,7 @@ PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
BASE_DIR = os.path.dirname(PROJECT_DIR)
|
||||
|
||||
MIDDLEWARE = [
|
||||
'allianceauth.menu.middleware.MenuSyncMiddleware',
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'allianceauth.authentication.middleware.UserSettingsMiddleware',
|
||||
|
Loading…
x
Reference in New Issue
Block a user