refactor(src/dashboard/board_views): improve ticket creation with atomic transaction and streamlined notification handling
Use transaction.atomic for safety and simplify notification creation while preserving existing functionality
This commit is contained in:
1 file changed
+49
-49
@@ -36,59 +36,59 @@ def board_create_ticket(request, project_id):
|
||||
messages.error(request, "Name and status are required.")
|
||||
return redirect("project-board-project", project_id=project_id)
|
||||
|
||||
status = get_object_or_404(TicketStatus, pk=status_id)
|
||||
with transaction.atomic():
|
||||
status = get_object_or_404(TicketStatus, pk=status_id)
|
||||
|
||||
ticket = Ticket.objects.create(
|
||||
project=project,
|
||||
status=status,
|
||||
name=name,
|
||||
description=description or None,
|
||||
priority_id=priority_id,
|
||||
epic_id=epic_id,
|
||||
start_date=start_date,
|
||||
due_date=due_date,
|
||||
created_by=request.user,
|
||||
)
|
||||
ticket = (
|
||||
Ticket.objects.select_related("project", "created_by")
|
||||
.prefetch_related("assignees")
|
||||
.get(pk=ticket.pk)
|
||||
)
|
||||
|
||||
for uid in assignee_ids:
|
||||
TicketUser.objects.get_or_create(ticket=ticket, user_id=uid)
|
||||
|
||||
TicketHistory.objects.create(
|
||||
ticket=ticket,
|
||||
user=request.user,
|
||||
status=status,
|
||||
)
|
||||
|
||||
notify_users = list(ticket.assignees.all())
|
||||
if ticket.created_by and ticket.created_by not in notify_users:
|
||||
notify_users.append(ticket.created_by)
|
||||
for u in notify_users:
|
||||
notification = Notification.objects.create(
|
||||
user=u,
|
||||
type="ticket_created",
|
||||
title=f"New Ticket: {ticket.uuid}",
|
||||
message=f"Ticket '{ticket.name}' created on board by {request.user.get_full_name() or request.user.username}",
|
||||
data={"ticket_id": ticket.id, "ticket_uuid": ticket.uuid},
|
||||
ticket = Ticket.objects.create(
|
||||
project=project,
|
||||
status=status,
|
||||
name=name,
|
||||
description=description or None,
|
||||
priority_id=priority_id,
|
||||
epic_id=epic_id,
|
||||
start_date=start_date,
|
||||
due_date=due_date,
|
||||
created_by=request.user,
|
||||
)
|
||||
# Send email notification
|
||||
send_notification_email(
|
||||
user=u,
|
||||
notification_type="ticket_created",
|
||||
title=notification.title,
|
||||
message=notification.message,
|
||||
data={
|
||||
"ticket_id": ticket.id,
|
||||
"ticket_uuid": ticket.uuid,
|
||||
"project_name": ticket.project.name,
|
||||
"user_name": request.user.get_full_name() or request.user.username,
|
||||
},
|
||||
ticket = (
|
||||
Ticket.objects.select_related("project", "created_by")
|
||||
.prefetch_related("assignees")
|
||||
.get(pk=ticket.pk)
|
||||
)
|
||||
|
||||
for uid in assignee_ids:
|
||||
TicketUser.objects.get_or_create(ticket=ticket, user_id=uid)
|
||||
|
||||
TicketHistory.objects.create(
|
||||
ticket=ticket,
|
||||
user=request.user,
|
||||
status=status,
|
||||
)
|
||||
|
||||
notify_users = list(ticket.assignees.all())
|
||||
if ticket.created_by and ticket.created_by not in notify_users:
|
||||
notify_users.append(ticket.created_by)
|
||||
for u in notify_users:
|
||||
notification = Notification.objects.create(
|
||||
user=u,
|
||||
type="ticket_created",
|
||||
title=f"New Ticket: {ticket.uuid}",
|
||||
message=f"Ticket '{ticket.name}' created on board by {request.user.get_full_name() or request.user.username}",
|
||||
data={"ticket_id": ticket.id, "ticket_uuid": ticket.uuid},
|
||||
)
|
||||
send_notification_email(
|
||||
user=u,
|
||||
notification_type="ticket_created",
|
||||
title=notification.title,
|
||||
message=notification.message,
|
||||
data={
|
||||
"ticket_id": ticket.id,
|
||||
"ticket_uuid": ticket.uuid,
|
||||
"project_name": ticket.project.name,
|
||||
"user_name": request.user.get_full_name() or request.user.username,
|
||||
},
|
||||
)
|
||||
|
||||
messages.success(request, f"Ticket '{ticket.name}' created successfully.")
|
||||
return redirect("project-board-project", project_id=project_id)
|
||||
|
||||
|
||||
Reference in new issue
Block a user