Beispiel #1
0
 def clean_due_date(self):
     data = self.cleaned_data['due_date']
     #TODO: add Google calendar update hook, store event id with ticket
     if data and helpdesk_settings.HELPDESK_UPDATE_CALENDAR:
         from helpdesk import calendars
         calendars.update_calendar(self.request)
     return data
Beispiel #2
0
 def clean_due_date(self):
     data = self.cleaned_data['due_date']
     #TODO: add Google calendar update hook, store event id with ticket
     if data and helpdesk_settings.HELPDESK_UPDATE_CALENDAR:
         from helpdesk import calendars
         calendars.update_calendar(self.request)
     return data
Beispiel #3
0
def update_ticket(request, ticket_id, public=False):
    if not (
        public
        or (
            request.user.is_authenticated()
            and request.user.is_active
            and (request.user.is_staff or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE)
        )
    ):
        return HttpResponseForbidden(_("Sorry, you need to login to do that."))

    ticket = get_object_or_404(Ticket, id=ticket_id)

    comment = request.POST.get("comment", "")
    new_status = int(request.POST.get("new_status", ticket.status))
    title = request.POST.get("title", "")
    public = request.POST.get("public", False)
    owner = int(request.POST.get("owner", None))
    priority = int(request.POST.get("priority", ticket.priority))
    due_year = int(request.POST.get("due_date_year"))
    due_month = int(request.POST.get("due_date_month"))
    due_day = int(request.POST.get("due_date_day"))
    due_date = datetime(due_year, due_month, due_day) if due_year and due_month and due_day else ticket.due_date
    tags = request.POST.get("tags", "")

    # We need to allow the 'ticket' and 'queue' contexts to be applied to the
    # comment.
    from django.template import loader, Context

    context = safe_template_context(ticket)
    # this line sometimes creates problems if code is sent as a comment.
    # if comment contains some django code, like "why does {% if bla %} crash",
    # then the following line will give us a crash, since django expects {% if %}
    # to be closed with an {% endif %} tag.
    comment = loader.get_template_from_string(comment).render(Context(context))

    if owner is None and ticket.assigned_to:
        owner = ticket.assigned_to.id

    f = FollowUp(ticket=ticket, date=datetime.now(), comment=comment)

    if request.user.is_staff or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
        f.user = request.user

    f.public = public

    reassigned = False

    if owner is not None:
        if owner != 0 and ((ticket.assigned_to and owner != ticket.assigned_to.id) or not ticket.assigned_to):
            new_user = User.objects.get(id=owner)
            f.title = _("Assigned to %(username)s") % {"username": new_user.username}
            ticket.assigned_to = new_user
            reassigned = True
        # user changed owner to 'unassign'
        elif owner == 0 and ticket.assigned_to is not None:
            f.title = _("Unassigned")
            ticket.assigned_to = None

    if new_status != ticket.status:
        ticket.status = new_status
        ticket.save()
        f.new_status = new_status
        if f.title:
            f.title += " and %s" % ticket.get_status_display()
        else:
            f.title = "%s" % ticket.get_status_display()

    if not f.title:
        if f.comment:
            f.title = _("Comment")
        else:
            f.title = _("Updated")

    f.save()

    files = []
    if request.FILES:
        import mimetypes, os

        for file in request.FILES.getlist("attachment"):
            filename = file.name.replace(" ", "_")
            a = Attachment(
                followup=f,
                filename=filename,
                mime_type=mimetypes.guess_type(filename)[0] or "application/octet-stream",
                size=file.size,
            )
            a.file.save(file.name, file, save=False)
            a.save()

            if file.size < getattr(settings, "MAX_EMAIL_ATTACHMENT_SIZE", 512000):
                # Only files smaller than 512kb (or as defined in
                # settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
                files.append(a.file.path)

    if title != ticket.title:
        c = TicketChange(followup=f, field=_("Title"), old_value=ticket.title, new_value=title)
        c.save()
        ticket.title = title

    if priority != ticket.priority:
        c = TicketChange(followup=f, field=_("Priority"), old_value=ticket.priority, new_value=priority)
        c.save()
        ticket.priority = priority

    if due_date != ticket.due_date:
        c = TicketChange(followup=f, field=_("Due on"), old_value=ticket.due_date, new_value=due_date)
        if helpdesk_settings.HELPDESK_UPDATE_CALENDAR:
            from helpdesk import calendars

            calendars.update_calendar(request, search_date=ticket.due_date)
        c.save()
        ticket.due_date = due_date

    if HAS_TAGGING_SUPPORT:
        if tags != ticket.tags:
            c = TicketChange(followup=f, field=_("Tags"), old_value=ticket.tags, new_value=tags)
            c.save()
            ticket.tags = tags

    if HAS_TAGGIT_SUPPORT:
        old_tags = [tag.name for tag in ticket.tags.all()]
        old_tags.sort()
        new_tags = tags.replace(" ", "").strip(",").split(",")
        new_tags.sort()
        if new_tags != old_tags:
            c = TicketChange(followup=f, field=_("Tags"), old_value=", ".join(old_tags), new_value=", ".join(new_tags))
            c.save()
            ticket.tags.set(*new_tags)

    if new_status in [Ticket.RESOLVED_STATUS, Ticket.CLOSED_STATUS]:
        ticket.resolution = comment

    messages_sent_to = []

    # ticket might have changed above, so we re-instantiate context with the
    # (possibly) updated ticket.
    context = safe_template_context(ticket)
    context.update(resolution=ticket.resolution, comment=f.comment)

    if (
        ticket.submitter_email
        and public
        and (f.comment or (f.new_status in (Ticket.RESOLVED_STATUS, Ticket.CLOSED_STATUS)))
    ):

        if f.new_status == Ticket.RESOLVED_STATUS:
            template = "resolved_submitter"
        elif f.new_status == Ticket.CLOSED_STATUS:
            template = "closed_submitter"
        else:
            template = "updated_submitter"

        send_templated_mail(
            template,
            context,
            recipients=ticket.submitter_email,
            sender=ticket.queue.from_address,
            fail_silently=True,
            files=files,
        )
        messages_sent_to.append(ticket.submitter_email)

        for cc in ticket.ticketcc_set.all():
            if cc.email_address not in messages_sent_to:
                send_templated_mail(
                    template, context, recipients=cc.email_address, sender=ticket.queue.from_address, fail_silently=True
                )
                messages_sent_to.append(cc.email_address)

    if (
        ticket.assigned_to
        and request.user != ticket.assigned_to
        and ticket.assigned_to.email
        and ticket.assigned_to.email not in messages_sent_to
    ):
        # We only send e-mails to staff members if the ticket is updated by
        # another user. The actual template varies, depending on what has been
        # changed.
        if reassigned:
            template_staff = "assigned_owner"
        elif f.new_status == Ticket.RESOLVED_STATUS:
            template_staff = "resolved_owner"
        elif f.new_status == Ticket.CLOSED_STATUS:
            template_staff = "closed_owner"
        else:
            template_staff = "updated_owner"

        if (
            not reassigned
            or (reassigned and ticket.assigned_to.usersettings.settings.get("email_on_ticket_assign", False))
        ) or (not reassigned and ticket.assigned_to.usersettings.settings.get("email_on_ticket_change", False)):
            send_templated_mail(
                template_staff,
                context,
                recipients=ticket.assigned_to.email,
                sender=ticket.queue.from_address,
                fail_silently=True,
                files=files,
            )
            messages_sent_to.append(ticket.assigned_to.email)

    if ticket.queue.updated_ticket_cc and ticket.queue.updated_ticket_cc not in messages_sent_to:
        if reassigned:
            template_cc = "assigned_cc"
        elif f.new_status == Ticket.RESOLVED_STATUS:
            template_cc = "resolved_cc"
        elif f.new_status == Ticket.CLOSED_STATUS:
            template_cc = "closed_cc"
        else:
            template_cc = "updated_cc"

        send_templated_mail(
            template_cc,
            context,
            recipients=ticket.queue.updated_ticket_cc,
            sender=ticket.queue.from_address,
            fail_silently=True,
            files=files,
        )

    ticket.save()

    if request.user.is_staff or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
        return HttpResponseRedirect(ticket.get_absolute_url())
    else:
        return HttpResponseRedirect(ticket.ticket_url)
