2020-03-19 01:26:06 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.template.defaultfilters import stringfilter
|
|
|
|
|
2020-03-19 15:12:52 +00:00
|
|
|
from note_kfet.middlewares import get_current_authenticated_user, get_current_session
|
2020-03-19 01:26:06 +00:00
|
|
|
from django import template
|
|
|
|
|
2020-03-20 13:43:35 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-03-19 01:26:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@stringfilter
|
|
|
|
def not_empty_model_list(model_name):
|
2020-03-20 14:58:14 +00:00
|
|
|
"""
|
|
|
|
Return True if and only if the current user has right to see any object of the given model.
|
|
|
|
"""
|
2020-03-19 01:26:06 +00:00
|
|
|
user = get_current_authenticated_user()
|
2020-03-20 00:46:59 +00:00
|
|
|
session = get_current_session()
|
2020-03-19 13:25:43 +00:00
|
|
|
if user is None:
|
|
|
|
return False
|
2020-03-20 00:46:59 +00:00
|
|
|
elif user.is_superuser and session.get("permission_mask", 0) >= 42:
|
2020-03-19 01:26:06 +00:00
|
|
|
return True
|
2020-03-20 00:46:59 +00:00
|
|
|
if session.get("not_empty_model_list_" + model_name, None):
|
|
|
|
return session.get("not_empty_model_list_" + model_name, None) == 1
|
2020-03-19 01:26:06 +00:00
|
|
|
spl = model_name.split(".")
|
|
|
|
ct = ContentType.objects.get(app_label=spl[0], model=spl[1])
|
2020-03-20 13:43:35 +00:00
|
|
|
qs = ct.model_class().objects.filter(PermissionBackend.filter_queryset(user, ct, "view")).all()
|
2020-03-20 00:46:59 +00:00
|
|
|
session["not_empty_model_list_" + model_name] = 1 if qs.exists() else 2
|
|
|
|
return session.get("not_empty_model_list_" + model_name) == 1
|
2020-03-19 01:26:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@stringfilter
|
|
|
|
def not_empty_model_change_list(model_name):
|
2020-03-20 14:58:14 +00:00
|
|
|
"""
|
|
|
|
Return True if and only if the current user has right to change any object of the given model.
|
|
|
|
"""
|
2020-03-19 01:26:06 +00:00
|
|
|
user = get_current_authenticated_user()
|
2020-03-20 00:46:59 +00:00
|
|
|
session = get_current_session()
|
2020-03-19 13:25:43 +00:00
|
|
|
if user is None:
|
|
|
|
return False
|
2020-03-20 00:46:59 +00:00
|
|
|
elif user.is_superuser and session.get("permission_mask", 0) >= 42:
|
2020-03-19 01:26:06 +00:00
|
|
|
return True
|
2020-03-20 00:46:59 +00:00
|
|
|
if session.get("not_empty_model_change_list_" + model_name, None):
|
|
|
|
return session.get("not_empty_model_change_list_" + model_name, None) == 1
|
2020-03-19 01:26:06 +00:00
|
|
|
spl = model_name.split(".")
|
|
|
|
ct = ContentType.objects.get(app_label=spl[0], model=spl[1])
|
|
|
|
qs = ct.model_class().objects.filter(PermissionBackend.filter_queryset(user, ct, "change"))
|
2020-03-20 00:46:59 +00:00
|
|
|
session["not_empty_model_change_list_" + model_name] = 1 if qs.exists() else 2
|
|
|
|
return session.get("not_empty_model_change_list_" + model_name) == 1
|
2020-03-19 01:26:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
register.filter('not_empty_model_list', not_empty_model_list)
|
|
|
|
register.filter('not_empty_model_change_list', not_empty_model_change_list)
|