-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
event_track_location.py
56 lines (50 loc) · 2.19 KB
/
event_track_location.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright 2017 Tecnativa - Jairo Llopis
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
from datetime import timedelta
from odoo import _, api, exceptions, fields, models
from oca.decorators import foreach
class EventTrackLocation(models.Model):
_inherit = "event.track.location"
overlappable = fields.Boolean(
help="Can this location have simultaneous tracks?"
)
@api.constrains("overlappable")
@foreach()
def _check_overlappable(self):
"""Ensure no overlaps happen with this location."""
# Skip locations that can be overlapped
if self.overlappable:
return
# Get tracks that could produce an overlap
remaining_tracks = self.env["event.track"].search([
("location_id", "=", self.id),
("stage_id.is_cancel", "=", False),
])
# Compare tracks overlapping among themselves
while remaining_tracks:
# Extract some track from the set
a_track = remaining_tracks[0]
remaining_tracks -= a_track
# Get track A's dates
a_start = a_track.date
a_end = a_start + timedelta(hours=a_track.duration)
# Compare with all remaining tracks
for b_track in remaining_tracks:
# Get track B's dates
b_start = b_track.date
b_end = b_start + timedelta(hours=b_track.duration)
# Fail if there's an overlap
if b_start <= a_end and b_end >= a_start:
msg = _(
"Track %(one)s (from event %(one_event)s) and "
"track %(other)s (from event %(other_event)s) would "
"overlap in the same location %(location)s"
)
params = {
"location": self.display_name,
"one_event": a_track.event_id.display_name,
"one": a_track.display_name,
"other_event": b_track.event_id.display_name,
"other": b_track.display_name,
}
raise exceptions.ValidationError(msg % params)