Beispiel #4
0
def update_ticket(request, ticket_id, public=False):
    if not (public or
            (request.user.is_authenticated() and request.user.is_active and
             (request.user.is_staff
              or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE))):
        return HttpResponseForbidden(_('Sorry, you need to login to do that.'))

    ticket = get_object_or_404(Ticket, id=ticket_id)

    comment = request.POST.get('comment', '')
    new_status = int(request.POST.get('new_status', ticket.status))
    title = request.POST.get('title', '')
    public = request.POST.get('public', False)
    owner = int(request.POST.get('owner', None))
    priority = int(request.POST.get('priority', ticket.priority))
    due_year = int(request.POST.get('due_date_year'))
    due_month = int(request.POST.get('due_date_month'))
    due_day = int(request.POST.get('due_date_day'))
    due_date = datetime(
        due_year, due_month,
        due_day) if due_year and due_month and due_day else ticket.due_date
    tags = request.POST.get('tags', '')

    # We need to allow the 'ticket' and 'queue' contexts to be applied to the
    # comment.
    from django.template import loader, Context
    context = safe_template_context(ticket)
    # this line sometimes creates problems if code is sent as a comment.
    # if comment contains some django code, like "why does {% if bla %} crash",
    # then the following line will give us a crash, since django expects {% if %}
    # to be closed with an {% endif %} tag.
    comment = loader.get_template_from_string(comment).render(Context(context))

    if owner is None and ticket.assigned_to:
        owner = ticket.assigned_to.id

    f = FollowUp(ticket=ticket, date=datetime.now(), comment=comment)

    if request.user.is_staff or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
        f.user = request.user

    f.public = public

    reassigned = False

    if owner is not None:
        if owner != 0 and (
            (ticket.assigned_to and owner != ticket.assigned_to.id)
                or not ticket.assigned_to):
            new_user = User.objects.get(id=owner)
            f.title = _('Assigned to %(username)s') % {
                'username': new_user.username,
            }
            ticket.assigned_to = new_user
            reassigned = True
        # user changed owner to 'unassign'
        elif owner == 0 and ticket.assigned_to is not None:
            f.title = _('Unassigned')
            ticket.assigned_to = None

    if new_status != ticket.status:
        ticket.status = new_status
        ticket.save()
        f.new_status = new_status
        if f.title:
            f.title += ' and %s' % ticket.get_status_display()
        else:
            f.title = '%s' % ticket.get_status_display()

    if not f.title:
        if f.comment:
            f.title = _('Comment')
        else:
            f.title = _('Updated')

    f.save()

    files = []
    if request.FILES:
        import mimetypes, os
        for file in request.FILES.getlist('attachment'):
            filename = file.name.replace(' ', '_')
            a = Attachment(
                followup=f,
                filename=filename,
                mime_type=mimetypes.guess_type(filename)[0]
                or 'application/octet-stream',
                size=file.size,
            )
            a.file.save(file.name, file, save=False)
            a.save()

            if file.size < getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE',
                                   512000):
                # Only files smaller than 512kb (or as defined in
                # settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
                files.append(a.file.path)

    if title != ticket.title:
        c = TicketChange(
            followup=f,
            field=_('Title'),
            old_value=ticket.title,
            new_value=title,
        )
        c.save()
        ticket.title = title

    if priority != ticket.priority:
        c = TicketChange(
            followup=f,
            field=_('Priority'),
            old_value=ticket.priority,
            new_value=priority,
        )
        c.save()
        ticket.priority = priority

    if due_date != ticket.due_date:
        c = TicketChange(
            followup=f,
            field=_('Due on'),
            old_value=ticket.due_date,
            new_value=due_date,
        )
        if helpdesk_settings.HELPDESK_UPDATE_CALENDAR:
            from helpdesk import calendars
            calendars.update_calendar(request, search_date=ticket.due_date)
        c.save()
        ticket.due_date = due_date

    if HAS_TAGGING_SUPPORT:
        if tags != ticket.tags:
            c = TicketChange(
                followup=f,
                field=_('Tags'),
                old_value=ticket.tags,
                new_value=tags,
            )
            c.save()
            ticket.tags = tags

    if HAS_TAGGIT_SUPPORT:
        old_tags = [tag.name for tag in ticket.tags.all()]
        old_tags.sort()
        new_tags = tags.replace(' ', '').strip(',').split(',')
        new_tags.sort()
        if new_tags != old_tags:
            c = TicketChange(
                followup=f,
                field=_('Tags'),
                old_value=', '.join(old_tags),
                new_value=', '.join(new_tags),
            )
            c.save()
            ticket.tags.set(*new_tags)

    if new_status in [Ticket.RESOLVED_STATUS, Ticket.CLOSED_STATUS]:
        ticket.resolution = comment

    messages_sent_to = []

    # ticket might have changed above, so we re-instantiate context with the
    # (possibly) updated ticket.
    context = safe_template_context(ticket)
    context.update(
        resolution=ticket.resolution,
        comment=f.comment,
    )

    if ticket.submitter_email and public and (
            f.comment or
        (f.new_status in (Ticket.RESOLVED_STATUS, Ticket.CLOSED_STATUS))):

        if f.new_status == Ticket.RESOLVED_STATUS:
            template = 'resolved_submitter'
        elif f.new_status == Ticket.CLOSED_STATUS:
            template = 'closed_submitter'
        else:
            template = 'updated_submitter'

        send_templated_mail(
            template,
            context,
            recipients=ticket.submitter_email,
            sender=ticket.queue.from_address,
            fail_silently=True,
            files=files,
        )
        messages_sent_to.append(ticket.submitter_email)

        for cc in ticket.ticketcc_set.all():
            if cc.email_address not in messages_sent_to:
                send_templated_mail(
                    template,
                    context,
                    recipients=cc.email_address,
                    sender=ticket.queue.from_address,
                    fail_silently=True,
                )
                messages_sent_to.append(cc.email_address)

    if ticket.assigned_to and request.user != ticket.assigned_to and ticket.assigned_to.email and ticket.assigned_to.email not in messages_sent_to:
        # We only send e-mails to staff members if the ticket is updated by
        # another user. The actual template varies, depending on what has been
        # changed.
        if reassigned:
            template_staff = 'assigned_owner'
        elif f.new_status == Ticket.RESOLVED_STATUS:
            template_staff = 'resolved_owner'
        elif f.new_status == Ticket.CLOSED_STATUS:
            template_staff = 'closed_owner'
        else:
            template_staff = 'updated_owner'

        if (not reassigned or
            (reassigned and ticket.assigned_to.usersettings.settings.get(
                'email_on_ticket_assign', False))) or (
                    not reassigned
                    and ticket.assigned_to.usersettings.settings.get(
                        'email_on_ticket_change', False)):
            send_templated_mail(
                template_staff,
                context,
                recipients=ticket.assigned_to.email,
                sender=ticket.queue.from_address,
                fail_silently=True,
                files=files,
            )
            messages_sent_to.append(ticket.assigned_to.email)

    if ticket.queue.updated_ticket_cc and ticket.queue.updated_ticket_cc not in messages_sent_to:
        if reassigned:
            template_cc = 'assigned_cc'
        elif f.new_status == Ticket.RESOLVED_STATUS:
            template_cc = 'resolved_cc'
        elif f.new_status == Ticket.CLOSED_STATUS:
            template_cc = 'closed_cc'
        else:
            template_cc = 'updated_cc'

        send_templated_mail(
            template_cc,
            context,
            recipients=ticket.queue.updated_ticket_cc,
            sender=ticket.queue.from_address,
            fail_silently=True,
            files=files,
        )

    ticket.save()

    if request.user.is_staff or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
        return HttpResponseRedirect(ticket.get_absolute_url())
    else:
        return HttpResponseRedirect(ticket.ticket_url)