Add comments and refactor variables names

This commit is contained in:
bbe 2020-10-30 22:18:08 +01:00
parent 00a18d8f54
commit 41b5715b7d

View File

@ -873,7 +873,12 @@ ticket_attributes = (
@staff_member_required @staff_member_required
def merge_tickets(request): def merge_tickets(request):
""" TODO """ """
An intermediate view to merge up to 3 tickets in one main ticket.
The user has to first select which ticket will receive the other tickets information and can also choose which
data to keep per attributes as well as custom fields.
Follow-ups and ticketCC will be moved to the main ticket and other tickets won't be able to receive new answers.
"""
ticket_select_form = MultipleTicketSelectForm(request.GET or None) ticket_select_form = MultipleTicketSelectForm(request.GET or None)
tickets = custom_fields = None tickets = custom_fields = None
if ticket_select_form.is_valid(): if ticket_select_form.is_valid():
@ -884,15 +889,15 @@ def merge_tickets(request):
for ticket in tickets: for ticket in tickets:
ticket.values = {} ticket.values = {}
# Prepare the value for each attributes of this ticket # Prepare the value for each attributes of this ticket
for attr, display_name in ticket_attributes: for attribute, display_name in ticket_attributes:
value = getattr(ticket, attr, default) value = getattr(ticket, attribute, default)
# Check if attr is a get_FIELD_display # Check if attr is a get_FIELD_display
if attr.startswith('get_') and attr.endswith('_display'): if attribute.startswith('get_') and attribute.endswith('_display'):
# Hack to call methods like get_FIELD_display() # Hack to call methods like get_FIELD_display()
value = getattr(ticket, attr, default)() value = getattr(ticket, attribute, default)()
ticket.values[attr] = { ticket.values[attribute] = {
'value': value, 'value': value,
'checked': str(ticket.id) == request.POST.get(attr) 'checked': str(ticket.id) == request.POST.get(attribute)
} }
# Prepare the value for each custom fields of this ticket # Prepare the value for each custom fields of this ticket
for custom_field in custom_fields: for custom_field in custom_fields:
@ -916,26 +921,27 @@ def merge_tickets(request):
) )
else: else:
# Save ticket fields values # Save ticket fields values
for attr, display_name in ticket_attributes: for attribute, display_name in ticket_attributes:
id_for_attr = request.POST.get(attr) id_for_attribute = request.POST.get(attribute)
if id_for_attr != chosen_ticket.id: if id_for_attribute != chosen_ticket.id:
try: try:
selected_ticket = tickets.get(id=id_for_attr) selected_ticket = tickets.get(id=id_for_attribute)
except Ticket.DoesNotExist: except (Ticket.DoesNotExist, ValueError):
pass continue
else:
# Check if attr is a get_FIELD_display # Check if attr is a get_FIELD_display
if attr.startswith('get_') and attr.endswith('_display'): if attribute.startswith('get_') and attribute.endswith('_display'):
# Keep only the FIELD part # Keep only the FIELD part
attr = attr[4:-8] attribute = attribute[4:-8]
value = getattr(selected_ticket, attr) # Get value from selected ticket and then save it on the chosen ticket
setattr(chosen_ticket, attr, value) value = getattr(selected_ticket, attribute)
setattr(chosen_ticket, attribute, value)
# Save custom fields values # Save custom fields values
for custom_field in custom_fields: for custom_field in custom_fields:
id_for_attr = request.POST.get(custom_field.name) id_for_custom_field = request.POST.get(custom_field.name)
if id_for_attr != chosen_ticket.id: if id_for_custom_field != chosen_ticket.id:
try: try:
selected_ticket = tickets.get(id=id_for_attr) selected_ticket = tickets.get(id=id_for_custom_field)
except (Ticket.DoesNotExist, ValueError): except (Ticket.DoesNotExist, ValueError):
continue continue
@ -956,8 +962,8 @@ def merge_tickets(request):
# Save changes # Save changes
chosen_ticket.save() chosen_ticket.save()
# For other tickets, save the link to ticket in which they have been merged to # For other tickets, save the link to the ticket in which they have been merged to
# and set status as DUPLICATE # and set status to DUPLICATE
for ticket in tickets.exclude(id=chosen_ticket.id): for ticket in tickets.exclude(id=chosen_ticket.id):
ticket.merged_to = chosen_ticket ticket.merged_to = chosen_ticket
ticket.status = Ticket.DUPLICATE_STATUS ticket.status = Ticket.DUPLICATE_STATUS