[UPDT] ACCESSIBILITY: Restrict all feature in employee restriction feature
This commit is contained in:
55
accessibility/cbv_decorators.py
Normal file
55
accessibility/cbv_decorators.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
employee/decorators.py
|
||||
"""
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.contrib import messages
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.shortcuts import redirect
|
||||
from accessibility.methods import check_is_accessible
|
||||
from base.decorators import decorator_with_arguments
|
||||
from horilla.horilla_middlewares import _thread_locals
|
||||
|
||||
@decorator_with_arguments
|
||||
def enter_if_accessible(function, feature, perm=None, method=None):
|
||||
"""
|
||||
accessible check decorator for cbv
|
||||
"""
|
||||
|
||||
|
||||
def check_accessible(self, *args, **kwargs):
|
||||
"""
|
||||
Check accessible
|
||||
"""
|
||||
path = "/"
|
||||
request = getattr(_thread_locals,"request")
|
||||
if not getattr(self,"request",None):
|
||||
self.request = request
|
||||
referrer = request.META.get("HTTP_REFERER")
|
||||
if referrer and request.path not in referrer:
|
||||
path = request.META["HTTP_REFERER"]
|
||||
accessible = False
|
||||
cache_key = request.session.session_key + "accessibility_filter"
|
||||
employee = getattr(request.user, "employee_get")
|
||||
if employee:
|
||||
accessible = check_is_accessible(feature, cache_key, employee)
|
||||
has_perm = True
|
||||
if perm:
|
||||
has_perm = request.user.has_perm(perm)
|
||||
|
||||
if accessible or has_perm or method(request):
|
||||
return function(self, *args, **kwargs)
|
||||
key = "HTTP_HX_REQUEST"
|
||||
keys = request.META.keys()
|
||||
messages.info(request, _("You dont have access to the feature"))
|
||||
if key in keys:
|
||||
return HttpResponse(
|
||||
f"""
|
||||
<script>
|
||||
window.location.href="{referrer}"
|
||||
</script>
|
||||
"""
|
||||
)
|
||||
return redirect(path)
|
||||
|
||||
return check_accessible
|
||||
@@ -18,6 +18,8 @@ def check_is_accessible(feature, cache_key, employee):
|
||||
|
||||
accessibility = DefaultAccessibility.objects.filter(feature=feature).first()
|
||||
|
||||
if accessibility and accessibility.exclude_all:
|
||||
return False
|
||||
if not feature or not accessibility:
|
||||
return True
|
||||
|
||||
|
||||
@@ -15,3 +15,4 @@ class DefaultAccessibility(HorillaModel):
|
||||
|
||||
feature = models.CharField(max_length=100, choices=ACCESSBILITY_FEATURE)
|
||||
filter = models.JSONField()
|
||||
exclude_all = models.BooleanField(default=False)
|
||||
|
||||
@@ -75,9 +75,44 @@
|
||||
</div>
|
||||
<div class="oh-accordion-meta__body d-none" id="{{accessibility}}_body">
|
||||
<form hx-post="" hx-target="#response" hx-swap="afterend">
|
||||
<div class="col-12 col-md-6" id="id_pk_parent_div" style="padding-right: 0;">
|
||||
<div class="oh-label__info" for="id_pk">
|
||||
<label class="oh-label " for="id_pk"><b>{% trans "Restrict All" %}</b></label>
|
||||
</div>
|
||||
<div id="dynamic_field_pk">
|
||||
<div class="oh-switch" onclick="event.stopPropagation()">
|
||||
<input data-accessibility="{{accessibility}}" type="checkbox" class="oh-switch__checkbox" name="exclude_all" onchange="
|
||||
$(this).closest('form').find('input[type=submit]').click();
|
||||
value = $(this).is(':checked');
|
||||
container = $('#{{accessibility}}formcontainer')
|
||||
console.log(typeof value)
|
||||
if (value=='true') {
|
||||
container.hide();
|
||||
}else{
|
||||
console.log('false');
|
||||
|
||||
$(container).show();
|
||||
}
|
||||
">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="{{accessibility}}formcontainer">
|
||||
<div class="mt-3" style="
|
||||
padding: 15px;
|
||||
background: #87cefa38;
|
||||
border-radius: 5px;
|
||||
border: solid 1px lightskyblue;
|
||||
border-left: solid #27a3ef 3px;
|
||||
">
|
||||
{% trans "Only those normal users/employees with any category mentioned in the form can access the" %} `<b>{{display}}</b>` {% trans "feature" %}
|
||||
<br>
|
||||
{% trans "If skip all the category fields in the form, then all normal users/employees can access the feature" %}
|
||||
</div>
|
||||
<input hidden type="text" name="feature" value="{{accessibility}}">
|
||||
{{accessibility_filter.form.structured}}
|
||||
<input hidden type="submit" value="submit">
|
||||
</div>
|
||||
</form>
|
||||
<script>
|
||||
$(document).ready(function () {
|
||||
@@ -85,26 +120,26 @@
|
||||
$.ajax({
|
||||
url: "{% url 'get-initial-accessibility-data' %}?feature={{accessibility}}",
|
||||
success: function (response) {
|
||||
console.log(response)
|
||||
for (let key in response) {
|
||||
if (response.hasOwnProperty(key)) {
|
||||
let values = response[key];
|
||||
let field = document.querySelector(`#{{accessibility}}_body [name="${key}"]`);
|
||||
|
||||
if (field) {
|
||||
// Handle select fields
|
||||
if (field.tagName === 'SELECT') {
|
||||
// Check if it's a multiple select
|
||||
if (field.multiple) {
|
||||
// Loop through the options and set selected if the value matches
|
||||
for (let option of field.options) {
|
||||
option.selected = values.includes(option.value);
|
||||
}
|
||||
} else {
|
||||
// For single select, set the value
|
||||
field.value = values[0];
|
||||
}
|
||||
}
|
||||
if (field.tagName === 'SELECT') {
|
||||
if (field.multiple) {
|
||||
for (let option of field.options) {
|
||||
option.selected = values.includes(option.value);
|
||||
}
|
||||
} else {
|
||||
field.value = values[0];
|
||||
}
|
||||
}else if(key=="exclude_all" && values[0] == "on"){
|
||||
$(field).attr("checked",true)
|
||||
accessibility = $(field).attr('data-accessibility')
|
||||
$(`#${accessibility}formcontainer`).hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
select = $("#accessibilityContainer #{{accessibility}}_body").find("select")
|
||||
|
||||
@@ -3,8 +3,8 @@ accessibility/templatestags/accessibility_filters.py
|
||||
"""
|
||||
|
||||
from django import template
|
||||
from accessibility.methods import check_is_accessible
|
||||
|
||||
from accessibility.methods import check_is_accessible
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ def user_accessibility(request):
|
||||
accessibility = accessibility if accessibility else DefaultAccessibility()
|
||||
accessibility.feature = feature
|
||||
accessibility.filter = dict(request.POST)
|
||||
accessibility.exclude_all = bool(request.POST.get("exclude_all"))
|
||||
accessibility.save()
|
||||
if len(request.POST.keys()) > 1:
|
||||
messages.success(request, _("Accessibility filter saved"))
|
||||
|
||||
Reference in New Issue
Block a user