haikal/inventory/context_processors.py
2025-07-15 17:27:03 +03:00

86 lines
3.0 KiB
Python

from django.conf import settings
def currency_context(request):
"""
Provides a context dictionary containing the currency setting. This is typically
used for rendering templates with the appropriate currency value.
:param request: The HTTP request object. This parameter is not used within
the function but is included to maintain compatibility with
context processor signature.
:type request: HttpRequest
:return: A dictionary with the key 'CURRENCY' containing the value of the
project's CURRENCY setting.
:rtype: dict
"""
return {"CURRENCY": settings.CURRENCY}
def breadcrumbs(request):
"""
Generates a breadcrumb trail for the given request path.
The function processes the request's path, splits it into individual
segments, and creates a list of breadcrumb entries, each representing
a segment of the path. Each breadcrumb entry consists of a 'name'
(key) and its associated 'url' (key). The 'name' is a title-cased
representation of the path segment, and the 'url' is the cumulative URL
path leading to that segment.
:param request: The request object containing the path to be processed.
:type request: Any
:return: A dictionary containing a list of breadcrumbs, where each
breadcrumb is represented by a dictionary with 'name' and 'url'.
:rtype: dict
"""
breadcrumbs = []
path = request.path.strip("/").split("/")
for i in range(len(path)):
url = "/" + "/".join(path[: i + 1]) + "/"
breadcrumbs.append({"name": path[i].capitalize(), "url": url})
return {"breadcrumbs": breadcrumbs}
def user_types(request):
"""
Sets various flags indicating the user's role types.
The flags are:
- request.is_dealer
- request.is_staff
- request.is_manager
- request.is_accountant
- request.is_sales
- request.is_inventory
:param request: The request object to set the flags upon.
:type request: HttpRequest
"""
request.is_dealer = False
request.is_staff = False
request.is_manager = False
request.is_accountant = False
request.is_sales = False
request.is_inventory = False
if hasattr(request.user, "dealer"):
request.is_dealer = True
elif hasattr(request.user, "staffmember"):
request.is_staff = True
staff = getattr(request.user.staffmember, "staff")
if "Accountant" in staff.groups.values_list("name", flat=True):
request.is_accountant = True
return {"is_accountant": True}
elif "Manager" in staff.groups.values_list("name", flat=True):
request.is_manager = True
return {"is_manager": True}
elif "Sales" in staff.groups.values_list("name", flat=True):
request.is_sales = True
return {"is_sales": True}
elif "Inventory" in staff.groups.values_list("name", flat=True):
request.is_inventory = True
return {"is_inventory": True}
return {}