Skip to content

Commit

Permalink
Super_admin and admin can change priority of a ticket/cant change sta…
Browse files Browse the repository at this point in the history
…tus, priority or assign a ticket when its marked as resolved
  • Loading branch information
Jeenyhus committed Oct 5, 2023
1 parent ec0026d commit e343544
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
7 changes: 7 additions & 0 deletions techsupport/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ def save(self, commit=True):
return instance



class TicketPriorityForm(forms.ModelForm):
class Meta:
model = SupportTicket
fields = ['priority']


class TicketAssignmentForm(forms.Form):
assigned_to = forms.ModelChoiceField(
queryset=User.objects.filter(role="technician")
Expand Down
30 changes: 25 additions & 5 deletions techsupport/templates/support_ticket/ticket_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,33 @@ <h6 class="card-title">
{% csrf_token %}
<div class="mb-3">
<label for="assigned_to"><strong>Assign to:</strong></label>
<select class="form-select" id="assigned_to" name="assigned_to" required>
<select class="form-select" id="assigned_to" name="assigned_to" required {% if ticket.status == 'Resolved' %}disabled{% endif %}>
{% for technician in technicians %}
<option value="{{ technician.id }}">{{ technician.username }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn bg-primary btn-block text-white"><i class="bi bi-hand-index-thumb me-2"></i><span class="mr-2"></span>Assign</button>
<button type="submit" class="btn bg-primary btn-block text-white" {% if ticket.status == 'Resolved' %}disabled{% endif %}><i class="bi bi-hand-index-thumb me-2"></i><span class="mr-2"></span>Assign</button>
</form>
{% endif %}

<!-- Ticket Priority Form -->
{% if user_role == 'admin' or user_role == 'super_admin' %}
<form method="post">
{% csrf_token %}
<div class="mb-3">
<label for="priority"><strong>Priority:</strong></label>
{% if ticket.status == 'Resolved' %}
<select class="form-select" id="priority" name="priority" disabled>
<option value="{{ ticket.priority }}">{{ ticket.get_priority_display }}</option>
</select>
{% else %}
{{ form_priority.priority }}
{% endif %}
</div>
<button type="submit" class="btn bg-primary btn-block text-white" {% if ticket.status == 'Resolved' %}disabled{% endif %}>
<i class="bi bi-caret-up"></i><span class="mr-2"></span>Update
</button>
</form>
{% endif %}
</div>
Expand All @@ -75,8 +95,8 @@ <h6 class="card-title">
</div>
</div>
<style>
.bg-primary {
background-color: #5A97D0 !important;
.bg-primary {
background-color: #5A97D0 !important;
}
</style>
{% endblock %}
{% endblock %}
51 changes: 40 additions & 11 deletions techsupport/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SupportTicketUpdateForm,
TicketResolutionForm,
TicketAssignmentForm,
TicketPriorityForm,
)
from .models import (
Region,
Expand Down Expand Up @@ -150,7 +151,7 @@ def get_ticket_insights():
except EmptyPage:
paginated_tickets = paginator.page(paginator.num_pages)

if user_role == "technician":
if user_role == "technician" or "admin" or "super_admin":
regions = Region.objects.all()
centres = Centre.objects.all()
else:
Expand Down Expand Up @@ -265,15 +266,15 @@ def ticket_details(request, ticket_id):
ticket = get_object_or_404(SupportTicket, id=ticket_id)
form_resolution = None
form_assignment = None
form_priority = None

if request.user.role in ["technician", "admin", "super_admin"]:
user_role = request.user.role
else:
user_role = None

if request.method == "POST":
if user_role in ["technician", "admin", "super_admin"]:
# Check if the form for ticket assignment is submitted
form_assignment = TicketAssignmentForm(request.POST)
if form_assignment.is_valid():
assigned_to = form_assignment.cleaned_data["assigned_to"]
Expand All @@ -288,7 +289,6 @@ def ticket_details(request, ticket_id):
messages.info(request, "Support ticket has been assigned.")
return redirect("dashboard")

# Check if the form for ticket resolution is submitted
form_resolution = TicketResolutionForm(request.POST, instance=ticket)
if form_resolution.is_valid():
ticket = form_resolution.save(commit=False)
Expand All @@ -303,24 +303,33 @@ def ticket_details(request, ticket_id):
)

ticket.save()

messages.info(request, "Support ticket status has been updated.")
return redirect("dashboard")

form_priority = TicketPriorityForm(request.POST, instance=ticket)
if form_priority.is_valid():
form_priority.save()

# Send the webhook message when ticket priority is changed
send_priority_webhook(
ticket.title, ticket.centre.name, request.user.username
)

messages.info(request, "Support ticket priority has been updated.")
return redirect("dashboard")

else:
# For other user roles, use the existing form
form = SupportTicketUpdateForm(request.POST, instance=ticket)
if form.is_valid():
form.save()
messages.info(request, "Ticket description has been updated.")
return redirect("dashboard")
else:
if user_role in ["technician", "admin", "super_admin"]:
# Show both ticket resolution form and ticket assignment form to technicians
form_resolution = TicketResolutionForm(instance=ticket)
form_assignment = TicketAssignmentForm()
form_priority = TicketPriorityForm(instance=ticket)
else:
# For other user roles, use the existing form
form = SupportTicketUpdateForm(instance=ticket)

technicians = User.objects.filter(role="technician")
Expand All @@ -330,13 +339,13 @@ def ticket_details(request, ticket_id):
"user_role": user_role,
"form_resolution": form_resolution,
"form_assignment": form_assignment,
"form_priority": form_priority,
"technicians": technicians,
}

return render(request, "support_ticket/ticket_details.html", context)



def send_assignment_webhook(ticket_title, ticket_centre, assigned_to):
webhook_url = settings.WEB_HOOK_URL

Expand Down Expand Up @@ -377,6 +386,28 @@ def send_resolution_webhook(ticket_title, ticket_centre, resolved_by):
except requests.exceptions.RequestException as e:
logger = logging.getLogger(__name__)
logger.error(f"Failed to send webhook notification: {str(e)}")


def send_priority_webhook(ticket_title, ticket_centre, changed_by):
webhook_url = settings.WEB_HOOK_URL

message = {
"text": f'Support Ticket *Title:* "{ticket_title}" at *{ticket_centre}* priority has been changed by {changed_by}.'
}

headers = {"Content-Type": "application/json; charset=UTF-8"}

try:
response = requests.post(
url=webhook_url,
headers=headers,
data=json.dumps(message),
)
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger = logging.getLogger(__name__)
logger.error(f"Failed to send webhook notification: {str(e)}")



@login_required
Expand All @@ -397,8 +428,6 @@ def create_ticket(request):
context = {"form": form}
return render(request, "support_ticket/create_ticket.html", context)

return render(request, "support_ticket/create_ticket.html", {"form": form})


def send_webhook_notification(support_ticket, user):
webhook_url = settings.WEB_HOOK_URL
Expand Down

0 comments on commit e343544

Please # to comment.