You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.7 KiB
Python

from django.shortcuts import render, get_object_or_404
from customercore.decorators import *
from customercore.models import *
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.conf import settings
@customer_login_required
def customer_add_ticket(request, *args, **kwargs):
customer_orders = Order.objects.filter(customer=request.user.customerprofile)
customer_orders_with_last_status = customer_orders.annotate(max_status_date=Max('orderstatus__date'))
customer_orders_completed = customer_orders_with_last_status.filter(orderstatus__status='Completed',orderstatus__date=F('max_status_date'))
customer_products = OrderItem.objects.filter(active__in=[True, None], item__type = 'Product', order__customer = request.user.customerprofile, order__in=customer_orders_completed )
customer_projects = Project.objects.filter(customer=request.user.customerprofile)
support_department = get_object_or_404(Department, name='Support')
if request.method == 'POST':
project = None
product = None
departments = [support_department]
regarding = 'General/Account/Billing'
if request.POST.get('project') and request.POST.get('regarding') == 'Project':
project = get_object_or_404(Project, id=request.POST.get('project'))
project_types = project.project_type.all()
departments = [project_type.department for project_type in project_types]
regarding = 'Project/Product'
elif request.POST.get('product') and request.POST.get('regarding') == 'Product':
product = get_object_or_404(Item, id=request.POST.get('product'))
departments = [product.item_type.department]
regarding = 'Project/Product'
ticket = Ticket(
status = 'Open',
customer = request.user.customerprofile,
title = request.POST.get('title'),
description = request.POST.get('description'),
regarding = regarding,
project = project,
product = product,
opened_by = request.user,
opened_date = datetime.now()
)
ticket.save()
ticket.departments.set(departments)
ticket_status = TicketStatus(
ticket = ticket,
status = 'Open',
added_by = request.user,
date_added = datetime.now()
)
ticket_status.save()
for file in request.FILES.getlist('files'):
ticket_attachment = TicketAttachment(
ticket=ticket,
file=file
)
ticket_attachment.save()
department_ids = [dept.id for dept in departments]
staff_profiles = StaffProfile.objects.filter(staff_position__department__id__in=department_ids, active=True)
print(staff_profiles)
for staff in staff_profiles:
subject = f"New Ticket Opened: {ticket.title}"
html_message = render_to_string('email_templates/new_ticket.html', {
'ticket': ticket,
'staff_first_name': staff.user.first_name
})
plain_message = strip_tags(html_message)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = staff.user.email
send_mail(subject, plain_message, from_email, [to_email], html_message=html_message)
return redirect('customerticketdetails', ticket_number=ticket.ticket_number)
context = {
'customer_products': customer_products,
'customer_projects': customer_projects,
}
return render(request, 'add_templates/customer-add-ticket.html', context)
@customer_login_required
def customer_add_ticket_update(request, ticket_id):
ticket = get_object_or_404(Ticket, id=ticket_id)
if request.method == 'POST':
ticket_update = TicketUpdate(
ticket = ticket,
description = request.POST.get('description'),
added_by = request.user,
date_added = datetime.now(),
)
ticket_update.save()
for file in request.FILES.getlist('files'):
ticket_attachment = TicketAttachment(
ticket_update=ticket_update,
file=file
)
ticket_attachment.save()
if ticket.ticket_members.exists():
members_ids = ticket.ticket_members.values_list('id', flat=True)
staff_profiles = StaffProfile.objects.filter(id__in=members_ids)
else:
department_ids = ticket.departments.values_list('id', flat=True)
staff_profiles = StaffProfile.objects.filter(staff_position__department__id__in=department_ids, active=True)
for staff in staff_profiles:
subject = f"New Ticket Update: {ticket.title}"
html_message = render_to_string('email_templates/new_ticket_update.html', {
'ticket_update': ticket_update,
'staff_first_name': staff.user.first_name
})
plain_message = strip_tags(html_message)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = staff.user.email
send_mail(subject, plain_message, from_email, [to_email], html_message=html_message)
return redirect('customerticketdetails', ticket_number=ticket.ticket_number)
context = {
'ticket': ticket,
}
return render(request, 'add_templates/customer-add-ticket.html', context)
@customer_login_required
def customer_add_ticket_update_reaction(request, reaction_type, ticketupdate_id):
ticket_update = get_object_or_404(TicketUpdate, id=ticketupdate_id)
existing_reaction = TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).first()
if existing_reaction:
# If the existing reaction type is equal to the new reaction, delete it
if existing_reaction.reaction == reaction_type:
existing_reaction.delete()
else:
# If not, delete all previous reactions and add a new one
TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).delete()
reaction = TicketUpdateReaction.objects.create(
ticket_update=ticket_update,
reaction=reaction_type,
customer=request.user
)
else:
# If there's no existing reaction, simply add the new one
reaction = TicketUpdateReaction.objects.create(
ticket_update=ticket_update,
reaction=reaction_type,
customer=request.user
)
return redirect('customerticketdetails', ticket_number=ticket_update.ticket.ticket_number)