filter import updated to Django recommended syntax, changed to decorator, and only filter user field if authenticated

This commit is contained in:
Thomas Weber 2020-10-13 11:20:09 -06:00
parent 30dc07d0d7
commit 39d810778a

View File

@ -5,15 +5,22 @@ templatetags/saved_queries.py - This template tag returns previously saved
queries. Therefore you don't need to modify queries. Therefore you don't need to modify
any views. any views.
""" """
from django.template import Library from django import template
from django.db.models import Q from django.db.models import Q
from helpdesk.models import SavedSearch from helpdesk.models import SavedSearch
register = template.Library()
@register.filter
def saved_queries(user): def saved_queries(user):
try: try:
user_saved_queries = SavedSearch.objects.filter(Q(user=user) | Q(shared__exact=True)) filters = Q(shared__exact=True)
if user.is_authenticated:
filters |= Q(user=user)
user_saved_queries = SavedSearch.objects.filter(filters)
return user_saved_queries return user_saved_queries
except Exception as e: except Exception as e:
import sys import sys
@ -21,7 +28,3 @@ def saved_queries(user):
file=sys.stderr) file=sys.stderr)
print(e, file=sys.stderr) print(e, file=sys.stderr)
return '' return ''
register = Library()
register.filter('saved_queries', saved_queries)