PDF: Deduplicate list of add-ons (#3490)

This commit is contained in:
Raphael Michel
2023-07-24 09:27:38 +02:00
committed by GitHub
parent 4b706339ed
commit 06f361cece

View File

@@ -43,7 +43,7 @@ import subprocess
import tempfile import tempfile
import unicodedata import unicodedata
import uuid import uuid
from collections import OrderedDict from collections import OrderedDict, defaultdict
from functools import partial from functools import partial
from io import BytesIO from io import BytesIO
@@ -361,14 +361,9 @@ DEFAULT_VARIABLES = OrderedDict((
}), }),
("addons", { ("addons", {
"label": _("List of Add-Ons"), "label": _("List of Add-Ons"),
"editor_sample": _("Add-on 1\nAdd-on 2"), "editor_sample": _("Add-on 1\n2x Add-on 2"),
"evaluate": lambda op, order, ev: "\n".join([ "evaluate": lambda op, order, ev: "\n".join([
'{} - {}'.format(p.item.name, p.variation.value) if p.variation else str(p.item.name) str(p) for p in generate_compressed_addon_list(op, order, ev)
for p in (
op.addons.all() if 'addons' in getattr(op, '_prefetched_objects_cache', {})
else op.addons.select_related('item', 'variation')
)
if not p.canceled
]) ])
}), }),
("organizer", { ("organizer", {
@@ -702,6 +697,30 @@ def get_seat(op: OrderPosition):
return None return None
def generate_compressed_addon_list(op, order, event):
itemcount = defaultdict(int)
addons = (
op.addons.all() if 'addons' in getattr(op, '_prefetched_objects_cache', {})
else op.addons.select_related('item', 'variation')
)
for pos in addons:
itemcount[pos.item, pos.variation] += 1
addonlist = []
for (item, variation), count in itemcount.items():
if variation:
if count > 1:
addonlist.append('{}x {} - {}'.format(count, item.name, variation.value))
else:
addonlist.append('{} - {}'.format(item.name, variation.value))
else:
if count > 1:
addonlist.append('{}x {}'.format(count, item.name))
else:
addonlist.append(item.name)
return addonlist
class Renderer: class Renderer:
def __init__(self, event, layout, background_file): def __init__(self, event, layout, background_file):