From bc3d89b55584b2ce300fa691de0cf92a4a8f7caa Mon Sep 17 00:00:00 2001 From: Benbb96 Date: Mon, 15 Aug 2022 20:15:02 +0200 Subject: [PATCH 1/2] Fix PyCharm warnings in staff.py --- helpdesk/views/staff.py | 151 ++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 85 deletions(-) diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index f31b89f6..9a91033b 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -6,6 +6,8 @@ django-helpdesk - A Django powered ticket tracker for small enterprise. views/staff.py - The bulk of the application - provides most business logic and renders all staff-facing views. """ +from django.contrib.auth.views import redirect_to_login + from ..lib import format_time_spent from ..templated_email import send_templated_mail from collections import defaultdict @@ -273,8 +275,7 @@ def followup_edit(request, ticket_id, followup_id): 'time_spent': format_time_spent(followup.time_spent), }) - ticketcc_string, __ = \ - return_ticketccstring_and_show_subscribe(request.user, ticket) + ticketcc_string = return_ticketccstring_and_show_subscribe(request.user, ticket)[0] return render(request, 'helpdesk/followup_edit.html', { 'followup': followup, @@ -356,7 +357,7 @@ def view_ticket(request, ticket_id): )[1] if show_subscribe: - subscribe_staff_member_to_ticket(ticket, request.user) + subscribe_to_ticket_updates(ticket, request.user) return HttpResponseRedirect(reverse('helpdesk:view', args=[ticket.id])) if 'close' in request.GET and ticket.status == Ticket.RESOLVED_STATUS: @@ -471,30 +472,19 @@ def subscribe_to_ticket_updates(ticket, user=None, email=None, can_view=True, ca _('When you add somebody on Cc, you must provide either a User or a valid email. Email: %s' % email) ) - ticketcc = TicketCC( - ticket=ticket, + return ticket.ticketcc_set.create( user=user, email=email, can_view=can_view, can_update=can_update ) - ticketcc.save() - - return ticketcc - - -def subscribe_staff_member_to_ticket(ticket, user, email='', can_view=True, can_update=False): - """used in view_ticket() and update_ticket()""" - return subscribe_to_ticket_updates(ticket=ticket, user=user, email=email, can_view=can_view, can_update=can_update) def get_ticket_from_request_with_authorisation( request: WSGIRequest, ticket_id: str, public: bool -) -> typing.Union[ - Ticket, typing.NoReturn -]: +) -> Ticket: """Gets a ticket from the public status and if the user is authenticated and has permissions to update tickets @@ -508,20 +498,14 @@ def get_ticket_from_request_with_authorisation( is_helpdesk_staff(request.user) or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE))): - key = request.POST.get('key') - email = request.POST.get('mail') - - if key and email: - ticket = Ticket.objects.get( + try: + return Ticket.objects.get( id=ticket_id, - submitter_email__iexact=email, - secret_key__iexact=key - ) - - if not ticket: - return HttpResponseRedirect( - '%s?next=%s' % (reverse('helpdesk:login'), request.path) + submitter_email__iexact=request.POST.get('mail'), + secret_key__iexact=request.POST.get('key') ) + except (Ticket.DoesNotExist, ValueError): + return redirect_to_login(request.path, 'helpdesk:login') return get_object_or_404(Ticket, id=ticket_id) @@ -562,10 +546,10 @@ def get_due_date_from_request_or_ticket( def get_and_set_ticket_status( - new_status: str, + new_status: int, ticket: Ticket, follow_up: FollowUp -) -> typing.Tuple[str, str]: +) -> typing.Tuple[str, int]: """Performs comparision on previous status to new status, updating the title as required. @@ -590,7 +574,7 @@ def get_and_set_ticket_status( follow_up.title = _('Updated') follow_up.save() - return (old_status_str, old_status) + return old_status_str, old_status def get_time_spent_from_request(request: WSGIRequest) -> typing.Optional[timedelta]: @@ -606,7 +590,7 @@ def update_messages_sent_to_by_public_and_status( ticket: Ticket, follow_up: FollowUp, context: str, - messages_sent_to: typing.List[str], + messages_sent_to: typing.Set[str], files: typing.List[typing.Tuple[str, str]] ) -> Ticket: """Sets the status of the ticket""" @@ -648,13 +632,10 @@ def add_staff_subscription( ) -> None: """Auto subscribe the staff member if that's what the settigs say and the user is authenticated and a staff member""" - if helpdesk_settings.HELPDESK_AUTO_SUBSCRIBE_ON_TICKET_RESPONSE and request.user.is_authenticated: - SHOW_SUBSCRIBE = return_ticketccstring_and_show_subscribe( - request.user, ticket - )[1] - - if SHOW_SUBSCRIBE: - subscribe_staff_member_to_ticket(ticket, request.user) + if helpdesk_settings.HELPDESK_AUTO_SUBSCRIBE_ON_TICKET_RESPONSE \ + and request.user.is_authenticated \ + and return_ticketccstring_and_show_subscribe(request.user, ticket)[1]: + subscribe_to_ticket_updates(ticket, request.user) def get_template_staff_and_template_cc( @@ -878,12 +859,13 @@ def mass_update(request): if not (tickets and action): return HttpResponseRedirect(reverse('helpdesk:list')) + user = kbitem = None + if action.startswith('assign_'): parts = action.split('_') user = User.objects.get(id=parts[1]) action = 'assign' if action == 'kbitem_none': - kbitem = None action = 'set_kbitem' if action.startswith('kbitem_'): parts = action.split('_') @@ -908,52 +890,50 @@ def mass_update(request): if action == 'assign' and t.assigned_to != user: t.assigned_to = user t.save() - f = FollowUp(ticket=t, - date=timezone.now(), - title=_('Assigned to %(username)s in bulk update' % { - 'username': user.get_username() - }), - public=True, - user=request.user) - f.save() + t.followup_set.create( + date=timezone.now(), + title=_('Assigned to %(username)s in bulk update' % {'username': user.get_username()}), + public=True, + user=request.user + ) elif action == 'unassign' and t.assigned_to is not None: t.assigned_to = None t.save() - f = FollowUp(ticket=t, - date=timezone.now(), - title=_('Unassigned in bulk update'), - public=True, - user=request.user) - f.save() + t.followup_set.create( + date=timezone.now(), + title=_('Unassigned in bulk update'), + public=True, + user=request.user + ) elif action == 'set_kbitem': t.kbitem = kbitem t.save() - f = FollowUp(ticket=t, - date=timezone.now(), - title=_('KBItem set in bulk update'), - public=False, - user=request.user) - f.save() + t.followup_set.create( + date=timezone.now(), + title=_('KBItem set in bulk update'), + public=False, + user=request.user + ) elif action == 'close' and t.status != Ticket.CLOSED_STATUS: t.status = Ticket.CLOSED_STATUS t.save() - f = FollowUp(ticket=t, - date=timezone.now(), - title=_('Closed in bulk update'), - public=False, - user=request.user, - new_status=Ticket.CLOSED_STATUS) - f.save() + t.followup_set.create( + date=timezone.now(), + title=_('Closed in bulk update'), + public=False, + user=request.user, + new_status=Ticket.CLOSED_STATUS + ) elif action == 'close_public' and t.status != Ticket.CLOSED_STATUS: t.status = Ticket.CLOSED_STATUS t.save() - f = FollowUp(ticket=t, - date=timezone.now(), - title=_('Closed in bulk update'), - public=True, - user=request.user, - new_status=Ticket.CLOSED_STATUS) - f.save() + t.followup_set.create( + date=timezone.now(), + title=_('Closed in bulk update'), + public=True, + user=request.user, + new_status=Ticket.CLOSED_STATUS + ) # Send email to Submitter, Owner, Queue CC context = safe_template_context(t) context.update(resolution=t.resolution, @@ -1183,19 +1163,17 @@ def check_redirect_on_user_query(request, huser): if query.find('-') > 0: try: queue, id_ = Ticket.queue_and_id_from_query(query) - id_ = int(id) + id_ = int(id_) except ValueError: - id_ = None - - if id_: + pass + else: filter_ = {'queue__slug': queue, 'id': id_} else: try: query = int(query) except ValueError: - query = None - - if query: + pass + else: filter_ = {'id': int(query)} if filter_: @@ -1364,7 +1342,7 @@ def load_saved_query(request, query_params=None): query_params = query_from_base64(b64query) except json.JSONDecodeError: raise QueryLoadError() - return (saved_query, query_params) + return saved_query, query_params @helpdesk_staff_member_required @@ -1377,14 +1355,14 @@ def datatables_ticket_list(request, query): """ query = Query(HelpdeskUser(request.user), base64query=query) result = query.get_datatables_context(**request.query_params) - return (JsonResponse(result, status=status.HTTP_200_OK)) + return JsonResponse(result, status=status.HTTP_200_OK) @helpdesk_staff_member_required @api_view(['GET']) def timeline_ticket_list(request, query): query = Query(HelpdeskUser(request.user), base64query=query) - return (JsonResponse(query.get_timeline_context(), status=status.HTTP_200_OK)) + return JsonResponse(query.get_timeline_context(), status=status.HTTP_200_OK) @helpdesk_staff_member_required @@ -1615,6 +1593,9 @@ def update_summary_tables(report_queryset, report, summarytable, summarytable2): metric3 = ticket.modified - ticket.created metric3 = metric3.days + else: + raise ValueError(f'report "{report}" is unrecognized.') + summarytable[metric1, metric2] += 1 if metric3: if report == 'daysuntilticketclosedbymonth': @@ -1797,7 +1778,7 @@ class EditUserSettingsView(MustBeStaffMixin, UpdateView): model = UserSettings success_url = reverse_lazy('helpdesk:dashboard') - def get_object(self): + def get_object(self, queryset=None): return UserSettings.objects.get_or_create(user=self.request.user)[0] From 238dd8d3d50fac4e7ca2ab2f39188b70b520e3b9 Mon Sep 17 00:00:00 2001 From: Benbb96 Date: Mon, 15 Aug 2022 20:21:19 +0200 Subject: [PATCH 2/2] Fix import order --- helpdesk/views/staff.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 9a91033b..9a5c0c3e 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -6,8 +6,6 @@ django-helpdesk - A Django powered ticket tracker for small enterprise. views/staff.py - The bulk of the application - provides most business logic and renders all staff-facing views. """ -from django.contrib.auth.views import redirect_to_login - from ..lib import format_time_spent from ..templated_email import send_templated_mail from collections import defaultdict @@ -16,6 +14,7 @@ from datetime import date, datetime, timedelta from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.decorators import user_passes_test +from django.contrib.auth.views import redirect_to_login from django.contrib.contenttypes.models import ContentType from django.core.exceptions import PermissionDenied, ValidationError from django.core.handlers.wsgi import WSGIRequest