2019-08-14 17:39:19 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2020-11-23 16:06:37 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
|
2019-08-14 17:39:19 +00:00
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
2020-04-22 16:55:33 +00:00
|
|
|
# Copyright © 2019 Arthur Grisel-Davy
|
|
|
|
# Copyright © 2020 Gabriel Détraz
|
2019-08-14 17:39:19 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2019-07-10 08:28:16 +00:00
|
|
|
from django.contrib import messages
|
2019-08-11 13:06:56 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2019-07-10 08:28:16 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2019-07-25 14:44:40 +00:00
|
|
|
from django.template.loader import render_to_string
|
2019-08-10 17:25:21 +00:00
|
|
|
from django.views.decorators.cache import cache_page
|
2019-08-14 17:39:19 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2019-07-10 08:28:16 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.forms import modelformset_factory
|
|
|
|
from re2o.views import form
|
2019-07-06 08:25:24 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
from re2o.base import re2o_paginator
|
2019-08-06 19:27:03 +00:00
|
|
|
|
2020-04-23 00:42:18 +00:00
|
|
|
from re2o.acl import (
|
|
|
|
can_view,
|
|
|
|
can_view_all,
|
|
|
|
can_edit,
|
|
|
|
can_create,
|
|
|
|
can_delete
|
|
|
|
)
|
2019-08-11 13:06:56 +00:00
|
|
|
|
2019-08-06 19:27:03 +00:00
|
|
|
from preferences.models import GeneralOption
|
2019-08-13 13:05:12 +00:00
|
|
|
|
2020-04-23 00:42:18 +00:00
|
|
|
from .models import Ticket, CommentTicket
|
2019-08-13 13:05:12 +00:00
|
|
|
|
2020-04-23 00:42:18 +00:00
|
|
|
from .forms import NewTicketForm, EditTicketForm, CommentTicketForm
|
2019-07-10 08:28:16 +00:00
|
|
|
|
2019-08-13 13:05:12 +00:00
|
|
|
|
2019-07-10 08:28:16 +00:00
|
|
|
def new_ticket(request):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display the creation form of tickets."""
|
2020-04-22 19:37:21 +00:00
|
|
|
ticketform = NewTicketForm(request.POST or None, request=request)
|
|
|
|
if ticketform.is_valid():
|
|
|
|
ticketform.save()
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_(
|
|
|
|
"Your ticket has been succesfully opened. We will take care of it as soon as possible."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
return redirect(reverse("index"))
|
|
|
|
else:
|
|
|
|
return redirect(
|
|
|
|
reverse("users:profil", kwargs={"userid": str(request.user.id)})
|
|
|
|
)
|
|
|
|
return form(
|
|
|
|
{"ticketform": ticketform, 'action_name': ("Create a ticket")}, "tickets/edit.html", request
|
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2019-07-08 10:21:51 +00:00
|
|
|
|
2019-08-11 13:06:56 +00:00
|
|
|
@login_required
|
|
|
|
@can_view(Ticket)
|
2019-08-11 14:01:44 +00:00
|
|
|
def aff_ticket(request, ticket, ticketid):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display a single ticket."""
|
2020-04-23 00:42:18 +00:00
|
|
|
comments = CommentTicket.objects.filter(parent_ticket=ticket)
|
2019-11-04 16:55:03 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"tickets/aff_ticket.html",
|
2020-04-23 00:42:18 +00:00
|
|
|
{"ticket": ticket, "comments": comments},
|
2020-04-22 19:37:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@can_edit(Ticket)
|
|
|
|
def change_ticket_status(request, ticket, ticketid):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to change a ticket's status."""
|
2020-04-22 19:37:21 +00:00
|
|
|
ticket.solved = not ticket.solved
|
|
|
|
ticket.save()
|
|
|
|
return redirect(
|
|
|
|
reverse("tickets:aff-ticket", kwargs={"ticketid": str(ticketid)})
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@can_edit(Ticket)
|
|
|
|
def edit_ticket(request, ticket, ticketid):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display the edit form of tickets."""
|
2020-04-22 19:37:21 +00:00
|
|
|
ticketform = EditTicketForm(request.POST or None, instance=ticket)
|
|
|
|
if ticketform.is_valid():
|
|
|
|
ticketform.save()
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_(
|
|
|
|
"Ticket has been updated successfully"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return redirect(
|
|
|
|
reverse("tickets:aff-ticket", kwargs={"ticketid": str(ticketid)})
|
|
|
|
)
|
|
|
|
return form(
|
|
|
|
{"ticketform": ticketform, 'action_name': ("Edit this ticket")}, "tickets/edit.html", request
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
|
|
|
|
2019-08-11 13:06:56 +00:00
|
|
|
|
2020-04-23 00:42:18 +00:00
|
|
|
@login_required
|
|
|
|
@can_view(Ticket)
|
|
|
|
def add_comment(request, ticket, ticketid):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to add a comment to a ticket."""
|
2020-04-23 01:21:11 +00:00
|
|
|
commentticket = CommentTicketForm(request.POST or None, request=request)
|
2020-04-23 00:42:18 +00:00
|
|
|
if commentticket.is_valid():
|
|
|
|
commentticket = commentticket.save(commit=False)
|
|
|
|
commentticket.parent_ticket = ticket
|
|
|
|
commentticket.created_by = request.user
|
|
|
|
commentticket.save()
|
|
|
|
messages.success(request, _("This comment was added."))
|
|
|
|
return redirect(
|
|
|
|
reverse("tickets:aff-ticket", kwargs={"ticketid": str(ticketid)})
|
|
|
|
)
|
|
|
|
return form(
|
|
|
|
{"ticketform": commentticket, "action_name": _("Add a comment")}, "tickets/edit.html", request
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@can_edit(CommentTicket)
|
|
|
|
def edit_comment(request, commentticket_instance, **_kwargs):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to edit a comment of a ticket."""
|
2020-04-23 00:42:18 +00:00
|
|
|
commentticket = CommentTicketForm(request.POST or None, instance=commentticket_instance)
|
|
|
|
if commentticket.is_valid():
|
|
|
|
ticketid = commentticket_instance.parent_ticket.id
|
|
|
|
if commentticket.changed_data:
|
|
|
|
commentticket.save()
|
|
|
|
messages.success(request, _("This comment was edited."))
|
|
|
|
return redirect(
|
|
|
|
reverse("tickets:aff-ticket", kwargs={"ticketid": str(ticketid)})
|
|
|
|
)
|
|
|
|
return form(
|
|
|
|
{"ticketform": commentticket, "action_name": _("Edit")}, "tickets/edit.html", request,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@can_delete(CommentTicket)
|
|
|
|
def del_comment(request, commentticket, **_kwargs):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to delete a comment of a ticket."""
|
2020-04-23 00:42:18 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
ticketid = commentticket.parent_ticket.id
|
|
|
|
commentticket.delete()
|
|
|
|
messages.success(request, _("The comment was deleted."))
|
|
|
|
return redirect(
|
|
|
|
reverse("tickets:aff-ticket", kwargs={"ticketid": str(ticketid)})
|
|
|
|
)
|
|
|
|
return form(
|
|
|
|
{"objet": commentticket, "objet_name": _("Ticket Comment")}, "tickets/delete.html", request
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-08-11 13:06:56 +00:00
|
|
|
@login_required
|
|
|
|
@can_view_all(Ticket)
|
2019-07-07 17:09:56 +00:00
|
|
|
def aff_tickets(request):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display all tickets."""
|
2019-11-04 16:55:03 +00:00
|
|
|
tickets_list = Ticket.objects.all().order_by("-date")
|
2019-08-06 19:27:03 +00:00
|
|
|
nbr_tickets = tickets_list.count()
|
|
|
|
nbr_tickets_unsolved = tickets_list.filter(solved=False).count()
|
2019-11-04 16:55:03 +00:00
|
|
|
if nbr_tickets:
|
2019-08-11 13:06:56 +00:00
|
|
|
last_ticket_date = tickets_list.first().date
|
|
|
|
else:
|
2019-08-14 17:39:19 +00:00
|
|
|
last_ticket_date = _("Never")
|
2019-08-06 19:27:03 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value("pagination_number")
|
|
|
|
|
|
|
|
tickets = re2o_paginator(request, tickets_list, pagination_number)
|
2019-08-06 16:06:53 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
context = {
|
|
|
|
"tickets_list": tickets,
|
|
|
|
"last_ticket_date": last_ticket_date,
|
|
|
|
"nbr_tickets": nbr_tickets,
|
|
|
|
"nbr_tickets_unsolved": nbr_tickets_unsolved,
|
|
|
|
}
|
|
|
|
|
|
|
|
return render(request, "tickets/index.html", context=context)
|
2019-08-05 14:38:53 +00:00
|
|
|
|
2019-08-11 14:01:44 +00:00
|
|
|
|
2020-05-30 09:33:25 +00:00
|
|
|
# Canonic views for optional apps
|
2020-10-18 12:51:58 +00:00
|
|
|
def aff_profil(request, user):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display the tickets on a user's profile."""
|
2019-11-04 16:55:03 +00:00
|
|
|
tickets_list = Ticket.objects.filter(user=user).all().order_by("-date")
|
2019-08-10 08:12:49 +00:00
|
|
|
nbr_tickets = tickets_list.count()
|
|
|
|
nbr_tickets_unsolved = tickets_list.filter(solved=False).count()
|
2019-11-04 16:55:03 +00:00
|
|
|
if nbr_tickets:
|
2019-08-11 13:06:56 +00:00
|
|
|
last_ticket_date = tickets_list.first().date
|
|
|
|
else:
|
2019-08-14 17:39:19 +00:00
|
|
|
last_ticket_date = _("Never")
|
2019-08-11 13:06:56 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value("pagination_large_number")
|
|
|
|
|
|
|
|
tickets = re2o_paginator(request, tickets_list, pagination_number)
|
2019-08-10 08:12:49 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
context = {
|
|
|
|
"tickets_list": tickets,
|
|
|
|
"last_ticket_date": last_ticket_date,
|
|
|
|
"nbr_tickets": nbr_tickets,
|
|
|
|
"nbr_tickets_unsolved": nbr_tickets_unsolved,
|
|
|
|
}
|
|
|
|
return render_to_string(
|
2020-10-18 12:51:58 +00:00
|
|
|
"tickets/aff_profil.html", context=context, request=request, using=None
|
2019-08-10 08:12:49 +00:00
|
|
|
)
|
|
|
|
|
2019-08-05 14:38:53 +00:00
|
|
|
|
2019-08-11 16:03:45 +00:00
|
|
|
def contact(request):
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display contact addresses to be used for tickets on the
|
|
|
|
contact page.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
return render_to_string("tickets/contact.html")
|
|
|
|
|
2019-08-11 16:03:45 +00:00
|
|
|
|
2019-09-20 17:56:19 +00:00
|
|
|
def navbar_user():
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display a link to tickets in the navbar (in the dropdown
|
|
|
|
menu Users).
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
return ("users", render_to_string("tickets/navbar.html"))
|
|
|
|
|
2019-08-11 16:03:45 +00:00
|
|
|
|
2019-09-20 17:56:19 +00:00
|
|
|
def navbar_logout():
|
2020-04-28 09:59:13 +00:00
|
|
|
"""View used to display a link to open tickets for logged out users."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return render_to_string("tickets/navbar_logout.html")
|