diff --git a/uniticket/uni_ticket/models.py b/uniticket/uni_ticket/models.py index b60ce287..24c4a104 100644 --- a/uniticket/uni_ticket/models.py +++ b/uniticket/uni_ticket/models.py @@ -1330,22 +1330,29 @@ class Meta: verbose_name_plural = _("Competenza Ticket") @staticmethod - def get_ticket_per_structure(structure, follow_check=True): + def get_ticket_per_structure(structure, follow_check=True, only_open_tickets=False): """ """ q_base = Q(office__organizational_structure=structure, office__is_active=True) q_follow = Q(follow=True) if follow_check else Q() + q_only_open = Q(ticket__is_closed=False) if only_open_tickets else Q() ticket_assignments = TicketAssignment.objects.filter( q_base, - q_follow + q_follow, + q_only_open ).values_list("ticket__code", flat=True) return set(ticket_assignments) @staticmethod - def get_ticket_in_office_list(office_list, follow_check=True): + def get_ticket_in_office_list(office_list, + follow_check=True, + only_open_tickets=False): """ """ + q_only_open = Q(ticket__is_closed=False) if only_open_tickets else Q() ticket_assignments = TicketAssignment.objects.filter( - office__in=office_list, office__is_active=True + q_only_open, + office__in=office_list, + office__is_active=True ).values("ticket__code", "follow") ticket_set = set() for assignment in ticket_assignments: diff --git a/uniticket/uni_ticket/utils.py b/uniticket/uni_ticket/utils.py index f38b1566..5141e87e 100644 --- a/uniticket/uni_ticket/utils.py +++ b/uniticket/uni_ticket/utils.py @@ -261,21 +261,24 @@ def format_slugged_name(field_name, capitalize=True): return f -def visible_tickets_to_user(user, structure, office_employee): +def visible_tickets_to_user(user, + structure, + office_employee, + only_open_tickets=False): """ Returns a list of tickets that are visible to user """ model = apps.get_model("uni_ticket", "TicketAssignment") - # default_office = office_employee.filter(office__is_default=True).first() - # if default_office: if user_is_in_default_office(user, structure): - tickets = model.get_ticket_per_structure(structure) + tickets = model.get_ticket_per_structure(structure=structure, + only_open_tickets=only_open_tickets) return tickets offices = [] for oe in office_employee: if oe.office not in offices: offices.append(oe.office) - tickets = model.get_ticket_in_office_list(offices) + tickets = model.get_ticket_in_office_list(offices_list=offices, + only_open_tickets=only_open_tickets) return tickets diff --git a/uniticket/uni_ticket/views/generic.py b/uniticket/uni_ticket/views/generic.py index e4037112..5575103d 100644 --- a/uniticket/uni_ticket/views/generic.py +++ b/uniticket/uni_ticket/views/generic.py @@ -328,7 +328,8 @@ def ticket_messages(request, structure_slug=None, structure=None, office_employe by_operator = False if user_type == "user": tickets = Ticket.objects.filter( - Q(created_by=request.user) | Q(compiled_by=request.user) + Q(created_by=request.user) | Q(compiled_by=request.user), + is_closed=False, ).values("code") # if user_type is 'user', retrieve messages leaved by a manager/operator # (linked to a structure) @@ -336,12 +337,16 @@ def ticket_messages(request, structure_slug=None, structure=None, office_employe elif user_type == "operator": # if user is an operator, retrieve his tickets tickets = visible_tickets_to_user( - user=request.user, structure=structure, office_employee=office_employee + user=request.user, + structure=structure, + office_employee=office_employee, + only_open_tickets=True ) else: # if user is a manager, get structure tickets ta = TicketAssignment - tickets = ta.get_ticket_per_structure(structure=structure) + tickets = ta.get_ticket_per_structure(structure=structure, + only_open_tickets=True) if by_operator: not_read = Count(