From 70f81d9305bd31106473d0fb7da879af173b4de3 Mon Sep 17 00:00:00 2001 From: Horilla Date: Wed, 28 Aug 2024 16:56:49 +0530 Subject: [PATCH] [UPDT] HORILLA BREADCRUMBS: Replaced user based breadcrumbs to session based breadcrumbs --- base/migrations/__init__.py | 5 +++ horilla/methods.py | 27 ++++++++++++++++ horilla_crumbs/context_processors.py | 46 +++++++++++++++------------- templates/index.html | 7 ++--- 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/base/migrations/__init__.py b/base/migrations/__init__.py index ca33a504e..3e93ef548 100644 --- a/base/migrations/__init__.py +++ b/base/migrations/__init__.py @@ -18,6 +18,7 @@ try: horilla_mail.save() horilla_mail_templates = HorillaMailTemplate.objects.all() + RecruitmentMailTemplate.objects.all().delete() except Exception as e: pass @@ -41,6 +42,7 @@ try: horilla.save() base_leaves = BaseHoliday.objects.all() + LeaveHoliday.objects.all().delete() except Exception as e: pass @@ -85,6 +87,7 @@ try: horilla.deduct_from_carry_forward = penalty.deduct_from_carry_forward horilla.save() penalty_accounts = PenaltyAccounts.objects.all() + PenaltyAccount.objects.all().delete() except Exception as e: pass @@ -106,6 +109,7 @@ try: horilla.save() base_leaves = BaseCompanyLeave.objects.all() + CompanyLeave.objects.all().delete() except Exception as e: pass @@ -140,5 +144,6 @@ try: new_work_record.save() new_work_records = WorkRecords.objects.all() + WorkRecord.objects.all().delete() except Exception as e: pass diff --git a/horilla/methods.py b/horilla/methods.py index 2e2dcbf7c..5c61e0c5e 100644 --- a/horilla/methods.py +++ b/horilla/methods.py @@ -1,5 +1,6 @@ import contextlib +from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType @@ -44,3 +45,29 @@ def dynamic_attr(obj, attribute_path): if obj is None: break return obj + + +def horilla_users_with_perms(permissions): + """ + Filters users who have any of the specified permissions or are superusers. + + :param permissions: A list of permission strings in the format 'app_label.codename' + or a single permission string. + :return: A queryset of users who have any of the specified permissions or are superusers. + """ + # Ensure permissions is a list even if a single permission string is provided + if isinstance(permissions, str): + permissions = [permissions] + + # Start with a queryset that includes all superusers + users_with_permissions = User.objects.filter(is_superuser=True) + + # Filter users based on the permissions list + for perm in permissions: + app_label, codename = perm.split(".") + users_with_permissions |= User.objects.filter( + user_permissions__codename=codename, + user_permissions__content_type__app_label=app_label, + ) + + return users_with_permissions.distinct() diff --git a/horilla_crumbs/context_processors.py b/horilla_crumbs/context_processors.py index 2f7ba06d5..25d57736e 100644 --- a/horilla_crumbs/context_processors.py +++ b/horilla_crumbs/context_processors.py @@ -132,11 +132,17 @@ sidebar_urls = [ "view-time-sheet", "templates", "sidebar.html", + "objective-detailed-view", + "mail-automations", + "faq-view", ] remove_urls = [ "feedback-detailed-view", "question-template-detailed-view", "employee-view-new", + "objective-detailed-view", + "ticket-detail", + "faq-view", ] user_breadcrumbs = {} @@ -144,15 +150,15 @@ user_breadcrumbs = {} def breadcrumbs(request): base_url = request.build_absolute_uri("/") - user_id = str(request.user) - if user_id not in user_breadcrumbs: - user_breadcrumbs[user_id] = [ + # Initialize breadcrumbs in the session if not already present + if "breadcrumbs" not in request.session: + request.session["breadcrumbs"] = [ {"url": base_url, "name": "Horilla", "found": True} ] try: - user_breadcrumb = user_breadcrumbs[user_id] + breadcrumbs = request.session["breadcrumbs"] qs = request.META.get("QUERY_STRING", "") pairs = qs.split("&") @@ -160,7 +166,7 @@ def breadcrumbs(request): filtered_query_string = "&".join(filtered_pairs) emp_query_string = None - for item in user_breadcrumb: + for item in breadcrumbs: if item["name"] in ["employee-view", "candidate-view"]: items = item["url"].split("?", 1) if len(items) > 1: @@ -210,8 +216,8 @@ def breadcrumbs(request): ] if len(parts) == 0: - user_breadcrumbs[user_id].clear() - user_breadcrumb.append({"url": base_url, "name": "Horilla", "found": True}) + request.session["breadcrumbs"].clear() + breadcrumbs.append({"url": base_url, "name": "Horilla", "found": True}) if len(parts) > 1: last_path = parts[-1] @@ -221,10 +227,9 @@ def breadcrumbs(request): or parts[-2] == "candidate-view" or parts[-2] == "view-payslip" ): - breadcrumbs = user_breadcrumbs[user_id] first_path = breadcrumbs[0] - user_breadcrumbs[user_id].clear() - user_breadcrumbs[user_id].append(first_path) + request.session["breadcrumbs"].clear() + request.session["breadcrumbs"].append(first_path) for i, item in enumerate(parts): path = path + item + "/" parsed_url = urlparse(path) @@ -232,7 +237,7 @@ def breadcrumbs(request): try: resolver_match = resolve(check_path) found = True - except Resolver404 as e: + except Resolver404: found = False new_dict = {"url": path, "name": item, "found": found} @@ -245,15 +250,15 @@ def breadcrumbs(request): if model_value: try: - object = model_value.objects.get(id=item) - new_dict["name"] = str(object) + obj = model_value.objects.get(id=item) # completed + new_dict["name"] = str(obj) except: pass key = "HTTP_HX_REQUEST" - names = [d["name"] for d in user_breadcrumb] + names = [d["name"] for d in breadcrumbs] if ( - new_dict not in user_breadcrumb + new_dict not in breadcrumbs and new_dict["name"] not in remove_urls + names and key not in request.META.keys() and not new_dict["name"].isdigit() @@ -261,10 +266,10 @@ def breadcrumbs(request): if new_dict["name"] in ["employee-view", "candidate-view"]: new_dict["url"] = f'{new_dict["url"]}?{emp_query_string}' - user_breadcrumb.append(new_dict) + breadcrumbs.append(new_dict) try: - prev_url = user_breadcrumb[-1] + prev_url = breadcrumbs[-1] prev_url["url"] = prev_url["url"].split("?")[0] if filtered_query_string: prev_url["url"] = f'{prev_url["url"]}?{filtered_query_string}' @@ -273,14 +278,13 @@ def breadcrumbs(request): except: pass - user_breadcrumbs[user_id] = user_breadcrumb + request.session["breadcrumbs"] = breadcrumbs except Exception as e: - user_breadcrumb[user_id].clear() - user_breadcrumbs[user_id] = [ + request.session["breadcrumbs"] = [ {"url": base_url, "name": "Horilla", "found": True} ] - return {"breadcrumbs": user_breadcrumbs[user_id]} + return {"breadcrumbs": request.session["breadcrumbs"]} urlpatterns.append( diff --git a/templates/index.html b/templates/index.html index 59a1f0113..9f3c31b4c 100755 --- a/templates/index.html +++ b/templates/index.html @@ -68,7 +68,7 @@ {% endfor %} {% endif %} - +
Attendances --> {% if breadcrumbs %} - {% comment %} {{breadcrumbs}} {% endcomment %}