mirror of
https://github.com/pretix/pretix.git
synced 2026-05-18 17:24:03 +00:00
Subevents: Allow to skip conflicting dates in bulk-creation (Z#23217384) (#6079)
* Subevents: Allow to skip conflicting dates in bulk-creation * Update src/pretix/control/templates/pretixcontrol/subevents/bulk.html * Fix overlap calc for consecutive subevents * Add test for skipping conflicting dates in bulk-creation --------- Co-authored-by: Richard Schreiber <schreiber@pretix.eu> Co-authored-by: Richard Schreiber <schreiber@rami.io> Co-authored-by: Kara Engelhardt <engelhardt@pretix.eu>
This commit is contained in:
@@ -917,6 +917,35 @@ class SubEventBulkCreate(SubEventEditorMixin, EventPermissionRequiredMixin, Asyn
|
||||
if len(subevents) > 100_000:
|
||||
raise ValidationError(_('Please do not create more than 100.000 dates at once.'))
|
||||
|
||||
if form.cleaned_data.get("skip_if_overlap") and subevents:
|
||||
def overlaps(a_from, a_to, b_from, b_to):
|
||||
if a_from == b_from:
|
||||
return True
|
||||
if a_from > b_from:
|
||||
# a starts after b
|
||||
# check if it starts before b ends
|
||||
return b_to and a_from < b_to
|
||||
# a starts before b
|
||||
# check if it ends before b starts
|
||||
return a_to and a_to > b_from
|
||||
|
||||
date_min = min(se.date_from for se in subevents)
|
||||
date_max = max(se.date_to or se.date_from for se in subevents)
|
||||
dates_existing = list(self.request.event.subevents.annotate(
|
||||
date_fromto=Coalesce('date_to', 'date_from'),
|
||||
).filter(
|
||||
date_from__lte=date_max,
|
||||
date_fromto__gte=date_min,
|
||||
).values('date_from', 'date_to'))
|
||||
subevents = [
|
||||
se for se in subevents if not any(
|
||||
overlaps(se.date_from, se.date_to, other['date_from'], other['date_to'])
|
||||
for other in dates_existing
|
||||
)
|
||||
]
|
||||
if not subevents:
|
||||
raise ValidationError(_('All dates would be skipped because they conflict with existing dates.'))
|
||||
|
||||
for i, se in enumerate(subevents):
|
||||
se.save(clear_cache=False)
|
||||
if i % 100 == 0:
|
||||
|
||||
Reference in New Issue
Block a user