import logging
from typing import List
from django import template
from moderation.models import Category
from discourse.models import AccessControl

logger = logging.getLogger(__name__)
register = template.Library()

@register.filter(name='is_allowed_discussion')
def is_allowed_discussion(user):
    allowed_category_id: List = AccessControl.objects.filter(authorize=True).values_list("category", flat=True)
    if not allowed_category_id:
        return False
    allowed_category: List = Category.objects.filter(id__in=allowed_category_id)
    try:
        user_categories = user.socialuser.category.all()
        logger.debug(f"user categories: {user_categories}")
        _is_allowed = bool(
            list(set(allowed_category).intersection(set(user_categories)))
        )
        logger.debug(f"_is_allowed: {_is_allowed}")
        return _is_allowed
    except:
        return False
@@ -141,16 +141,58 @@
 sControl
+%0Afrom django.db.utils import DatabaseError
 %0A%0Alogger
@@ -333,16 +333,29 @@
 (user):%0A
+    try:%0A    
     allo
@@ -458,16 +458,148 @@
 t=True)%0A
+    except DatabaseError as e:%0A        logger.error(%22Error on database table AccessControl.%22, e)%0A        allowed_category_id = None%0A
     if n

handle no table exception